$(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").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).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"));
			}
		});

		$("form.validate .submit").click(function(e){
			e.preventDefault();
			$(this).closest("form").submit();
		});

	}

	if($(".boxA").length) {
		$(".boxA").wrapInner('<div class="boxA-m2b clearfix" />').wrapInner('<div class="boxA-m2t" />').wrapInner('<div class="boxA-shadow" />').prepend('<div class="boxA-before"></div>').append('<div class="boxA-after"></div>');
	}

	if($("hr").length) {
		$("hr").replaceWith('<div class="hr"></div>');
	}

	/* Case Studies */

	if($("#team-listing").length) {

		$("#team-listing > li").hide();

		$("#team-listing-toc li a").bind('click',function(){
			$("#team-listing > li").hide();
			$("#team-listing-toc li a.on").removeClass("on");
			$(this).show_fragment();
		});

		var myFile = document.location.toString();

		if(myFile.match('#')) {
			var myAnchor = myFile.split('#')[1];
			$("#team-listing-toc li a[href='#" + myAnchor + "']").click();
		} else {
			$("#team-listing-toc li a.on").click();
		}
	}

	if($(".promo.play").length) {

		var p = $(".promo.play");
		var offset = p.offset();
		var offset_left = 30 + $('.promo.play').innerWidth();

		var l2 = $('.promo.play li.i2').css('left');
		var l3 = $('.promo.play li.i3').css('left');
		
		$('.promo.play li').css('left', '-' + offset_left + "px").show();

		$('.promo.play li.i1').animate({
			left: '0'
		}, 3500, 'easeOutElasticCustom', function() {});

		setTimeout(function() {

			var t2 = $('.promo.play li.i2').css('top');

			$('.promo.play li.i2').css('top', '100px');
			
			$('.promo.play li.i2').animate({
				left: l2,
				top: t2
			}, 3500, 'easeOutElasticCustom', function() {});

		}, 1700 );

		setTimeout(function() {

			var t3 = $('.promo.play li.i3').css('top');

			$('.promo.play li.i3').css('top', '-100px');
			
			$('.promo.play li.i3').animate({
				left: l3,
				top: t3
			}, 3500, 'easeOutElasticCustom', function() {});
			

		}, 3400 );

	}

	if($("a.loading[href]").length) {

		$("a.loading[href]").each(function(){

			var self = $(this);

			$.get($(this).attr("href"), function(data) {
				self.after(data).remove();
			});

		});

	}

	if($("#content > .colgroup > .colB > .boxA").length) {

		$("#content > .colgroup > .colB > .boxA").each(function(){
		
			var colgr = $(this).closest('.colgroup');
			var colAh = $('> .colA', colgr).innerHeight();
			var colBh = $('> .colB', colgr).innerHeight();

			if(colAh > colBh) {
				$(".boxA-m2b", this).css('height', (colAh - 40) + "px");
			}
		});
	}

});

$.fn.show_fragment = function() {
	var fragment = $(this).attr("href");
	$(this).addClass("on");
	$(fragment).addClass("on").show();
}

$.fn.hide_fragment = function() {
	var fragment = $(this).attr("href");
	$(this).removeClass("on");
	$(fragment).removeClass("on").hide();
}
