/* 
	Written By: Peter Evans
				Chillifish.net
	Last Modified: 09/03/2009
*/


(function($) {
    var root = 'http://new.prettygreen.com/community/ajax/';
    
	jQuery.stripPx = function(str) {
		return str.substring(0, str.length - 2);	
	};
	jQuery.showHide = function(id) {
		($(id).is(":hidden") ? $(id).fadeIn("slow") : $(id).fadeOut("slow"));
	};
	jQuery.showSlide = function(id) {
		($(id).is(":hidden") ? $(id).slideDown("slow") : $(id).slideUp("slow"));
	};
	jQuery.validEmail = function(email) {
		if(!(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(email))){
			return false;
		}
		return true;
	};
	jQuery.dateSuffix = function(date) {
		if (date==1) suffix=("st");
		else if (date==2) suffix=("nd");
		else if (date==3) suffix=("rd");
		else if (date==21) suffix=("st");
		else if (date==22) suffix=("nd");
		else if (date==23) suffix=("rd");
		else if (date==31) suffix=("st");
		else suffix=("th");
		
		return suffix;
	};
	jQuery.monthString = function(month) {
		if (month==1) monthName=("January");
		else if (month==2) monthName=("February");
		else if (month==3) monthName=("March");
		else if (month==4) monthName=("April");
		else if (month==5) monthName=("May");
		else if (month==6) monthName=("June");
		else if (month==7) monthName=("July");
		else if (month==8) monthName=("August");
		else if (month==9) monthName=("September");
		else if (month==10) monthName=("October");
		else if (month==11) monthName=("November");
		else monthName=("December");
		
		return monthName;
	};
	jQuery.AMPM = function(hour) {
		var ap = "AM";
		if (hour > 11) { ap = "PM"; }
		if (hour > 12) { hour = hour - 12; }
		if (hour == 0) { hour = 12; }
		
		return ap;
	};
	jQuery.appendComment = function(name, message) {
		var date = new Date();
		
		var hour = date.getHours();
		var min = date.getMinutes();
		var ampm = $.AMPM(hour);
		
		var time = hour + ':' + min + ' ' + ampm;
		
		var day = date.getDate();
		var daySuffix = $.dateSuffix(day);
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var smonth = $.monthString(month);
		
		var date = day + daySuffix + ' ' + smonth + '<br/>' + year;

		var html = '<div class="comment"><div class="comment-stats"><p align="right"><span class="bold">' + name + '</span><br/>' + time + '<br/>' + date + '<br/></p></div><div class="comment-msg"><p>' + message + '<br/><br/><br/><span class="comment-pending">This comment is pending - it will appear in this post when it has been approved.</span></p></div></div>';
		
		$("#comments").append(html);
	};
	jQuery.submitComment = function(post_id) {
		var message = $("#comment_msg").val();
		var default_msg = $("#comment_msg").attr('title');
		var visinfo = $("#visinfo").val();
		
		if(message.length > 0) {
			if(message != default_msg) {
				$.post(root + 'ajaxcomment.php', {'post_id': post_id, 'message': message, 'visinfo': visinfo}, function(i) {
					$.appendComment(i, message);
					$("#comment_msg").val('type your message to Liam here...');
				});
			}
			else {
				$.throwSubmitError('Enter your message here...', true);
			}
		}
		else {
			$.throwSubmitError('Enter your message here...', true);
		}
	};
	jQuery.throwSubmitError = function(msg, do_flash) {
		$("#comment_msg").val(msg);
		if(do_flash) {
			$("#comment_msg").css("border", "1px solid #FF0000");
		}
		setTimeout('$("#comment_msg").val("type your message to Liam here...")', 4000);
		setTimeout('$("#comment_msg").css("border", "1px solid #CCCCCC");', 4000);
	};	
	jQuery.submitReset = function(field) {
		$("#name").css('border', '1px solid #bababa');
		$("#email").css('border', '1px solid #bababa');
		$("#phone").css('border', '1px solid #bababa');
	};
})(jQuery);

$(document).ready(function() {
	function filterPath(string) {
		return string
		.replace(/^\//,'')
		.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
		.replace(/\/$/,'');
	 }
  	var locationPath = filterPath(location.pathname);
  	
  	$('a[href*=#]').each(function() {
 		var thisPath = filterPath(this.pathname) || locationPath;
 		if (  locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
   			var $target = $(this.hash), target = this.hash;

   			if (target) {
  				var targetOffset = $target.offset().top;
  				$(this).click(function(event) {
    				event.preventDefault();
    				$('html, body').animate({scrollTop: targetOffset}, 400, function() {
   						location.hash = target;
    				});
  				});
   			}
 		}
  	});
	
	$("input").bind("focus", function() {
		var val = $(this).attr('alt');
		 if($(this).val() == val) {
		  	$(this).val('');
		 }
	});
	$("input").bind("blur", function() {
		var val = $(this).attr('alt');
		 if($(this).val() == '') {
		 	$(this).val(val);
		 }
	});
	
	$("textarea").bind("focus", function() {
		var val = $(this).attr('title');
		 if($(this).val() == val) {
		  	$(this).val('');
		 }
	});
	$("textarea").bind("blur", function() {
		var val = $(this).attr('title');
		 if($(this).val() == '') {
		 	$(this).val(val);
		 }
	});
});