// JavaScript Document
var SAFARI_STYLESHEET_REFERENCE = "./_resources/css/styles_safari.css";
var FIREFOX_STYLESHEET_REFERENCE = "./_resources/css/styles_firefox_mac.css";
//debugger;
/********************************************************************/
/*START: Fix IE 6 Text Selection Bug & Flickering Background Images */
if (window.createPopup && document.compatMode && document.compatMode=="CSS1Compat" && !window.XMLHttpRequest) {
	document.onreadystatechange = function fixIE6AbsPos()
	{
		if( !document.body ) {
			return;
		}
		if( document.readyState == "complete" ) {
			document.body.style.height = document.documentElement.scrollHeight + 'px';
		}
	}

	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
}
/*END: Fix IE 6 Text Selection Bug & Flickering Background Images   */
/********************************************************************/
/********************************************************************/
/*START: browser detection*/
function BrowserDetection()
{
	var sUserAgent = navigator.userAgent.toLowerCase();
	var sUserPalttform = navigator.platform.toLowerCase();

	// browser name
	this.isIE = (sUserAgent.indexOf('msie') != -1);
	this.isIE7 = (this.isIE && typeof window.XMLHttpRequest != "undefined");
	this.isSafari = (sUserAgent.indexOf('safari') != - 1);
	this.isMozilla = (sUserAgent.indexOf('gecko') != -1 && this.isSafari == false);
	this.isFirefox = (this.isMozilla == true && sUserAgent.indexOf('firefox') != -1);
	this.isFirefoxMac = (this.isMozilla == true && sUserAgent.indexOf('mac') != -1);
	this.isOpera = (sUserAgent.indexOf('opera') != -1 || typeof window.opera != "undefined");
}

var oBrowser = new BrowserDetection();
/*END: browser detection*/
/********************************************************************/
/********************************************************************/
/*START: Safari Browser Detection and Serve Styles */
if (oBrowser.isSafari)
{
	document.write ("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + SAFARI_STYLESHEET_REFERENCE + "\" />");
}
/*END: Safari Browser Detection and Serve Styles */
/********************************************************************/
/********************************************************************/
/*START: Firefox MAC Browser Detection and Serve Styles */
if (oBrowser.isFirefoxMac) 
{
    document.write ("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + FIREFOX_STYLESHEET_REFERENCE + "\" />");
}
/*END: Firefox MAC Browser Detection and Serve Styles */
/********************************************************************/
/********************************************************************/
/*START: Functions to open popups */
function openPopup(url, popupName, width, height, posX, posY)
{
	var pos = getWindowPos(width, height, posX, posY);
	if (pos != undefined)
	{
		posX = pos[0];
		posY = pos[1];
	}
	popupwindow = window.open(url, popupName, 'width='+width+', height='+height+', toolbar=no, menubar=no, scrollbars=yes, resizable=yes, top='+posY+', left='+posX);
	popupwindow.focus();
}

function getWindowPos(width, height, posX, posY)
{
	var standardX = 255;
	var standardY = 70;
	
	if(posX == undefined)
	{
		if(window.screenLeft != undefined){
			//for microsoft ie
			if(window.screenLeft + standardX + width < screen.width - 100)
			{
				posX = window.screenLeft + standardX;
			} 
			else 
			{
				posX = screenLeft - 50;
			}
			
			if(window.screenTop + standardY + height < screen.height - 30)
			{
				posY = window.screenTop + standardY;
			}
			else
			{
				posY = standardY;
			}
		}
		else 
		{
			if(window.screenX + standardX + width < screen.width - 100)
			{
				posX = window.screenX + standardX;
			} 
			else 
			{
				posX = screenX - 50;
			}
			
			if(window.screenY + standardY + height < screen.height - 30)
			{
				posY = window.screenY + standardY + 111;
			}
			else
			{
				posY = standardY + 111;
			}
		}
	return [posX,posY]
	}
}
/*END: Functions to open popups */
/********************************************************************/
/********************************************************************/
/*START: Functions to open flyouts in IE 6  */

