/*******************************************************************************
	BASE FUNCTIONS
*******************************************************************************/
	

/* SHOW / HIDE AN OBJECT */
	function toggle(obj) {
		var el = document.getElementById(obj);
		
		if ( el.style.display != 'none' ) {
			el.style.display = 'none';
		}
		else {
			el.style.display = 'block';
		}
		
	}
	
/* GET ELEMENT BY CLASS */
	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;
	}	
	
/* Find the position of an object and what it relates to */
	function findPos(obj) {
		var curleft = topheight = parentheight = curtop = curheight = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			curheight = obj.offsetHeight 
			parentheight = obj.offsetParent.offsetHeight 

			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
				curheight += obj.offsetHeight 
			}
		}
		curheight = curheight - parentheight;
		topheight = Number(((curtop + (parentheight * 2)) - curheight) + curtop);
		return [curleft,curtop,curheight,topheight,parentheight];
	}
	

/*  FIND THE HEIGHT OF THE PAGE */
	function findHeight() {
	  var myHeight = 0;
	  if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	  }
	  return myHeight;
	}



/* Browser Detect Script */
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};		
	
/*******************************************************************************
	SPECIFIC FUNCTIONS
*******************************************************************************/

	function shellFix(){
		var pageHeight = findHeight();
		var shellDiv = document.getElementById('shell');
		var contentDiv = document.getElementById('livearea');
		var contentHeight = contentDiv.clientHeight;
		
		if(contentHeight < pageHeight){
			shellDiv.style.height = pageHeight + "px";	
		} else {
			shellDiv.style.height = contentHeight + "px";	
		}
	}

	function toolsFix(){
		var toolsDiv = document.getElementById('head_tools');
		var toolsPos = findPos(toolsDiv)[1] - 35;
		
		toolsDiv.style.visibility = "visible";
		//toolsDiv.style.top = "-" + toolsPos + "px";
		//alert(toolsPos);
	}


	function checkInput(obj, text){
		if(obj.value == text){
			obj.value = "";
		} else if(obj.value == " " || obj.value == ""){
			obj.value = text;
		}
	}
	
	function quoteFix(){
		var quote_close = getElementsByClass('quote_close');	
		if(quote_close.length >= 1){
			for (i = 0; i < quote_close.length; i++) {
					quote_close[i].innerHTML = quote_close[i].innerHTML + "<img src=\"/includes/quote_end.gif\" height=\"7\" width=\"10\" alt=\"close quote\" border=\"0\" />";
			}
		}
	}

	function externalFix() {
		var ext_links = getElementsByClass('external');	
		if(ext_links.length >= 1){
			for (i = 0; i < ext_links.length; i++) {
					ext_links[i].innerHTML = ext_links[i].innerHTML + "<img src=\"/includes/icn_external.gif\" height=\"9\" width=\"12\" alt=\"external link\" border=\"0\" />";
			}
		}	
	}
	function disableDropDowns(){
		var tab_account = document.getElementById('tab_account');
		var tab_account_activate = getElementsByClass('tab_activate', tab_account, 'a')[0];
		var tab_account_title = getElementsByClass('tab_title', tab_account, 'a')[0];
		
		tab_account_activate.style.display = 'none';
		
		tab_account_title.style.background = "url(/includes/mac_nav_login.gif) 0 0 no-repeat";
		tab_account_title.style.width = "68px";
				
	}
	
	function extendDiv(divName, ht_extension){
		var div = document.getElementById(divName);
		
		if(div && (Number(ht_extension) > 0)){
			var currentHeight = Number(div.style.height.split('p')[0]);
			div.style.height = (currentHeight + ht_extension) +"px";
		}
	}
	
/*******************************************************************************
	TAB FUNCTIONS
*******************************************************************************/

	function toggleTab(tabID){
		var alltabs= getElementsByClass('tab_content');
		var alltab_right = getElementsByClass('tab_right');
		var alltab_left = getElementsByClass('tab_left');
		var tab_content = getElementsByClass('tab_content',document.getElementById(tabID), 'div')[0];
		var tab_right = getElementsByClass('tab_right',document.getElementById(tabID), 'span')[0];
		var tab_left = getElementsByClass('tab_left',document.getElementById(tabID), 'span')[0];
		//alert(tab_content.innerHTML);
		
		if ( tab_content.style.display != 'none' ) {
			tab_content.style.display = 'none';
			tab_right.style.background = 'none';
			tab_left.style.background = 'none';
		}
		else {
			for (i = 0; i < alltabs.length; i++) {
				if ( alltabs[i].style.display != 'none') {
					alltabs[i].style.display = 'none';
					alltab_right[i].style.background = 'none';
					alltab_left[i].style.background = 'none';
				}
			}
			tab_content.style.display = 'block';
			tab_right.style.background = 'url(/includes/tab_right.gif) right top no-repeat';
			tab_left.style.background = 'url(/includes/tab_left.gif) left top no-repeat';
		}
		
	}
	
	/* START NEW CODE */
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name,"",-1);
	}

	function expandVideo(){
		$("#home #head").css("height","auto");
		$("#home_flash_container").animate({
			height: 365
		}, 750, "easeout", function(){
			$("#home #head").css("height","460px");
			shellFix();
		});
		
		return false;
	}
	/* END NEW CODE */
	
/*******************************************************************************
	INIT FUNCTIONS
*******************************************************************************/

	function init() {
		// use this function for all newer browsers 	
		var home_flash = document.getElementById('home_flash');	
		
		if(home_flash){
			toolsFix();
			BrowserDetect.init();
			if(BrowserDetect.OS == "Mac"){
				
			}
		}
		shellFix();
		quoteFix();
		externalFix();	
		//toolsFix();		
	}

	
/*******************************************************************************
	NON-FUNCTIONS SCRIPTS
*******************************************************************************/

if (document.getElementById && document.getElementsByTagName && document.createElement) {
	window.onload = init;
	window.onresize = shellFix;
}


