var Globals = new function() 
{
	this.hosts = {};
	
	
	this.redirect = function (url) 	
	{
		window.location = url;
	}
}

var ModalWindows = new function() 
{
	this.showLoading = function () 
	{	
		$("#box-hide").css({display:"block",height: $(document).height(),width: $(document).width()}).animate({opacity:"0.9"}, 500,function(){
			var ltie7 = IElt7();
			if (ltie7) {
				$("select").css('visibility','hidden');
			}
			$("#modals").css("display","block");
		});	
	}
	
	this.hideLoading = function () 
	{
		$("#box-hide").css({display:"none",width: $(document).width()}).animate({opacity:"0"}, 500,function(){
			var ltie7 = IElt7();
			if (ltie7) {
				$("select").css('visibility','visible');
			}
			
			$("#modals").html('<img src="' + Globals.hosts.wwwstatic + '/images/default/loading.gif" alt="loading" class="loading" />');
			$("#modals").css("display","none");
			
		});	
		
	}
	
	
	this.showModal = function (data) 
	{	
		$("#modals").html(data);
	}
	
	this.closeModal = function (url) 
	{			
		$("#box-hide").animate({opacity:"0"}, 500, function(){$("#box-hide").css("display","none")});
		var ltie7 = IElt7();
		if (ltie7) $("select").css('visibility','visible');
		$("#modals").css("display","none");
		$("#modals").html('<img src="' + Globals.hosts.wwwstatic + '/images/default/loading.gif" alt="loading" class="loading" />');
		if (url && url != '') {
			Globals.redirect(url);
		}
	}	

}

var Game = new function() 
{
	
	this.loadQuestion = function (questionNumber) 
	{	
		ModalWindows.showLoading();
		
		$.ajax({
			type: "POST",
			url: '/game/question/',
			data: ({id: questionNumber}),
			ataType: "html",
			success: function(result){
				$('#questionContainer').html(result);
				ModalWindows.hideLoading();
			}
		});
	}

	this.saveQuestion = function (questionNumber) 
	{
		ModalWindows.showLoading();
		
		$.ajax({
			type: "POST",
			url: '/game/save-question/question/'+questionNumber,
			data: $('#questionForm').serialize(),
			dataType: "json",
			success: function(result){
				result = eval(result);
				if (result.messages && result.messages.length) {
					ModalWindows.showModal(result.messages);				
				} else if (result.nextQuestion) {
					Game.loadQuestion(result.nextQuestion);
				} else {
					Globals.redirect('/game/');
				}
				 
			}
		});
		
	}
	
}

function submitForm(form) {
	
	ModalWindows.showLoading();
	$.ajax({
		type: "POST",
		url: form.action,
		data: $('#' + form.id).serialize(),
		dataType: "json",
		success: function(errors){			
			var result = eval(errors);
			if (result.hasError) {
				ModalWindows.showModal(result.html);				
			} else if(result.url) {
				Globals.redirect(result.url);
			} else if(result.action) {
				form.action = result.action;
				$('input[name=data]').val(result.data);
				form.submit();
			}
		}
	});
}

function showPasswordModal(url) {
	
	ModalWindows.showLoading();
	
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		success: function(response){
			
			var result = eval(response);
			if (result.html) {
				ModalWindows.showModal(result.html);				
			}
		}
	});
}

function showGameFlash(name, flashId, sid, highestScore) {
	
	var flashvars = {};
	flashvars.userSession = sid;
	flashvars.highScore = highestScore;
	flashvars.gate = Globals.hosts.www + '/';
	
	var params = {};
	//params.wmode = "transparent";
	params.wmode = "window";
	params.bgcolor = "#000000";
	params.allowscriptaccess = "always";
	params.allowFullScreen = "false";
	
	var attributes = {};
	
	swfobject.embedSWF(
			Globals.hosts.wwwstatic + '/flash/' + name, 
			flashId, 
			"700", 
			"300", 
			"9.0.0", 
			Globals.hosts.wwwstatic + '/flash/expressInstall.swf', 
			flashvars, 
			params, 
			attributes
		);
}

function fbClick(serviceUrl, title) { 
	window.open(
		'http://www.facebook.com/sharer.php?u='+encodeURIComponent(serviceUrl)+'&t='+encodeURIComponent(title),
		' sharer', 
		'toolbar=0, status=0, width=626, height=436'
	); 
	return false; 
}

/**
 * Ajax captcha refresh
 * 
 * @param captcha_id
 * @param url
 * 
 */
function refreshCaptcha(captchaId, url)
{
	
	$.getJSON(url, {}, function(json){
		$('#'+captchaId).html(json.captcha);
	});
}



$(document).ready(function(){
	
	$("input[name=selectall]").click(function(){
		var checked_status = this.checked;
		$("input[class=checkbox]").each(function(){
			this.checked = checked_status;
		});
	});  
});