$.fn.image = function(src, f){
  return this.each(function(){
    $("<img>").appendTo(this).src( src ).load( f );
  });
};


var Marketing = {
	activateIndexSlideShow: function(){
		var it = [ "https://pasapp.com/images/marketing/fpo_john.jpg", 
				   "https://pasapp.com/images/marketing/fpo_larry.jpg",
				   "https://pasapp.com/images/marketing/fpo_scott.jpg"];
		var i = 0;
		setInterval(function(){
			i = i + 1
			if (i == 3) {i = 0};
			$('#slideshow img').attr('src',it[i]);
		},6000);
	},
	activateBenefitsSlideShow: function(){
		var benefits = ["https://pasapp.com/images/marketing/fpo_benefits_feedback_paul.jpg",
				   "https://pasapp.com/images/marketing/fpo_benefits_feedback_bill.jpg", 
				   "https://pasapp.com/images/marketing/fpo_benefits_feedback_marty.jpg",
				   "https://pasapp.com/images/marketing/fpo_benefits_feedback_heidi.jpg"];
		var i = 0;
		setInterval(function(){
			i = i + 1
			if (i == 4) {i = 0};
			$('#feedback_cta a img').attr('src',benefits[i]);
		},7000);
	},
	activateUsingPasRollovers: function(){
	    $("#using_pas .btm .image").mouseover(function(){
	        $(this).find('.btm_overlay').show();
	    }).mouseout(function(){
	        $(this).find('.btm_overlay').hide();
	    });
	},	  
	usingFreeTrial: function(){
		$('#btn_pasport').click(function() {
			$(this).hide();
			$('#btn_no_pasport').show();
			$('#new_user').hide();
			$('#existing_user').show();
			return false;
		});
		
		$('#btn_no_pasport').click(function() {
			$(this).hide();
			$('#btn_pasport').show();
			$('#existing_user').hide();
			$('#new_user').show();
			return false;
		});	
	},
	activateAccountNameCopy: function(){
		var focus_url = null;

		$('#account_account_subdomain').focus(function(){focus_url = true});
		$('#account_account_subdomain').keyup(function(){
			Marketing.replaceSpaces();
		});
		$('#account_account_name').keyup(function(){
			if (!focus_url) {
				var oo = $(this).val();
				$('#account_account_subdomain').val(oo)
				Marketing.replaceSpaces();
			};
		});
	},
	replaceSpaces: function(val){
		var oo = $("#account_account_subdomain").val();
	    oo = oo.replace(/(\s|\W)/g,"").toLowerCase();
		$('#account_account_subdomain').val(oo);
	},
	activateEmailValidation: function(){
		$('#account_person_email').blur(function() {
			Marketing.validateEmail();
		});
	},
	validateEmail: function(){
		var person_email = $('#account_person_email').val();
		var params = { email:person_email };

		$.ajax({
		  url: '/has_pasport',
		  type: 'GET',
		  dataType: 'json',
		  data: $.param( params ),
	
  		  success: function(has_pasport) {
				Marketing.validateEmailSuccess(has_pasport);
 			}
		});
	},
	validateEmailSuccess: function(has_pasport){
		 var person_form = $('#person_forms');
	       if (has_pasport) {
		   		person_form.parents('form').attr('action', '/accounts/create_from_existing_pasport');
    	   		$('#has').show().find('input').each(function(index) {
			     	$(this).attr('disabled', '')
					if (index === 0) {$(this).focus();};
			    })
				$('#needs').hide().find('input').each(function(index) {
					$(this).attr('disabled', 'disabled');
				});
 	  	   } else {
		   		person_form.parents('form').attr('action', '/accounts');
		   		$('#needs').show().find('input').each(function(index) {
			    	$(this).attr('disabled', '');
			        if (index === 0) {$(this).focus();};
			    })
				$('#has').hide().find('input').each(function(index) {
					$(this).attr('disabled', 'disabled');
				});
		   }
	}
}

activatorClass(Marketing)
