function currentPage() {
	var sPath = window.location.href;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
};
function ifSamePage(current, clicked) {
	var currentPageIndex = current.indexOf('#section');
	var clickedPageIndex = clicked.indexOf('#section');
	var currentPage = current;
	if (currentPageIndex > -1) {
		currentPage = current.substring(0, currentPageIndex);
	}
	var clickedPage = clicked;
	if (clickedPageIndex > -1) {
		clickedPage = clicked.substring(0, clickedPageIndex);
	}
	if (currentPage == clickedPage) {
		return true;
	} else {
		return false;
	}
};
function repositionOfPage(page) {
	var idIndex = page.indexOf('#');
	var target = '';
	if (idIndex > -1) {
		target = page.substring(idIndex);
	}
	if (target !== '') {
		//we have an anchor
		$(target).scroll();
		//var yPos = getScrollY(target);
		//alert(sc);
		//$("html:not(:animated),body:not(:animated)").animate({ scrollTop: yPos}, 500 );
		var position = $(target).position();
		//alert(parseInt(position.top));
		var anchorPos = "" + parseInt(position.top) + "px";
		$('html, body').animate({scrollTop: anchorPos}, 0); 
		//alert(anchorPos);
	}

};
function getScrollY(target) {
  return $(target).scrollTop();
}
$(document).ready(function() {
	$('html, body').animate({scrollTop: 0}, 0);
	$('#overlay').hide();
	$( "#accordion" ).accordion({ disabled: false, header: 'h2', autoHeight: false, collapsible: true, navigation: true/*, active:true */});
	$( "#accordion0" ).accordion({ disabled: false, header: 'h2', autoHeight: false, collapsible: true, navigation: true, active:false });
	$( "#accordion1" ).accordion({ disabled: false, header: 'h2', autoHeight: false, collapsible: true, navigation: true, active:false });
	$(".nav a").mouseover( function () {
		if ($(this).parent().hasClass('withsub')){
			$(".subnav").removeClass('visible');
			$(this).parent().children('div.subnav').addClass('visible');
			$(".nav a").removeClass('hover');
			$(this).addClass('hover');
		}
	});
	$(".nav").mouseleave(function (e) {
		$(".subnav").removeClass('visible');
		$(".nav a").removeClass('hover');
	});
	$('.subnav a').click(function() {
		var subHref = $(this).attr('href');
		var idIndex = subHref.indexOf('#section');
		if ((idIndex > -1) && (ifSamePage(currentPage(), subHref))) {
			var activeTabNum = parseInt(subHref.substring(idIndex+8), 10) - 1;
			if (activeTabNum > 100) {
				activeTabNum = activeTabNum - 110;
				$( "#accordion1" ).accordion( "activate" , activeTabNum );
			} else {
				$( "#accordion" ).accordion( "activate" , activeTabNum );
				$( "#accordion0" ).accordion( "activate" , activeTabNum );
			}
			/*repositionOfPage(subHref);*/
			return false;
		} else {
			return true;
		} 
		
	}); 
});
$(window).load(function () {
	/*repositionOfPage(currentPage());*/
});

