window.onload = WindowOnload;

function WindowOnload() {
     //SetFontSize();
	 ShowFunctions(true, true, true);
	 FeedArchive();
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

/*
	Examples of how to call the function:
	
	To get all a elements in the document with a "info-links" class:
    getElementsByClassName(document, "a", "info-links");
    
	To get all div elements within the element named "container", with a "col" and a "left" class:
    getElementsByClassName(document.getElementById("container"), "div", ["col", "left"]);
*/

function ShowFunctions(iprint, idown, iup) {
	functionIcons = getElementsByClassName(document, "div", "function-icons");
	if (typeof(RadEditorCommandList) != "undefined") {
		for (i=0; i<functionIcons.length; i++) {
			functionIcons[i].style.display = "none";
		}
	}
	else {
		for (i=0; i<functionIcons.length; i++) {
			for (a=0; a<functionIcons[i].childNodes.length; a++) {
				if (functionIcons[i].childNodes[a].tagName == "UL") {
					functionList = functionIcons[i].childNodes[a];
				}
			}
			if (iprint == true) {
				functionPrint = document.createElement("li");
				functionPrint.className = "func_print";
				functionPrint.innerHTML = "<a href=\"#Print_Page\" onclick=\"return PrintPage();\" title=\"Print this page\">Print this page<\/a>";
				functionList.appendChild(functionPrint);
			}
			if (idown == true) {
				functionDown = document.createElement("li");
				functionDown.className = "func_down";
				functionDown.innerHTML = "<a href=\"#Resize_Down\" onclick=\"return ResizeDown();\" title=\"Reduce font size\">Reduce font size<\/a>";
				functionList.appendChild(functionDown);
			}
			if (iup == true) {
				functionUp = document.createElement("li");
				functionUp.className = "func_up";
				functionUp.innerHTML = "<a href=\"#Resize_Up\" onclick=\"return ResizeUp();\" title=\"Increase font size\">Increase font size<\/a>";
				functionList.appendChild(functionUp);
			}
		}
	}
}

function PrintPage() {
	window.print();
	return false;
}

function SetFontSize() {
	fontCookie = ReadCookie("fontSize");
	if (fontCookie) {
		document.getElementById("cim_page-wrapper").style.fontSize = "" + fontCookie + "";
	}
}

function ResizeUp() {
	if (!document.getElementById("cim_page-wrapper").style.fontSize) {
		document.getElementById("cim_page-wrapper").style.fontSize = '110%';
		CreateCookie("fontSize", "110%", 365);
	}
	else {
		var str_fontsize = document.getElementById("cim_page-wrapper").style.fontSize;
		var num_fontsize = str_fontsize.substring(0,str_fontsize.length-1);
		var num_newsize = parseInt(num_fontsize) + 10;
		document.getElementById("cim_page-wrapper").style.fontSize = num_newsize + '%';
		CreateCookie("fontSize", num_newsize + '%', 365);
	}
	return false;
}

function ResizeDown() {
	if (!document.getElementById("cim_page-wrapper").style.fontSize) {
		document.getElementById("cim_page-wrapper").style.fontSize = '90%';
		CreateCookie("fontSize", "90%", 365);
	}
	else {
		var str_fontsize = document.getElementById("cim_page-wrapper").style.fontSize;
		var num_fontsize = str_fontsize.substring(0,str_fontsize.length-1);
		var num_newsize = parseInt(num_fontsize) - 10;
		if (num_newsize > 0) {
			document.getElementById("cim_page-wrapper").style.fontSize = num_newsize + '%';
			CreateCookie("fontSize", num_newsize + '%', 365);
		}
	}
	return false;
}

function SetHPPanel() {
	hpPanelCookie = ReadCookie("hpPanel");
	if (hpPanelCookie) {
		ToggleInfoPanel(hpPanelCookie,hpPanelCookie)
	}
}

function ToggleInfoPanel(element, linkID) {
	if (!linkID) {
		parentID = element.href.split("#panel_")[1];
	}
	else {
		parentID = linkID;
	}
	infoLinks = document.getElementById("nav_info").getElementsByTagName("li");
	for (i=0; i<infoLinks.length; i++) {
		targetID = infoLinks[i].getElementsByTagName("a")[0].href.split("#")[1];
		target = document.getElementById(targetID);
		if (target){
			if (infoLinks[i].className == "active") {
				infoLinks[i].className = "";
				target.className = "hp_info_panel";
			}
			if (infoLinks[i].id == parentID) {
				infoLinks[i].className = "active";
				target.className = "hp_info_panel active";
				CreateCookie("hpPanel", parentID);
			}
		}
	}
	if (!linkID) {
		element.blur();
	}
	return false;
}

function CreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function AddPerson(button) {
	person2 = document.getElementById("person_2");
	person3 = document.getElementById("person_3");
	personComments = document.getElementById("person_comments");
	
	if ((person2) && (person2.className != "visible")) {
		person2.className = "visible";
	}
	else if ((person3) && (person3.className != "visible")) {
		person3.className = "visible";
		button.value = "Add additional comments to suspicious people";
	}
	else if ((personComments) && (personComments.className != "visible")){
	    personComments.className = "visible";
	    button.style.display = "none";
	}
	return false;
}

function AddVehicle(button) {
	vehicle2 = document.getElementById("vehicle_2");
	vehicle3 = document.getElementById("vehicle_3");
	vehicleComments = document.getElementById("vehicle_comments");
	
	if ((vehicle2) && (vehicle2.className != "visible")) {
		vehicle2.className = "visible";
	}
	else if ((vehicle3) && (vehicle3.className != "visible")) {
		vehicle3.className = "visible";
		button.value = "Add additional comments to suspicious vehicles";
	}
	else if ((vehicleComments) && (vehicleComments.className != "visible")){
	    vehicleComments.className = "visible";
	    button.style.display = "none";
	}
	return false;
}

function FeedArchive() {
	feedArchive = document.getElementById("feed_archive")
	if (feedArchive) {
		feedArchiveYears = feedArchive.getElementsByTagName("strong");
		for (i=0; i<feedArchiveYears.length; i++) {
			feedArchiveYears[i].parentNode.onclick = function() {
				if (this.parentNode.className == "active") {
					this.parentNode.className = "";
				}
				else {
					this.parentNode.className = "active";
				}
				return false;
			}
		}
	}
}