$(function(){
	positionFooter();

	//Start the timer
	if(typeof endDate !== 'undefined') {
		$('#timeleft .timer').countdown({
			until: endDate,
			format: 'HMS',
			layout: '{hn}hrs {mn}mins {sn}secs',
			alwaysExpire: true,
			onExpiry: function(){
				$('#timeleft .timer').countdown('destroy').text('This Bundle is no longer available').addClass('expired');
			}
		});//.after('<br />' + endDate.toString());
	}
	
	$("#signup").validate({
		rules: {
			pwd: "required",
			cpwd: {
				equalTo: "#pwd"
			}
		},
		messages : {
			cpwd : {
				equalTo : "This field must match the first password entered."
			}
		}
	});
	$("#account").validate({
		rules: {
			pwd: "required",
			cpwd: {
				equalTo: "#npwd"
			}
		},
		messages : {
			cpwd : {
				equalTo : "This field must match the first password entered."
			}
		}
	});
	$("#signin, #forgot_pass, #contact").validate();
	
	//Checkout page, update quantity
	$('#purchase_bundle #btn_quantity_update').click(update_checkout_qty);
	
	$("#purchase_bundle #Month, #purchase_bundle #Year, .certificate_name").addClass('required');
	$("#purchase_bundle").validate({
		submitHandler: function(form) {
			$('image, submit', form).attr('disabled', true);
			form.submit();
 		}
	});
		
	//Checkout page, disable enter button on form fields
	$("#purchase_bundle").keypress(function(e) {
		if (e.which == 13) {
			update_checkout_qty();
			return false;
		}
	});
	$("#purchase_bundle #quantity").keypress(function(e){
		$('.payment-div').slideUp('fast', function(){
			$('.payment-div').empty();
		});
	});
	
	$(".dailymail").focusClear();
		
	//Fix for div height on main page:
	var minHeight = 500;
	$('#slider img').each(function(i, element) {
		if(minHeight > $(element).height()) minHeight = $(element).height();
	});
	
	$('#slider').nivoSlider({pauseTime:5000}).height(minHeight);
});

$(window).load(function(){
	//look for the cookie
	var dismissedEmailBox = $.cookie('dismissed_email');
	if(dismissedEmailBox != 'true' && window.location.protocol != 'https:' && !$('body').hasClass('logged-in'))
	{
		$.fancybox(
			'body',
			{
	        	'type'				: 'iframe',
				'href'				: '/subscribe/landing.php',
				'autoDimensions'	: false,
				'width'         	: 595,
				'height'        	: 440,
				'scrolling'			: 'no',
				'showCloseButton'	: true,
				'onClosed'			: closedEmailBox
			}
		);
	}	
});

function update_checkout_qty() {
	//Get the value of the total box.
	var qty = $("#purchase_bundle #quantity").val();
	
	//Redirect the browser to the correct place:
	window.location.search = "?quantity=" + qty;	
}

function positionFooter(){
	if($(document.body).height() < $(window).height()) {
		$("#footer").css({'marginTop':($(window).scrollTop()+$(window).height()-$("#footer").height()-$("#wrapper").height())+"px"})
	}	
}

function fb_loggedin() {
	if(typeof redirect == "undefined")
		window.location.href = 'http://www.inbundles.com/';
	else	
		window.location.href = 'http://www.inbundles.com/' + redirect + '/';
}

function fb_logout() {
 FB.getLoginStatus(function(response) {
  if (response.session) {
    FB.logout(function(response) {
      window.location.href = 'http://www.inbundles.com/user/signout/';
    });
  } else {
  	window.location.href = 'http://www.inbundles.com/user/signout/';
  }
 });
 return false;
}

//Search box clear
$.fn.focusClear = function(){
    return this.focus(function(){
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function(){
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
};

function closedEmailBox()
{
	//set a cookie so that it doesn't show up again.
	$.cookie('dismissed_email', 'true', {
		expires: 180,
		path: '/', 
		domain: 'inbundles.com'
	});	
}
