if (top.location != location) {
    top.location.href = document.location.href ;
}

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


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);
	showBox('RegistrationForm');
}

function register_player(){
	$.post("ajax/registration.php",$('#registration').serialize(), function(response) {
 		if(response != "done") {
 			$('#registrationMessage').html(response);
 		} else {
 			$('#registrationMessage').html('<center><img src="img/gate/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() {
 	$.post("ajax/forgotten_password.php",$('#forgottenpassword').serialize(), function(response) {
		$('#forgottenMessage').html(response);
	});
	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;
        }
    });
}

var statesMap = new Array();
statesMap[1] = {'left': 2, 'center': 1, 'right': 3};
statesMap[2] = {'left': 1, 'center': 2, 'right': 3};
statesMap[3] = {'left': 1, 'center': 3, 'right': 2};

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 addMapListeners() {
	$('#skinZone1').bind('mouseover click', function() {
		var skin = $('body').attr('id').match(/-(\d)$/);
		currentSkinId = skin[1];
		setSkin(statesMap[currentSkinId].left);
	});

	$('#skinZone3').bind('mouseover click', function() {
		var skin = $('body').attr('id').match(/-(\d)$/);
		currentSkinId = skin[1];
		setSkin(statesMap[currentSkinId].right);
	});
}

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