/********************************************
* This function opens up a full-screen pop-up.
* Currently NETSCAPE and OPERA will open a new tab instead of a pop-up window :(
* */
var cogWindow;
var homeTextsArr = new Array();
var form  = document.createElement("form");
form.style.display = "none";
var detect;
var verNum;
var demoNum;

/**
 * Launch the demo for external sites (NOT standalone)
 */
function begin(lang, country, demoVer, demoIndex, path){
  var getStr = "";
  getStr += (lang)? "lang=" + lang : "lang=en";
  getStr += (country)? "&country=" + country : "&country=us";
  getStr += (demoVer)? "&demoVer=" + demoVer : "&demoVer=2";
  getStr += (demoIndex)? "&demoIndex=" + demoIndex : "&demoIndex=2";
  if (!path)
    path = "http://program.cognifit.com/OnLineProjectAs3";
  openFullWindow(path + "/cog_demo.htm?" + getStr, "cog");
}
/********************************************
* */
function start(demoData){
	demoNum = "2";
	verNum = "1";	
	lang = "";
	if(demoData.length > 1){
		var tmpArr1 = demoData[1].split("&");
		var tmpArr2;
		for(var i=0;i<tmpArr1.length;i++){
			tmpArr2 = tmpArr1[i].split("=");
			switch(tmpArr2[0]){
				case "demo":
					demoNum = tmpArr2[1];
					break;
				case "ver":
					verNum = tmpArr2[1];
					break;
				case "lang":
					lang = "_"+tmpArr2[1];
					break;
			}
		}		
	}
	openFullWindow("demo"+lang+".htm", "cog");
}
/*************************
* Create an HTML form input field
* */
function createInput(inputName, arrVal){
	var tmpInput = document.createElement("input");
	tmpInput.setAttribute("type", "hidden");
	tmpInput.setAttribute("name", inputName);
	tmpInput.setAttribute("value", arrVal);
	form.appendChild(tmpInput);
}
/********************************************
* This function is for backwords compatibility, for the old html files
* */
function openFullWindow(theURL,winName) { //v2.0
	if(cogWindow == undefined || cogWindow.closed){
		var detect = navigator.userAgent.toLowerCase();
  	//Detect the browser.
  	var browserArr = new Array("opera","msie","firefox","netscape","gecko");
  	for(var i = 0 ; i < browserArr.length ; i++){
  		if(detect.indexOf(browserArr[i]) > -1){
  			detect = browserArr[i];
  			break;
  		}
  	}
  	//Act according to browser detected.
  	switch(detect){
  		case "firefox":
  			cogWindow = window.open(theURL,winName,"status=no,fullscreen");
  			cogWindow.moveTo(0,0);
    		cogWindow.resizeTo(screen.availWidth,screen.availHeight + 24);
  			break;

  		case "gecko":
  			cogWindow = window.open(theURL,winName,"status=no");
  			cogWindow.moveTo(0,0);
    		cogWindow.resizeTo(screen.availWidth,screen.availHeight);
  			break;

  		/*case "msie":
  			cogWindow = window.open(theURL,winName,"fullscreen,status=no");
  			break;*/

  		default:
			cogWindow = window.open(theURL, winName, 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=no,width='+(screen.availWidth-8)+',height='+(screen.availHeight-24)+',left=0, top=0,screenX=0,screenY=0');
  	}
	} else {
		//The window is already opened
		cogWindow.focus();
	}
}
/*************************
* Initialize an array to hold home params
* */
function setHomeTexts(txtArr){
	homeTextsArr = txtArr;
}
/********************************************
* Find operating system
* */
function checkOS() {
    switch(true) {
		case (navigator.appVersion.indexOf("Win")!=-1):
			return "win";
		case (navigator.appVersion.indexOf("Mac")!=-1):
			return "mac";
		case (navigator.appVersion.indexOf("X11")!=-1 || navigator.appVersion.indexOf("Linux")!=-1):
			return "unix";
	}
}

/********************************************
* Check if cookies enabled
* Create a cookie and then check for it's existence
* */

/********************************************
* Start the check
* */
function checkCookies(){
	setCogCookie();
	setTimeout('beginCheck();',200)
}
/********************************************
* */
function setCogCookie(){
	//var oldDate = new Date(1970, 1, 1);
	setCookie("cogCookie","CogniFit_cookie_test");
}
/********************************************
* Check operating system and set 'Require' texts accordingly.
* If cookies are NOT allowed - show the 'no_cookies' div.
* Else show the 'clickStart' div.
* */
function beginCheck(){
	//Check cookies
	if(!allowCookies()){
		//Cookies blocked
		setDivsByOs("no_cookies");
	} else {
		setDivsByOs("require");
		//Cookies allowed
		document.getElementById('clickStart_hide').id='clickStart_show';
	}
}
/********************************************
* Show and hide divs according to operating system (Mac or other)
* @param	divId - The div's id (e.g. 'require')
* */
function setDivsByOs(divId) {
	//First show the 'Windows' div
	try {
		document.getElementById(divId).style.display = "block";
	} catch(e){}

	if(checkOS() == "mac") {
		//Now if there is a 'Mac' div - show it instead of the 'Windows' div
		var mac_div = document.getElementById(divId + "_mac");
		if(mac_div != null) {
			if(mac_div.innerHTML != "" && mac_div.innerHTML.toLowerCase() != "<p></p>") {
				document.getElementById(divId).style.display = "none";
				mac_div.style.display = "block";
			}
		}
	}
}
/********************************************
* Find the cookie previously created for this check
* */
function getCookie(c_name){
	if (document.cookie.length>0){
		var c_start = document.cookie.indexOf(c_name + "=")
		if (c_start > -1){
		  return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
/********************************************
* Try to Create the cognifit cookie
* */
function setCookie(c_name,value){
	document.cookie=c_name+ "=" +escape(value);
}
/********************************************
* Try to retrieve the cognifit cookie
* */
function allowCookies(){
	return getCookie("cogCookie");
}
