$(document).ready(function(){

	if($("form.validate").length) {

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

			$(".alert", this).removeClass("alert");

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

			$(this).find(".required").not("span").each(function() {
				var value = $(this).val();
				if(value == "" || ($(this).prop("title") && value == $(this).prop("title")) || ($(this).is(".email") && !email_pattern.test(value))) {
					valid = false;
					var label = $(this).prev("label").text() || $(this).next("label").text() || $(this).prop("title");
					msg += '\n-> '+label.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;
			}
		});

		$("form.validate input, form.validate textarea").bind("focus", function(){
			$(this).addClass("focus");
			if($(this).val() == $(this).attr("title")) {
				$(this).val("");
			}
		});

		$("form.validate input, form.validate textarea").bind("blur", function(){
			$(this).removeClass("focus");
			if($(this).val() == "") {
				$(this).val($(this).attr("title"));
			}
		});

	}

	if($("a.loading[href]").length) {

		$("a.loading[href]").each(function(){

			var self = $(this);

			$.get($(this).attr("href"), function(data) {
				self.after(data).remove();
			});

		});

	}

	/* Case Studies */

	if($(".book").length) {

		var myFile = document.location.toString();

		if(myFile.match('#')) {

			var myAnchor = myFile.split('#')[1];

			$(".book").each(function(){
				activate_book_page($(this), $("ul.toc > li[rel='"+myAnchor+"']", this));
			});

		} else {

			$(".book").each(function(){
				activate_book_page($(this), $("ul.toc > li.on", this));
			});
		}

		$(".book ul.toc > li").click(function(){
			var book = $(this).closest(".book");
			activate_book_page(book, $(this));
			return false;
		});

		$(".book ul.toc > li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});

		$(".book a[href='#next']").click(function(){
			var book = $(this).closest(".book");
			var next = ($("ul.toc > li.on", book).next().length) ? $("ul.toc > li.on", book).next() : $("ul.toc > li:first", book);
			activate_book_page(book, next);
			return false;
		});

		$(".book a[href='#prev']").click(function(){
			var book = $(this).closest(".book");
			var prev = ($("ul.toc > li.on", book).prev().length) ? $("ul.toc > li.on", book).prev() : $("ul.toc > li:last", book);
			activate_book_page(book, prev);
			return false;
		});

	}

	if($(".widget-title").length) {
		$(".widget-title").wrapInner("<span></span>");
	}

	if($(".fade").length) {
		$(".fade").innerfade({
			'timeout': 7000, // this stands for the pause length between the transitions, 7 seconds
			'speed': 2000 // this one defines the transition length, 2 seconds
		});
	}

});

function activate_book_page(book, i) {

	$("ul.toc:first li.on", book).removeClass("on");
	i.addClass("on");
	var rel = $('a:first', i).attr("href").replace('#', '');
	$(".colA li.on", book).removeClass('on');
	$(".colA li#"+rel, book).addClass('on');

}
