/* 
	Author: marele, Framfab
	31/08/05
*/

/*
	Page Constructor. 
	Is initiated in file: initpage.js
*/
function Page()
{
	this.initialized = false;
}

/*
	Page inherits from Utils
*/
Page.prototype = new Utils();

/*
	Initializes the page by running all functions.
	Is run in file: initpage.js
*/
Page.prototype.init = function()
{
	if(!this.initialized){
		this.initialized = true;
		this.init_Menu("main-menu", false);
		this.init_FallBackStyles(["main-menu","sub-menu"]);
		this.init_Form();
		this.init_ArrowLinks();
	}
}
/*
	Adds extra padding to all <a>'s in the menu to make them expand to the full width of the page.
	@param sMenuId String : id of the menu to work on
*/
Page.prototype.init_Menu = function(sMenuId, bCreateSpacing)
{
	if (bCreateSpacing){
		var o = this.getObj(sMenuId);
		if(o){
			var oUL = o.getElementsByTagName("UL")[0];
			if(oUL){
				var iULWidth = oUL.offsetWidth;
				var aLis = oUL.getElementsByTagName("LI");
				var iTotal = this.getAllItemsLength(aLis);
				var iLeftToShare = iULWidth - iTotal;
				var iAddToA = Math.floor((iLeftToShare/aLis.length)/2);
			
				for(var i=0; i<aLis.length; i++){
					var aHref = aLis[i].firstChild;
					var iCurPad = this.getPadding(aHref,"Left");
					var iAdd = iCurPad + iAddToA + 1;
					aHref.style.paddingLeft = iAdd + "px";
					aHref.style.paddingRight = iAdd + "px";
				}
				
				if(this.getAllItemsLength(aLis) < iULWidth){
					var iRestPixels = iULWidth - this.getAllItemsLength(aLis);
					var oLastA = aLis[aLis.length-1].firstChild;
					var iExistingPaddingLeft = this.getPadding(oLastA,"Left");
					oLastA.style.paddingLeft = (iRestPixels + iExistingPaddingLeft) + "px";
				}
			}
		}
	}
	document.getElementsByTagName("body")[0].style.visibility = "visible";
}

