
var lasthash='';

var lastRequest;

function site_onload()
{
	
	$.ajaxSetup(AjaxSetup);

	load_scripts();
	load_menu_json();

	if(Debug == 1)
	{

		SetDebug();

	}

}

function add_ajaxhandler()
{

	$(ContentID).ajaxError(function(e, xhr, settings, exception) {

  		$(ContentID).load(ErrorFile);

		if(Debug == 1)
		{
			
			$(ContentID).append('Message: <br/> <p>' + exception + '</p>' );

		}

	});

//	$(ContentID).ajaxStart(function() {
//
//		$(ContentID).empty();
//		$(ContentID).append(LoadingText);
//
//	});

	$(ContentID).ajaxSend(function(e, jqxhr, settings) {

		lastRequest = jqxhr;

	});

	$(ContentID).ajaxComplete(function() {	

		$(ContentID + ' .ajax_form').submit(function () { 

			$(ContentID).load(SitesLocation + $(this).attr('action'),$(this).serializeArray());
			
			return false; 
	
		});

		//$(ContentID + ' a[rel^="lightbox"]').slimbox({loop:true});

		//$(ContentID + ' .accordion').accordion({ header: 'h3',autoHeight: false, clearStyle: true ,fillSpace: false});
		//$(ContentID + ' .accordion').accordion( "resize" );

		$(ContentID + ' .button').button();

	});

}

function load_scripts()
{

	for (var i = 0; i < ScriptFiles.length; i++)
	{

		$.getScript(ScriptFiles[i]);

	}

}

function load_css()
{

	for (var i = 0; i < SyleSheets.length; i++)
	{

		SyleSheets[i];

	}


}

function load_menu_json()
{

	$.getJSON(SitesLocation + MenuFile, function(data) {

			MenuItems = data;
			createMenu();
			window.setInterval('capture_anker()', 100);
			add_ajaxhandler();
    	});

}


function createMenu()
{
	
	$(MenuID).empty();
	$(MenuID).append('<ul>');

	for (var i = 0; i < MenuItems.length; i++)
	{	

		$(MenuID + ' ul').append('<li><a href="#' + MenuItems[i]['Site'] +'">' + MenuItems[i]['Name'] + '</a></li>');
	
	}

}

function load_content(site)
{

	if(lastRequest != null) {

		lastRequest.abort();

	}

	$(ContentID).empty();
	$(ContentID).append(LoadingText);

	$(ContentID).load(SitesLocation + site);

}

function capture_anker()
{


	if(window.location.hash == '')
	{

		window.location.hash='#' + StartSite;

	}

	if(window.location.hash != lasthash)
	{


		lasthash = window.location.hash;
		load_content(window.location.hash.replace('#',''));

		$(MenuID +' .selected').removeClass('selected');

		$(MenuID +' a[href="' + window.location.hash + '"]').addClass('selected');		

	}


}

function SetDebug()
{
	$("div").css('border', '1px solid #FF00FF');
}



