var IS_FOCUSED = false;

$(document).ready(function(){

	$('#socialise').append('<span class="st-link st_twitter_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_digg_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_reddit_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_stumbleupon_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_delicious_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_facebook_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_gbuzz_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_linkedin_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_tumblr_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><span class="st-link st_email_large" st_title="'+TITLE+'" st_url="'+URL+'" displayText="share"></span><div class="clear"></div>');

	
	$('a[rel*=external]').attr("target", "_blank");
	$('a[rel*=tag],.info a[rel*=bookmark]').attr("class", "underline");
	
	$("#contact_form-form").contactify({
		loadingImage: '/wp-content/themes/Andy_v5/images/loading.gif',
		scriptUrl: '/contact.php'
    });
	
	$(".more-content").each(function(){
                var linkTitle = 'View more';
                if ($(this).attr("title") && $(this).attr("title").length) linkTitle = $(this).attr("title");
		$(this).css("display", "none").before('<a class="more-content-link" rel="'+$(this).attr("id")+'" title="'+linkTitle+'" href="#">'+linkTitle+'</a><div class="clear"></div>');
	});
	var animating = false;
	$(".more-content-link").click(function(e){
		e.preventDefault();
		if (!animating){
			animating = true;
			$(".remove-on-more").slideUp(800);
			$(".more-content-link").stop().slideDown(300);
			$(this).stop().slideUp(300);
			$(".more-content").stop().slideUp(800);
			$("#"+$(this).attr("rel")).stop().slideDown(800);
			setTimeout(function(){animating = false}, 1000);
		}
	});
	
	$("a[rel*='popup']").fancybox({ 
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition'	:	'over',
		'onComplete'	:	function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").show();
			}, function() {
				$("#fancybox-title").hide();
			});
		}
	});
	
	$(".post.loop").each(function(){
		var url = $(this).children("header").children("h2").children("a").attr("href");
		var title = $(this).children("header").children("h2").children("a").attr("title");
		var first_p = $(this).children("article").children(".the-post").children("p:first");
		if ($(first_p).children("img").length)
			$(first_p).html('<a class="no-underline" href="'+url+'" title="'+title+'">'+$(first_p).html()+'</a>');
	});
	
	$(".post").each(function(){
		$(this).children("article").children(".the-post").children("p:first").addClass("post-image-shadow");
	});
	
	$("ul.gallery-links").css("display", "none");
	
	
	
	var projectAnimating = false;
	
	if ($("#project-holder").length){
		$("#project-holder").css("height", (Math.ceil($(".project").length/4)*235)+"px");
		if (window.location.hash.length && !window.location.hash.replace("#", "").match("all")) showProjects();
	}
	
	$("a[rel*='project-nav']").click(function(e){
		e.preventDefault();
		if (!projectAnimating && !$(this).hasClass("selected")){
			var show;
			var label = $(this).html();
			var labelLower = label.toLowerCase();
			if (labelLower.match("all")) show = "all";
			else if (labelLower.match("coding")) show = "coding";
			else if (labelLower.match("websites")) show = "websites";
			else if (labelLower.match("winterboard")) show = "winterboard";
			else if (labelLower.match("ui")) show = "uidesign";
			
			window.location.hash = show;
			
			showProjects();
		}
	});
	
	function showProjects(){
	
		projectAnimating = true;
		
		var show = window.location.hash.replace("#", "");
		var button = null;
		var buttons = ['all', 'coding', 'websites', 'winterboard', 'uidesign'];
		
		for (var i=0;i<buttons.length;i++){
			if (show == buttons[i]){
				button = document.getElementById('button-'+(i+1));
				break;
			}
		}
		
		if (button == null){
			window.location.hash = "all";
			show = "all";
			button = document.getElementById('button-1');
		}
	
		$("a[rel*='project-nav']").each(function(){
			$(this).removeClass("selected");
		});
		
		$(button).addClass("selected");
		
		$("a.project").each(function(){
			if ($(this).css("display") != "none") $(this).fadeTo(500, 0);
			var element = $(this);
			setTimeout(function(){$(element).css("display", "none")}, 500);
		});
		
		setTimeout(function(){
			$("a.project").each(function(){
				if ($(this).attr("rel").match(show) || show == "all"){
					$(this).fadeTo(750, 1);
				}
			});
		}, 550);
		
		setTimeout(function(){projectAnimating = false;}, 1400);
		
	}
	
	$("input,textarea,select").live({
		focus: function(){ IS_FOCUSED = true },
		blur: function(){ IS_FOCUSED = false }
	});
	
	function scroll(dir, done){
		var currentY = $(window).scrollTop();
		var currentX = $(window).scrollLeft();
		done++;
		var finalY = (dir == "up") ? (currentY-done) : (currentY+done);
		window.scroll(currentX, finalY);
		if (done < 30) setTimeout(function(){scroll(dir, done)}, 5);
	}
	
	$(document).keypress(function(e){
		if (IS_FOCUSED) return;
		switch(e.keyCode){
			case 48:
				e.preventDefault();
				scroll('down', 0);
				break;
			case 49:
				e.preventDefault();
				window.location.href = "/";
				break;
			case 50:
				e.preventDefault();
				window.location.href = "/projects";
				break;
			case 51:
				e.preventDefault();
				window.location.href = "/contact";
				break;
			case 57:
				e.preventDefault();
				scroll('up', 0);
				break;
			
		}
	});
	
});
