
$(function() {
	$('a.lightbox').lightBox(); // Select all links with lightbox class
}); 

function regBox(type) {
	if(type == 'hide') {
		$('#dialogBox').fadeOut('slow',function(){
			$('#dialogOverlay').hide();
		});
	} else {
		$('#dialogOverlay').show();
		$('#dialogBox').fadeIn('slow');
	}
}

function showBox(type) {
	$("#dialogBox div.firstDivIEFix").hide();
	$("#" + type).show();
	regBox('show');
}


function chooseWorld(world_id) {
	$('#world_reg').val(world_id);
	$('#reg_world').val(world_id);
	showBox('RegistrationForm');
}

function register_player(){
	$.post("ajax/registration.php",$('#registration').serialize(), function(response) {
 		if(response != "done") {
 			$('#registrationMessage').html(response);
 			$('#registrationMessage').css('display', 'block');
 		} else {
 			$('#registrationMessage').html('<center><img src="' + GLOBAL_HTTP_PATH_IMAGES + 'img/gate_2/loading_animation.gif" border="0" width="48px" height="48px" /><center>');
			
 			var world_id = $('#world_reg').val();
			var loginForm = document.getElementById('loginForm');
 			loginForm.world_id.options[loginForm.world_id.selectedIndex].value = world_id;
 			
 			loginForm.action = 'http://' + worldsArray[world_id].url + '/login.php';
 			
			$('#user').val($('#user_reg').val());
			$('#pass').val($('#pass_reg').val());
			
			loginForm.submit();	
 		}
	});
	return false;
}
 
function forgotten_password() {
	$('#ForgottenPasswordForm').prepend('<span id="forgottenLoader"><center><img src="' + GLOBAL_HTTP_PATH_IMAGES + 'img/gate_2/loading_animation.gif" border="0" width="48px" height="48px" /><center></span>');
 	$.post("ajax/forgotten_password.php",$('#forgottenpassword').serialize(), function(response) {
		$('#forgottenMessage').html(response);
		$('#forgottenLoader').remove();
	});
	return false;
}

function forgotten_password_change() {
 	$.post("ajax/change_password.php",$('#forgottenpasswordchange').serialize(), function(response) {
		$('#forgottenMessageChange').html(response);
	});
	return false;
}
 
function subscribe_ajax(){
  	$.post("ajax/subscribe.php",$('#subscribe').serialize(), function(response) {
		alert(response);
	});
	return;
}

function gameTour(tour_id) {
	
	$('#GameTour > div').each(function(){
        if($(this).css('display') == 'block') {
            if(this.id == ('TourHolder-' + tour_id)) return false;
            if($.browser.msie) {
                $(this).hide();
                $('#TourHolder-' + tour_id).show();
            } else {
                $(this).fadeOut('normal',function(){
                    $('#TourHolder-' + tour_id).fadeIn('normal');
                });
            }
            return false;
        }
    });
}

function setSkin(skinId) {
	var skin = $('body').attr('id').match(/-(\d)$/);
	currentSkinId = skin[1];
		
	$('body').attr('id', 'raceSkin-' + skinId);

	$("a[id^='gss-']").each(function() {
		this.href = this.href.replace('_r' + currentSkinId, '_r' + skinId);
	});
}

function showRegistration(worldCount) {
	if (worldCount == 0) {
		return false;
	} else if ($('#crm_hash').val() != "" || worldCount < 2) {
		showBox('RegistrationForm');
	} else {
		showBox('RegistrationUniverse');
	}
}

function countDownSeconds(fieldId, seconds, callback) {
	if (seconds <= 0) {
		callback.call();
	} else {
		$('#' + fieldId).html(SecToTime(seconds) + '');
		window.setTimeout("countDownSeconds('" + fieldId + "', " + (seconds - 1) + "," + callback + " )", 1000);
	}
}

function SecToTime(s) {
	if(typeof(s) == 'undefined') return 0;
	var m=0;
	var h=0;
	if(s>59) {
		m=Math.floor(s/60);
		s-=m*60;
	}
	if(m>59) {
		h=Math.floor(m/60);
		m=m-h*60;
	}
	if(s<10) s="0"+s;
	if(m<10) m="0"+m;
	if(h<10) h="0"+h;
	
	return h+":"+m+":"+s;
}

