$.fn.input = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			$(this).val("");
			$(this).addClass("focus");
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).removeClass("focus");
		}
	});
};

jQuery.fn.equalCols = function(){
	//Array Sorter
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	//Push each height into an array
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		//Set each column to the max height
		$(this).css({'height': maxHeight});
	});
};

function createCookie(cookie_name,value,expiredays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=cookie_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function deleteCookie(cookie_name){
  var cookie_date = new Date();  // current date & time
  cookie_date.setDate(cookie_date.getDate()-1);
  document.cookie = cookie_name + "=; expires=" + cookie_date.toGMTString() + ";path=/";
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function checkCookie(cookie_name){
  cookie = getCookie(cookie_name);
  if (cookie!=null && cookie!=""){
    return true;
  } else {
	return false;
  }
}

$(document).ready(function(){
	$('#home #content, #home #sidebar').equalCols();
	
	var navWidth = $("#header ul").outerWidth();
	var width = 0;
	$("#header ul li").each(function() {
		width += $(this).outerWidth();
	});
	$("#header ul li.spacer").width(navWidth-width);
	
	$(":input").input();
		
	$("#send-email").hide();

	$("#email").click(function() {
		$("#loading").hide();
		$("button span").text("Send to Friend");
		$("#send-email").slideToggle(function(){
			$('#content, #sidebar').equalCols();
		});
	});
	
	$("#leave-comment").click(function() {
		$("#comments").slideToggle();
	});
	
	$('#submit-comment').click(function(){
		var comment = $("#comment").val();
		var commentName = $("#comment-name").val();
		var commentEmail = $("#comment-email").val();
		var commentUrl = $("#comment-url").val();
		
		if(comment == "Comment") { $("#comment").val(""); }
		if(commentName == "Your Name") { $("#comment-name").val(""); }
		if(commentEmail == "Email") { $("#comment-email").val(""); }
		if(commentUrl == "Website (Optional)") { $("#comment-url").val(""); }
		
		this.form.submit();
	});
	
	$("#send-to-friend").click(function(){
		var message = $("#share-message").val();
		var shareName = $("#share-name").val();
		var shareEmail = $("#share-email").val();
		var shareFriendName = $("#share-friend-name").val();
		var shareFriendEmail = $("#share-friend-email").val();
		
		if(message == "Add a note (Optional)") { $("#share-message").val("").addClass("focus"); }
		if(shareName == "Your Name") { $("#share-name").val("").addClass("focus"); }
		if(shareEmail == "Your Email Address") { $("#share-email").val("").addClass("focus"); }
		if(shareFriendName == "Friend's Name") { $("#share-friend-name").val("").addClass("focus"); }
		if(shareFriendEmail == "Friend's Email Address") { $("#share-friend-email").val("").addClass("focus"); }
		
		$("#send-form").ajaxSubmit({
			target:"#hiddenDIV", 
			beforeSubmit:function() {
				$("#send-to-friend #loading").fadeIn("fast");
				$("#send-to-friend span").text("Sending...");
			},
			success:function(rtn) {
				if(rtn=="success") {
					$("#send-form :input").removeClass("focus");
					$("#send-to-friend #loading").fadeOut("fast");
					$("#send-to-friend span").text("Send to Friend");
					$("#send-email").slideUp();
					$("#send-form").resetForm();
					$("#msg").empty();
				} else {
					$("#msg").html($("#hiddenDIV #content ul").html());
					$("#msg").show();
					$("#send-to-friend #loading").fadeOut("fast");
					$("#send-to-friend span").text("Send to Friend");
					$("#hiddenDIV").empty();

					message = $("#share-message").val();
					shareName = $("#share-name").val();
					shareEmail = $("#share-email").val();
					shareFriendName = $("#share-friend-name").val();
					shareFriendEmail = $("#share-friend-email").val();

					if(message == "") { $("#share-message").val("Add a note (Optional)").removeClass("focus"); }
					if(shareName == "") { $("#share-name").val("Your Name").removeClass("focus"); }
					if(shareEmail == "") { $("#share-email").val("Your Email Address").removeClass("focus"); }
					if(shareFriendName == "") { $("#share-friend-name").val("Friend's Name").removeClass("focus"); }
					if(shareFriendEmail == "") { $("#share-friend-email").val("Friend's Email Address").removeClass("focus"); }
		     	}
			}
		});
		return false; 
	});
	var launch = new Date(2009,9,21,15,00,00);
	$("#countdown").countdown({until: launch, timezone: -4, format: 'DHM',layout: '<h3>Identity <span>Launching in:</span></h3><div class="days">{dnn}<span> {dl}</span></div><div class="hours">{hnn}<span> {hl}</span></div><div class="minutes">{mnn}<span> {ml}</span></div>'});
	
	$('#go-away, #mask').click(function(){
		createCookie("mask","yes","365");
		$('#popup').hide();
		$('#mask').hide();
	});

	$('#no-thanks').click(function(){
		createCookie("trial","yes","365");
		$('#popup').hide();
		$('#mask').hide();
	});

	$('#trial').click(function(){
		$('#popup').hide();
		$('#mask').hide();
	});

	$('#take-me').click(function(){
		createCookie("mask","yes","365");
		window.location('http://grownups.heyjosh.com/identity/');
	});
});
