$(document).ready(function() { setup(); });

function setup()
{
	
	if ($('#index').length == 1) { }
    else if ($('#journal').length == 1) { journal(); }
    else if ($('#portfolio').length == 1) { portfolio(); }
    else if ($('.project').length == 1) { project(); }
    else
    {
    	$('html .main p:last-child').css("margin-bottom", "0"); 
     	$('html .main li:last-child').css("margin-bottom", "0");
     }
    
    fix();
    goUp();
    adjustHeader();
}

function journal()
{
	var posts = $(".post");
	var up  = $(".post-navigation .previous");
	var down  = $(".post-navigation .next");
	var ids = new Array();
	var goto;
	var i = 0;
	
	$(posts).each(function() { ids.push($(this).attr("id")); });
	
	$(up).click(function(e)
	{
   		e.preventDefault();
		if (i > 0) { --i; }
		toggle(ids[i]);
	});
	
	$(down).click(function(e)
	{
   		e.preventDefault();
		if (i < ids.length-1) { ++i; }
		toggle(ids[i]);
	});
	
	function toggle(val)
	{
		goto = "#"+val;
		$("body,html,document").animate({
			scrollTop: $(goto).offset().top 
  		}, "fast");
	}
}

function portfolio()
{
	$('.go-up').remove();

	$('.wip').each(function()
	{
		$(this).click(function(e)
		{
			e.preventDefault();
		});
   	});

	function delimiter()
	{
		var count = $('.portfolio li').length;
		var target, search;
		
		$('#search').focus();
		
		$('.count').text(count);
		
		$('#search').keyup(function ()
		{
			target = $('.portfolio li');
			target.hide();
			
			search = new RegExp(this.value.replace(/\\/g, ''), "ig");
			
			target.filter(function() { return $(this).text().match(search); }).show();
			
			count = $('.portfolio li:visible').length;
			
			$('.count').text((count > 0) ? count : "0");
				
			$('.portfolio li span').each(function()
			{
				$(this).innerHTML = $(this).text();
				$(this).html($(this).text().replace(search,function($1){return "<em>"+$1+"</em>"}));
			});
		});
	}
	$('.info a').hover(function() {$ ('.info div').toggle(); });
	
	delimiter();
}

function project()
{
	var under960;
	
	($(window).width() <= 960) ? under960 = true : under960 = false;
	
	$(window).resize(function()
	{
  		if ($(window).width() <= 960 && !under960)
  		{
  			under960 = true;
  			update();
  		}
  		else if ($(window).width() > 960 && under960)
  		{
  			under960 = false;
  			update();
  		}
  	});

	if ($('.link a').length > 2) { $('.link h3').append('s'); }
	
	var items = $('.item');
	var num = items.length;
	
	if ($('.meta').children().size() == 0)
	{
		$('.meta').remove();
		$('.main').addClass("full");
	}
	
	var currNum = 0;
	var currItem = items.get(currNum);
    
    if (num == 0)
    {
    	$('.media').remove();
    	$('hr').css("margin-bottom", "0");
    	$('hr').css("border", "0");
    }
    else
    {
	    var currItemBtn = $(".media-navigation button:not(.next)").get(currNum);
	    var prevItem = currItem;
	    var prevItemBtn = currItemBtn;
	    var title, prop, height;
		var i = 0;
		var j = 0;
		
	    $('.main p:last-child').css("margin-bottom", "1.5em");
	    
    	items.each(function()
	    {
	    	if (++i > 1)
	    	{
	    		$(this).hide();
	    	}
	    });
	    
	    $(currItem).css("z-index",12);
	    
    	if (num != 1)
	    {
	    	$(currItemBtn).addClass("current");
	    }
	    else
	    {
	    	$(currItemBtn).remove();
    		$('.media-navigation').remove();
	    }
	    
	    $(".media-navigation button:not(.next)").each(function() { $(this).text(++j); });
	    	    
	    $(".media-navigation button:not(.next)").click(function()
	    {
	    	if ($(this).text()-1 != currNum)
	    	{
	   	 		currNum = $(this).text()-1;
	    		
	    		$(prevItemBtn).removeClass("current");
	    		currItemBtn = $(this);
	    		$(this).addClass("current");
	    		
	    		currItem = items.get(currNum);
	    		
	    		items.each(function() { $(this).css("z-index",11); });
	    		
	    		$(currItem).css("z-index",12);
	    		$(prevItem).hide();
	    		$(currItem).show();
	    		prevItem = currItem;
	    		prevItemBtn = this;
				
				update();	
	    	}
	    });
	    	    
	    $(".media-navigation .next").click(function()
	    {
	    	(currNum == num-1) ? currNum = 0 : currNum++;
	    	
	    	$(prevItemBtn).removeClass("current");
	    	currItemBtn = $(".media-navigation button:not(.next)").get(currNum);
	    	$(currItemBtn).addClass("current");
	    	currItem = items.get(currNum);
	    	
	    	items.each(function() { $(currItem).css("z-index",11); });
	    		
	    	$(currItem).css("z-index",12);
	    	$(prevItem).hide();
	    	$(currItem).show();
	    	prevItem = currItem;
	    	prevItemBtn = currItemBtn;
			
			update();
	    });
	    
		update();
	}
	
	function update()
	{
		if ($(currItem).hasClass("image"))
		{
			prop = $(currItem).attr("width") / ($('.media').width());
			height = $(currItem).attr("height") / prop;
			height = Math.floor(height);
			if ($(currItem).attr("width") > $('.media').width())
			{
				$(currItem).height(height);
			}
		}
		else if ($(currItem).hasClass("html"))
		{
			$(currItem).height($("currItem, iframe").attr("height"));
		}
		else
		{
			$(currItem).height(height);
		}
		
		
		title = $(currItemBtn).attr("title");
		if (title.indexOf('.') > 0) { title = title.substr(0, title.indexOf('.')); }
		$('.title').text("| "+title);
	}
	
	update();
}

function goUp()
{
	$(window).scroll(function ()
	{
        if (window.pageYOffset > $(".wrapper").height()*0.2) { $('.go-up').fadeIn(100); }
        else { $('.go-up').fadeOut(150); }
	});
		
	$('.go-up').click(function(e)
	{
   		e.preventDefault();
		$("body,html,document").animate({
			scrollTop: 0 
  		}, "slow");
   	});
}

function adjustHeader()
{
	var len;
	var maxLen = 36;
	
	$("h2").each(function()
	{
		len = $(this).text().length;
		len = len-$(".date", this).text().length;
		if (len > maxLen) { $(this).css("marginTop", "-61px"); }		
	});
}

function fix()
{
	$('.content').css("margin-bottom", "-24px");
	
	/*if (window.devicePixelRatio)
	{
		$('.content').css("float", "left");
	}
	
	window.onload = function()
	{
		if (window.devicePixelRatio)
		{
			if ($('.project').length != 1)
			{
				$('.go-up').css("bottom","86px");
				$('.content').height(Math.floor($('.content').height())-34);
				$('.content').css("margin-bottom", "-34px");
			}
			else
			{
				$('.content').height(Math.floor($('.content').height())-24);
				$('.content').css("margin-bottom", "-24px");
			}
		}
	}*/
} 
