/*	Page Layout Behaviours by Chris Constandinou, constantology@gmail.com, please give me credit if you use this, good karma to you regardless...	*/

	var docRoot = "http://constantology.com";
//	var docRoot = "http://dev.constantology.net";
	
// run function on load and resize
	if ( got.dom )
	{
		if ( got.ns ) document.getElementsByTagName( "html" ).className = "hide";
		else document.getElementsByTagName( "html" )[0].className = "hide";
		
		window.onload = init;
		if ( got.ie && widthStyle == "liquid") window.onresize = resize;
	}
	
	function init()
	{
//	declare objects
	
		if ( got.ns )
		{
			var oHTML = document.getElementsByTagName( "html" );
			var oBody = document.getElementsByTagName( "body" );
			var oHead = document.getElementsByTagName( "head" );
		}
		else
		{
			var oHTML = document.getElementsByTagName( "html" )[0];
			var oBody = document.getElementsByTagName( "body" )[0];
			var oHead = document.getElementsByTagName( "head" )[0];
		}
		var oMasterWpr = document.getElementById( "master-wpr" );
		var oTop = document.getElementById( "top" );
		var oDate = document.getElementById( "date" );
		var oNavigation = document.getElementById( "navigation" );
		var oLevel1 = document.getElementById( "level1" );
		var oCopy = document.getElementById( "copy" );
		var oTable = oCopy.getElementsByTagName( "table" );
		var oFooter = document.getElementById( "footer" );
		var theCookie = getCookie( "customClasses" );
		
		loadStyleSheet( docRoot + "css/init_hide.css", oHead );
		oMasterWpr.className = "domhide";
		
		if ( theCookie != null )
		{
			oBody.className = theCookie.substring( theCookie.indexOf( "bodyClasses=" ) + 12, theCookie.indexOf( "&masterwprClasses=" ) );
			
			oMasterWpr.className = theCookie.substring( theCookie.indexOf( "&masterwprClasses=" ) + 18, theCookie.indexOf( "&copyClasses=" ) );
			
			if ( oMasterWpr.className.indexOf( " " ) > -1 ) oMasterWpr.className = columnStyle + " " + oMasterWpr.className.substring( oMasterWpr.className.indexOf( " " ), oMasterWpr.className.length );
			else oMasterWpr.className = columnStyle;
			
			oCopy.className = theCookie.substring( theCookie.indexOf( "&copyClasses=" ) + 13, theCookie.length );
		}
		
		if ( oTop != null && oNavigation != null ) oTop.appendChild( oNavigation );
		
/*		if ( oLevel1 != null )
		{
			makeButtons( oLevel1 );
			addRolloverListeners( oLevel1 );
		}*/
		if ( oTable != null ) formatDataTables( oTable );
		if ( oCopy != null && !got.ns )
		{
			addMediaViewer( oCopy );
			makePopUps( oCopy );
		}
		if ( oFooter != null ) makeCustomiser( oFooter );
		
//	these values can be found in the file: layout.js
		if ( got.ie && widthStyle == "liquid" ) resize();
		else oBody.className = alignStyle + " " + widthStyle;
		
		oMasterWpr.className = columnStyle;
		
		oHTML.className = "showb";
	}
	
	function resize()
	{
//	declare objects
		var oBody = document.getElementsByTagName( "body" )[0];
		var ieWidthStyle = "narrow";
		
		oBody.clientWidth < 1024 ? ieWidthStyle = "narrow" : oBody.clientWidth < 1280 ? ieWidthStyle = "wide" : ieWidthStyle = "wider";
		oBody.className = alignStyle + " " + ieWidthStyle;
	}
	
//	JavaScript Functions
	function openWindow( url, winName, winAttr )
	{
		var popup = window.open( url, winName, winAttr );
	}
	
