/**
 * 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
$j(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;
    $j('#content').wrap(wrapDiv);
    
    // Once we've loaded the new content, show it
    function showNewContent() {
        // Show the wrapper that contains the content
        $j("#" + contentWrapID).slideDown();
        
        // Hide the ajax loader icon
        $j('#load').fadeOut();
        
        // Make Fancyboxes fancy
        $j('a.fancybox').fancybox();
    }
    
    // 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
            $j("#" + contentWrapID).slideUp('fast',
                function()
                {
                    // Snag the GET parameters
                    var vars = GETVars();
                    var stringVars = "";
                    
                    // Add in GET variables
                    for (var v in vars)
                    {
                        // Have to watch out for Prototype adding junk to the array prototype
                        if (typeof vars[v] != "function")
                        {
                            // Put the vars in the string
                            stringVars += v + "=" + vars[v] + "&";
                        }
                    }
                    // If we actually had any vars
                    if (stringVars.length > 0)
                    {
                        // Prepend the question mark for the query
                        stringVars = "?" + stringVars;
                    }
                    
                    // Load the #content div from page of the mentioned in the hash
                    $j("#" + contentWrapID).load(hash + stringVars + " #content",'',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($j('img:last',this).get(0)) {
                            $j('img:last',this).load(function(){
                                showNewContent();
                            });
                        } else {
                            showNewContent();
                        }

                    });
                }
            );


        } else {
            // Load this page
            pageload(window.location.href);
        }
    }
    // Initialize history
    $j.historyInit(pageload);
    
    // Adds the onclick function to the links
    $j('#nav li a').click(function(){
        
        // Get the hash from the link that was clicked
        var hash = $j(this).attr('href');
        var qMark = hash.indexOf("?");
        var nSign = hash.indexOf("#");
        if (qMark > -1 && ((nSign > -1 && qMark < nSign) || nSign == -1))
        {
//            hash = hash + "?" + hash.replace(/^.*\?/, '');
        }
        else
        {
            hash = hash.replace(/^.*#/, '');
        }

        // Load this hash with the history plugin
        $j.historyLoad(hash);
        
        if(!$j('#load').get(0)) {
            $j('#wrapper').append('<span id="load">LOADING...</span>');
        }
        $j('#load').fadeIn('normal');
        return false;
    });
});

// Returns all the GET parameters in url
// Added by Patrick on 04/02/2010
function GETVars()
{
    // Grab the url string
    var loc = document.location.search;         
    var vars = new Array();

    // Split off the parameters
    var sections = loc.split("?");
    // If there were no parameters, return
    if (sections.length < 2)
    {
        return vars;
    }
    // There are some url parameters
    else
    {
        // Get rid of the anchor
        var paramString = sections[1].split("#")[0];
        // Given a URL like http://www.example.com/?var1=lala&var2=3&var3=test&#blog.php
        // we should have just isolated the "var1=lala&var2=3&var3=test&" portion
        
        // Split the variables apart
        var params = paramString.split("&");
        for (p in params)
        {
            // If there wasn't really a parameter here
            if (params[p].length < 1) {continue;}
            
            // Must avoid Prototype's junk
            if (typeof params[p] == "function") {continue;}
            
            // Split the key and value apart
            var keyValue = params[p].split("=");
            
            // If there was only a key
            if (keyValue.length == 1)
            {
                vars[keyValue[0]] = "";
            }
            // If there was a key and value
            else
            {
                vars[keyValue[0]] = keyValue[1];
            }
        }
    }
    // Send back the arrayw
    return vars;
}
 
 
 
 
 
 
 
 
 
 
 
