jQuery.extend({
	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("tag-") < 0) jQuery.contentLoad(id);
		else jQuery.tagClick(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");
		$.ajax({
			type: "POST",
			url: "core/functions/get_entry.php",
			data: "id="+id,
			beforeSend: function(){
				jQuery.contentTarget.animate({'opacity':0},200);
            	$("#core-loader").show();
            },
			success: function(data){
				$('html,body').stop().animate({scrollTop: 0}, 600);
				jQuery.latestLoad = id;
				jQuery.contentInsert(data);
                              
			},
			error: function(){
        			jQuery.contentTarget.html('Major lazor error');
   				 
   			},
   			complete: function(){
 				$("#core-loader").fadeOut();
 				
 				Cufon.refresh();
 				
 				$("#close-entry").click(function(){
					$(".entry-text").removeClass("link-active");
					jQuery.latestLoad = "";
					$(this).parent().html("");
				});
				
 				

            }
		});
	},
	
	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 = "";
			$(this).parent().html("");
		});
		
		jQuery.contentTarget.stop().animate({"opacity":1},200);
	},
	
	//tags
	tagArray: undefined,
	
	tagClick: function(id) {
		var tag,arr,ex;
		
		$(".tag-link.link-active").each(function(){
			$(this).removeClass("link-active");								 
		});
		
		$(".tag-link[href='#"+id+"']").each(function(){
			$(this).addClass("link-active");
			tag = $(this).attr("name");
		});
		
		arr = jQuery.tagArray[tag];
		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);
		});
	},
	
	tagAll: function() {
		$("#load-content").hide();
		$.latestLoad = "";
		$(".link-active").each(function() {
			$(this).removeClass("link-active");
		});
									 
		$(".entry-link").each(function() {
			$(this).fadeTo(200,1.0);
		});
	}
	

});

