$(document).ready(function() {
    
    $('img').hide();
	    
	$("header h1").fadeIn(500);
	
    var cellheight = $("#content li:first").height();
    
    adjustfont(cellheight);
    roundwindow();
    
    $(window).resize(function(event) {
        cellheight = $("#content li:first").height();
        adjustfont(cellheight);
        roundwindow();
    });
    
    function adjustfont(cellheight) {
        $("header h1").css("font-size", cellheight/2 + "px");
    }
    
    // Window
    
    function roundwindow() {
        $("#page").css("right", "24px");
        $("#page").css("bottom", "24px");
        var width = $("#page").width();
        var height = $("#page").height();
        //console.log(height%3 + " - " + height);
        if (width%4 != 0) {
            $("#page").css("right", 24+(width%4) + "px");
        } else {
            $("#page").css("right", "24px");
        }
        if (height%3 != 0) {
            $("#page").css("bottom", 24+(height%3) + "px");
        } else {
            $("#page").css("bottom", "24px");
        }
    }
    
    // Portfolio
    
    var location = document.location.toString();
    
    if (location.match('#')) {
        var itemid = '#port-' + location.split('#')[1];
        showItem(itemid, "location");
    }
    
    $("#content .overview li a").click(function(event){
        event.preventDefault();
        var itemid = "#port-" + $(this).attr("class");
        $(itemid).show();
        $(itemid + " img").fadeIn(250);
        $(itemid + " .meta").prepend('<a href="#" class="close">Close</a>');
        $("#content .meta .close").click(function(){
            $(itemid).hide();
            $(this).remove();
        });
    });
    
    $("#content .overview li a").hover(
        function(event) {
            var itemid = "#port-" + $(this).attr("class");
            var name = $(itemid + " h1").text();
            var year =  $(itemid + " .year").text();
            var html = "<div class=\"hovermeta\"><span class=\"name\">" + name +  "</span><span class=\"year\">" + year +  "</span></div>";
            $("#content").append(html);
        },
        function(event) {
            $("#content .hovermeta").remove();
        }
    );
    
    // Images

    $("#content article .images img").mousemove(function(event){
        var imgHeight = $(this).height();
        var divHeight = $(this).parent().height();        
        var posY = (event.pageY - 96)/divHeight;
        // Speed Up
        if (posY > divHeight/2) {
            posY = posY+(posY*0.25);
        } else {
            posY = posY-(posY*0.25);
        }
        // Round off
        posY = (posY > 1) ? 1 : posY;
        posY = (posY < 0) ? 0 : posY;
        $(this).css("top", "-" + (imgHeight-divHeight)*posY + "px");
    });
    
    // Twitter
    
    $("#tweets").getTwitter({
    	userName: "vefstofan",
		numTweets: 1,
		loaderText: "Loading ...",
		slideIn: true,
		slideDuration: 750,
		showHeading: false,
		headingText: "Latest Tweets",
		showProfileLink: false,
		showTimestamp: true
	});
	
	// About
	
	$("footer a").click(function(event){
        event.preventDefault();
        $("#sidebar").show();
        $("#sidebar").append('<a href="#" class="close">Close</a>');
        $("#sidebar .close").click(function(){
            $("#sidebar").hide();
        });
    });
	

    
});

// Preload
	
var i = 0;//initialize
var int=0;//Internet Explorer Fix
$(window).bind("load", function() {//The load event will only fire if the entire page or document is fully loaded
	var int = setInterval("doThis(i)",500);//500 is the fade in speed in milliseconds
});

function doThis() {
	var imgs = $('img').length;//count the number of images on the page
	if (i >= imgs) {// Loop the images
		clearInterval(int);//When it reaches the last image the loop ends
	}
	$('img:hidden').eq(0).fadeIn(50);//fades in the hidden images one by one
	i++;//add 1 to the count
}
