jQuery(document).ready(function($) {
	function colorize(ele){
		ele.stop(true,false)
		.animate({backgroundColor:"#1f54ff"},1000)
		.animate({backgroundColor:"#1ffaff"},1000)
		.animate({backgroundColor:"#00ffff"},1000)
		.animate({backgroundColor:"#f8ff1f"},1000)
		.animate({backgroundColor:"#ff1f44"},1000)
		.animate({backgroundColor:"#f31fff"},1000)
		.animate({backgroundColor:"#ccc"}, 2000);
	}
	$(".header-img").mouseover(function(){
		colorize($(this));
	});
	$(".grid").mouseover(function(){
		if($(this).attr("id") != "active") colorize($(this).find(".bg-color"));
	});
	$(".pre-load").each(function(){
		$("body").append("<div class='hide' id='"+$(this).attr("id")+"-preload'>");
		getLoaded($(this).attr("id"),$(this).attr("href"));
	});
	function getLoaded(ele,hrefs){
		var nele = $("#"+ele+"-preload");
		if(nele.attr("data-ready") == true){
			return true;
		}
		nele.load(hrefs+" .main-content",function(){
			$(this).attr("data-ready",true);
			$(this).find(".main-content").removeClass("main-content").addClass("contain");
			return true;
		});
	}
	$(window).bind('statechange',function(){
		var State = window.History.getState();
		loadContent(State.url);
	  });
	$(".mini-links a, .load-it, .more-link, .load-it-parent a").live('click',function(){
		var hrefs = $(this).attr("href");
		var regexp = new RegExp('#([^\\s]*)','g');
		History.pushState({ link: hrefs }, "", hrefs.replace(regexp, ''));
		return false;
	});
	function loadContent(hrefs){
		if(typeof($(".main-content").attr('class')) == 'undefined'){
			$(".content-below").append("<div class='main-content'></div>");
			$(".main-content").slideUp(0,function(){
				$(".loading").removeClass("hide");
				$(this).html("").load(hrefs+" .main-content", function(){
					$(this).animate({opacity:0},0).slideDown(500).animate({opacity:1});
					$(".loading").addClass("hide");
				});
			});
			$(".nav-more").stop(true,true).animate({opacity:0}).slideUp(500);
			$(".drop-down").attr("data-down",false);
		}
		else{
			$(".main-content").stop(true,true).animate({opacity:0}).slideUp(500,function(){
				$(".loading").removeClass("hide");
				$(this).html("").load(hrefs+" .main-content", function(){
					$(this).animate({opacity:0},0).slideDown(500).animate({opacity:1});
					$(".loading").addClass("hide");
				});
			});
		}
		$("#active").attr("id","").find(".bg-color").animate({backgroundColor:"#ccc"}, 1000);
		$(this).parent().attr("id","active").find(".bg-color").stop(true,false).css("background-color","#26b2fe");
		$(".mini-links").animate({paddingBottom:0});
	}
	$("body.blog-active .drop-down").attr("data-down",false);
	$(".drop-down").click(function(){
		if($(".drop-down").attr("data-down") == 'false' || $(".drop-down").attr("data-down") == false){
			$(".nav-more").stop(true,true).slideDown(500).animate({opacity:1});
			$(".drop-down").attr("data-down",true)
		}
		else{
			$(".nav-more").stop(true,true).animate({opacity:0}).slideUp(500);
			$(".drop-down").attr("data-down",false)
		}
	});
	$(".header-img").click(function(){
		$(".main-content").stop(true,true).animate({opacity:0}).slideUp(500,function(){
			$(this).remove();
			$(".drop-down").attr("data-down",false).click();
			History.pushState({ foo: "bar" }, "", "/");
		});
	});
});
