//Javascript
//Auth token must be declared globally before this...

lastResponse = false;
windowWidth = 0;
windowHeight = 0;

//function applyClick(id, json){ }

function createXHR(){
	try {return new XMLHttpRequest();} catch(e) {}
	try {return new ActiveXObject('Msxml2.XMLHTTP.6.0');} catch (e) {}
	try {return new ActiveXObject('Msxml2.XMLHTTP.3.0');} catch (e) {}
	try {return new ActiveXObject('Msxml2.XMLHTTP');} catch (e) {}
	try {return new ActiveXObject('Microsoft.XMLHTTP');} catch (e) {}
	alert('XMLHttpRequest not supported');
	return null;
}

function handleResponse(xhr){
	var response = false;
	if(xhr.readyState == 4 && xhr.status == 200){
		var responseArray = new Array();
		var parsedResponse = xhr.responseText;
		responseArray = parsedResponse.split('|');
		if(responseArray[0] == authToken) {
			if(responseArray[1] != ''){
				var ajaxObject = eval('('+responseArray[1]+')');
				eval(ajaxObject['x']);
				lastResponse = true;
			}
			else{
				lastResponse = false;
			}
		}
		else{
			alert('no match '+parsedResponse);
		}
		//loaderControl('hide');
		response = true;
	}

	return response;
}

function sendRequest(json, countRequests){
	var xhr = createXHR();
	countRequests++;
	if(countRequests >= 3){
		//json = "errorPayload=>1|"+json;
		if(xhr) {
			xhr.open("POST", "ajax.php", true);
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xhr.onreadystatechange = function () {
				var checkReturn = handleResponse(xhr);
				if(checkReturn == true) {
					clearTimeout(timeoutObj);
				}
			};
			json = json.replace(/\+/g, "~plus;");
			json = json.replace(/&/g, "~amp;");
			//xhr.send("payload="+payload+"|authToken=>"+authToken);
		}
	}
	else {
		if(xhr)	{
			xhr.open("POST", "ajax.php", true);
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xhr.onreadystatechange = function () {
				var checkReturn = handleResponse(xhr);
				if(checkReturn == true) {
					clearTimeout(timeoutObj);
				}
			};
			//payload = payload.replace(/\+/g, "~plus;");
			//payload = payload.replace(/&/g, "~amp;");
			xhr.send("json="+json+"&authToken="+authToken+"&loadToken="+loadToken);

			var twentySecondRetry = function() {
				xhr.abort();
				sendRequest(json, countRequests);
			};

			var timeoutObj = setTimeout(twentySecondRetry, 7000);
		}
	}
}

function keyedArray(){
	var returnArray = new Array();
	for(var i=0; i<arguments.length;i++){
		var splitResult = arguments[i].split('=>');
		returnArray[splitResult[0]] = splitResult[1];
	}
	return returnArray;
}

function defaultSendRequest(json){
	sendRequest(json, 0);
}

//Jquery reliant functions below this (append with jq_):
function jq_selectButton(parent_id, child_id){
	$('#'+parent_id+' > div').each(function(){
		if($(this).hasClass('navButtonSelect')){
			$(this).toggleClass('navButton');
			$(this).removeClass('navButtonSelect');
		}
	});
	$('#'+child_id).toggleClass('navButtonSelect');
	$('#'+child_id).toggleClass('navButton');
}

function jq_initSize(){
	//This is a temp function until we have something more robust, and have a better idea of our needs
	this.resizeFunc = function(){
		windowWidth = $('#body_id').width();
		windowHeight = $('#body_id').height();
		//$('clickLayer').css('width', windowWidth);
		if($('#midRow').length){
			if($('#midRow').height() != windowHeight-96){
				$('#midRow').height(windowHeight-96);
			}
		}
		if($('#col3').length){
			var subtractVal = $('#col2').width()+190;
			$('#col3').width(windowWidth-subtractVal);
		}

		if($('#onTop').length){
			var left = 0;
			if(windowWidth > 600){
				left = (windowWidth/2)-300;
			}
			
			var top = 0;
			if(windowHeight > 600){
				top = (windowHeight/2)-300;
			}

			$('#onTop').css('left', left);
			$('#onTop').css('top', top);
		}
	}

	this.resizeFunc();
	$(window).resize(function(){this.resizeFunc();})

	/*
	setInterval(function(){
		windowWidth = $('#body_id').width();
		windowHeight = $('#body_id').height();
		if($('#midRow').length){
			if($('#midRow').height() != windowHeight-96){
				$('#midRow').height(windowHeight-96);
			}
		}
		if($('#col3').length){
			var subtractVal = $('#col2').width()+190;
			$('#col3').width(windowWidth-subtractVal);
		}

	}, 100)
	*/
}

/*
 * 		//There has to be a better way to do this...
		var ss = document.styleSheets;
		for (var i=0; i<ss.length; i++)	{
			var rules = ss[i].cssRules || ss[i].rules;
			for (var j=0; j<rules.length; j++){
				if (rules[j].selectorText === ".clickLayer"){
					//rules[j].style.width = windowWidth+"px";
				}
			}
		}

 */
