var AjaxContent = function() {
    var container_div = ''; 
	var content_div = ''; 
	return {
		getContent : function(url) {
			 // the callback, loads the content with ajax
			//$(container_div).load(url+" "+content_div, //only loads the selected portion
            var hash = new String(url);
            var index = hash.indexOf("?");
            hash = hash.substring(index + 3); //?p=hash
            
            if (hash == 'talklist') {
                Modal.Open('modal/talks.php', 600, 480, null);
            } else {
                window.location.hash = hash;
                checkURL(hash);
            }
			
            //function(){						   
			//   $(container_div).animate({opacity:1}); //and finally bring back the opacity back to 1
            //}
       
		},
		ajaxify_links: function(elements) {
            $(elements).click(function(){
                // Fix for iPad as can't hover over menu - expand if clicked first time
                if (FullMenu == false && $(this).parent().parent().hasClass('nav')) {
				    slideIn();
                } else {
                    AjaxContent.getContent(this.href);
                }
				return false; //prevents the link from beign followed
			});
		},
		init: function(params) { //sets the initial parameters
			container_div = params.containerDiv; 
			content_div = params.contentDiv;
			return this; //returns the object in order to make it chainable
		}
	}
}();


var lasturl="";	//here we store the current URL hash

function checkURL(hash)
{
	if(!hash) {        
        hash = window.location.hash;	//if no parameter is provided, use the hash value from the current address
        hash = hash.substr(1);
    } 
    
	if(hash != lasturl)	// if the hash value has changed
	{
        if (hash == '') {
            var hash2 = new String(window.location.href);
            var index = hash2.indexOf("?");
            hash = hash2.substring(index + 3);
            window.location.hash = hash;
        }
    
		lasturl = hash;	//update the current hash
		loadPage(hash);	// and load the new page
	}
}

var speed = 700;

function loadPage(url)	//the function that loads pages via AJAX
{
	
    
    
    //url=url.replace('#page','');	//strip the #page part of the hash and leave only the page number

	//$('#loading').css('visibility','visible');	//show the rotating gif animation
    setHover();
    
    //Turn the opacity to 0
    //$("#column_content").animate({opacity:0}, 1000, function(){ 
    $("#body").slideUp(speed, function(){ 
        $.ajax({	//create an ajax request to load_page.php
            type: "POST",
            url: "ajax.php",
            data: 'p='+url,	//with the page number as a parameter
            dataType: "html",	//expect html to be returned
            success: function(msg){
    
                //if(parseInt(msg)!=0)	//if no errors
                //{
                    $('#text').html(msg);	//load the returned html into pageContent
                    AjaxContent.ajaxify_links("#text a[href*='?p=']");
                    //$("#column_content").animate({opacity:1}); //and finally bring back the opacity back to 1
                    $("#body").slideDown(speed); //and finally bring back the opacity back to 1
                    //$('#loading').css('visibility','hidden');	//and hide the rotating gif
                //}
            }
    
        });
       _gaq.push(['_trackPageview', '/?p='+url]);
        
    }); 

	

}

var FullMenu = true;

function slideOut() {
    var tpos = $("#column_left ul li a[href='?p="+lasturl+"']").offset().top - $("#column_left ul").offset().top - 5;
    var bpos = 300 - tpos - $("#column_left ul li a[href='?p="+lasturl+"']").parent().height() + 8;
    $("#column_left ul li a[href='?p="+lasturl+"'] img").fadeTo(speed, 1);
    $("#column_left ul li a[href!='?p="+lasturl+"'] img").fadeTo(speed, 0); //.show('slide', { direction: 'down' }, 1000);
    $("#topnav").animate({'margin-top': +tpos+'px'},speed);
    $("#bottomnav").animate({'margin-top': '-'+bpos+'px'},speed);
    FullMenu = false;
}

function slideIn() {
    $("#column_left ul li a img").fadeTo(speed, 1.0); //.hide('slide', { direction: 'up' }, 1000);
    $("#topnav").animate({'margin-top': '0px'},speed);
    $("#bottomnav").animate({'margin-top': '0px'},speed);
    FullMenu = true;
}

function setHover() {
    
    if(lasturl == 'home' || lasturl == 'giving' || lasturl == '') {
        // no opacity
        $("#column_left ul").unbind('hoverIntent');
        slideIn();
        
    } else {
        slideOut();

        // sliding functions
        $("#column_left ul").unbind('hoverIntent');
        $("#column_left ul").hoverIntent(        
            // ON MOUSE OVER
            function() {
                slideIn();
            },
        
            // ON MOUSE OUT
            function () {
                slideOut();
            }
        );
    }
}


$(document).ready(function() {	//executed after the page has loaded

    $('body').supersleight({shim: 'spacer.gif'});


	checkURL();	//check if the URL has a reference to a page and load it
    
    AjaxContent.init({containerDiv:"#column_content", contentDiv:"#text"}).ajaxify_links("a[href*='?p=']");

//	$('ul li a').click(function (e){	//traverse through all our navigation links..
//
//			checkURL(this.hash);	//.. and assign them a new onclick event, using their own hash as a parameter (#page1 for example)
//
//	});

	setInterval("checkURL()", 250);	//check for a change in the URL every 250 ms to detect if the history buttons have been used
    
    //setHover();
        



});

