/********************************************
* 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;
/********************************************
* */
function start(){
	setHomeTexts(locArr);
	openFullWindow_php();
}
function launch() {
	start();
}
/**
* This function serves as a middle func to be called externally from companys site
*/
function exLaunch(_getStr) { //v2.0
	openFullWindow_php(_getStr);
}
/********************************************
* */
function openFullWindow_php(_getStr) { //v2.0
	if(cogWindow == undefined || cogWindow.closed){
		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;
      		}
      	}
      	//Get the BODY element
		var body = document.getElementsByTagName("body")[0];

		//Find out the home.php location according to the cog_functions.js script src attribute
		//In the opener file there must be an id attribute to the script tag (<script src='...cog_functions.js' id='cogScript')
		/*var scriptObj = document.getElementById("cogScript");
		var homePath = "";
		if(scriptObj && scriptObj.src.indexOf("http") > -1){
			var pathArr = scriptObj.src.split("/");
			for(var i = 0 ; i < pathArr.length ; i++){
				if(pathArr[i] == "scripts") break;
				homePath += pathArr[i] + "/";
			}
		}*/

		//Replace for QA / Dev / External
		//var server = "http://www.mindfitnessforum.com/OnLineProject/";
		var server = "http://program.cognifit.com/OnLineProject/";
		
		form.setAttribute("target", "flashapp");
		form.setAttribute("method", "post");
		form.onSubmit = doFullScreenPost();
		
		//Server path
		createInput("server", server);
		
		if(!_getStr){
			form.setAttribute("action", "http://" + locArr["HTTP_HOST"] + "/" + locArr["localDir"] + "/home.php");
			
			//Language
			createInput("language", homeTextsArr["language"]);
			//Country
			createInput("country", homeTextsArr["country"]);
			//Product
			createInput("project", homeTextsArr["project"]);
			//Product
			createInput("product", homeTextsArr["product"]);
			//Login mode
			createInput("loginmode", homeTextsArr["LOGIN_MODE"]);
			//Style
			createInput("added_style", homeTextsArr["STYLESHEET"]);
			//Xml path
			createInput("xmlpath", homeTextsArr["xmlpath"]);
			
			//Allow script access
			if(homeTextsArr["ALLOW_SCRIPT_ACCESS"]){
				createInput("allowScriptAccess", homeTextsArr["ALLOW_SCRIPT_ACCESS"]);
			} else {
				createInput("allowScriptAccess", "always");
			}
			//Preloader background music
			if(homeTextsArr["bgsound"]) {
				createInput("bgsound", homeTextsArr["bgsound"]);
			}
		} else {
			form.setAttribute("action", "http://www.cognifit.com/OnLineProject/home.php");
			var pairsArr = _getStr.split("&");
			var tmpArr;
			for(var i = 0 ; i < pairsArr.length ; i++){
				tmpArr = pairsArr[i].split("=");
				if(tmpArr[1] != ""){
					createInput(tmpArr[0], tmpArr[1]);
				}
			}
		}
      	body.appendChild(form);
		form.submit();
  	}
	cogWindow.focus();
}
/********************************************
* */
function createInput(inputName, arrVal){
	var tmpInput = document.createElement("input");
	tmpInput.setAttribute("type", "hidden");
	tmpInput.setAttribute("name", inputName);
	tmpInput.setAttribute("value", arrVal);
	form.appendChild(tmpInput);
}
/********************************************
* */
function doFullScreenPost(){
	switch(detect){
		case "firefox":
			cogWindow = window.open("", form.target, "channelmode=yes,location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=no");
			cogWindow.moveTo(0,0);
			cogWindow.resizeTo(screen.availWidth,screen.availHeight + 24);
			break;
		default:
			//To hide the launch bar add channelmode=yes, to features
			cogWindow = window.open("", form.target, "location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=no,width="+(screen.availWidth-8)+",height="+(screen.availHeight-24)+",left=0, top=0,screenX=0,screenY=0");
		}
}
/********************************************
* 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();
	}
}
/********************************************
* */
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");
}
//-------------------------------------------------------------
/**
* Temporary functions
*/
function swap_img(imgPath) {
	var _img = document.getElementById("launch_img");
	_img.src = "projects/" + imgPath;
}
//-------------------------------------------------------------
/**
* Temporary functions
*/
function addSWF(swfPath) {
	var params = {
		quality: "high",
		loop: true,
		menu: false
	}
	swfobject.embedSWF(swfPath, "flash", "388", "105", "8.0.0", null, null, params);
}