//	DOM mutator methods
	function swap( id )
	{		
		var obj = document.getElementById( id );
		var objArray = obj.src.split( ".gif" );
		if ( obj.src.indexOf( "_o.gif" ) > -1 ) obj.src = objArray[0].substring( 0, objArray[0].indexOf( "_o" ) ) + ".gif";
		else obj.src = objArray[0] + "_o.gif";
	}
	
	function loadStyleSheet( path, target )
	{
/* 	create the stylesheet to hold all the style for the DOM tranformations as...
	var styleSheet = document.createCSSStyleSheet( '/shared/css/design_dom.css', 'screen' );
	doesn't work in mozilla or IE.	*/
		var styleSheet = document.createElement( "link" );
		styleSheet.href = path;
		styleSheet.rel = "stylesheet";
		styleSheet.type = "text/css";
		target.appendChild( styleSheet );
	}
	
	function changeScroll()
	{
		var obj = document.getElementById( "copy" );
		var oBody = document.getElementsByTagName( "body" )[0];
		var oMasterWpr = document.getElementById( "master-wpr" );
		
		if ( obj.className != "fixedheight" ) obj.className = "fixedheight";
		else obj.className = "autoheight";
		
		setCookie( "customClasses", "bodyClasses=" + oBody.className + "&masterwprClasses=" + oMasterWpr.className + "&copyClasses=" + obj.className, window.location.hostname );
	}
	
	function changeBackground()
	{
		var obj = document.getElementById( "master-wpr" );
		var oBody = document.getElementsByTagName( "body" )[0];
		var oCopy = document.getElementById( "copy" );
		
		if ( obj.className.indexOf( "nobg" ) > -1 ) obj.className = obj.className.substring( 0, obj.className.indexOf( " " ) );
		else obj.className += " nobg";
		
		setCookie( "customClasses", "bodyClasses=" + oBody.className + "&masterwprClasses=" + obj.className + "&copyClasses=" + oCopy.className, window.location.hostname );
	}
	
	function increaseFont()
	{
		var obj = document.getElementsByTagName( "body" )[0];
		var oMasterWpr = document.getElementById( "master-wpr" );
		var oCopy = document.getElementById( "copy" );
		
		if ( obj.className.indexOf( "lg" ) > -1 )
		{
			var objClass = obj.className.substring( 0, obj.className.indexOf( " lg" ) );
			switch ( obj.className.substring( obj.className.indexOf( "lg" ), obj.className.indexOf( "lg" ) + 4 ) )
			{
				case "lg01" : obj.className = objClass + " lg02";
					break;
				case "lg02" : obj.className = objClass + " lg03";
					break;
				case "lg03" : return;
					break;
			}
		}
		else obj.className += " lg01";
		
		setCookie( "customClasses", "bodyClasses=" + obj.className + "&masterwprClasses=" + oMasterWpr.className + "&copyClasses=" + oCopy.className, window.location.hostname );
	}
	
	function decreaseFont()
	{
		var obj = document.getElementsByTagName( "body" )[0];
		var oMasterWpr = document.getElementById( "master-wpr" );
		var oCopy = document.getElementById( "copy" );
		
		if ( obj.className.indexOf( "lg" ) > -1 )
		{
			var objClass = obj.className.substring( 0, obj.className.indexOf( " lg" ) );
			switch ( obj.className.substring( obj.className.indexOf( "lg" ), obj.className.indexOf( "lg" ) + 4 ) )
			{
				case "lg01" : obj.className = objClass;
					break;
				case "lg02" : obj.className = objClass + " lg01";
					break;
				case "lg03" : obj.className = objClass + " lg02";
					break;
			}
		}
		
		setCookie( "customClasses", "bodyClasses=" + obj.className + "&masterwprClasses=" + oMasterWpr.className + "&copyClasses=" + oCopy.className, window.location.hostname );
	}

// DOM constructors
/*	function makeButtons( obj )
	{
		var anchorList = obj.getElementsByTagName( "a" );
		
		for ( var i = 0 ; i < anchorList.length ; i++ ) anchorList[i].innerHTML = '<img alt="' + anchorList[i].title + '" id="' + anchorList[i].title + '" src="/media/nav/top/' + anchorList[i].title + '.gif" />';
	}*/
	
