﻿var $j = jQuery.noConflict();
var pathname, page;

$j(function(){
    $j.history.init(pageload);

    if(page == null || page == '')
        page = getPage();
    
    setSelected($j('ul#navigation li a[href*="' + page + '"]')); //select the appropriate nav
    
    $j('ul#navigation li a').click(function(){
        //Set the selected tab
        setSelected(this);                
        
        var hash = null;                
        var home = isHome();
        if(home) //load the page using ajax
        {
            if($j(this).parent('li').index() == 0)
            {
                page = 'default.aspx'; //load the home page
                loadit();
            }
            else
            {
                window.location.hash = $j(this).attr('rel');
                page = $j(this).attr('href'); //load other pages
                
                //No need to call loadit.  Adding the hash to the history will call it.
                hash = window.location.hash.replace(/^.*#/, '');
                $j.history.load(hash);
            }
            
            return false;
        }
        else //go home and tell it which page to load
        {
            console.log($j('head base').attr('href') + $j(this).attr('rel'));
            //redirect for post page, change the hash if we're not
            
            if(window.location.href.indexOf('.aspx') > -1) //Redirect if we're actually on a page
            {
                window.location.href = $j('head base').attr('href') + $j(this).attr('rel');//Redirect to home page and set the hash to the page we want to load.
                loadit();
            }
            else //load the hash/page if we're still on the default page.
            {
                window.location.hash = $j(this).attr('rel'); //Set the hash
                page = getPage();
                
                //No need to call loadit.  Adding the hash to the history will call it.
                hash = window.location.hash.replace(/^.*#/, '');
                $j.history.load(hash);
            }
            
            return false;
        }
    });
});

function pageload(hash) {
    if(hash && hash.indexOf('#x') == -1)
    {
        page = getPage();
        loadit();
    }
}

function setSelected(obj){
    $j('ul#navigation li').not($j(obj).parent('li')).each(function(){
        setDeselected(this);
    });
    $j(obj).addClass('selected');
    $j(obj).parent('li').addClass('selected');
}

function setDeselected(obj){
    $j(obj).removeClass('selected');
    $j(obj).children('a').removeClass('selected');
}

function loadit(){
    $j('#placeholder').html('');
    $j('#loading').show();
    
    //momentary delay before loading page
    setTimeout(function(){
        var url = $j('head base').attr('href') + (page == '/' ? '' : page) + (window.location.search == '' ? '?' : '&') + 'ajax=true'
        
        console.log('url: ' + url);
        $j.get(url, function(rslt){
            $j('#loading').hide();
            $j('#placeholder').html(rslt);
        });
    }, 200);
}

function getPage(){
    var loc = window.location.href;            
    if(loc.indexOf('.aspx') > -1)
        return loc.replace($j('head base').attr('href'), '');
    else
    {
        //If a hash exists, return that page.  Otherwise, load the home page.
        if(window.location.hash != '' && 
           window.location.hash != $j('ul#navigation li a:first').attr('rel'))
        {
            console.log('load custom page');
            return window.location.hash.replace('#', '') + '.aspx';
        }
        else
            return "/";
    }
}

function isHome(){
    var p = getPage();
    if(p == '/' || p.toLowerCase() == 'default.aspx')
        return true;
    else if(window.location.href == $j('head base').attr('href') && window.location.hash != '')
        return true;
        
    return false;
}
