var indexurl = "/snserver/geeklogik/common/equations/geeklogikindex.html";
var latest = "/snserver/geeklogik/common/equations/latest.html";
var gEquationsURL = "/snserver/geeklogik/common/equations";
var htmlRequest=null;
var response = null;
var currentEq ="";
var topRightURL= "http://www.scientificblogging.com";
var bottomLeftURL= "http://www.stargatehub.com";
var geekblogURL="http://www.scientificblogging.com/geek_logik/geek_logik_the_science_of_decisions_and_foolproof_equations_for_a_perfect_life";


var firstLoad = true;
function cBeforeAdShow(){
	
	var adObject= this;
	//$("advert").setStyle({display:"block","z-index":"99"});
	if($("equationIndex")){		
		$("equationIndex").disabled = true;
	}
	$("advert").setOpacity(0.0);
	$("advert").setStyle({display:"block",zIndex:"99"});
	
	//$("advert").setOpacity(1.0);	
	if($("adClick")){	
		$("adClick").setStyle({display:"block",zIndex:"100"});		
	}
}

function cAfterAdHide(){
	var adObject= this;
	//$("advert").setStyle({display:"none", "z-index":"1"});
	//$("advert").style.display = "none";
	//$("advert").style.zIndex = "1";
	//$("adClick").style.display = "none";
	setTimeout("window.scrollTo(0,1)", 300);
	$("advert").setStyle({display:"none",zIndex:"1"});
	$("advert").setOpacity(0.0);
	if($("adClick")){
		$("adClick").setStyle({display:"none", zIndex:"1"});
	}
	$("equationIndex").disabled = false;
	if((adObject.adsEnabled == false) || (adObject.startOnFrame == 0 && adObject.eventsFired > 1) || (adObject.startOnFrame > 0) ){
		GEEKLOGIK.onCalculate();
	}

}	
function cAfterAdShow(){
	$("advert").setOpacity(1.0);
	//$("adFrame").setStyle({display:"block"});	
}
function cBeforeAdHide(){
}

function getIndexHTML(url){
	new Ajax.Updater('indexDiv',url,{onComplete:afterIndexUpdate});	
}
function getContentHTML(url){
	new Ajax.Updater('GLUpdateContainer',url, {evalScripts: true, onComplete:afterContentUpdate});
	
}
function afterIndexUpdate(){
	var eq = $("equationIndex");
	if($("equationIndex") && undefined === $("equationIndex")._eventID ){
		if(currentEq){
			eq.selectedIndex = currentEq;
		}
		Event.observe($("equationIndex"),"change",changeEquation);
	}
}
function afterContentUpdate(){
	getIndexHTML(indexurl);
	GEEKLOGIK.advert = new Advert({beforeAdShow: cBeforeAdShow, afterAdHide: cAfterAdHide, afterAdShow: cAfterAdShow, beforeAdHide:cBeforeAdHide, adCfgFile: "/snserver/geeklogik/common/ads/web/adscfg.json"});
	var scroll = ScrollBar.create({scrollAxis: "horizontal", Container:'varContainer', innerElemClass:'GLVariable'});
	setTimeout(addGLHandlers,100);
		
}
function addGLHandlers(){
	Event.observe($("GLLogoDiv"),"mousedown",openTopLeftURL);	
	Event.observe($("GLTopRightLogo"),"mousedown", openTopRightURL);
	//Event.observe($("stargateLogo"),"mousedown", openBottomLeftURL);
	GEEKLOGIK.addHandlers();
	
}
function removeGLHandlers(){
	GEEKLOGIK.removeHandlers();
	Event.stopObserving($("GLLogoDiv"),"mousedown",openTopLeftURL);	
	Event.stopObserving($("GLTopRightLogo"),"mousedown", openTopRightURL);
	//Event.stopObserving($("stargateLogo"),"mousedown", openBottomLeftURL);
}
function openTopRightURL(){
	openBrowserURL(topRightURL);
}
function openTopLeftURL(){
	openBrowserURL(geekblogURL); 
}
function openBottomLeftURL(){
	openBrowserURL(bottomLeftURL); 
}

function changeEquation(inEvt){
	if((inEvt.element()).value){
		//var equationURL = gEquationsURL+"/"+(inEvt.element()).value;
		var equationURL= gEquationsURL+"/"+(inEvt.element()).value;
		currentEq = (inEvt.element()).selectedIndex;
		GEEKLOGIK.removeHandlers();
		Event.stopObserving($("GLLogoDiv"),"mousedown",openTopLeftURL);	
		Event.stopObserving($("GLTopRightLogo"),"mousedown", openTopRightURL);
		//Event.stopObserving($("stargateLogo"),"mousedown", openBottomLeftURL);
		getContentHTML(equationURL);
		$("GLContent").style.display= "none";
		GEEKLOGIK.handlers.reset();
		
	}
}
function load()
{
	GEEKLOGIK.trackAction({ action: GEEKLOGIK.consts.kTrack_Initialized, isError: false });
	
	getContentHTML(latest);
	
	
}

function hideElem(elem){
	elem.style.display="none";
}

function openBrowserURL(url){
	window.open(url);
}
function goToImaginova(event)
{
	if (window.widget)
	{
		widget.openURL("http://www.imaginova.com");
	}
}




function captureFrameClick(inEvt){
	var loc = $("adFrame").contentDocument.links[0].href;
	if(window.widget){
		widget.openURL(loc);
	}
	

}



