jQuery.extend({
	
	// Content
	firstLoad: function(id) {
		if(!location.hash && id) {
			$.sendRequest(id);
		}
	},
	
	contentTarget: undefined,
	
	latestLoad: undefined,
	
	sendRequest: function(id) {
		if(!id) return;
		id = decodeURIComponent(id);
		if(id == jQuery.latestLoad) return;
		if(id.search("filter-") < 0) jQuery.contentLoad(id);
		else jQuery.filterClick(id);
	},
	
	contentLoad: function(id) {
		
		//$(".link-text, .page-link").removeClass("link-active");
		//$(".page-link[href='#"+id+"']").addClass("link-active");
		//$("#thumb"+id+" .link-text,#list"+id+" .link-text").addClass("link-active");
		
		$(".link-text").removeClass("link-active");
		$(".page-link").removeClass("page-link-active");		
		$(".page-link[href='#"+id+"']").addClass("page-link-active");		
		$("#thumb"+id+" .link-text,#list"+id+" .link-text").addClass("link-active");
		
		jQuery.contentTarget.animate({"opacity":0},200);
		
		$.ajax({
			type: "POST",
			url: "viper/functions/get_entry.php",
			data: "id="+id,
			success: function(data){
				$("#load-content").show();
				$('html').animate({scrollTop:0}, 200);
				jQuery.latestLoad = id;
				jQuery.contentInsert(data);
			}
		});
	},
	
	contentInsert: function(data) {
		var id = jQuery.contentTarget.attr("id");
		jQuery.contentTarget.html(data);
		
		$("#entry-close").click(function(){
			$(".link-text").removeClass("link-active");
			$(".page-link").removeClass("page-link-active");
			jQuery.latestLoad = "";
			$("#load-content").html("");
			$("#load-content").hide();
		});
		
		jQuery.contentTarget.animate({"opacity":1},200);
	},
	
	// Filters
	filterArray: undefined,
	
	filterClick: function(id) {
		var filter,arr,ex;
		
		$(".filter-link.link-active").each(function() {
			$(this).removeClass("link-active");								 
		});
		
		$(".filter-link[href='#"+id+"']").each(function() {
			$(this).addClass("link-active");
			filter = $(this).attr("name");
		});
		
		arr = jQuery.filterArray[filter];
		if(!arr) arr = [];
		
		$(".entry-link").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);
		});
	},
	
	filterAll: function() {
		$("#load-content").hide();
		$.latestLoad = "";
		$(".link-active").each(function() {
			$(this).removeClass("link-active");			
		});
		
		$(".page-link-active").each(function() {
			$(this).removeClass("page-link-active");
		});
		
		$(".entry-link").each(function() {
			$(this).fadeTo(200,1.0);
		});
	}
});

