$(document).ready(function() {
	//custom easing
	$.ui.accordion.animations.eoe = function(settings) {
	   this.slide(settings, {
	     easing: "easeOutExpo",
	     duration: 1000
	   });
	 }
	
	//URL in the address bar
	$("#accordion a, #copyright_text a, #tabs > ul > li > a").livequery(function(){
	  $(this).click(function(event) {
	  	window.location.hash=this.hash;
	  });
	});
	 
	//get the URL for bookmarking
	var pathname  = window.location.href.split("#")[1];
	if (pathname!=undefined){
		var sub_path = pathname.split("-")[0] + "-";
			
		if (sub_path== sub_path.split("-")[0] + '-') {
			var contentDiv  = $('#gallery' + pathname.split("-")[0]);
			var destination = "index.php/welcome/content/" + pathname.split("-")[0];
		  contentDiv.load(destination);
	  } else {	
			var contentDiv  = $('#' + 'gallery' + pathname);
			var destination = "index.php/welcome/content/" + pathname;// + '#' + 'gallery' + pathname;
		  contentDiv.load(destination);
		};
	
	} else {
		var contentDiv  = $('#' + 'gallery' + pathname);
		var destination = "index.php/welcome/content/" + pathname;//+ '#' + 'gallery' + pathname;
		contentDiv.load(destination);
	};
	
	//Accordion
	if(sub_path=='about-'){
		
		$("#accordion").accordion({ header: "h3", active: 1, navigation: true, animated: 'eoe', collapsible: true });
			
		$("#accordion a, .foot_nav a").click(function(evt) {
			$("#removecontact").slideUp("slow", function() { $(this).remove(); } );
			var a = $(this);
			
			var contentDiv  = $('#' + 'gallery' + a.attr('href').split('#')[1]);
			var destination = "index.php/welcome/content/" + a.attr('href').split('#')[1];
			contentDiv.load(destination);
			//blur accordion head by clicking hidden anchor
			var doIt = function() {
		    $('a#clicky_click').focus();
			}
			setTimeout(doIt, 1050);
		});

	} else if(sub_path=='news-') {
	
		$("#accordion").accordion({ header: "h3", active: 4, navigation: true, animated: 'eoe', collapsible: true });
			
		$("#accordion a, .foot_nav a").click(function(evt) {
			$("#removecontact").slideUp("slow", function() { $(this).remove(); } );
			var a = $(this);
			
			var contentDiv  = $('#' + 'gallery' + a.attr('href').split('#')[1]);
			var destination = "index.php/welcome/content/" + a.attr('href').split('#')[1];
			contentDiv.load(destination);
			//blur accordion head by clicking hidden anchor
			var doIt = function() {
		    $('a#clicky_click').focus();
			}
			setTimeout(doIt, 1050);
		});
		
	} else {
	
		// Accordion
		$("#accordion").accordion({ header: "h3", active: 0, navigation: true, animated: 'eoe', collapsible: true });
		
		$("#accordion a, .foot_nav a").click(function(evt) {
			$("#removecontact").slideUp("slow", function() { $(this).remove(); } );
			var a = $(this);
			
			var contentDiv  = $('#' + 'gallery' + a.attr('href').split('#')[1]);
			var destination = "index.php/welcome/content/" + a.attr('href').split('#')[1];// + '#gallery' + a.attr('href').split('#')[1];
			contentDiv.load(destination);
			//blur accordion head by clicking hidden anchor
			var doIt = function() {
		    $('a#clicky_click').focus();
			}
			setTimeout(doIt, 1050);
		});	
	
	}
	
	//ajax links
	$(".ajax_link").livequery(function(){
		$(this).click(function(evt) {
			$("#removecontact").slideUp("slow", function() { $(this).remove(); } );
			var a = $(this);
			var contentDiv = $('#add_ajax_content_here');
			var destination = "index.php/welcome/content/" + a.attr('href').split('#')[1] + ' #removecontact';
			contentDiv.load(destination);
		});
	});
	
	$("ul#sitemap_list li a").livequery(function(){
		$(this).click(function(evt) {
			$("#removecontact").slideUp("slow", function() { $(this).remove(); } );
			var a = $(this);
			var contentDiv  = $('#' + 'gallery' + a.attr('href').split('#')[1]);
			var destination = "index.php/welcome/content/" + a.attr('href').split('#')[1];// + '#gallery' + a.attr('href').split('#')[1];
			//alert (destination);
			contentDiv.load(destination);
		});
	});
	
	// AJAX tabs
	$('#tabs').livequery(function(){													 
		$(this).tabs();
	});
	
	$('#sub_tabs').livequery(function(){													 
		$(this).tabs();
	});
		
	// Scroll pane
	$('.scroll-pane').livequery(function(){
		$(this).jScrollPane();
		reinitialiseScrollPane = function()
		{
			$(this).jScrollPane();
		}
		
		//reinitialize the correct scroll pane based on the current URL
		$("#accordion a, #copyright_text a, #tabs > ul > li > a").bind(
			'click',
			function()
			{
				var reint = window.location.hash=this.hash;
				var parentp = reint.split("#")[1].split("-")[0];
				//alert("index.php/welcome/content/about #reloadme-" + reint.split("#")[1]);
				$('.scroll-pane').load("index.php/welcome/content/" + parentp + " " + "#reloadme-" + reint.split("#")[1], '', reinitialiseScrollPane);
			}
		);
		
	});
});
