var bTriggerWarning = 0;
var profiles =
{
	TOS:
	{
		height:500,
		width:750,
		status:0,
		toolbar:0,
		scrollbars:1,
		resizable:1,
		name:"tos",
		center:1
	},

	game:
	{
		height:610,
		width:994,
		status:0,
		toolbar:0,
		scrollbars:0,
		resizable:0,
		name:"game",
		center:1
	},

	signup:
	{
		height:610,
		width:994,
		status:0,
		toolbar:0,
		scrollbars:0,
		resizable:0,
		center:1,
		name:"game",
		href:"/game/php/register.php?pending=true"
	}
}

jQuery(document).ready(function() {
	jQuery(".popupWindow").popupwindow(profiles);

	jQuery("#bubble_register_button").click(function() {
		jQuery("#bubble_login").fadeOut('fast', function() {
			jQuery("#bubble_register").fadeIn('fast');
			jQuery("#bubble_tab_register").removeClass('inactive');
			jQuery("#bubble_tab_login").addClass('inactive');
		});
	});

	jQuery("#bubble_login_button").click(function() {
		jQuery("#bubble_register").fadeOut('fast', function() {
			jQuery("#bubble_login").fadeIn('fast');
			jQuery("#bubble_tab_login").removeClass('inactive');
			jQuery("#bubble_tab_register").addClass('inactive');
		});
	});

	jQuery("#btn_register").click(function() {
		var email 	= jQuery("#sfa_email_step_1").val();
		var password= jQuery("#sfa_password_step_1").val();

		if (email=='' || email=='E-Mail')
			fieldWarning('sfa_email_step_1','Required');

		if (password=='' || password=='Password')
			fieldWarning('sfa_password_step_1','Required');

		if (bTriggerWarning==1) {
			bTriggerWarning = 0;
			return false;
		}

		jQuery("#bubble_content .step_1").fadeOut('fast', function() {
			jQuery("#bubble_content .step_2").fadeIn('fast', function() {
				jQuery("#sfa_email_step_2").val(email);
				jQuery("#sfa_password_step_2").val(password);
			});
		});
	});

	jQuery("#btn_register_back").click(function() {
		jQuery("#bubble_content .step_2").fadeOut('fast', function() {
			jQuery("#bubble_content .step_1").fadeIn('fast');
		});
	});

	jQuery("#btn_register_now").click(function() {
		var email_1 = jQuery("#sfa_email_step_1").val();
		var email_2 = jQuery("#sfa_email_confirm").val();
		if (email_1!=email_2)
			fieldWarning('sfa_email_confirm','E-Mail address did not match');
		if (!jQuery('#accept_tos').attr('checked'))
			fieldWarning('toc_confirm','You need to accept the Terms & Conditions');

		if (bTriggerWarning==1) {
			bTriggerWarning = 0;
			return false;
		}

		jQuery('#register_link').trigger('click');

		var form = jQuery("form#register_step_2");
		form.submit();
	});

	jQuery("#fb_logout").click(function() {

	});
	
	handleTextfieldHint('#sfa_email');
	handleTextfieldHint('#sfa_email_step_1');
	handlePasswordfieldHint('#sfa_password_step_1', '#sfa_password_step_1_dummy');
	
	FB.init("6f7c8cc99e7ba4ee5b464ff89b878d6a", "http://www.starfeveragency.com/game/flash/xd_receiver.htm");

});

function refresh() {
	location.href=location.href.replace('?logout=true','');
}

function fieldWarning(field,message) {
	bTriggerWarning = 1;
	var offset 	= jQuery('#'+field).offset();
	var left 	= offset.left + 10;
	var top 	= offset.top + 20;
	jQuery('#wrapper').after("<div id='warning_"+field+"' class='warning hidden'>"+message+"</div>");
	jQuery("#warning_"+field).css( { "left": left + "px", "top": top + "px" } );
	jQuery("#warning_"+field).fadeIn();
	jQuery('#'+field).hover( function() {
		field = jQuery(this).attr('id');
		jQuery("#warning_"+field).fadeOut();
	});
	jQuery('#'+field).click( function() {
		field = jQuery(this).attr('id');
		jQuery("#warning_"+field).fadeOut();
	});
}

function parentAlert(step,fields,str,child) {
	child.close();
	var x = 0;
	if (step!='null') {
		stepR = step == 1 ? 2 : 1;
		jQuery("#bubble_content .step_"+stepR).fadeOut('fast', function() {
			jQuery("#bubble_content .step_"+step).fadeIn('fast');
		});
		for (x=0; x<fields.length;x++) {
			setTimeout('fieldWarning("'+fields[x]+'","'+str+'")',1000);
		}
	} else {
		for (x=0; x<fields.length;x++) {
			fieldWarning(fields[x],str);
		}
	}
}

function handleTextfieldHint(id) {
	jQuery(id).css({'color': '#A0A0A0'});
	
	jQuery(id).focus(function(){ 
	  if(jQuery(this).val() == jQuery(this).attr('defaultValue'))
	  {
	    jQuery(this).val('');
	    jQuery(this).css({'color': '#000000'});
	  }
	});
	
	jQuery(id).blur(function(){
	  if(jQuery(this).val() == '')
	  {
	    jQuery(this).type = "text";
	    jQuery(this).val(jQuery(this).attr('defaultValue'));
	    jQuery(this).css({'color': '#A0A0A0'});
	  } 
	});
}

function handlePasswordfieldHint(id, idDummy) {
	
	jQuery(idDummy).css({'color': '#A0A0A0'});
	jQuery(idDummy).show();
	
	jQuery(id).css({'color': '#000000'});
	jQuery(id).hide();
	
	jQuery(idDummy).focus(function() {
		jQuery(idDummy).hide();
		jQuery(id).show();
		jQuery(id).focus();
	});
	
	jQuery(id).blur(function() {
		if(jQuery(id).val() == '') {
			jQuery(idDummy).show();
			jQuery(id).hide();
		}
	});
}