function loginUser() {
	if ($('#user').val() == ''
		|| $('#user').val() == 'undefined'
		|| $('#pass').val() == ''
		|| $('#pass').val() == 'undefined'
		|| $('#user').data('empty')) {
		return false;
	}
	
	document.loginForm.submit();
	return false;
}

function setLoginLabels(usernameLabel) {
	// Attach to body onload to avoid writting in all templates
	$(document).ready(function() {
		if ($('#user').val() == '') {
			$('#user').val(usernameLabel);
			$('#pass').val('********');
			$('#user').data('empty', true);
		} else {
			$('#user').data('empty', false);
		}
		var userElement = document.getElementById('user');
		var passElement = document.getElementById('pass');
		userElement.onfocus = function () {
			$('#user').val('');
			$('#pass').val('');
			$('#user').data('empty', false);
			passElement.onfocus = '';
			userElement.onfocus = '';
			userElement.blur();
			setTimeout('document.getElementById(\'user\').focus()', 1); // SetTimeout fo FF not working focus work-around
		};	
		passElement.onfocus = userElement.onfocus;
	});
}


function MadmooCheckFields() {
	$.post('ajax/madmoo/register.php', $('#reg_form').serialize() + '&check=1', function(response) {
		var info = eval('('+ response + ')');
		
		if(info.error != null && info.error != '') {
			$('#reg_status').style('display', 'block');
			$('#reg_status').html(info.error + "").addClass('error').show();
		} else {
			$('#reg_status').html("").removeClass('error').hide();
		}
	});	
}

function MadmooRegister() {
    $.post('ajax/madmoo/register.php', $('#reg_form').serialize(), function(response) {
    	var info = eval('('+ response + ')');
		
		if(info.error != null && info.error != '') {
			$('#reg_status').html(info.error + "").addClass('error').show();
		} else {
			window.location.href = info.url;
		}
	});
	
}

function MadmooLogin() {
	$.post('ajax/madmoo/login.php', $('#login_form').serialize(), function(response) {
		
    	var info = eval('('+ response + ')');
		
		if(info.error != null && info.error != '') {
			//$('#reg_status').html(info.error + "").show();
			alert(info.error);
		} else {
			window.location.href = info.url;
		}
	});
}

function HallOfFameLoadNavigation() {
	if ($.trim($('#HallOfFamePopupNavigation').html()) != "") {
		return;
	}
	
	$('#HallOfFamePopupNavigation').hide();
	
	$.post(
		'ajax/hall_of_fame.php', {
			type: 'loadNavigation'
		}, function(response){
			$('#HallOfFamePopupNavigation').html(response).show();
			HallOFFameSelectWorld();
		});
}

function HallOfFameLoadTable() {
	var i_world_id = $('#hof_world_id').val()
	var i_round_num = $('#hof_round_num_' + $('#hof_world_id').val()).val();
	var i_table_type = $('#hof_table_type').val();
	
	if (i_world_id == undefined
		|| i_round_num == undefined
		|| i_table_type == undefined) {
		$('#HallOfFamePopupTable').html("");
		return;
	}
	
	$('#HallOfFamePopupTable').html('<center><img src="' + GLOBAL_HTTP_PATH_IMAGES + 'img/gate_2/loading_animation.gif" border="0" width="48px" height="48px" /></center>');
	$.post(
		'ajax/hall_of_fame.php', {
			type: 'loadTable',
			world_id: i_world_id,
			round_num: i_round_num,
			table_type: i_table_type
		}, function(response){
			$('#HallOfFamePopupTable').addClass('textbox');
			$('#HallOfFamePopupTable').html(response);
		});
}

function HallOFFameSelectWorld() {
	$('[id^="hof_round_num_"]').hide();
	$('#hof_round_num_' + $('#hof_world_id').val()).show();
	
	HallOfFameLoadTable();
}

function HallOfFameSelectRound() {
	HallOfFameLoadTable();
}

function HallOfFameSelectType() {
	HallOfFameLoadTable();
}

function HallOfFameShowAlliancePlayers(allianceId) {
	$('[id^="hof_alliance_players_"]').hide();
	$('#hof_alliance_players_' + allianceId).show();
}

function chooseTrailer(id) {
	$('#trailerButtonsHolder').addClass('thin');
	$('#gameTrailerHolder').css('display', 'none');
	$('#gameplayTrailerHolder').css('display', 'none');
	$('#' + id).css('display', 'block');
}

