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("tag-") < 0) jQuery.contentLoad(id);
		else jQuery.tagClick(id);
	},
	
	contentLoad: function(id) {
                $(".link-text").removeClass("link-active");
                $(".page-link").removeClass("page-active");
                $(".page-link[href='#"+id+"']").addClass("page-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.slideUp('slow');
                       
                        },
                        success: function(data){
                                $('html').animate({scrollTop:0}, 200); 
                                jQuery.latestLoad = id; 
                                jQuery.contentInsert(data);
								
                        },
                        error: function(){
                                jQuery.contentTarget.html('Whoha');

                        },
                        complete: function(){
							$("#load-content").slideDown('slow');
							$('#media .slideshow').cycle({
								slideExpr: 'img',
                                  fx: 'fade',
                                  next: '.slideshow img',
                                  timeout: 0,
                                  speed: 500,
                                  after: onAfter,

							});
							
                             function onAfter(curr,next,opts) {
                                        var msg = (opts.currSlide + 1) + ' / ' + opts.slideCount;
                                        $('#caption').html(msg);
										$('.slideshow img').css('cursor', 'pointer').attr('title', 'next image');

										
                                };
								
								$("form.jqtransform").jqTransform();
								
								$('#myForm').ajaxForm(function(data) {
									if (data==1){
										$('#myForm').fadeOut("slow");
										$('#bademail').fadeOut("slow");
										$('#myForm').resetForm();
										$('#success').fadeIn("slow");
									}
									else if (data==2) $('#badserver').fadeIn("slow");
									else if (data==3) {
										$('#bademail').fadeIn("slow");
										$('#emaillabel').css("color","red");
										$('#emailinput').focus();
									}
								});
								
								$('.field').example(function() {
								  return $(this).attr('title');
								});
								
                
                                $("#close-entry").click(function(){
                                        $(".link-text").removeClass("link-active");
                                         $(".page-link").removeClass("page-active");
                                         jQuery.latestLoad = "";
                                         $(this).parent().html("");
                                         $("#load-content").slideUp('slow');
                                });
                        }
                       
                });
        },
       
        contentInsert: function(data) {
                var id = jQuery.contentTarget.attr("id");
                jQuery.contentTarget.empty().html(data);
                jQuery.contentTarget.animate({"opacity":1},200);
        },
	
	//tags
	tagArray: undefined,
	
	tagClick: function(id) {
		var tag,arr,ex;
		$("#load-content").slideUp('slow');
		$(".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('slow',1.0);
			else $(this).fadeTo('slow',0.10);
		});
	},
	
	tagAll: function() {
		$("#load-content").slideUp('slow');
		$.latestLoad = "";
		$(".page-link").removeClass("page-active");
		$(".portfolio").addClass("page-active");
		$(".link-active").each(function() {
			$(this).removeClass("link-active");
		});
									 
		$(".entry-link").each(function() {
			$(this).fadeTo(200,1.0);
		});
	}
});

