/*=== Define Necessary Variables ===*/
var riDiv = document.getElementsByTagName('div');
var riTd = document.getElementsByTagName('td');
var riTable = document.getElementsByTagName('table');
var script1 = "<script type='text/javascript' language='JavaScript' src='http://roddyinnovations.com/pbhacks/kacs/";
var script2 = ".js'></script>";

/*=== Insert Link in Admin Panel ===*/
if(location.href.match(/action=(admin|\w+3)/i) && !location.href.match(/page=catsplit/i)){
	for(x=riDiv.length-1; x>=0; x--){
		if(riDiv[x].innerHTML.match(/Headers\sand\sFooters/i)){
			riDiv[x].appendChild(document.createElement("BR"));
			riDiv[x].appendChild(document.createElement("A"));
			riDiv[x].lastChild.href = 'index.cgi?action=headersfooters2&id=-&page=catsplit';
			riDiv[x].lastChild.appendChild(document.createTextNode('Category Splitter Settings'));
			break;
		}
	}
}

/*=== Define Which Options are Selected by Default ===*/
var typeSelected = (splitType == 'type2') ? " selected='selected'" : "";
var categorySelected = (enableCategory == 'no') ? " selected='selected'" : "";
var categoryExpColSelected = (expColCategory == 'no') ? " selected='selected'" : "";
var newsFaderSelected = (enableNewsFader == 'no') ? " selected='selected'" : "";
var newsFaderExpColSelected = (expColNewsFader == 'no') ? " selected='selected'" : "";
var infoCenterSelected = (enableInfoCenter == 'no') ? " selected='selected'" : "";
var infoCenterExpColSelected = (expColInfoCenter == 'no') ? " selected='selected'" : "";

/*=== Create the New Page ===*/
if(location.href.match(/page=catsplit/i)){
	for(x=4; x<riTable.length; x++){
		riTable[x].style.display = 'none';
	}
	var tehNewTab = "<table width='92%' cellspacing='1' cellpadding='4' border='0' class='bordercolor' align='center'>";
	tehNewTab += "<td class='titlebg'><font size='2'><b>Category Splitter Settings</b></font></td></tr>";
	tehNewTab += "<tr><td class='windowbg'><font size='2'><b>General Settings</b><hr size='1'/>";
			tehNewTab += "<table width='650' cellpadding='2' cellspacing='2' border='0'><tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>URL of Head Image:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><input type='text' id='headurl' size='35' value='"+headImage+"'/></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>Enter in the URL of the head image that you wish to use for the splitter code.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Height of Head Image:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><input type='text' id='headheight' size='10' value='"+headImageHeight+"'/></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>Enter in the height of the head image that you chose here.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>URL of Base Image:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><input type='text' id='baseurl' size='35' value='"+baseImage+"'/></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>Enter in the URL of the base image that you wish to use for the splitter code.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Splitter Type:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='splittypesettings'><option value='type1'>Type 1</option><option value='type2'"+typeSelected+">Type 2</option></select></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>You have two options: Type 1 divides the categories and places the head image above the category and the base image below. Type 2 will turn the category bar into the head image.</font></td></tr>";
			tehNewTab += "</table><br/><font size='2'><b>Category Settings</b></font><hr size='1'>";
			tehNewTab += "<table width='650' cellpadding='2' cellspacing='2' border='0'><tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Enable Category Splitter:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='catsplitenable'><option value='yes'>Yes</option><option value='no'"+categorySelected+">No</option></select></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>By choosing \"no\", you will disable the category splitting option. You will still be able to choose if you want info center or news fader head/base images though.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Height of Gap:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><input type='text' id='catgapdistance' size='10' value='"+categoryDist+"'/></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>This is how much distance will be between each category. Only needed if you enabled this feature.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Enable Expand/Collapse Feature:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='catsplitexpcol'><option value='yes'>Yes</option><option value='no'"+categoryExpColSelected+">No</option></select></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>If you select \"Yes\", you will enable this code's advanced expand/collapse with memory feature for the categories.</font></td></tr>";
			tehNewTab += "</table><br/><font size='2'><b>News Fader Settings</b></font><hr size='1'>";
			tehNewTab += "<table width='650' cellpadding='2' cellspacing='2' border='0'><tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Enable News Fader Split:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='nfenable'><option value='yes'>Yes</option><option value='no'"+newsFaderSelected+">No</option></select></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>This feautre will split the news fader from the rest of the boards and add head/base images.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Height of Split:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><input type='text' id='newsgapdistance' size='10' value='"+newsFaderDist+"'/></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>This is how much distance you want between the news fader and the boards.</font></td></tr>";
			tehNewTab += "<td width='160' valign='top'><font size='-1'>Enable Expand/Collapse Feature:</font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='newsfaderexpcol'><option value='yes'>Yes</option><option value='no'"+newsFaderExpColSelected+">No</option></select></font></td>";
			tehNewTab += "<td width='245' valign='top'><font size='-2'>If you select \"Yes\", you will enable this code's advanced expand/collapse with memory feature for the news fader.</font></td></tr>";
			tehNewTab += "</table><br/><font size='2'><b>Info Center Settings</b></font><hr size='1'>";
			tehNewTab += "<table width='650' cellpadding='2' cellspacing='2' border='0'><tr>";
	tehNewTab += "<td width='160' valign='top'><font size='-1'>Enable Info Center Head/Base:</font></td>";
	tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='icenable'><option value='yes'>Yes</option><option value='no'"+infoCenterSelected+">No</option></select></font></td>";
	tehNewTab += "<td width='245' valign='top'><font size='-2'>This feature will add head/base images to the info center when active.</font></td></tr>";
	tehNewTab += "<td width='160' valign='top'><font size='-1'>Enable Expand/Collapse Feature:</font></td>";
	tehNewTab += "<td width='245' valign='top'><font size='-1'><select id='icexpcol'><option value='yes'>Yes</option><option value='no'"+infoCenterExpColSelected+">No</option></select></font></td>";
	tehNewTab += "<td width='245' valign='top'><font size='-2'>If you select \"Yes\", you will enable this code's advanced expand/collapse with memory feature for the info center.</font></td></tr>";
	tehNewTab += "</table><br/><input type='submit' value='Save Settings' onClick='updateCatSplit();' /></td></tr></table>";
	document.write(tehNewTab);
}

