$(document).ready(function() {

	$("#form1").validate({
		errorLabelContainer: $("#form1 div.error")
	});

   
$(".expander").click(function() {
	var numVal = false;
	if ($("#contentFull" + $(this).attr("num")).is(':hidden')) {
		$(this).text("Read Less");
		$("#contentFull" + $(this).attr("num")).show();
		$("#contentExcerpt" + $(this).attr("num")).hide();
		numVal = true;
	} else {
		$(this).text("Read More");
		$("#contentFull" + $(this).attr("num")).hide();
		$("#contentExcerpt" + $(this).attr("num")).show();
	}
	
	$.ajax({
	   type: "GET",
	   url: "changeSessionVariable.php",
	   data: "num=" + $(this).attr("num") + "&val=" + numVal,
	   success: function(msg){
	     //alert( "Data Saved: " + msg );
	   }
	 })

	
});

$(".expander").each(function(i) {
	var id = $(this).attr("num");
	var htmlT = $("#contentExcerpt" + id).html();
	if (htmlT.length < 10) {
		$(this).hide();
		$("#contentFull" + id).show();
		$("#contentExcerpt" + id).hide();
	}
	
	if ($(this).attr("startShown") == "true") {
		$(this).text("Read Less");
		$("#contentFull" + $(this).attr("num")).show();
		$("#contentExcerpt" + $(this).attr("num")).hide();
	}
});

$('#gameBar a').lightBox();
/*
$("#emailQuestion").click(function() {

	var name = $(".chalkBackground textarea").val();
  		if (name == "") {
  		$(".chalkBackground textarea").css("color","red");
  		$(".chalkBackground textarea").animate({ color: "#ffffff" }, 3000);
        $(".chalkBackground textarea").val("Please ask a question!");
        $(".chalkBackground textarea").focus();
        //$(".chalkBackground textarea").select();
        return false;
      }
	$.ajax({
	   type: "GET",
	   url: "sendMail.php",
	   data: "question=" + $("#questionText").val(),
	   success: function(msg){
	     //alert( "Data Saved: " + msg );
	   }
	 })
	
	$(".chalkBackground textarea").val("Thanks for the submission!");
	
});
*/
( function()
{
	var $AskQuestionForm = $( '#askme' ),
		$TextArea = $AskQuestionForm.find( 'textarea[id="questionText"]' ).val( '' ),
		ThankYouText = 'Thank for your anonymous submission!',
		NoEntryText = 'Please ask a question!',
		WaitText = 'Sending question...',
		ErrorText = 'Sorry, there was an error submitting your question.';

	$TextArea.data( 'originalColor', $TextArea.css( 'color' ) );

	$AskQuestionForm.find( 'input[id="emailQuestion"]' ).click( function( e )
	{
		var entry = $.trim( $TextArea.val() );

		if( entry !== WaitText )
		{
			if( entry === '' || entry === ThankYouText || entry === NoEntryText || entry === ErrorText )
			{
				$TextArea.css( 'color', 'red' ).
					animate( {
						color: $TextArea.data( 'originalColor' )
					}, 3000 ).
					val( NoEntryText ).
					focus();
			}
			else
			{
				$TextArea.val( WaitText );

				$.ajax( {
					type: 'GET',
					url: 'sendMail.php',
					data: {
						question: entry
					},
					success: function(msg)
					{
						$TextArea.val( ThankYouText );
					},
					error: function()
					{
						$TextArea.val( ErrorText );
					}
				} );
			}
		}

		e.stopPropagation();
		return false;
	} );	
}() );

$("#emailGuess").click(function() {
	$.ajax({
	   type: "GET",
	   url: "sendGuess.php",
	   data: "name=" + $("#subName").val() + "&email=" + $("#subEmail").val() + "&guess=" + $("#subGuess").val(),
	   success: function(msg){
	     //alert( "Data Saved: " + msg );
	   }
	 })
	
	$("#gameBar").html("<p>Thanks for the submission!</p><p>Winner will be announced in an upcoming blog.</p><p>Pomp and Circumstance to follow...</p>");
	
});

$("#subName").click(function() {
	if ($(this).val() == "Name") {
		$(this).val("");
	}
});

$("#subEmail").click(function() {
	if ($(this).val() == "Location") {
		$(this).val("");
	}
});

$("#subGuess").click(function() {
	if ($(this).val() == "What's your guess?") {
		$(this).val("");
	}
});

});

function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];

     var parsed_date = Date.parse(time_value);

     var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
     var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

     if(delta < 60) {
         return 'less than a minute ago ';
     } else if(delta < 120) {
         return 'about a minute ago ';
     } else if(delta < (45*60)) {
         return (parseInt(delta / 60)).toString() + ' minutes ago ';
     } else if(delta < (90*60)) {
         return 'about an hour ago ';
     } else if(delta < (24*60*60)) {
         return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago ';
     } else if(delta < (48*60*60)) {
         return '1 day ago ';
     } else {
         return (parseInt(delta / 86400)).toString() + ' days ago ';
     }
  }
  
	function twitCallback(obj) {

		var twitS = " ";
		offset = 0;
		
		for (var i = 0; i < obj.length; i++) {
			var tempS = obj[i].text;
			if (tempS.substring(0,1) != "@" && offset < 5) {
				var b = obj[i].text;
				var temp = new Array();
				temp = b.split(' ');
				for (j = 0; j < temp.length; j++) {
					var c = temp[j];
					if (c.substr(0, 4) == "http") {
						temp[j] = "<a href='" + c + "'>" + c + "</a>";
					}
				}
				d = temp.join(" ");
				
				twitS += "<p class='twitterPost'>" + d + "</p>";
				twitS += "<p class='twitterTime'>" + relative_time(obj[i].created_at) + "</p>";
				
				offset++;
			}
		}
		document.getElementById('twitterHolder').innerHTML = twitS;
	}
	
	function openpopup(popurl){
	var winpops=window.open(popurl,"","width=970,height=650")
	}
	
	function openpopup2(popurl){
	var winpops=window.open(popurl,"","width=970,height=800")
	}
	
	var $body = $(this.ie6 ? document.body : document);
	$('#lightbox-overlay').css({
		width:		$body.width(),
		height:		$body.height()
	});
	delete $body;