$(function() {
	$(".nav").mouseover(function(){
		var img_src = $(this).attr("src");		
		switch (img_src) {
			case 'images/saw-on-bus.gif' : 			
				$(this).attr({ 
				  src: "images/saw-on-bus_over.gif",
				  title: "Saw an ad on the back of a bus.",
				  alt: "Saw an ad on the back of a bus."
				});
				break;
			case 'images/heard-on-radio.gif' : 			
				$(this).attr({ 
				  src: "images/heard-on-radio_over.gif",
				  title: "Heard ad on the radio.",
				  alt: "Heard ad on the radio."
				});
				break;
			case 'images/friend-sent-me.gif' : 			
				$(this).attr({ 
				  src: "images/friend-sent-me_over.gif",
				  title: "A friend sent me here.",
				  alt: "A friend sent me here."
				});
				break;
		}

	});
	
	$(".nav").mouseout(function(){
		var img_src = $(this).attr("src");		
		switch (img_src) {
			case 'images/saw-on-bus_over.gif' : 			
				$(this).attr({ 
				  src: "images/saw-on-bus.gif",
				  title: "Saw an ad on the back of a bus.",
				  alt: "Saw an ad on the back of a bus."
				});
				break;
			case 'images/heard-on-radio_over.gif' : 			
				$(this).attr({ 
				  src: "images/heard-on-radio.gif",
				  title: "Heard ad on the radio.",
				  alt: "Heard ad on the radio."
				});
				break;
			case 'images/friend-sent-me_over.gif' : 			
				$(this).attr({ 
				  src: "images/friend-sent-me.gif",
				  title: "A friend sent me here.",
				  alt: "A friend sent me here."
				});
				break;
		}
	});
	
	$(".learn_more").mouseover(function(){
		$(this).attr({ 
			src: "images/learn-more_over.gif",
			title: "LEARN MORE",
			alt: "LEARN MORE"
		});
	});
	
	$(".learn_more").mouseout(function(){
		$(this).attr({ 
			src: "images/learn-more.gif",
			title: "LEARN MORE",
			alt: "LEARN MORE"
		});
	});
	
	$(".pedman-prev").mouseover(function(){
		$(this).attr({ 
			src: "images/btn-prev_over.gif",
			title: "Previous Image",
			alt: "Previous Image"
		});
	});
	
	$(".pedman-prev").mouseout(function(){
		$(this).attr({ 
			src: "images/btn-prev.gif",
			title: "Previous Image",
			alt: "Previous Image"
		});
	});
	
	$(".pedman-next").mouseover(function(){
		$(this).attr({ 
			src: "images/btn-next_over.gif",
			title: "Next Image",
			alt: "Next Image"
		});
	});
	
	$(".pedman-next").mouseout(function(){
		$(this).attr({ 
			src: "images/btn-next.gif",
			title: "Next Image",
			alt: "Next Image"
		});
	});
	
	$(".send-btn").mouseover(function(){
		$(this).attr({ 
			src: "images/btn-send-to-friend_over.gif",
			title: "Send this image to a friend",
			alt: "Send this image to a friend"
		});
	});
	
	$(".send-btn").mouseout(function(){
		$(this).attr({ 
			src: "images/btn-send-to-friend.gif",
			title: "Send this image to a friend",
			alt: "Send this image to a friend"
		});
	});
	
	$(".download-btn").mouseover(function(){
		$(this).attr({ 
			src: "images/btn-download_over.gif",
			title: "Download this image",
			alt: "Download this image"
		});
	});
	
	$(".download-btn").mouseout(function(){
		$(this).attr({ 
			src: "images/btn-download.gif",
			title: "Download this image",
			alt: "Download this image"
		});
	});

	$(".next-prev").click(function() {
		var direction = $(this).attr("id");
		var img_src = $("#promo_img").attr("src");
		var img_num = img_src.substr(13,1);
		switch (direction) {
			case 'prev' : img_num--;
						  if (img_num == 0) img_num = 8;
						  break;
			case 'next' : img_num++;
						  if (img_num == 9) img_num = 1;
						  break;
		}
		img_src = "images/promo_" + img_num + ".gif";
		lg_img_src = "../images/promo_" + img_num + "_lg.jpg";
		$("#promo_img").attr({ 
				  src: img_src
		});
		
		$("#download").attr({
			//href: "includes/dlimgs.php?jpg=" + lg_img_src	
			href: "includes/dlimgs.php?jpg=" + img_num	
		});
	});

	$(".download").click(function() {
		var img_src = $("#promo_img").attr("src");
		var img_num = img_src.substr(13,1);
		img_src = "images/promo_" + img_num + "_lg.jpg";
		var dataString = 'jpg='+ img_src;
		//alert (dataString); return false;
		$.ajax({
		type: "POST",
		url: "includes/dlimgs.php",
		data: dataString,
		success: function() {
			
		}
		});
		return false;
	});
	
	$(".send").click(function() {
		$('#controls').hide();
		$('#form_fields').show();
	});

	$(".sub_answer").click(function() {
		var referral = $(this).attr("id");
		var dataString = 'referral='+ referral;
		//alert (dataString); return false;
		$.ajax({
		type: "POST",
		url: "includes/process.php",
		data: dataString,
		success: function() {
		  $('#contact_form').hide();
		  $('#thanks').show();
		  $('#road_sign').hide();
		  $('#send_to_friend').show();
		}
		});
		return false;
	});
	
	// show card controls, hide send-to-friend form fields
	$("#more_cards").click(function() {
		  $('#form_fields').hide();
		  $('#controls').show();
	});
	
	// use this to reset a single form
	$("#reset").click(function() {
		$('.error').hide();
		$("form")[0].reset();
	});
	
    $(".error").hide();
	$("#submit").click(function() {
		// validate and process form here      
		$('.error').hide();
		var img_src = $("#promo_img").attr("src");
		var ecard = img_src.substr(13,1);
		
		var friend_name = $("input#friend_name").val();  
		if (friend_name == "") {  
			$("label#friend_name_error").show();  
			$("input#friend_name").focus();  
			return false;  
		}
		
		var friend_email = $("input#friend_email").val();  
		if (friend_email == "") {  
			$("label#friend_email_error").show();  
			$("input#friend_email").focus();  
			return false;  
		}
		
		var sender_name = $("input#sender_name").val();  
		if (sender_name == "") {  
			$("label#sender_name_error").show();  
			$("input#sender_name").focus();  
			return false;  
		}
		
		var sender_email = $("input#sender_email").val();  
		if (sender_email == "") {  
			$("label#sender_email_error").show();  
			$("input#sender_email").focus();  
			return false;  
		}
		
		var captcha = $("input#captcha").val();  
		if (captcha == "") {  
			$("label#captcha_error").show();  
			$("input#captcha").focus();  
			return false;  
		}

		var dataString = 'ecard='+ ecard + '&friend_name='+ friend_name + '&friend_email='+ friend_email + '&sender_name='+ sender_name + '&sender_email='+ sender_email + '&captcha='+ captcha;
		//alert (dataString); return false;
		$.ajax({
		type: "POST",
		url: "includes/send-card.php",
		data: dataString,
		success: function(msg) {
			recommendSuccess(msg);
		},
		error: function(msg, error) {
			recommendError(msg, error);
		}
		});
		return false;
	});
	
	function recommendSuccess(msg) {
		$(msg).find('status').each(function(i) {
			status = $(this).text();
		});
		
		if (status == 'success') {
			$('#form_fields').hide();
		  	$('#send_another').show();
		} else if (status=='captchafail') {
			alert('Incorrect code.  Please try again.');
			refreshimg();
		} else {
			alert('error: ' + status);
		}
	}
	
	function recommendError(msg) {
		$(msg).find('status').each(function(i) {
			status = $(this).text();
		});
		alert('error func: ' + status);
	}

	$(".another").click(function() {
		var answer = $(this).attr("id");
		if (answer == 'Yes') {
		  	$('#send_another').hide();
			$('#form_fields').show();	
			$('.error').hide();
			$("form")[0].reset();		
		} else {
		  	$('#send_another').hide();	
			$('#controls').show();
			$('.error').hide();
			$("form")[0].reset();	
		}
	});
	
	$(function() {
		$('#road_sign').cycle({ 
			fx:    'fade', 
			pause:  1 ,
			speed: 1000,
			timeout: 4000
		});
	});
});