/*	function addRolloverListeners( obj )
	{
		var anchorList = obj.getElementsByTagName( "a" );
		
		for ( var i = 0 ; i < anchorList.length ; i++ )
		{
 not working in IE attaches same event to each object rather than creating new one :(
			if ( got.ie )
			{
				var id = anchorList[i].getElementsByTagName( 'img' )[0].id
				
				anchorList[i].attachEvent( "onmouseover", function() { swap( id ); return false; } );
				anchorList[i].attachEvent( "onmouseout", function() { swap( id ); return false; } );
			}
			else
			{
				anchorList[i].setAttribute( "onmouseover", "swap( this.getElementsByTagName( 'img' )[0].id );" );
				anchorList[i].setAttribute( "onmouseout", "swap( this.getElementsByTagName( 'img' )[0].id );" );
//			}
		}
	}*/
	
	function formatDataTables( obj )
	{
//	make all tables with class="data-table" have interlaced rows
		if ( obj != null )
		{
			for ( i = 0 ; i < obj.length ; i++ )
			{
//	if the table is well formatted and has a tbody tag then do this
				if ( obj[i].className.indexOf( "data-table" ) > -1 )
				{
					tbodyTemp = obj[i].getElementsByTagName( "tbody" );
					if ( tbodyTemp != null )
					{
						for ( j = 0 ; j < tbodyTemp.length ; j++ )
						{
							trTemp = tbodyTemp[j].getElementsByTagName( "tr" );
							for ( k = 0 ; k < trTemp.length ; k++ ) if ( k % 2 != 0 ) trTemp[k].className += " even";
						}
					}
//	if this the table is not well formated do this
					else
					{
						trTemp = obj[i].getElementsByTagName( "tr" );
//	starts at (j = 1) because I'm assuming that the first row is a table header (th) tag 
						for ( j = 1 ; j < trTemp.length ; j++ ) if ( j % 2 == 0 ) trTemp[j].className += " even";
					}
				}
			}
		}
	}
	
	function addMediaViewer( obj )
	{
		var anchorList = obj.getElementsByTagName( "a" );
		
		var regex = /\/media\/.*\.(gif|jpg|png)$/i;
		
		for ( var i = 0 ; i < anchorList.length ; i++ ) if ( regex.test( anchorList[i].href ) ) anchorList[i].href = "/mediaviewer.html?media=" + anchorList[i].href;
	}
	
	var imagePopupAttr = "location=0,menubar=0,personalbar=0,resizable=1,scrollbars=1,status=0,titlebar=0,toolbar=0,height=450,width=576";
	
	function makePopUps( obj )
	{
		var anchorList = obj.getElementsByTagName( "a" );
		var aRel, aHref;
		
		for ( var i = 0 ; i < anchorList.length ; i++ )
		{
			if ( anchorList[i].rel.indexOf( "popup" ) > -1 )
			{
				aRel = anchorList[i].rel.split( "|" );
				
				if ( anchorList[i].rel.indexOf( "image" ) > -1 ) anchorList[i].setAttribute( "href", "javascript:openWindow( '" + anchorList[i].href + "','" + aRel[1] + "','" + imagePopupAttr + "' );" );
				else anchorList[i].setAttribute( "href", "javascript:openWindow( '" + anchorList[i].href + "','" + aRel[1] + "','" + aRel[2] + "' );" );
				
				anchorList[i].target = "";
			}
		}
	}
	
	function makeCustomiser( obj )
	{
		var oCustomiser = document.createElement( "div" );
		oCustomiser.id = "customiser";
		var oUL = document.createElement( "ul" );
		var oToolbar = document.createElement( "ul" );
		oToolbar.id = "toolbar";
		var oLi = document.createElement( "li" );
		oLi.innerHTML = "<span>Customise your experience</span>";
		
		var elLi = document.createElement( "li" );
		var elA = document.createElement( "a" );
		elA.setAttribute( "href", "javascript:changeScroll();" );
		elA.innerHTML = "change scroll style";
		elLi.appendChild( elA );
		oToolbar.appendChild( elLi );
		
		elLi = document.createElement( "li" );
		elA = document.createElement( "a" );
		elA.setAttribute( "href", "javascript:changeBackground();" );
		elA.innerHTML = "background images on/ off";
		elLi.appendChild( elA );
		oToolbar.appendChild( elLi );
		
		elLi = document.createElement( "li" );
		elA = document.createElement( "a" );
		elA.setAttribute( "href", "javascript:increaseFont();" );
		elA.innerHTML = "increase font size";
		elLi.appendChild( elA );
		oToolbar.appendChild( elLi );
		
		elLi = document.createElement( "li" );
		elA = document.createElement( "a" );
		elA.setAttribute( "href", "javascript:decreaseFont();" );
		elA.innerHTML = "decrease font size";
		elLi.appendChild( elA );
		oToolbar.appendChild( elLi );
		
		oLi.appendChild( oToolbar );
		oUL.appendChild( oLi );
		oCustomiser.appendChild( oUL );
		obj.appendChild( oCustomiser );

		var oItem = oToolbar.getElementsByTagName( "li" );
		
		oCustomiser.getElementsByTagName( "span" )[0].appendChild( document.createTextNode( " : " ) );
		
		for ( i = 0 ; i < oItem.length - 1 ; i++ )
		{
			elTemp = document.createTextNode( " | " );
			oItem[i].appendChild( elTemp );
		}
	}

//	cookie get and set methods
	function getCookie( name )
	{
		var index = document.cookie.indexOf( name + "=" );
		if ( index == -1 ) return null;
		index = document.cookie.indexOf( "=", index ) + 1; // first character
		var endstr = document.cookie.indexOf( ";", index );
		if ( endstr == -1 ) endstr = document.cookie.length; // last character
		return unescape( document.cookie.substring( index, endstr ) );
	 }
	
	function setCookie( name, value, domain )
	{
		if ( value != null && value != "" )
		{
			var date = new Date();
			date.setTime( date.getTime() + ( 365*24*60*60*2.5 ) );
			var expires = "; expires=" + date.toGMTString();
			document.cookie = name + "=" + value + expires + "; path=/;domain=" + domain +";";
		}
	}