var count = 1;
jQuery( function() {
	$(".clickable").hover(function() {
		change_content($(this));
		
		return false;
	});
	
	$("#mesgStack").hide();
	$('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;
	});
	
	$("#ask-personal-trainer").click(function() {
		// open query modal window
		$("#dialog").attr("title","Ask My Personal Trainer");
		$("#dialog").html('<form method="post" action="" id="ask-trainer-form">Enter Your Question<br /><textarea style="width:275px;height:50px;" name="trainer-question"></textarea><br /><input id="send-question" type="submit" value="Send Question" /><div id="question-response"></div></form>');
		$(function() {
			$("#dialog").dialog({bigframe: true, modal: true});
			$("#dialog").dialog("open");
		});
		
		$("#ask-trainer-form").submit(function() {
			// make sure user is logged in
			if ($('[name=user-email]').val() == '') {
				$("#question-response").html('You just be logged in to submit a question.');
				return false;
			}
			// send form if question is present
			if ($('[name=trainer-question]').val() != '') {
			
				$("#send-question").hide();
				$("#question-response").html("<center><img src='images/ajax-loader.gif' alt='Loading...' /><br />Sending Email...</center>");
			
				$.post('auth.php', 
					{
					message: $('[name=trainer-question]').val(),
					email: $('[name=user-email]').val(),
					cmd: "sendTrainerQuestion"
					},
					function(data) {
						if (data.success) {
							// show success message on return
							$("#dialog").attr("title","Question Sent");
							$("#dialog").html("Your question has been sent to the FitSavers personal trainer.");
							$(function() {
								$("#dialog").dialog({bigframe: true, modal: true});
								$("#dialog").dialog("open");
							});
						} else {
							$("#dialog").attr("title","Error Sending Question");
							$("#dialog").html(data.message);
							$(function() {
								$("#dialog").dialog({bigframe: true, modal: true});
								$("#dialog").dialog("open");
							});
						}
					},
					'json'
				);
			}
			else {
				$("#question-response").html('Please enter your question.');
			}
			
			return false;
		});
		
		
		return false;
	});
	
	$("#ask-personal-dietician").click(function() {
		// open query modal window
		$("#dialog").attr("title","Ask My Personal Dietician");
		$("#dialog").html('<form method="post" action="" id="ask-dietician-form">Enter Your Question<br /><textarea style="width:275px;height:50px;" name="dietician-question"></textarea><br /><input type="submit" value="Send Question" id="send-question" /><div id="question-response"></div></form>');
		$(function() {
			$("#dialog").dialog({bigframe: true, modal: true});
			$("#dialog").dialog("open");
		});
		
		$("#ask-dietician-form").submit(function() {
			// make sure user is logged in
			if ($('[name=user-email]').val() == '') {
				$("#question-response").html('You just be logged in to submit a question.');
				return false;
			}
			// send form if question is present
			if ($('[name=dietician-question]').val() != '') {
			
				$("#send-question").hide();
				$("#question-response").html("<center><img src='images/ajax-loader.gif' alt='Loading...' /><br />Sending Email...</center>");
			
			
				$.post('auth.php', 
					{
					message: $('[name=dietician-question]').val(),
					email: $('[name=user-email]').val(),
					cmd: "sendDieticianQuestion"
					},
					function(data) {
						if (data.success) {
							// show success message on return
							$("#dialog").attr("title","Question Sent");
							$("#dialog").html("Your question has been sent to the FitSavers personal dietician.");
							$(function() {
								$("#dialog").dialog({bigframe: true, modal: true});
								$("#dialog").dialog("open");
							});
						} else {
							$("#dialog").attr("title","Error Sending Question");
							$("#dialog").html(data.message);
							$(function() {
								$("#dialog").dialog({bigframe: true, modal: true});
								$("#dialog").dialog("open");
							});
						}
					},
					'json'
				);
			}
			else {
				$("#question-response").html('Please enter your question.');
			}
			
			return false;
		});
		
		
		return false;
	});
	
	$('#merchant-logos').cycle({
		fx: 'scrollHorz',
		prev: '#logos-arrow-left',
		next: '#logos-arrow-right',
		timeout: 5000,
		easing: 'easeInOutBack'
	});
	
});

function change_content(e) {
	var parent = e.parents('.changable');
	var img = e.find('img')[0];
	
	var src = img.src;
	if (src.indexOf('-sel.png') == -1) {
		parent.find('.clickable').each(function(index) {
			$(this).find('img').each(function(index) {
				img_src = this.src;
				if (img_src.indexOf('-sel.png') > -1) {
					this.src = img_src.split('-sel.png')[0] + '.png';
				}
			});
		});
		
		var url = src.split('.png')[0];
		var new_bullet_url = url + '-sel.png';
		img.src = new_bullet_url;
		
		url = url.split('/').pop();
		
		var content_elements = parent.find('.selected-content');
		content_elements.removeClass('selected-content');
		$('#' + url + '-content').addClass('selected-content');
	}
}

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 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").attr("title","FitSavers New Customer Registratioin");
	$("#dialog").html("Thank You! Your Registration Is Complete.");
	$(function() {
		$("#dialog").dialog({bigframe: true, modal: true});
		$("#dialog").dialog("open");
	})
}