/*
	AirwaySim
	General JS functions.
	Copyright Sami Puro / 2011.
*/

var forum=0;
var mainNav=1;
var reload='/';

var gameArea=0;
var LeftMenuShown=true;



// Cufon.
if (forum<1 && typeof(Cufon)!="undefined" && Cufon!=null) {
	Cufon.replace("h1");
	Cufon.replace("h2");
	Cufon.replace("h3");
	Cufon.replace("h4");
	Cufon.replace("h5");
	Cufon.replace(".Warn .Title");
	Cufon.replace(".Error .Title");
	Cufon.replace(".Msg .Title");
	Cufon.replace(".Info .Title");
	Cufon.replace(".Demo .Title");
	Cufon.replace(".TableBox .Title");
	Cufon.replace(".TableBox .Title2");
	Cufon.replace(".TableBox .Title2b");
}


// General items onload.
addLoadEvent(function() {
	if (forum>0)
		return;

	// Fix IE7 cufon bleed.
	if (navigator.appVersion.indexOf('MSIE 7.')!=-1) {
		$("#naviMainArea").css('z-index', '151');
		$("#naviMainAreaCompact").css('z-index', '151');
	}

	// Dropdown menu.
	if (mainNav>0) {
		$(".naviMain .navbtn").mouseover(function(){
			if ($(this).children('a:first').attr('id')!='active') {
				$(this).children('a:first').attr('class','btnHover');
				$(this).children('a:first').css('padding','7px 14px');
			}
		});
		$(".naviMain .navbtn").mouseout(function(){
			if ($(this).children('a:first').attr('id')!='active') {
				$(this).children('a:first').attr('class','btnNorm');
				$(this).children('a:first').css('padding','8px 15px');
			}
		});
	}


	// Fancy buttons.
	var btn=new Array('btn', 'btnR', 'btnC');
	for (var btnvar in btn) {
		$('.' + btn[btnvar]).each(function(){
			var b = $(this);
			var tt = b.text() || b.val();
			if ($(':submit,:button',this)) {
				b = $('<a>').insertAfter(this). addClass(this.className).attr('id',this.id);
				$(this).remove();
			}
			b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span>').
			text(tt).append('<i></i><span></span>'));
		});
		$('.' + btn[btnvar]).click(function(){
			$(this).parents('form').submit();
		});
	}


	// Checkbox.
//	$('input:checkbox').checkbox();
//	$('input:radio').checkbox();


	// Game area functions.
	if (gameArea>0)
		loadGameJS();
});



//  Onload function handler.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload!='function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload)
				oldonload();
			func();
		}
	}
}



//  Header image minimizer.
function chkHeader() {
	var viewportwidth=1500;
	if (typeof window.innerWidth!='undefined')
		viewportwidth = window.innerWidth;
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0)
		viewportwidth = document.documentElement.clientWidth;
	else
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;

	if (screen.width<=1100 || viewportwidth<=1100) {
		var field=document.getElementById('contentHeader');
		if (field!=null)
			field.style.backgroundImage="url('/layout/img/bg_contentHeader2.jpg')";
	}
}





// Ajax related functions, common usage.
if (typeof(document.getElementById) == "undefined") {		// Define document.getElementById for Internet Explorer 4.
	document.getElementById = function (id)	{
		// Just return the corresponding index of all.
		return document.all[id];
	}
} else if (!window.XMLHttpRequest && window.ActiveXObject) {	// Define XMLHttpRequest for IE 5 and above.
	window.XMLHttpRequest = function () {
		return new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") != -1 ? "Microsoft.XMLHTTP" : "MSXML2.XMLHTTP");
	};
}


function makeHttpRequest(url, params, callback_function, return_xml) {
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			if (return_xml)
				http_request.overrideMimeType('text/xml');
			else if (!return_xml)
				http_request.overrideMimeType('text/html');
		}

	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Browser doesn\'t support Ajax');
		return false;
	}

	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", params.length);
	http_request.setRequestHeader("Connection", "close");

	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			var status=http_request.status;
			if (status==200) {	// OK
				if (return_xml) {
					eval(callback_function + '(http_request.responseXML)');
				} else {
					eval(callback_function + '(http_request.responseText)');
				}
			} else {	// error
				if (status!=0 || status==12029 || status==12152)
					alert("The page could not be loaded at the moment (could not connect to the server). Please try to reload. Status code was: " + status);
				else if (status!=0)
					alert("The page could not be loaded at the moment. Please try to reload. Status code was: " + status);
			}
		}
	}

	http_request.send(params);
}

