$(document).ready(function() {
	
	var zipEntered = false;
	var curZip = $("input#zip").val();
	if (curZip != '') {
		var zipEntered = true;
		$('#step1').css('background-image','url(/images/number1.gif)');
		$('#step2').css('background-image','url(/images/number2-on.gif)');
	}
	
	/* Sign in box */
	$("a.inline-signup").fancybox({
		'autoDimensions': false,
		'width': 400,
		'height': 220,
		'transitionIn': 'elastic',
		'transitionOut': 'fade'
	});
	
	/* Info boxes on member signup page */
	$("a.memberWhy").fancybox({
		'autoDimensions': false,
		'width': 400,
		'height': 120,
		'transitionIn': 'elastic',
		'transitionOut': 'fade'
	});
	
	/* Image upload in advertiser profile */
	$("a#upload-image").fancybox({
		'autoDimensions': false,
		'width': 340,
		'height': 180,
		'transitionIn': 'elastic',
		'transitionOut': 'fade'
	});

	/* Advertiser profile edit form */
/*	$("a#editAdvertiserProfile").fancybox({
		'autoDimensions': false,
		'width': 550,
		'height': 500,
		'transitionIn': 'elastic',
		'transitionOut': 'fade'
	}); */
	
	/* Zip code entry forms */
	
	$("#zipSearch").validate({
		'errorElement': "div",
		rules: {
			zip: {
				minlength: 5
			}
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				target: '#currentZipCode',
				success: function() {
					if (success == 0) {
						$('#zipSearch').append('<div class="error">Zip code not found in our database. Please try again.</div>');
					} else {
						if ($('#zipSearch').hasClass("secondary")) {
							$('.blueButton').attr("disabled","disabled");
							location.reload();
							return false;
						} else {
							zipEntered = true;
							scroll(0,0);
							$('#step1').css('background-image','url(/images/number1.gif)');
							$('#step2').css('background-image','url(/images/number2-on.gif)');
							$('#step2').effect('bounce', { times: 2 }, 300);
							$('#zipIntroText').html('Your Zip Code:');
							$('#zipSearch').hide();
							$('#changeZip').show();
							$('#currentZipCode').show();
						}
					}
				}
			});
		}
	});
	
	/* Member and advertiser signup forms */
	$(".signupForm").validate({
		rules: {
			password_first: "required",
			password_confirm: {
				equalTo: "#password_first"
			}
		}
	});
	
	/* Share with Friend form */
	$('#sendToFriend').validate();
	
	$('#changeZip').click(function() {
		$('#currentZipCode').hide();
		$('#changeZip').hide();
		$('#step1').css('background-image','url(/images/number1-on.gif)');
		$('#step2').css('background-image','url(/images/number2.gif)');
		$('#step2').html('Click a Category');
		$('#step3').css('background-image','url(/images/number3.gif)');
		$('#zipSearch').show();
	});
	
	$('a.categoryChoice').click(function() {
		if (zipEntered != true) {
			scroll(0,0);
			$('#step1').effect('bounce', { times: 2 }, 300);
		} else {
			var curCategory = $(this).html();
			scroll(0,0);
			$('#step2').html('Category: <b>' + curCategory + '</b>');
			$('#step2').css('background-image','url(/images/number2.gif)');
			$('#step3').css('background-image','url(/images/number3-on.gif)');
			$('#step3').effect('bounce', { times: 2 }, 300);
			$('#chosenCategory').val(curCategory);
			$('#processCategory').submit();
		}
	});
	
	/* Coupon mailing */
	$("#generateCoupon :input").click(function() {
			var promo = $(this).attr('id');
			$('#generateCoupon').attr({ action: '/mail-coupon.php?promo=' + promo });
			$(this).addClass('clicked');
			$(this).attr("disabled","disabled");
			$(this).val('Generating...');
			$('#couponStatus').overlay({ top: 260 });
			var api = $("#couponStatus").data("overlay");
			api.load();
			
			$('#generateCoupon').ajaxSubmit({
				target: '#successMessage',
				success: function() {
					if (success == 1) {
						// show success message
						$('input.clicked').val('Coupon Sent!');
						$('#couponStatus').stop().fadeOut(400);
						api.close();
						$('#couponSuccess').overlay({ top: 260 });
						api = $("#couponSuccess").data("overlay");
						api.load();
						$('#couponSuccess').delay(2500).fadeOut(400);
						api.close();
					} else {
						// make the user log in
						$('#couponStatus').fadeOut(400);
						window.location = "/member-not-signed-in.php?promo=" + promo;
					}
				}
			});
			
			return false;
	});
	
	/* Tooltips on advertiser search page */
	$("a[title]").tooltip({
		effect: "fade",
		position: "top right",
		offset:  [0,-30]
	});
	
	/* Coupon creation - scroll sample coupon with page */
	// NOTE: This is temporarily disabled due to display problems on small/wide monitors
	/* var $scrollingDiv = $("#couponSample");

	$(window).scroll(function(){
	       $scrollingDiv
	              .stop()
	              .animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow" );
	}); */
	
	/* Premium coupons - make them gold on creation page */
	// $('#couponSample').prepend('<span class="hover"></span>');
	
	$("#premium_coupon").change(function(){
		if(this.checked) {
			$("#couponSample").css("background","url('images/bg-gold.png') top left repeat-x #e4e3be");
			/* $('.hover').stop().animate({
	              'opacity': 1
	              }, 200); */
		} else {
			$("#couponSample").css("background","#fff");
			/* $('.hover').stop().animate({
	              'opacity': 0
	              }, 200); */
		}
	});
});

// ADVERTISERS: Deleting of coupons and images
function confirmDelete(type,id,num) {
	if (type == 'coupon') {
		if (confirm("Are you sure you want to delete this coupon?")) {
			$.post("/coupon-delete.php", { coupon_id: id }, function(data){
				location.reload();
			});
		}
	} else if (type == 'image') {
		if (confirm("Are you sure you want to delete this image?")) {
			$.post("/image-delete.php", { image_path: id, image_num: num }, function(data){
				location.reload();
			});
		}
	}
}
