var Home = {
	container: null,
	homeForm: null,

	init: function() {
		
		Home.container = $('div.container');
		
		//first step
		Home.homeForm = Home.container.find('form#homeForm');
		//second step
		Home.createAccountForm = Home.container.find('form#createAccountForm');

		//set the country in the contact me section from geoIP
		Home.homeForm.find('select#country').find('option[value="' + AppData.GEOIPCountry + '"]').attr('selected','selected');

		//bind form element to be 'sticky'
		Home.homeForm.find('input[type="text"]:not(input[name="captcha"])').each( function(){ 
			$(this).bind('focus', {defaultValue: AppData.lang[this.name]}, General.onFocusTextElement)
				   .bind('blur', {defaultValue: AppData.lang[this.name]}, General.onBlurTextElement);
		});

		Home.createAccountForm.find('input[type="text"]').each( function(){
			$(this).bind('focus', {defaultValue: AppData.lang[this.name]}, General.onFocusTextElement)
				   .bind('blur',  {defaultValue: AppData.lang[this.name]}, General.onBlurTextElement);
		});



        Home.createAccountForm.find('li.terms div').bind('click',{},function(){
            $.modaldialog.success(AppData.termsText, {
				title: 'Terms and Conditions'
			});
            $('div#dialog').css('top','144px');
        });
   

		//swfobject.embedSWF("appProxy/OptionTide_slideshow.swf", "steps", "300", "120", "9.0.0");
		var flashvars = false;
		var params = {
		  wmode: "transparent",
		  loop: false
		}
		swfobject.embedSWF("appProxy/OptionTide_slideshow.swf", "reasonsList", "558", "390", "9.0.0", "expressInstall.swf", flashvars, params);



		Home.createAccountForm.find('input[name="passwordFake"]').bind('focus', {container: 'li.password'}, General.onFocusPassword);
		Home.createAccountForm.find('input[name="password"]').bind('blur', {container: 'li.password'}, General.onBlurPassword);

		Home.createAccountForm.find('input[name="password"]').bind('keypress',Home.copyPassword);

		//check if the affiliate has completed
		if (AppData.stepNumber == '2'){
			//show the second step
			Home.homeForm.addClass('hidden');

			Home.createAccountForm.removeClass('hidden');
			Home.createAccountForm.addClass('visible');
			Home.container.find('div.homeCenter').css('background','url(appProxy/main_page_center_bg2.jpg) no-repeat');
		}

		
		$.validator.addMethod('notDefault', function(value, element) {
			return value != $(element).attr('default');
		}, 'Value can\'t be the default text');

		$('form#homeForm').validate( {
			groups: {
    			username: "countryCodeFake areaFake PhoneFake"
  			},
			errorPlacement: function(error, element) {
				//error.appendTo($('span#' + element.attr('name')));
				if (element.attr('name') == 'email')
					element.css('background','url(appProxy/main_reg_form_big_text_error.gif) no-repeat scroll left top');

				else if (element.attr('name') == 'FirstName')
					element.css('background','url(appProxy/main_reg_form_small_text_error.gif) no-repeat scroll left top');
				else if (element.attr('name') == 'LastName')
					element.css('background','url(appProxy/main_reg_form_small_text_last_error.gif) no-repeat scroll left top');
				else if ($(element).attr('name') == 'captcha')
					$(element).css('background','url(appProxy/captcha_code_error.gif) no-repeat scroll left top');
			},
			unhighlight: function (element){
				if ($(element).attr('name') == 'email')
					$(element).css('background','url(appProxy/main_reg_form_big_text.gif) no-repeat scroll left top');
				else if ($(element).attr('name') == 'FirstName')
					$(element).css('background','url(appProxy/main_reg_form_small_text.gif) no-repeat scroll left top');
				else if ($(element).attr('name') == 'LastName')
					$(element).css('background','url(appProxy/main_reg_form_small_text_last.gif) no-repeat scroll left top');
				else if ($(element).attr('name') == 'captcha')
					$(element).css('background','url(appProxy/captcha_code.gif) no-repeat scroll left top');
			},
			submitHandler: function(form) {
				Home.formSubmit();
   			}

		});

		$('form#createAccountForm').validate( {
			groups: {
    			username: "countryCodeFake areaFake PhoneFake passwordFake"
  			},
			errorPlacement: function(error, element) {
				//error.appendTo($('span#' + element.attr('name')));
				if ($(element).attr('id') == 'prefix' || $(element).attr('id') == 'phone')
					element.css('background','url(appProxy/main_reg_form_small_text_error.gif) no-repeat scroll left top');
				else
					element.css('background','url(appProxy/main_reg_form_big_text_error.gif) no-repeat scroll left top');
			},
			unhighlight: function (element){
				if ($(element).attr('id') == 'prefix' || $(element).attr('id') == 'phone')
					$(element).css('background','url(appProxy/main_reg_form_small_text.gif) no-repeat scroll left top');
				else
					$(element).css('background','url(appProxy/main_reg_form_big_text.gif) no-repeat scroll left top');

			},
			submitHandler: function(form) {
				Home.createAccountFormSubmit();
   			}

		});


		Home.homeForm.find('select#country').selectbox(
				{
					inputClass: 'selectboxBigLongMain',
					containerClass: 'selectbox-wrapperBigLongMain',
					scrollBar: true,
					hoverClass: 'selectHover'
				}
		)

	},

	copyPassword: function (event){
		Home.createAccountForm.find('input[name="passwordFake"]').val($(this).val());
	},

	onFocusEmail: function(event){
		var field = $(event.target);
		var value = $.trim(field.val());
		if(value == AppData.lang.email)
			field.val('');
	},

	onBlurEmail: function(event){
		var field = $(event.target);
		var value = $.trim(field.val());
		if(value == '')
			field.val(AppData.lang.email)

	},

	/* first step form submit */
	formSubmit: function(event){
		var formParams = $('#homeForm').serializeArray();

		$.post('home/register', formParams,

		function(data){
			if (data.status!=true){
				$.each(data,function(i,error){
					if (i!='status')
						alert(error);
				});
			}else{ //success
				
				//show the next step
				Home.homeForm.addClass('hidden');
				
				Home.createAccountForm.removeClass('hidden');
				Home.createAccountForm.addClass('visible');
				Home.container.find('div.homeCenter').css('background','url(appProxy/main_page_center_bg2.jpg) no-repeat');

				

				//location.reload(true); //fix for the jscrollpane bug (doesnt initiate well)
			}

		}
		, 'json');


		return false;
	},

	/* second step form submit */
	createAccountFormSubmit: function(event){
		var formParams = $('#createAccountForm').serializeArray();

		$.post('home/createAccount', formParams,

		function(data){
			if (data.status!=true){
				$.each(data,function(i,error){
					if (i!='status')
						alert(error);
				});
				window.location = "http://www.optiontide.com/"
			}else{ //success

				Home.createAccountForm.removeClass('visible').addClass('hidden');
				$('div.startProfiting').removeClass('hidden').addClass('visible');


				Home.container.find('div.homeCenter').css('background','url(appProxy/main_page_center_bg3.jpg) no-repeat');

				//location.reload(true); //fix for the jscrollpane bug (doesnt initiate well)
			}

		}
		, 'json');


		return false;
	}

}

$(document).ready(function() {
	Home.init();
});
