var questionID = "";
var answerID = "";
function resizeQuestionFrm() {
  // this function should resize the .question div in order to make it large if the image is smaller
  width = $('div.img_content').children('img').width;
}

function goToNextQuestion() {
	location.href = getCurrentPage();
}

function btnHover() {
	$(this).toggleClass("hover");
}

function btnEffectsHandler() {
	$("#answers li").mouseover(btnHover);
	$("#answers li").mouseout(btnHover);
	$("#answers li").mousedown(btnClickHandler);
	$("#answers li").click(btnClickHandler);
  Cufon.replace('h3');
}

var _timeOutID = -1234;
var _strPageToLoad = "";
function btnClickHandler() {
	if (_timeOutID != -1234) {
		clearTimeout(_timeOutID);
	}
	var answerID = $(this).attr("id");
	var questionID = $("div.question").attr("id");
	var strDataToSend = "getanswers.php?questionID="+questionID+"&answerID="+answerID;
	var strPageToLoad = getCurrentFolder()+strDataToSend;
	_strPageToLoad = strPageToLoad;
	$(this).addClass("highlight");
	_timeOutID = setTimeout("doClick();", 500);
}

function doClick() {
	_timeOutID = -1234;
	//alert("strPageToLoad='"+strPageToLoad+"'");
	//window.location.href = strPageToLoad;
	//return;
	$("#game_content").children().remove();
	$("#points").children().remove();
	$("#game").append('<div id="results"></div>'); 
	$("#game_content").load(getCurrentPage()+"getquestions.php?start=1",btnEffectsHandler);
	$("#results").load(_strPageToLoad,{rnd:Math.floor(Math.random()*1000)},doneWithQuestionLoad);
	$("#answers li").unbind("mousedown");
	$("#answers li").unbind("click");
	loadNewBanner();
}

function sendFlashCommand(strCommand) {
	if(window.flash) {
		window.document["flash"].SetVariable("_strCommand", strCommand);
	}
	if(document.flash) {
		document.flash.SetVariable("_strCommand", strCommand);
	}
}

function doneWithQuestionLoad() {
  Cufon.replace('h4');
  Cufon.replace('h5');
  resizeQuestionFrm();
	var strLoaded = $("#results").text();
	if (strLoaded.indexOf("Sorry") >= 0) {
		return;
	}
	$("#points").load(getCurrentPage()+"getpoints.php");
	sendFlashCommand("add_people");
}


function getCurrentFolder() {
	var Lret = getCurrentPage();
	var Lindex = Lret.lastIndexOf("/");
	Lret = Lret.substring(0, Lindex+1);
	return Lret;
}

function getCurrentPage() {
	var Lret;
	try {
		Lret = window.location.href.split("?");
	} catch (e) {
		Lret = [window.location.href];
	}
	return Lret[0];
}

function loadNewBanner() {
	//$("#ads_content").load(getCurrentPage()+"bannerRotator.php");
  //$("#ads_content").load(getCurrentPage()+"banner_test/index.php a");
  var url = getCurrentPage()+"banner_test/index.php";
  var div = $('#adsense_content');
  div.empty();
  if ($('#bannerFrame').length <= 0) {
    div.append('<iframe src="'+url+'" id="bannerFrame" />');
  }
  else {
    $('#bannerFrame').remove();
    div.append('<iframe src="'+url+'" id="bannerFrame" />');
  }
}


function startup() {
	$("#game_content").load(getCurrentPage()+"getquestions.php?start=1",btnEffectsHandler);
	$("#points").load(getCurrentPage()+"getpoints.php");
  $('#howitworks').corner();
  $('#rightBox').corner();
  $('#aboutUsContainer').corner();
  Cufon.replace('h2');
  Cufon.replace('#tagline-header p');
  Cufon.replace('#toptext');
  Cufon.replace('.faqItem p');
  Cufon.replace('#footerText');
}

$(document).ready(startup);