/*=== Change Information on Successful Update ===*/
if(document.referrer.match(/page=catsplit/i)){
	for(x=0; x<riTd.length; x++){
		if(riTd[x].innerHTML.match(/headers and footers have/i) && riTd[x].className.match(/windowbg/i)){
			riTd[x].innerHTML = riTd[x].innerHTML.replace('Your headers and footers have been successfully updated.','Your category splitter settings have been successfully upadated.');
		}
	}
}

/*=== Update the Main Header Information ===*/
function updateCatSplit(){
	for(x=0; x<document.forms.length; x++){
		if(document.forms[x].header){
			var hreplace = document.forms[x].header;
			var headURL = document.getElementById('headurl').value;
			var headHeight = document.getElementById('headheight').value;
			var baseURL = document.getElementById('baseurl').value;
			var splitSettings = document.getElementById('splittypesettings').value;
			var catSplitEnable = document.getElementById('catsplitenable').value;
			var catGapDistance = document.getElementById('catgapdistance').value;
			var categoryExpCol = document.getElementById('catsplitexpcol').value;
			var newsFaderEnable = document.getElementById('nfenable').value;
			var newsGap = document.getElementById('newsgapdistance').value;
			var nfExpCol = document.getElementById('newsfaderexpcol').value;
			var icEnable = document.getElementById('icenable').value;
			var icExpCol = document.getElementById('icexpcol').value;
			hreplace.value = hreplace.value.replace(/var enableAny = ".*"/i,'var enableAny = "yes"');
			hreplace.value = hreplace.value.replace(/var headImage = ".*"/i,'var headImage = "'+headURL+'"');
			hreplace.value = hreplace.value.replace(/var headImageHeight = ".*"/i,'var headImageHeight = "'+headHeight+'"');
			hreplace.value = hreplace.value.replace(/var baseImage = ".*"/i,'var baseImage = "'+baseURL+'"');
			hreplace.value = hreplace.value.replace(/var splitType = ".*"/i,'var splitType = "'+splitSettings+'"');
			hreplace.value = hreplace.value.replace(/var enableCategory = ".*"/i,'var enableCategory = "'+catSplitEnable+'"');
			hreplace.value = hreplace.value.replace(/var categoryDist = ".*"/i,'var categoryDist = "'+catGapDistance+'"');
			hreplace.value = hreplace.value.replace(/var expColCategory = ".*"/i,'var expColCategory = "'+categoryExpCol+'"');
			hreplace.value = hreplace.value.replace(/var enableNewsFader = ".*"/i,'var enableNewsFader = "'+newsFaderEnable+'"');
			hreplace.value = hreplace.value.replace(/var newsFaderDist = ".*"/i,'var newsFaderDist = "'+newsGap+'"');
			hreplace.value = hreplace.value.replace(/var expColNewsFader = ".*"/i,'var expColNewsFader = "'+nfExpCol+'"');
			hreplace.value = hreplace.value.replace(/var enableInfoCenter = ".*"/i,'var enableInfoCenter = "'+icEnable+'"');
			hreplace.value = hreplace.value.replace(/var expColInfoCenter = ".*"/i,'var expColInfoCenter = "'+icExpCol+'"');
			document.forms[x].submit();
		}
	}
}

/*=== Define Expand/Collapse Function ===*/
function expCol(id){
	var tehId = document.getElementById(id);
	tehId.style.display = (tehId.style.display == 'none') ? '' : 'none';
	var dispInfo = (tehId.style.display == 'none') ? '1' : '0';
	var d = new Date();
	d.setTime(d.getTime()+2592000000);
	var exp = d.toString();
	document.cookie = id+"="+dispInfo+"; expires="+exp+";";
}

/*=== Call the Necessary Scripts ===*/
if(enableAny == "yes"){
	if(location.href.match(/action=home/i) || !location.href.match(/(action|board)=/i)){
		if(enableCategory == "yes"){
			document.write(script1 + "catsplit" + script2);
		}
		if(enableNewsFader == "yes"){
			document.write(script1 + "newsfader" + script2);
		}
		if(enableInfoCenter == "yes"){
			document.write(script1 + "infocenter" + script2);
		}
	}
}