
/**
 * Changes CSS values in a stylesheet, not an inline style.
 * 
 * @param element
 *            The css id, class, or tag
 * @param property
 *            The css property
 * @param value
 *            The new value
 */
function changeCSS( element, property, value )
{
	// Last Updated on June 23, 2009
	// documentation for this script at
	// http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	var cssRules;

	var added = false;
	for ( var S = 0; S < document.styleSheets.length; S++)
	{

		if (document.styleSheets[S]['rules'])
		{
			cssRules = 'rules';
		}
		else
			if (document.styleSheets[S]['cssRules'])
			{
				cssRules = 'cssRules';
			}
			else
			{
				// no rules found... browser unknown
			}

		for ( var R = 0; R < document.styleSheets[S][cssRules].length; R++)
		{
			if (document.styleSheets[S][cssRules][R].selectorText == element)
			{
				if (document.styleSheets[S][cssRules][R].style[property])
				{
					document.styleSheets[S][cssRules][R].style[property] = value;
					added = true;
					break;
				}
			}
		}
		if (!added)
		{
			if (document.styleSheets[S].insertRule)
			{
				document.styleSheets[S].insertRule(element + ' { ' + property + ': ' + value + '; }', document.styleSheets[S][cssRules].length);
			}
			else
				if (document.styleSheets[S].addRule)
				{
					document.styleSheets[S].addRule(element, property + ': ' + value + ';');
				}
		}
	}
}

/**
 * Returns the property value for an element in a stylesheet.
 * 
 * @param element
 * @param property
 * @return
 */
function getCSSProperty( element, property )
{
	var cssRules;

	var added = false;
	for ( var S = 0; S < document.styleSheets.length; S++)
	{

		if (document.styleSheets[S]['rules'])
		{
			cssRules = 'rules';
		}
		else if (document.styleSheets[S]['cssRules'])
		{
			cssRules = 'cssRules';
		}
		else
		{
			// no rules found... browser unknown
		}
	

		for ( var R = 0; R < document.styleSheets[S][cssRules].length; R++)
		{
			if (document.styleSheets[S][cssRules][R].selectorText == element)
			{
				if (document.styleSheets[S][cssRules][R].style[property])
				{
					return document.styleSheets[S][cssRules][R].style[property];
				}
			}
		}
	}
}

/**
 * Swaps depths of elements defined in a stylesheet
 * 
 * @param element1
 *            A CSS element defined in a stylesheet, don't forget to put the #,
 *            or . if necessary
 * @param element2
 *            A CSS element defined in a stylesheet
 * @return
 */
function swapDepths( element1, element2 )
{
	  var origZ = getCSSProperty(element1, "zIndex");
	  var swapZ = getCSSProperty(element2, "zIndex");
	  
	  if (origZ > swapZ)
	  {
		  changeCSS( element1, 'zIndex', swapZ );
		  changeCSS( element2, 'zIndex', origZ );
	  }
}
/**
 * Allows multiple methods to be executed with the onload event.
 * @param func
 */
function addLoadEvent(func) 
{ 
	if (window.addEventListener)
	{
		window.addEventListener( 'load', func, false );
		return true;
	}
	else if ( window.attachEvent)
	{
		var r = window.attachEvent('onload', func );
		return r;
	}
	else 
	{
		return false
	};
} 


//Methods for making Flash menus respond to mouse overs
//scroll flicker for PC firefox
function flashMenuRolloverHandler( bg )
{
	bg = document.getElementById( bg );
	
	function flicker()
	{
				bg.style.backgroundColor = "transparent";
	}
	
	var d = new Date();
	var scrollStart = 0;
	var waitForScroll = false;
	window.onscroll = function(){
	        if(BrowserDetect.browser == "Firefox" && BrowserDetect.OS == "Windows"){
	                d = new Date();
	                scrollStart = d.getTime();
	                waitForScroll = true;
	        }
	
	}
	
	function scrollLoop(){
	        d = new Date();
	        if(waitForScroll && scrollStart < (d.getTime() - 250)){
	                waitForScroll = false;
	                bg.style.backgroundColor = "#FFFFFF";
	                setTimeout(flicker, 10);
	        }else{
	        	
	        }
	        setTimeout(scrollLoop, 50);
	}
	
	if(BrowserDetect.browser == "Firefox" && BrowserDetect.OS == "Windows")
	{
	        scrollLoop(); 
	}
}
/**
 * Crops a browser window, during window.onload
 * @param measuredElement  The element to measure, usually an element contained inside the element to be cropped.  
 * @param croppedCSSElement		The element to be cropped, this must be a reference to a CSS stylesheet element,
 *																so must have a # or a . prefix unless its a top level tag.
 * @param cropOffset		The number of pixels to add or subtract from the crop
 */
function cropWindow( measuredElement, croppedCSSElement, cropOffset )
{
		middle = document.getElementById( measuredElement )	
		
		if (!middle) return;
		
		function cropIt()
		{
			clientHeight = middle.clientHeight + cropOffset;
			changeCSS( croppedCSSElement, "height", clientHeight+'px' );
			changeCSS( croppedCSSElement, "overflow", 'hidden' );
			var loc = location.pathname;
			if (loc.substring(0,5) == '/tour')
			{
				onComplete();
			}
		}
		
		addLoadEvent( cropIt );
}

function setWindowHeight( offset )
{
	var version = navigator.userAgent;
	var findIE = version.indexOf('MSIE');
	if(findIE == -1)
	{
		windowHeight = window.innerHeight;
	} 
	else
	{
		windowHeight = document.documentElement.clientHeight;
	}
	document.body.style.height = (windowHeight + offset) + "px";
	//IE sucks hack
	document.getElementById("genericContainer").style.height =  (windowHeight + offset) + "px";
}

function getWindowHeight()
{
	var version = navigator.userAgent;
	var findIE = version.indexOf('MSIE');
	if(findIE == -1)
	{
		windowHeight = window.innerHeight;
	} 
	else
	{
		windowHeight = document.documentElement.clientHeight;
	}
	
	return windowHeight;
}

function needsCropping()
{
	if ( document.getElementById("genericContainer").style.height < getWindowHeight())
	{
		return false;
	}
	return true;
}

function prettyPhotoFromAS(target)
{
	target += '?iframe=true&width=675&height=600';
	$.prettyPhoto.open(target, 'Be Aware Proclamation');
}

 function switchElementPos ( toFront, toBack )
{
	if ($(toFront).css("zIndex") < $(toBack).css('zIndex'))
	{
		changeCSS(toFront, 'zIndex', "20");
		changeCSS(toBack, 'zIndex', "10");
	}
}
//Various jQuery functions
