/*
	Copyright Sami Puro / 2010.
*/

var forum=0;
var mainNav=1;
var reload='/';


//  Message closure.
function msgClose(data, varname, valname) {

	var data=document.getElementById(data);
	if (data!=null) {
		var url = '/game/Info/Settings/Save/';
		var params = varname + '=' + valname;
		makeHttpRequest(url, params, '', false);
		$(data).fadeOut("slow");
	}
}




//  Facebook login dialog.
function fbLogin() {
	FB.ensureInit(function() {
		FB.Connect.requireSession(function() {
			var session=FB.Facebook.apiClient.get_session();
			if (!session || !session.uid || session.uid<1)
				return false;
			else {
				FB.Facebook.apiClient.fql_query("SELECT email FROM permissions WHERE uid=" + session.uid, function(rows) {
					if (rows!=null && rows[0].email>0) {
						document.location=reload;
					} else {
						// Proceed to registration if not done.
						document.location='/Register/?action=facebook';
					}
				});
			}
		});
	});
}



//  Facebook registration function.
function fbRegister() {
	var terms=document.getElementById('FbacceptTerms');
	if (!terms.checked) {
		alert("Please read and accept the Terms of Service.");
		return false;
	}

	// Ask for login and permission.
	FB.Connect.requireSession(function() {
		FB.Connect.showPermissionDialog('email', function() {
			var session=FB.Facebook.apiClient.get_session();
			if (!session || !session.uid || session.uid<1)
				return false;
			else {
				FB.Facebook.apiClient.fql_query("SELECT email FROM permissions WHERE uid=" + session.uid, function(rows) {
					if (rows!=null && rows[0].email>0) {
						document.forms["fbReg"].submit();
					} else {
						// Permission for email not given, ask for it.
						var btn=document.getElementById("buttonFbLogin");
						$(btn).fadeOut('slow');
						var data=document.getElementById("fbEmail");
						$(data).fadeIn('slow');
						var btn=document.getElementById("fbSubmit");
						$(btn).fadeIn('slow');
					}
				});
			}
		});
	});
}




//  Facebook merge function.
function fbMerge() {
	// Ask for login and permission.
	FB.Connect.requireSession(function() {
		FB.Connect.showPermissionDialog('email', function() {
			var session=FB.Facebook.apiClient.get_session();
			if (!session || !session.uid || session.uid<1)
				return false;
			else {
				FB.Facebook.apiClient.fql_query("SELECT email FROM permissions WHERE uid=" + session.uid, function(rows) {
					if (rows!=null && rows[0].email>0) {
						document.forms["formMerge"].submit();
					}
				});
			}
		});
	});
}




function includeJS(jsPath){
	var script = document.createElement("script");
	script.setAttribute("type", "text/javascript");
	script.setAttribute("src", jsPath);
	document.getElementsByTagName("head")[0].appendChild(script);
}

// 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");
}


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')";
	}
}



window.onload=function() {

	if (forum>0)
		return;

	// 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();

}




// Ajax related.
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
					alert("The page could not be loaded at the moment. Please try to reload. Status code was: " + status);
			}
		}
	}

	http_request.send(params);
}