/*
  Intializes all links on the page that has the classname "arrow"
*/
Page.prototype.init_ArrowLinks = function()
{
	var oLinks = document.getElementsByTagName("a");
	var oSpanArrow = document.createElement("span");
	var oContent = document.createTextNode(" ");
	oSpanArrow.id = "oImgCloneTarget";
	oSpanArrow.className = "span-arrow";
	oSpanArrow.appendChild(oContent);
	document.body.appendChild(oSpanArrow);
	for(var i=0; i<oLinks.length; i++){
		if(oLinks[i].className.indexOf("arrow") != -1){
			var myCloneNode = document.getElementById("oImgCloneTarget").cloneNode(true)
			myCloneNode.removeAttribute("id");
			oLinks[i].appendChild(myCloneNode);
			
			oLinks[i].onmouseover = function (){ var nt = this.childNodes[0].nodeType
													var thisNode;
												if(this.childNodes[0].nodeType==3){
													thisNode = this.childNodes[1];
												}else{
													thisNode = this.childNodes[0];
												}
												thisNode.className = "span-arrow-hover"; }
			oLinks[i].onmouseout  = function (){ // check the first node id it is a textnode and not an element node get the next one, firefox and ie do not have the same dom structure.
												var nt = this.childNodes[0].nodeType
													var thisNode;
												if(this.childNodes[0].nodeType==3){
													thisNode = this.childNodes[1];
												}else{
													thisNode = this.childNodes[0];
												}
												thisNode.className = "span-arrow"; }
		}
	}
}
Page.prototype.goToURL = function()
{
	window.location.href = this.getAttribute("href");
	return false; 
}
/*
	1) Initializes any form on the page (adds eventhandlers to its buttons).
	   Conditioned by: 1) <input> must be of type "submit" of "reset", 2) <input>'s class-attribute must contain "btn".
	2) Adds an onsubmit to the form if its className-attribute contains "validate-empty-fields". The onsubmit returns
	   false if any field with the type "text" or "password" is empty and appends " not-valid" to its className. If the
	   the value is not empty the className " not-valid" is removed.
*/
Page.prototype.init_Form = function()
{
	var oThis = this;
	var aForms = document.body.getElementsByTagName("form");

	for(var i=0; i<aForms.length; i++){
		var oForm = aForms[i];
		
		//
		try {
			var sFocusElementName = oForm.getAttribute("focus");
			if(sFocusElementName != ""){
				oForm.elements[sFocusElementName].focus();
			}
		} catch(e){ }
		//
		var oInp,sAttFor,sAttAccesskey,bAddEvent=false,bMatch=false,oMatchedLabel=null;
		var aInps = oForm.getElementsByTagName("input");
		var aLabels = oForm.getElementsByTagName("label");
		for(var j=0; j<aInps.length; j++){
			oInp = aInps[j];
			if( (oInp.type == "submit" || oInp.type == "reset" || oInp.type == "button") && oInp.className.indexOf("btn") != -1) addEventHandlers(oInp);
			
			// Only add validation handlers to text and password fields
			if(oInp.type == "text" || oInp.type == "password") oInp.onfocus = function (){
				if(this.className.indexOf("not-valid") != -1) this.className = this.className.replace(" not-valid",""); 
			}
			for(var r=0; r<aLabels.length; r++){
					var q = aLabels[r].attributes["for"];
					q = q.value;
					if(oInp.id == q){
						oMatchedLabel= aLabels[r];
						bMatch = true;
						break;
					}
			}
			if(bMatch && (oInp.type == "text" || oInp.type == "password")){
				if(oInp.getAttribute("accesskey") != ""){
					oLabel = oMatchedLabel;
					oAttFor = oLabel.attributes["for"];
					sAttFor = oAttFor.value;
					sAttAccesskey = oInp.getAttribute("accesskey");
					
					if(sAttFor == oInp.getAttribute("id") && sAttAccesskey != ""){
						var sLabVal = oLabel.firstChild.nodeValue;
						// loop through strings chars
						for(var h=0; h<sLabVal.length; h++){
							if(sLabVal.charAt(h).toLowerCase() == sAttAccesskey.toLowerCase()){
								
								// underlined node
								var oCharNode = document.createElement("span");
								var oChar = document.createTextNode(sLabVal.charAt(h));
								oCharNode.appendChild(oChar);
								//oCharNode.style.fontWeight = "bold";
								oCharNode.style.textDecoration = "underline";
								oCharNode.style.display = "none";
								oCharNode.className = "showAltKey";
								oLabel.firstChild.splitText(h);
								oLabel.insertBefore(oCharNode,oLabel.firstChild.nextSibling);
								
								// normal node
								oLabel.childNodes[2].splitText(1);
								oCharNode = document.createElement("span");
								var oChar2 = document.createTextNode(sLabVal.charAt(h));
								oCharNode.appendChild(oChar2);
								oCharNode.style.display = "inline";
								oCharNode.className = "hideAltKey";
								oLabel.insertBefore(oCharNode,oLabel.childNodes[3]);
								oLabel.removeChild(oLabel.childNodes[2]);
								
								bAddEvent = true;
								
								break;
							}
						}
					}
				}
			}
			if(bAddEvent){
				document.onkeydown = function(E){
					E = window.event ? window.event : E;
					if(E.keyCode == 18){ // 18 = altKey
							for(var i=0; oSpan = document.getElementsByTagName("span")[i]; i++){
								if(oSpan.className.indexOf("showAltKey") != -1){
									oSpan.style.display = "inline";
								} else if(oSpan.className.indexOf("hideAltKey") != -1){
									oSpan.style.display = "none";
								}
							}
					}
				};
				document.onkeyup = function(E){
					E = window.event ? window.event : E;
					if(E.keyCode == 18){ // 18 = altKey
						for(var i=0; oSpan = document.getElementsByTagName("span")[i]; i++){
							if(oSpan.className.indexOf("showAltKey") != -1){
								oSpan.style.display = "none";
							} else if(oSpan.className.indexOf("hideAltKey") != -1){
								oSpan.style.display = "inline";
							}
						}
					}
				};
			}
			
			//
			
		}
		//
		if(oForm.className.indexOf("validate-empty-fields") != -1){
			oForm.onsubmit = function (){
				return oThis.checkEmptyVals(this);
			}
		}
	}
	function addEventHandlers(o){
		o.onmouseover = function (){ this.className += " hover"; }
		o.onmouseout  = function (){ this.className = this.className.replace(" hover",""); }
		o.onfocus 	  = function (){ this.className += " hover"; }
		o.onblur 	  = function (){ this.className = this.className.replace(" hover",""); }
	}
}
Page.prototype.checkEmptyVals = function(o)
{
	var aBtns = o.getElementsByTagName("input");
	var bValid = true; 
	for(var i=0; i<aBtns.length; i++){
		try {
			var oB = aBtns[i];
			if(oB.type == "text" || oB.type == "password"){
				if(oB.value == ""){
					oB.className += " not-valid";
					bValid = false;
				}
			}
		} catch(e){
			//alert(e.type)
		}
	}
	return bValid;
}
/*
	Hardcoding inline styles for <a>-tags for browsers who can not dynamically retrieve styles
	from linked stylesheets, ala IE's "currentStyle" or MOZ's window.getComputedStyle.
	Conditioned by not supporting Object.currentStyle (IE - mac/win) and window.getComputedStyle (all Gecko-based browsers).
	
 	@param aMenuIds Array : An array of the id's of the menus you wish to search.
*/
Page.prototype.init_FallBackStyles = function(aMenuIds)
{
	if(!document.documentElement.currentStyle && !window.getComputedStyle){
		for(var p=0; p<aMenuIds.length; p++){
			if(o = this.getObj(aMenuIds[p])){
				var oLIs = o.getElementsByTagName("UL")[0].getElementsByTagName("LI");
				for(var i=0; i<oLIs.length; i++){
					for(var j=0; a = oLIs[i].getElementsByTagName("A")[j]; j++){
						a.style.marginLeft 	= p == 0 ? "-2px" : "0px";
						a.style.paddingLeft 	= "10px";
						a.style.paddingRight 	= "10px";
					}
				}
			}
		}
	}
}