/* ===========================================================================
 * SCHEDULE THE BEHAVIOURS
 * =========================================================================== 
 */

var numBannerImages=3; 
 
 
 
attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	initLinks();
	initMenu();
	initBannerImage();
	initShowMore();
	if(typeof loadMap==='function')
		loadMap();
}


function resizePage()
{
	if(typeof loadMap==='function')
		loadMap();
}

function unloadPage() 
{
	if(typeof GUnload==='function')
		GUnload();
}


function resizePage()
{

}

function unloadPage() 
{

}



function initShowMore()
{
	var morediv = document.getElementById("more");
	
	
	if(morediv) 
	{
	
	/*
	 	var theNewParagraph = document.createElement('p');
	 	var theNewAnchor = document.createElement('a');
	 	theNewAnchor.setAttribute("href", "#more"+i);
	 	theNewAnchor.setAttribute("title", "Read the full description");
		var theTextOfTheParagraph = document.createTextNode('Read more...');
		theNewAnchor.appendChild(theTextOfTheParagraph);
		theNewParagraph.appendChild(theNewAnchor);
	*/	
		var theNewParagraph1 = document.createElement('p');
		var theNewAnchor1 = document.createElement('a');
		theNewAnchor1.setAttribute("href", "#more");
		theNewAnchor1.setAttribute("title", "Hide the full description");
		var theTextOfTheParagraph1 = document.createTextNode('Hide full description...');
		theNewAnchor1.appendChild(theTextOfTheParagraph1);
		theNewParagraph1.appendChild(theNewAnchor1);
		
	//	morediv.parentNode.insertBefore(theNewParagraph,morediv);
		
		morediv.appendChild(theNewParagraph1);
 		//setClass(morediv,"hidden");
 		
 		
 		showAnchor = document.getElementById("showmore");
 		
		showAnchor.onclick = function() 
    	{
      		showMore();
      		return false;
    	};
    	
    	theNewAnchor1.onclick = function() 
    	{
      		showLess();
    	};
    	
		
		
	}	
}

/* Author: Rebecca Skeers */
function setClass(el, str)
{
	if(el)
	{
		if (el.className == "")
			el.className = str;
		else
			el.className += " "+str;	
	}	
	return true;
}

/* Author: Rebecca Skeers */
function removeClass(el, str)
{
	if(el)
	{
		el.className=el.className.replace(new RegExp(" "+str+"\\b"), "");
		el.className=el.className.replace(new RegExp(str+"\\b"), "");	
	}
}

function showMore()
{
	showAnchor = document.getElementById("showmore");
	morediv = document.getElementById("more");
	setClass(showAnchor.parentNode,"hidden");
	setClass(morediv,"visible");
}


function showLess()
{
	showAnchor = document.getElementById("showmore");
	morediv = document.getElementById("more");
	removeClass(showAnchor.parentNode,"hidden");
	removeClass(morediv,"visible");
	
	/*
	
	morediv = para.parentNode;
	showdiv = morediv.previousSibling;	
	//setClass(morediv,"hidden");
	removeClass(showdiv,"hidden");
	removeClass(morediv,"visible");
	//setClass(showdiv,"visible");
	
	*/
}



function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		
 	var b = document.getElementsByTagName("body");
 	theBody = b[0];
 	if(theBody.className.match("popup"))
 	{
	 	is_popup=true;
 		 window.focus();
	}
 
 	var anchors = document.getElementsByTagName("a");
 	for (var i=0; i<anchors.length; i++) 
 	{
   		var anchor = anchors[i];
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
   		{
     		anchor.target = "_blank";
 		}	
     	else if (anchor.className.match("print")) 
     	{
        	anchor.onclick = function() 
        	{
          		printPage();
          		return false;
        	};
    	}
    	else if (anchor.className.match("window")) 
     	{
        	anchor.onclick = function() 
        	{
          		popUp(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("file")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.open(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("close")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.close();
          		return false;
        	};
    	}
 	}
}



function initBannerImage()
{
	var div = document.getElementById("branding");
	
	randomNumber = Math.ceil(Math.random() * numBannerImages);
	
	if (div.className == "")
		div.className = "banner"+randomNumber;
	else
		div.className += " banner"+randomNumber;
};


function initMenu() 
{
	var nav = document.getElementById("navigation");
	
	if(nav != null)
	{
		var listItems = nav.getElementsByTagName("li");
	
		for (var i=0; i<listItems.length; i++) 
		{
			listItems[i].onmouseover=function() 
			{
				if(this.className == "")
					this.className="hover";
				else
					this.className+=" hover";
			}
			
			listItems[i].onmouseout=function() 
			{
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
				this.className=this.className.replace(new RegExp("hover\\b"), "");
			}
			
			listItems[i].onfocus=function() 
			{
				if(this.className == "")
					this.className="hover";
				else
					this.className+=" hover";
			}
			
			listItems[i].onblur=function() 
			{
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
				this.className=this.className.replace(new RegExp("hover\\b"), "");
			}
		}
	}
}



/* The JavaScript Anthology - James Edwards & Cameron Adams */
function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{ 
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event)
		{
			if(typeof event == "undefined")
			{
				event = window.event
			};

			target["e" + functionString](event);
        };
		target.attachEvent("on" + eventType, target[functionString]);
	}
	else
	{
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
			target[eventType] = function()
			{
				oldListener();
				return functionRef();
			}
		}
		else
		{
			target[eventType] = functionRef;
		}
	}

	return true;
};
