/*
*	Shawn Lawson - http://www.shawnlawson.com
*
*	cc by-nc
*	Creative Commons License: Attribution, NonCommercial
*/

//forward to crudeoils if necessary.
if (location.hostname == "www.crudeoils.us" || location.hostname == "crudeoils.us" ){
	window.location.assign("/site/index.html");
}

//fix scroll pane
function scrollPaneHelper() {
	$('.scroll-pane').each(function() {
		$(this).jScrollPane({
			scrollbarWidth : 10,
			hideFocus : true,
			autoReinitialise : true
		});
	});
}

//rebuild colorbox
function colorBoxHelper() {
	$("a[rel='colorBoxMe']").colorbox({
		onClosed : function() {
			scrollPaneHelper()
		},
		returnFocus : false,
		current : ""
	});
}

$(document).ready(function() {
	//build menu system
	$("ul.sf-menu").superfish({
		delay : 400, 
		animation : {
			opacity : 'show',
			height : 'show'
		}, // fade-in and slide-down animation
		speed : 'fast',                          // faster animation speed
	}).click(function() {
		$(this).find('li.sfHover').hideSuperfishUl();
	});
	
	//set up menu click handler
	$('#menuSystem ul li ul li a.itemLoad').click(function() {
		var location = $(this).attr('href').substr(1);
		var title = $(this).attr('rel')
		$('#content').load('html/' + location, function() {
			scrollPaneHelper();
			colorBoxHelper();
			window.document.title = '~{   ' + title + '   }~'
		});
	});
	
	// detect and load content when #hashtag present
	var tag = window.location.hash;
	var page;
	if(tag) {
		var pageTest = tag.substr(1).toLowerCase();
		if(pageTest.length >= 5) {
			if(".html" != pageTest.substring(pageTest.length - 5, pageTest.length)) {
				page = pageTest+".html";
			}else{
				page = pageTest;
			}
		}else{
			page = pageTest+".html";
		}
	} else {
		page = "news.html";
		window.location = window.location+"#news.html";
	}

	$('#content').load('html/' + page, function() {
		scrollPaneHelper();
		colorBoxHelper();
		var pageShort = page.substring(0, page.length - 5);
		var title = $("a[href='#" + page + "']").attr('rel');
		window.document.title = '~{   ' + title + '   }~'
	});
});

