window.onload=function()
{
	setupfixbodydimensions();
	bootstrap();
	setuphashpoll();
}

function setupfixbodydimensions()
{
	window.onresize=function()
	{
		fixbodydimensions();
	}
	fixbodydimensions();
}
function fixbodydimensions(rel)
{
	// getting the relationship of the link
	if(rel)
		var rel=rel.split('step')[1];
	if(rel > 2 || !rel)
		var rel=0;
	var relcounter=1;
	
	// stopping old animations
	$("div#container").stop();

	// retrieving the old width and the new width
	var oldw=rel == 0 ? $('div#body').width() : 0;
	var w=0;
	
	// resetting the body height and width
	$('div#body').height('auto');
	$('div#body').width(5000);
	var h=$(document).height();
	$('div#body').height(h);
	
	// retrieving the number of columns (because we need to add one px for each one's border
	var offset=$('div.columncontainer').length;
	
	// setting the new width
	$('div.columncontainer').each(function(){ if(relcounter<=rel && rel != 0) oldw+=$(this).width(); w+=$(this).width(); relcounter++; });
	$('div#body').width(w+offset);
	setTimeout("fixbodydimensions()", 2300);
	return [oldw,w+offset-1];
}

settings={};
settings.rooturl='www.sherman-scaf.org.au';
settings.currenthash='';
settings.prelim='/new/';

function setuphashpoll()
{
	// set up the polling engine
	hashinterval=window.setInterval("gohash()", 500);
}
function gohash()
{
	// get the hash
	var hash=window.location.hash;
	hash=hash.replace(/#/,'');
	if(hash == settings.currenthash) return;
	
	gosectionbyurl(hash);
}
function sethash(hash)
{
	if(hash.charAt(hash.length-1)!='/')hash+='/';
	window.location.hash=settings.currenthash=hash;
}

function bootstrap()
{
	var cols=$('div.columncontainer').length;
	
	// setting up the step
	settings.step=cols;
	
	// setting up the AJAX call
	$('a.ajax').click(function(ev){ 
    // alert($(this).attr('href'));
    // alert(ev.target.href);
    //console.log(ev);
        if (! ev.target.href) {
            gosection($(this).attr('href'),$(this).attr('rel')); ev.preventDefault(); 
        } else {
            gosection(ev.target.href,ev.target.rel); ev.preventDefault(); 
        }
		//alert(ev.target.rel);
		//_gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']);
        scroll(0,0);
	});
    $('a.ajax img').bind('mouseenter', function() {
        $(this).parent().find('span').show();
    });
    $('a.ajax').bind('mouseleave', function() {
        $(this).find('span').hide();
    });


	$('a.ajax').focus(function(ev){ 
		ev.target.blur() 
	});
	
	// bootstrapping secondary systems
	bootstrapmenu();
	bootstrapgallery();
	
	fixbodydimensions();
}
// implemented elsewhere
function bootstrapmenu() { }
function bootstrapgallery() { }

function gosection(url,rel)
{
	retrievesection(url,rel);
	
	// set the window hash
	if(url.indexOf(settings.rooturl) != -1)
		url = url.split(settings.rooturl)[1];
	sethash(url);
	
	cleanupmenus();
}

function gosectionbyurl(url)
{
	cleanupcolumns('step2');

	retrievesection(url,'step2');
	
	// set the window hash
	sethash(url);
	
	cleanupmenus();
}

function retrievesection(url,rel)
{
	$.post(url, { ajax: 'true', step: rel },
		function(data)
		{
			opensection(data,rel);
		});
}

function cleanupcolumns(rel)
{
	var columnnum=rel.split('step')[1];
	
	while(true)
	{
		columnnum++;
		if(!$('div.column'+columnnum).size() > 0)
			break;
		$('div.column'+columnnum).remove();
	}
}

function cleanupmenus()
{
	// this function assumes that menus start from column 2
	
	// clean the menus
	$('div.columncontainer li').each(function(){ $(this).removeClass('sel'); });
	
	// get the currently hashed URL
	var hash=settings.currenthash;
	if(hash.indexOf(settings.prelim) == 0)
		hash=hash.split(settings.prelim)[1];
	hash=hash.split('/');
	
	for(var i=0;i<hash.length;i++)
	{
		var columnnum=i+2;
		var section=hash[i];
		if(section=='') break;
		
		$('div.column'+columnnum+' li').each(
			function()
			{
				var part = '';
				var url = $('a', this).attr('href');
				// dropping the domain part of the URL
				if(url.indexOf(settings.rooturl) != -1)
					url = url.split(settings.rooturl)[1];
				// dropping the prelim part of the URI
				if(url.indexOf(settings.prelim) == 0)
					url=url.split(settings.prelim)[1];
				// splitting the URI
				url=url.split('/');
				// getting the positional part of the URL
				part = url[i];
				
				if(part == section)
					$(this).addClass('sel');
			}
		)
	}
}


function opensection(data,rel)
{
	cleanupcolumns(rel);
	
	var data=data.split('ajaxelement').join('ajaxadded');
	
//	$('div#body').append(data);
	$('div#body').html($('div#body').html()+data);
	
	var widths=fixbodydimensions(rel);
	bootstrap();
	
	$('div#container').css('overflow','hidden');
    // alert('setting width to ' + widths[0]);
	//$('div#container').width(1000);
	
	$('div#container').animate({'width':widths[1]},1000,"easeOutExpo");
	
	$('div.ajaxadded').removeClass('ajaxadded');
}
