$(document).ready(function(){

	$("#blog_feed").load("/blog_feed.php");

	if($("body#contact").length) {
		var colgroup_height = $("#contact #colgroup1").innerHeight();
		var first_col_height = $("#contact #colgroup1 > .col:first").innerHeight();
		var diff = colgroup_height - first_col_height;
		if(diff > 0) {
			var elem_height = $("#contact #colgroup1 > .col:first > *:last").height();
			var new_height = elem_height + diff;
			$("#contact #colgroup1 > .col:first > *:last").height(new_height+"px");
		}
	}

	if($(".loading").length) {
		$(".loading").each(function(){
			$(this).load($("a:first",this).attr("href"), function(){
				$(this).removeClass("loading");
			});
		});
	}

	if($(".fade").length) {
		$(".fade").innerfade({
			speed: "1000",
			timeout: "6000"
		});
	}

	$("form.submit input[type=text]:first").focus();

	$("form.submit").submit(function() {

		$("form.submit .alert").removeClass("alert");

		var msg = '';
		var valid = true;
		var email_pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/i;
		var form_id = $(this).attr("id");

		$(this).find(".required").each(function() {

			var value = $(this).val();
			var title = $(this).attr("title");

			if(value == "" || (value == title) || ($(this).is(".email") && !email_pattern.test(value))) {
				valid = false;
				msg += '\n-> '+$(this).prev("label").text().replace(/[\*|:]/gi, "")+' is required';
				$(this).addClass("alert");
			}

		});

		if(!valid) {

			if(msg != '') {
				alert('Please fill in these fields. Thanks.'+msg);
				$(".alert:first", this).focus();
				return false;
			}

			return false;
		}
	});

	/* Home Page */

	if($("#home").length) {

		var init_index = $("#home #nav li a.on").closest("li").index();

		$("#home #nav li").hover(function(){
			var index = $(this).index();
			$("#home #banner .box.quote ul li.on").removeClass("on");
			$("#home #banner .box.quote ul li:eq("+index+")").addClass("on");
		},function(){
			$("#home #banner .box.quote ul li.on").removeClass("on");
			$("#home #banner .box.quote ul li:eq("+init_index+")").addClass("on");
		});
	}

	/* Case Studies */

	if($("#case-studies").length) {

		$("#case-studies ul.main > li").hide();

		var myFile = document.location.toString();

		if(myFile.match('#')) {
		  var myAnchor = '#'+myFile.split('#')[1];
		  $("#case-studies .table-of-contents a.on").removeClass("on");
		  $("a[href="+myAnchor+"]").addClass("on");
		  $(myAnchor).show();
		} else {
		  $("#case-studies .table-of-contents li > a.on").show_fragment();
		}

		$("#case-studies .table-of-contents li a").click(function(){
			var parent = $(this).parents(".table-of-contents");
			$("li > a.on", parent).hide_fragment();
			$(this).show_fragment();
		});

		$("#case-studies .case-study a.next").click(function(){
			var selected = $("#case-studies .table-of-contents li > a.on");
			$(selected).hide_fragment();
			selected.parent("li").next("li").find("a").show_fragment();
			return false;
		});

	}    


	inverse = true;
            
	 $("#btnSortColumn").click(function(){

		 if(inverse === true) {
			 $("#btnSortColumn").text("date ascending");
		 } else  {
			 $("#btnSortColumn").text("date descending");
		 }
		
		var header = $("table thead th:first"),
			index = header.index();
			
		header
			.closest('table')
			.find('td')
			.filter(function(){
				return $(this).index() === index;
			})
			.sort(function(a, b){
				
				a = $(a).text();
				b = $(b).text();
				
				return (
					isNaN(a) || isNaN(b) ?
						a > b : +a > +b
					) ?
						inverse ? -1 : 1 :
						inverse ? 1 : -1;
					
			}, function(){
				return this.parentNode;
			});
		
		inverse = !inverse;
		
	});

});

$.fn.show_fragment = function() {
	var fragment = $(this).attr("href");
	$(this).addClass("on");
	$(fragment).show();
}

$.fn.hide_fragment = function() {
	var fragment = $(this).attr("href");
	$(this).removeClass("on");
	$(fragment).hide();
}
