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

checkBrowserWidth();
attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	initLinks();
	initMenu();
	addSquares();
	//geo();
	if(typeof loadMap==='function')
		loadMap();
}

function resizePage()
{
	var enddate = new Date("December 31, 2060");
	document.cookie = "res_layout=" + ";expires=" + enddate.toGMTString();
	checkBrowserWidth();
	if(typeof loadMap==='function')
		loadMap();
}

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



function addSquares()
{
	
	
	var b = document.getElementsByTagName("body");
 	theBody = b[0];
 	
 	var wrapper = document.getElementById("wrapper1");
 	
	var square1 = document.createElement('div');
	setClass(square1,"square");
	
	var square2 = document.createElement('div');
	setClass(square2,"square");
	
	var square3 = document.createElement('div');
	setClass(square3,"square");
	
	var square4 = document.createElement('div');
	setClass(square4,"square");
	
	var square5 = document.createElement('div');
	setClass(square5,"square");
	
	var strip = document.createElement('div');
	setClass(strip,"strip");
		
	
/*
	square1.appendChild(document.createTextNode('1'));
	square2.appendChild(document.createTextNode('2'));
	square3.appendChild(document.createTextNode('3'));
	square4.appendChild(document.createTextNode('4'));
	square5.appendChild(document.createTextNode('5'));
*/	
	
	var randomnumber=Math.ceil(Math.random()*6);
	var theWidth = getBrowserWidth();

	
	switch(randomnumber)
	{
		
		case 1:
		
			square1.style.left = theWidth <= 1056 ? "194px" : "290px";
			square1.style.top = "192px";
			theBody.insertBefore(square1,wrapper);
			
			square2.style.left = theWidth <= 1056 ? "354px" : "450px";
			square2.style.top = "160px";
			theBody.insertBefore(square2,wrapper);
			
			square3.style.left = theWidth <= 1056 ? "610px" : "706px";
			square3.style.top = "192px";
			theBody.insertBefore(square3,wrapper);
			
			square4.style.left = theWidth <= 1056 ? "226px" : "322px";
			square4.style.top = "288px";
			theBody.insertBefore(square4,wrapper);
			
			square5.style.left = theWidth <= 1056 ? "706px" : "802px";
			square5.style.top = "160px";
			theBody.insertBefore(square5,wrapper);
			
			break;
			
			    
		case 2:
			square1.style.left = theWidth <= 1056 ? "162px" : "258px";
			square1.style.top = "160px";
			theBody.insertBefore(square1,wrapper);
			
			square2.style.left = theWidth <= 1056 ? "354px" : "450px";
			square2.style.top = "192px";
			theBody.insertBefore(square2,wrapper);
			
			square3.style.left = theWidth <= 1056 ? "610px" : "706px";
			square3.style.top = "224px";
			theBody.insertBefore(square3,wrapper);
			
			square4.style.left = theWidth <= 1056 ? "194px" : "290px";
			square4.style.top = "288px";
			theBody.insertBefore(square4,wrapper);
			
			square5.style.left = theWidth <= 1056 ? "802px" : "898px";
			square5.style.top = "160px";
			theBody.insertBefore(square5,wrapper);
			
			break;
			
		case 3:
	
			square1.style.left = theWidth <= 1056 ? "194px" : "290px";
			square1.style.top = "192px";
			theBody.insertBefore(square1,wrapper);
			
			square2.style.left = theWidth <= 1056 ? "482px" : "578px";
			square2.style.top = "160px";
			theBody.insertBefore(square2,wrapper);
			
			square3.style.left = theWidth <= 1056 ? "610px" : "706px";
			square3.style.top = "224px";
			theBody.insertBefore(square3,wrapper);
			
			square4.style.left = theWidth <= 1056 ? "162px" : "258px";
			square4.style.top = "288px";
			theBody.insertBefore(square4,wrapper);
			
			square5.style.left = theWidth <= 1056 ? "834px" : "930px";
			square5.style.top = "160px";
			theBody.insertBefore(square5,wrapper);
			
			break;
			 
		case 4:
			square1.style.left = theWidth <= 1056 ? "224px" : "320px";
			square1.style.top = "160px";
			theBody.insertBefore(square1,wrapper);
			
			square2.style.left = theWidth <= 1056 ? "544px" : "640px";
			square2.style.top = "224px";
			theBody.insertBefore(square2,wrapper);
			
			square3.style.left = theWidth <= 1056 ? "610px" : "706px";
			square3.style.top = "192px";
			theBody.insertBefore(square3,wrapper);
			
			square4.style.left = theWidth <= 1056 ? "194px" : "290px";
			square4.style.top = "256px";
			theBody.insertBefore(square4,wrapper);
			
			square5.style.left = theWidth <= 1056 ? "770px" : "866px";
			square5.style.top = "192px";
			theBody.insertBefore(square5,wrapper);
			
			break;
			
		case 5:
			square1.style.left = theWidth <= 1056 ? "258px" : "354px";
			square1.style.top = "224px";
			theBody.insertBefore(square1,wrapper);
			
			square2.style.left = theWidth <= 1056 ? "482px" : "578px";
			square2.style.top = "160px";
			theBody.insertBefore(square2,wrapper);
			
			square3.style.left = theWidth <= 1056 ? "546px" : "642px";
			square3.style.top = "258px";
			theBody.insertBefore(square3,wrapper);
			
			square4.style.left = theWidth <= 1056 ? "162px" : "258px";
			square4.style.top = "192px";
			theBody.insertBefore(square4,wrapper);
			
			square5.style.left = theWidth <= 1056 ? "770px" : "866px";
			square5.style.top = "192px";
			theBody.insertBefore(square5,wrapper);
			
			break;
			
			
		case 6:
			square1.style.left = theWidth <= 1056 ? "256px" : "352px";
			square1.style.top = "288px";
			theBody.insertBefore(square1,wrapper);
			
			square2.style.left = theWidth <= 1056 ? "610px" : "704px";
			square2.style.top = "160px";
			theBody.insertBefore(square2,wrapper);
			
			square3.style.left = theWidth <= 1056 ? "546px" : "642px";
			square3.style.top = "258px";
			theBody.insertBefore(square3,wrapper);
			
			square4.style.left = theWidth <= 1056 ? "162px" : "258px";
			square4.style.top = "192px";
			theBody.insertBefore(square4,wrapper);
			
			square5.style.left = theWidth <= 1056 ? "770px" : "866px";
			square5.style.top = "192px";
			theBody.insertBefore(square5,wrapper);
			
			break;
			 
	}
	
	
	theBody.insertBefore(strip,wrapper);
}




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 getBrowserHeight()
{
	if (window.innerHeight)
		return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight != 0)
		return document.documentElement.clientHeight;
	else if (document.body)
		return document.body.clientHeight;
	
	return 0;
}

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 popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=550,height=550');");
}



