jQuery.extend({
	
	contentTarget: undefined,
	
	latestLoad: undefined,
	
	chips: undefined,
	
	sendRequest: function(id) {
		if(!id) return;
		if(id == jQuery.latestLoad) return;
		if(id.search("tag-") < 0) {
			jQuery.contentLoad(id);
		} else {
			jQuery.tagClick(id);	
		} 
	},
	
	contentLoad: function(id) {
		$.trackEvent("pageload", id);
		//Set the appropriate thumb to the active state
		$(".link-text, .page-link").removeClass("link-active");
		$(".thumb").removeClass("thumb-active");
		$(".page-link[href='#"+id+"']").addClass("link-active");
		$("#thumb"+id+" .link-text,#list"+id+" .link-text").addClass("link-active");
		$("#thumb"+id).addClass("thumb-active");
		
		$("#bg-holder").animate({"opacity":0},250);
		jQuery.contentTarget.animate({"opacity":0},250);
		setTimeout(function() { $.requestEntry(id); }, 250);
	},
	
	
	requestEntry: function(id) {
		$.ajax({
			type: "POST",
			url: "core/functions/get_entry.php",
			data: "id="+id,
			success: function(data){
				$("#load-content").delay(250).show();
				//$('html').animate({scrollTop:0}, 500);
				$('html, body').animate({scrollTop:0}, 500);
				jQuery.latestLoad = id;
				jQuery.contentInsert(data);
			}
		});
	},
	
	contentInsert: function(data) {
		var id = jQuery.contentTarget.attr("id");
		jQuery.contentTarget.html(data);
		
		$("#close-entry").click(function(){
			$(".link-text, .page-link").removeClass("link-active");
			jQuery.latestLoad = "";
			$("#load-content").html("");
			//$("#load-content").hide();
			$(".thumb img").removeClass("link-active");
		});
		
		jQuery.contentTarget.animate({"opacity":1},200);
		$("#bg-holder").delay(300).animate({"opacity":1},1000)
	},
	
	
	loadBackground: function(img) {
		$("#bg-holder").html("");
		$("#bg-holder").html("<img src=\""+img+"\" />");
	},
	
	clearBackground: function() {
		$("#bg-holder").html("");
	},
	
	//color chips
	expandChips: function() {
		$(".color_chip").animate({"height":22},150);
		$("#chip_text").show();
		$("#chip_text").animate({"opacity":1},150);
	},
	
	collapseChips: function() {
		$(".color_chip").animate({"height":4},150);
		$("#chip_text").animate({"opacity":0},150);
	},
	
	loadColors: function(colors) {
		//alert("Colors 0:"+colors[0]);
		for (i in $.chips) {
			$.chips[i].animate({backgroundColor:colors[i]},250);
		}
	},
	
	showHex: function(value) {
		$("#chip_text").html(value);
	},
	
	//tags
	tagArray: undefined,
	
	tagClick: function(id) {
		var tag,arr,ex;
		
		$(".tag-link a.link-active").each(function(){
			$(this).removeClass("link-active");								 
		});
		
		$(".tag-link a[href='#"+id+"']").each(function(){
			$(this).addClass("link-active");
			tag = $(this).attr("name");
		});
		
		arr = jQuery.tagArray[tag];
		if(!arr) arr = [];

		$(".thumb").each(function() {
			ex = false;
			for (var i = 0;i<arr.length;i++){
				var e_id = $(this).attr("id").replace(/list/,'').replace(/thumb/,'');
				if( e_id == arr[i] ) ex = true;
			}
			
			if(ex) $(this).fadeTo(200,1.0);
			else $(this).fadeTo(200,0.25);
		});
	},
	
	tagAll: function() {
		$("#load-content").animate({"opacity":0},200);
		$("#load-content").delay(200).hide();
		$(".thumb").removeClass("thumb-active");
		$.latestLoad = "";
		$(".link-active").each(function() {
			$(this).removeClass("link-active");
		});
									 
		$(".thumb").each(function() {
			$(this).fadeTo(200,1.0);
		});
		
		$('#main_container').animate( { backgroundColor: "rgb(225,225,225)" }, 500);
		$.loadColors(["rgb(255,29,35)","rgb(212,13,18)","rgb(148,9,13)","rgb(92,0,2)","rgb(69,0,3)"]);
		$.clearBackground();
	},
	
	setBG: function(r,g,b){
		$('#main_container').animate( { backgroundColor: "rgb("+r+","+g+","+b+")" }, 500);
	}
});

