$(document).ready(function(){

	// blog feed
	$("#featured-blog-feeds").load("/blog_feed.php");
	
	
	/* Simple Form Validation */

	$("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;
		}
	});

	/* Main Navigation Animation */

	$("#navbar #nav a").hover(function(){
		$(this).animate({top: ".75em"}, 100);
	},function(){
		$(this).animate({top: ".5em"}, 100);
	});

	/* Jquery Carousels */

	if($(".jcarousel-skin-1").length) {
		$('.jcarousel-skin-1').jcarousel({
			animation: 500,
			easing: 'easeInOutExpo',
			scroll: 1,
			wrap: 'circular',
			auto: 5
		});
	}

	if($(".jcarousel-skin-2").length) {
		$('.jcarousel-skin-2').jcarousel({
			animation: 500,
			easing: 'easeInOutExpo',
			scroll: 3
		});
	}

	if($(".jcarousel-skin-3").length) {
		$('.jcarousel-skin-3').jcarousel({
			animation: 500,
			easing: 'easeInOutExpo',
			scroll: 13
		});
	}

	$('.jcarousel-clip').wrap('<div class="jcarousel-clip-wrapper" />');

	/* Loading Areas */

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

	/* Image Gallery */

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

		$(".gallery .thumbs li a").live('click', function(){
			var parent = $(this).closest(".gallery");
			$("ul li a.on", parent).removeClass("on");
			$(this).addClass("on");
			load_img(this, $(".large", parent));
			return false;
		});

	}

	/* Video Gallery */

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

		$(".videos li a").live('click', function() {
			var parent = $(this).closest(".videos");
			$("li a.on", parent).removeClass("on");
			$(this).addClass("on");
			var anchor = '#'+$(this).attr("href").split('#')[1];
			var url = $(this).attr("href").split('#')[0];
			$(anchor).attr("src", url);
			return false;
		});

	}

	/* Colorbox */

	if($("*[rel='colorbox']").length) {
		$("*[rel='colorbox']").colorbox();
	}

	/* Text toggling */

	if($(".texttoggle").length) {
		$(".texttoggle").click(function(){
			$(this).toggleClass("compact").prev(".tail").toggle();
			$(this).text( ($(this).is(".compact")) ? "Less" : "More" )
			return false;
		});
	}

});

function load_img(source, destination) {
	var path = $(source).attr('href');
	$('img:first', destination).remove();
	var imgsrc = path;
	var newimg = new Image();
	newimg.src = imgsrc;
	newimg.onload = function() {
		var randomnumber = Math.floor(Math.random()*11);
		$(destination).append('<img src="'+imgsrc+'" />');
		newimg.onload = function(){};
	}
	newimg.src = imgsrc;
}

// EASING

jQuery.extend(jQuery.easing,{
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutExpo: function(x,t,b,c,d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	}
});
