jQuery.extend({
	
	//content
	firstLoad: function(id) {
		if(!location.hash && id) {
			$.sendRequest(id);			
		}
	},
	
	contentTarget: undefined,
	
	portfolioPage: 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");

		jQuery.contentTarget.animate({"opacity":0},200);
		
		$.ajax({
			type: "POST",
			url: "core/functions/get_entry.php",
			data: "id="+id,
			success: function(data){
				$("#load-content").show();
				$('html').animate({scrollTop:0}, 200);
				// if(/^\d*$/.test(id) || id == $.portfolioPage) $.showMenu();
				// else $.hideMenu();
				
				jQuery.latestLoad = id;
				jQuery.contentInsert(data);
			}
		});
	},
	
	showThumbs: function() {
		$("#thumbs").fadeIn(100);
	},
	
	hideThumbs: function() {
		$("#thumbs").fadeOut(100);
	},
	
	thumbOver: function() {
		$(".thumb").hover(function() {
			// console.log('over');
        	$(this).css("background-color","#ECECEC");  
			// $t(this).animate().stop({opacity: 0.0}, 800)
	    }, function() {
			// console.log('out'); 
	        $(this).css("background-color", "#ffffff");  
			// $t(this).animate().stop({opacity: 1.0}, 800)
	    });
	},
	
	contentInsert: function(data) {
		var id = jQuery.contentTarget.attr("id");
		jQuery.contentTarget.html(data);
		
		var slidePercentage = "90";
		var imgWidth = $(".slideshow img").width();
		var imgHeight = $(".slideshow img").height();
		var slideWidth = Math.round((imgWidth / 100)*slidePercentage);
		var slideHeight = Math.round((imgHeight / 100)*slidePercentage);
		// console.log('slide width is:' +slideWidth);
		// console.log('slide height is:' +slideHeight);
		// console.log(slideScale);
				
		$('.slideshow').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			prev: '#prev',
		    next: '#next, .slideshow img',
			speed: '600',
			width: slideWidth,
			height: slideHeight,
			fit: 1,
		    timeout: 0
		});
		$("#close-entry").click(function(){
			$(".link-text, .page-link").removeClass("link-active");
			jQuery.latestLoad = "";
			$("#load-content").html("");
			$("#load-content").hide();
		});
		$("#close-entry-top").click(function(){
			$(".link-text, .page-link").removeClass("link-active");
			jQuery.latestLoad = "";
			$("#load-content").html("");
			$("#load-content").hide();
		});
		jQuery.contentTarget.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);
		});
	},
	
	sendAnnotatedMailTo: function() {
		$(".contact").click(function(name, company, domain, subject) {
			locationstring = 'mai' + 'lto:' + name + '@' + company + '.' + domain + "?subject=" + escape(subject);
			window.location.replace(locationstring);
		});	
	},
	
	obfMail: function() {
		
		coded = "xu@xgpvlnOuvnni1.p2x";
		key = "pj6hi7u9LnPGryHSDl2YBzUvgJQfV4MTK1bZAOEF8mcWXkdtswIe0CN53Rqaox";
		shift = coded.length;
		link = "";
		
		for (i = 0; i <coded.length; i++) {
			if (key.indexOf(coded.charAt(i))==-1) {
				ltr = coded.charAt(i);
				link += (ltr);
			} else {     
				ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length;
				link += (key.charAt(ltr));
			};			
		};

		$(".mail").append("<a href='mailto:"+link+"'>"+link+"</a>");
		
	}
	
});


