var count = 1;
$(document).ready( function() {
	$("#mesgStack").hide();
	if (rotate) {
		theRotator();
	}
	$('form[name=loginForm]').submit(function(){	
		$.post('auth.php', 
				{username: $('[name=username]').val(), password: $('[name=password]').val(), cmd: "login"},
				function(data) {
					if (data.success) {
						location.href=data.redirect;
					} else {
						$('[name=username]').val("");
						$('[name=password]').val("");
						$("#dialog").attr("title","Login Failed");
						$("#dialog").html(data.message);
						$(function() {
							$("#dialog").dialog({bigframe: true, modal: true});
							$("#dialog").dialog("open");
						});
					}
				},
				'json');
		return false;
	});
	
});

function theRotator() {		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
	
}

function showReferAFriendProgram() {
	
	$("#dialog").attr("title","FitSavers Refer-A-Friend Program");
	$("#dialog").html(
			"FitSavers Refer-A-Friend program allows you to earn $10 a month for each person you refer that becomes a FitSavers Plus member. <br /><br />" +
			"In order to qualify for the Refer-A-Friend program you need be a FitSavers Plus member. " +
			"Once your a FitSavers Plus member, simply tell your friends about all the cool stuff you like about fitsavers." +
			"All your friend needs to do is enter your email address in the 'Refering Email' field on this page when they register!<br /><br />" +
			"Then when someone you refers signs up for a FitSavers Plus membership we'll pay you $10 each month " +
			"for as long as that person remains a member!<br /><br />It's that simple! What are you waiting for? Get your FitSavers Plus membership today!");
	$(function() {
		$("#dialog").dialog({bigframe: true, modal: true});
		$("#dialog").dialog("open");
	});
}

function rotate() {	
	switch (count) {
		case 1:
			$("#home-hero").fadeOut("fast", function() { $("#home-hero-1").fadeIn("slow") });
			count++;
			break;
		case 2:
			$("#home-hero-1").fadeOut("fast", function() { $("#home-hero-2").fadeIn("slow") });
			count++;
			break;
		case 3:
			$("#home-hero-2").fadeOut("fast", function() { $("#home-hero").fadeIn("slow") });
			count=1;
			break;
	}
};

function bouncersUpdate(action, data) {
  if (action == "insert"){
    $.post('widgets/captureStep1.php', 
		{
			firstName : $('[name=firstName]').val(), 
			lastName : $('[name=lastName]').val(), 
			email : $('[name=email]').val(), 
			phone : $('[name=phone]').val(), 
			zip : $('[name=zip]').val(), 
			action : "insert", 
			source : "2"
		},
	    function(result) {//this is the call back function.  the 'result' object is the json sting that is output by the caputre1.php
	      if(result.success){
	    	if (data.submitRegisterStep1) {
				$('form[name=register_step1]').submit();
			}
			return true;
	      } else {
	    	  return false;
	      }
	    },
    	'json'
	   );
  } else if (action == "remove"){
    $.post('widgets/captureStep1.php', 
		{
			firstName : data.firstName, 
			lastName : data.lastName, 
			email : data.email, 
			phone : data.phone, 
			zip : data.zip, 
			action : "remove", 
			source : "2"
		},
	    function(result) {
	      if(result.success){
	    	  return true;
	      } else {
	    	  return false;
	      }
	    },
		'json'
    );
  }
}

function registrationComplete(){
	$('#dialog').dialog({dialogClass: 'alert'});
	$("#dialog").attr("title","FitSavers New Customer Registratioin");
	$("#dialog").html("Your Registration Is Complete.");
	$(function() {
		$("#dialog").dialog({bigframe: true, modal: true});
		$("#dialog").dialog("open");
	})
}
