/**
 * Original Script by James Padosley for Nettuts.com
 * Updated to be based on his script as of Nov 18, 2009
 *
 * Modified by Patrick Eisenmann to support history and lightbox/PrototypeJS libraries
 *	(peisenmann@gatech.edu)
 *
 * Modified by Andre Bluehs to support PHP (and any other user added extensions)
 *	(contagious@gatech.edu)
 */

// Do this when the document loads
$(document).ready(function() {    
    // Id of the tag that will wrap the content
    var contentWrapID = '___content-wrapper';
    // Wrapping the content. This will keep us from duplicating the content div
    // This is an important change from James' script because his would not work
    // for Safari
    var wrapDiv = document.createElement("div");
    wrapDiv.id = contentWrapID;
    $('#contentAjax').wrap(wrapDiv);
    
    // Once we've loaded the new content, show it
    function showNewContent() {
	
        // Show the wrapper that contains the content
			setTimeout( function()
			{
        		$("#" + contentWrapID).fadeIn('fast');
			}, 500);
			
        // Hide the ajax loader icon
			setTimeout( function()
			{
        		$('#load').fadeOut('fast');   
			}, 400);
    }
    
    // Load a page based on a hash (Given from the address bar)
    function pageload(hash) {
        // If the hash is not empty or undefined
        if(hash) {            
            // Hide the content
            $("#" + contentWrapID).fadeOut('fast',
                function()
                {
                    // Load the #projectsAjax div from page of the mentioned in the hash
                    $("#" + contentWrapID).load(hash + " #contentAjax",'',function (responseText, textStatus, XMLHttpRequest) {
                        
                        // Don't bother if the hash was busted
                        if (textStatus == "error") return;
                        
                        // This helps show the content only when it's all loaded
                        if($('img:last',this).get(0)) {
                            $('img:last',this).load(function(){
                                showNewContent();
                            });
                        } else {
                            showNewContent();
                        }

                    });
                } 
            );


        } else {
            // Load this page
            pageload(window.location.href);
        }
    }
    // Initialize history
    $.historyInit(pageload);
    
    // Adds the onclick function to the links
    $('#tab1 li a, #tab2 li a').click(function(){
        
        // Get the hash from the link that was clicked
        var hash = $(this).attr('href');
        hash = hash.replace(/^.*#/, '');

        // Load this hash with the history plugin
        $.historyLoad(hash);
        
        if(!$('#load').get(0)) {
            $('#loadBar').append('<img id="load" src="http://www.popfabryk.nl/assets/templates/popfabryk/images/ajax-loader.gif" />');
        }
		setTimeout( function()
		{
        	$('#load').fadeIn('fast');
}, 100);
        	return false;
		
    });
});
