function updateMenuPos(){
	var wrapperOffset = $("#wrapper").offset().left;
	
	$("#mainNav li > ul").each(
			function(){
				$(this).show();
				$(this).css({"left":"auto", "margin-left":0});
				
				var theWidth = $(this).width();
				var relPosX = $(this).offset().left - wrapperOffset;
				
				if (relPosX + theWidth > 930) {
					$(this).css({"margin-left": (0 - (relPosX + theWidth - 930))});
				}
				if (theWidth > 920) {
					$(this).css({"margin-left": (0 - (relPosX + (theWidth - 950)/2))});
				}
				$(this).hide();
			}
		);
}
function updateBackground(){
	$('#backImg').css({ height: 1 });
	var arrPageSizes = ___getPageSize();
	$('#backImg').css({ height: arrPageSizes[1] });
}
$().ready(function(){
	updateMenuPos();
	
	$("#mainNav > li").hover(
			function(){
				$(this).find("ul:first").show();
			},
			function(){
				$(this).find("ul:first").hide();
			}
		);

	updateBackground();
	
	$(window).resize(function() {
			updateBackground();
		});
		
		
});

function URLEncode(str) {
   var nstr = escape(str);

   nstr = nstr.replace(/\+/g, '%2B');
   nstr = nstr.replace(/\%20/g, '+');

   return nstr;
}
function Set_Cookie( name, value, expires, path, domain, secure ){
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) {
		return null;
	}
}

if (jQuery.browser.mozilla == true && jQuery.browser.version.substring(0, 5) == "1.8.1" && parseFloat(jQuery.browser.version.substring(6)) < 12) {
	var flashWithOpacity = false;
} else {
	var flashWithOpacity = true;
}

if (jQuery.browser.safari == true && jQuery.browser.version == "418.8" && Get_Cookie('SafariWarning') != "1") {
	alert("This site uses javascript that this version of Safari doesn't support.\nPlease consider updating your browser to the latest version.");
	Set_Cookie('SafariWarning', '1', 30, '/', '', '');
}

sfHover = function() {
	$('#mainNav li').hover(
		function(){
			$(this).addClass('sfhover');
		},
		function(){
			$(this).removeClass('sfhover');
		}
	);
	if (sfArr.length > 0) {
		for (s=0; s<sfArr.length; s++) {
			$('#'+sfArr[s][0]+' '+sfArr[s][1]+'').hover(
				function(){
					$(this).addClass('sfhover');
				},
				function(){
					$(this).removeClass('sfhover');
				}
			);
		}
	}
}

$(document).ready(function(){
	sfHover();
	updateMenuPos();
	updateBackground();
});
$(window).load(function(){
	updateMenuPos();
	updateBackground();
});

function validateEmail(form_id,email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.forms[form_id].elements[email].value;
	if(reg.test(address) == false) {
		return false;
	} else {
		return true;
	}
}




/*
 THIRD FUNCTION
 getPageSize() by quirksmode.com
 
 return Array Return an array with page width, height and window width, height
*/
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

/*
 THIRD FUNCTION
 getPageSize() by quirksmode.com
 
 return Array Return an array with page width, height and window width, height
*/
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};