function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		return false;
	}
	
	return true;
};





/* 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;
};



/* 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"), "");	
	}
}

/* Author: Rebecca Skeers */
function getPreviousSibling (elem)
{
	elem = elem.previousSibling;
	
	while (elem != null && (elem.tagName == null || elem.tagName == 'undefined')) 
	{
		elem = elem.previousSibling;
	}

	return elem;
}

/* Author: Rebecca Skeers */
function getNextSibling (elem)
{
	elem = elem.nextSibling;
	
	while (elem != null && (elem.tagName == null || elem.tagName == 'undefined')) 
	{
		elem = elem.nextSibling;
	}

	return elem;
}


function printPage() 
{
  	if (window.print)
		window.print()
	else
		alert("Sorry, your browser doesn't support the print feature. Use the File menu on your browser to select Print.");
}






function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)res_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	if (theWidth <= 1056)
	{
		setStylesheet("Small screen");
		var enddate = new Date("December 31, 2060");
		document.cookie = "res_layout=" + escape("Small screen") + ";expires=" + enddate.toGMTString();
	}
	else
	{
		setStylesheet("");
		var enddate = new Date("December 31, 2060");
		document.cookie = "res_layout=" + ";expires=" + enddate.toGMTString();
	}
	
	return true;
};




function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};




function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};