/********************************************************************
	Sniffing the browser type based on code from CodeHouse.
	You can obtain this script at http://www.codehouse.com
********************************************************************/
function CJL_BrowserSniffer()
{
	var ua = navigator.userAgent;
	this.isOpera = function()
	{
		return /Opera/.test(ua);
	}

	this.isSafari = function()
	{
		return /Safari/.test(ua);
	}

	this.isGecko = function()
	{
		return navigator.product == "Gecko" && ! ( this.isOpera() || this.isSafari() );
	}

	this.isIEWin = function()
	{
		return window.external && /Win/.test(ua);
	}

	this.isIEMac = function()
	{
		return window.external && /Mac/.test(ua);
	}

	this.getVersion = function()
	{
		if( this.isIEWin() || this.isIEMac() )
		{
			return Number(ua.match(/MSIE ([0-9.]+)/)[1]);
		}
		else if ( this.isSafari() )
		{
			return Number(ua.match(/[0-9.]+$/));
		}
		else if ( this.isGecko() )
		{
			var n = ua.match(/rv:([0-9.]+)/)[1];
			var ar = n.split(".");
			var s = ar[0] + ".";
			for(var i = 1; i < ar.length; ++i)
			{
				s += ("0" + ar[i]).match(/.{2}$/)[0];
			}

			return Number(s);
		}
		else if ( this.isOpera() )
		{
			return Number(ua.match(/Opera ([0-9.]+)/)[1]);
		}
		else
		{
			return null;
		}
	}
}

sniffer = new CJL_BrowserSniffer();

if ( sniffer.isIEWin() )
{
	browserType = "iewin";
}
else if ( sniffer.isIEMac() )
{
	browserType = "iemac";
}
else if ( sniffer.isSafari() )
{
	browserType = "safari"
}
else if ( sniffer.isGecko() )
{
	browserType = "gecko";
}
else if ( sniffer.isOpera() )
{
	browserType = "opera";
}
else
{
	browserType = "unknown";
}

// alert( "Your browser type is \"" + browserType + "\"" );

/********************************************************************
	Allows us to choose an alternet style sheet. A call on load will
	be made to getPreferredStyleSheet to deal with a improper
	implementation made in MSIE.
********************************************************************/
function setActiveStyleSheet(title)
{
	var i, a, main;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
			if(a.getAttribute("title") == title)
			{
				a.disabled = false;
//				 alert( "You are now using an alternate style sheet with the name \"" + title + "\"" );
			}
		}
	}
}

function getPreferredStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if((a.getAttribute("rel").indexOf("style") != -1) &&
			(a.getAttribute("rel").indexOf("alt") == -1) &&
			(a.getAttribute("title")))
			return a.getAttribute("title");
	}

	return null;
}

/********************************************************************
	Do so misc. page maintenance during a page load.
	Want to do this as early as possible in this file so the css file
	is loaded quickly.
********************************************************************/
window.onload = function(e)
{
	preloadImages();
}


/********************************************************************
	Toggle display status of a block based on id name.
********************************************************************/
function showHideById( name )
{

	// check for DOM1 compatibility
	if( document.getElementById )
	{

		// loop thru all the elements
		for( var i = 0; document.getElementById( name + i ) != null; i++ )
		{
			var element = document.getElementById( name + i );
	
			if( element.style.display != 'none' )
			{
				element.style.display = 'none';
			}
			else
			{
				element.style.display = 'block';
			}
		}
	}

}
//
// Set the browser's title
//
function setTitle(title)
{
	window.document.title = "GetEnergyAware.com | " + title;
}