sfHover = function() {
	if(!document.getElementById("gen_nav")) return;
	var sfEls = document.getElementById("gen_nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*END: Functions to open flyouts in IE 6 */
/********************************************************************/
/********************************************************************/
/*START: Functions to position footer */

function getWindowInnerSize() 
{ 
	
	var objWin = new Object();
	if (self.innerHeight) { 
	// all except Internet Explorer 
		objWin.width = self.innerWidth; 
		objWin.height = self.innerHeight; 
	} else if (document.documentElement && document.documentElement.clientHeight) { 
	// Explorer 6 Strict Mode 
		objWin.width = document.documentElement.clientWidth; 
		objWin.height = document.documentElement.clientHeight; 
	} else if (document.body) { 
	// other Explorers 
		objWin.width = document.body.clientWidth; 
		objWin.height = document.body.clientHeight; 
	} 
	return objWin; 
}

function getPos(id) 
{ 
	var obj = document.getElementById(id); 
	var pos = {left:0, top:0}; 
	if(typeof obj.offsetLeft != 'undefined') { 
		while (obj) { 
			pos.left += obj.offsetLeft; 
			pos.top += obj.offsetTop; 
			obj = obj.offsetParent; 
		} 
	} else { 
		pos.left = obj.left; 
		pos.top = obj.top; 
	} 
	return pos; 
}
function getHeight(element) 
{ 
	element = document.getElementById(element); 
	return element.offsetHeight; 
}
function setHeight(id, height) 
{ 
	var obj = document.getElementById(id); 
	obj.style.height = height + "px"; 
} 

function initLayout() 
{ 
	var iWindowHeight = getWindowInnerSize().height; 
	var iFooterHeight = (document.getElementById("footer")) ? getHeight("footer") : 0; 
	var iFooterTopPosition = (document.getElementById("footer")) ? getPos("footer").top : 0; 
	if (document.getElementById("footer_position_placeholder")) { 
		var iFooterPlaceholderHeight = getHeight("footer_position_placeholder"); 
		if (((iFooterTopPosition - iFooterPlaceholderHeight) + iFooterHeight) < iWindowHeight) { 
			var height = iFooterPlaceholderHeight + (iWindowHeight-(iFooterTopPosition+ iFooterHeight)); 
			setHeight("footer_position_placeholder", height) 
		} else { 
			setHeight("footer_position_placeholder", 0) 
		} 
	} 
} 

window.onload = function () {
	initLayout();
}

window.onresize = function () {
	initLayout();
}

/*END: Functions to position footer */
/********************************************************************/
/********************************************************************/
/*START: add event  */

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

/*END: add event */
/********************************************************************/
/********************************************************************/
/*START: Functions to toggle hidden links  */
function initHiddenLinks(){
	$('li.hidden-links').hide();
	$('li.hidden-links-trigger').show();
	$('li.hidden-links-trigger').click(function() {
		$(this).parent().find('li.hidden-links').slideToggle("fast");
		$(this).toggleClass('shown');
	});
}

function initPortletVisuals(){
	$('div.portlet-text-visual').each(function(i){
		if($(this).prev("a").get(0)){ //find corresponding link in visual
			$(this).css("cursor", "pointer");
			$(this).click(function() {
				location.href = $(this).prev("a").get(0).href;
			});
		}
	});
}
//   !!!!!  Error    !!!!!!!!
//$(document).ready(function(){
//	initHiddenLinks();
//	initPortletVisuals();
//});
//   !!!!!  Error    !!!!!!!!




/*END: Functions to toggle hidden links */
/********************************************************************/
/********************************************************************/
/*START: Functions to add the rounded end to each rounded submit button */

/* http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
			    oldonload();
			}
			func();
		}
	}
}

/* http://www.dustindiaz.com/getelementsbyclass/ */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* by Jeremy Keith */
function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	}
	else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

function buttonEndings() {
	if (!document.getElementsByTagName) {
		return false
	}
	
	var buttons = getElementsByClass("rounded-button");
	/* loop through all buttons and attach a child div */
	//for (i=0; i < buttons.length; i++) {
//		var div = document.createElement("div");
//		div.className = "buttonEnding";
//		insertAfter(div, buttons[i]);
//	}
}

addLoadEvent(buttonEndings);

///*END: Functions to add the rounded end to each rounded submit button */
///********************************************************************/
