// JavaScript Document

$(function() {

 	$('#tiles').infinitescroll({
		debug           : false,
		nextSelector    : "div.navigation a:first",
		loadingImg      : "/images/loaders/AnimatedLoad.gif",
		text            : "Loading the next posts...",
		donetext        : "<em>Nice! You've hit the end.</em>",
		navSelector     : "div.navigation",
		contentSelector : "#tiles",
		itemSelector    : "#tiles > div.tile"
	},function(){ 
		pageTracker._trackPageview( "/page/"+ this.id.match(/\d/) +"/" );
	});
	
	bindActions();


});

function load_wall_messages(){
	$(".encouragement-wall-tile").each(
		function (){
			var message_div_id = $(this).find("div:first").attr("id");
    		var id = message_div_id.replace("wallDisplay", "");

			if((content = $("#wallDisplay" + id).html()) == 'Encouragement Wall'){
				$("#wallDisplay" + id).html($('#wall_message_holder').html());
				$("#wallDisplay" + id +" li").hide();
				
				var total = $("#wallDisplay" + id +" li").size();
				var count = Math.round(Math.random()*total);
				$("#wallDisplay" + id +" li:nth-child("+count+")").show();
				
				var refresh_id = "refresh" + id;
				refresh_id = setInterval(function(){
					$("#wallDisplay" + id +" li:nth-child(" + count + ")").hide();
					count++;
					if (count == total) {
						count = 1;
					}
					$("#wallDisplay" + id +" li:nth-child(" + count + ")").show();
				}, 7000);
			}
		}
	);
}

function bindActions() {

	$('input[type=radio].star').rating();

	$('.vote').ajaxForm({ 
		target: '#voteReturn',
		dataType: 'json',
 		success: function(data) { 
			if (data.note == 'Success') {
				$('#voteButton' + data.id).css({ 'background-position' : '-1006px -33px'});
				$('#voteButton' + data.id).attr("disabled", "true");
				$('#voteButton' + data.id).attr("value", "Thanks for voting");
				$('#voteScore' + data.id).html(data.score).fadeIn('slow');
			} else if (data.note == 'Closed') {
				$('#voteButton' + data.id).attr("value", "Sorry, voting is closed!").fadeIn('slow');
			} else {
				$('#voteButton' + data.id).attr("value", "Sorry, please try again!").fadeIn('slow');
			}
		} 
	}); 

	$('form.wall').ajaxForm({ 
		target: '#wallReturn',
		dataType: 'json',
 		success: function(data) { 
			if (data.note == 'Success') {
				$('#wallReturn' + data.id).html('<ul id="success"><li>Thank you!</li></ul>').fadeIn('slow');
				setInterval(function () { $('#wallReturn' + data.id).html(''); }, '10000');
			} else {
				$('#wallReturn' + data.id).html('<ul id="error">' + data.note + '</ul>').fadeIn('slow');
				setInterval(function () { $('#wallReturn' + data.id).html(''); }, '10000');
			}
		} 
	});
    
	$('form.bmi').ajaxForm({ 
		target: '#bmiReturn',
		dataType: 'json',
 		success: function(data) { 
			if (data.note == 'Success') {
				$('#bmiText' + data.id).css({ 'height' : '177px'}).html(data.text);
				$('#bmiScore' + data.id).empty().html(data.score);
				$('#bmiResult' + data.id).show();
				$('#bmiStart' + data.id).hide();
			} else {
				$('#bmiErrors' + data.id).html('<ul id="error">' + data.note + '</ul>').fadeIn('slow');
			}
		} 
	});

	$("div.link").click(
		function(){
			wholeid = $(this).parent().attr("id");
			id = wholeid.replace("bmiResult","");
			$('#bmiErrors' + id).html('Enter your height and weight (to the nearest whole number), then click the "Calculate" button.');
			$('#bmiResult' + id).hide();
			$('#bmiStart' + id).show();
		}
	);

	$( "form.wall .textfield" ).each(
		function(){
			if($(this).val() == "") {
				$(this).val("Your Email");
			}
			$(this).bind ("click", function(){
				if($(this).val() == "Your Email") {
					$(this).val("");
				}
			});
			$(this).bind ("blur", function(){
				if($(this).val() == "") {
					$(this).val("Your Email");
				}
			});
		}
	);

	$( "form.wall .textarea" ).each(
		function(){
			if($(this).val() == "") {
				$(this).val("Message");
			}
			$(this).bind ("click", function(){
				if($(this).val() == "Message") {
					$(this).val("");
				}
			});
			$(this).bind ("blur", function(){
				if($(this).val() == "") {
					$(this).val("Message");
				}
			});
		}
	);
    
	myBigTarget();
	load_wall_messages();
	position_tiles();
}