var currItem = 0;
var tabScroll = 1;

$(document).ready(function(){
	
	$("#overlaybodywrapper").height($("body").height());
	
	$("#overlaybodywrapper").click(function(){
		$("#overlaybodywrapper").css("display","none");
	});
	$("#overlay").click(function(){
		$("#overlaybodywrapper").css("display","none");
	});
	$("#overlaywrapper").click(function(){
		$("#overlaybodywrapper").css("display","none");
	});
	
	
// Start Tabs
	
	//reset on new page or tab
	function resetTABS(){
		$('#homeSliderTabs .homeSliderArea .heroIMG ul').css("margin-left", "0");
		$('#homeSliderTabs .homeSliderArea .heroDES ul').css("margin-left", "0");
		$("#homeSliderTabs .homeSliderArea .heroNAV ul li a").removeClass("activeHERO");
		$("#homeSliderTabs .homeSliderArea .heroNAV ul li:first-child a").addClass("activeHERO");
		currItem = 0;
	}
	
	resetTABS();

	// create tabs
    $('#homeSliderTabs, #homeNewsTabs').tabs({
		select: function(event, ui) { 
			resetTABS();
		}
	});


// Toggle Hero Images on click
	
	$('#homeSliderTabs .homeSliderArea .heroNAV ul li a').click(
	function(e){
		e.preventDefault();
		scrollTo(this);
	});

	
	
	$('.newsTab li').click(function(){
		location.href = $(this).find("h3 a").attr("href");
	});
	
	// make latestHeader a link
	$('div.latestHeader').click(function(){
		location.href = $(this).find("h3 a").attr("href");
	});
	
	var s = setTimeout(tabSlider,10000); 
	
	$("#homeSliderTabs").mouseover(function() {
    	tabScroll=0;
    	console.log(tabScroll);
    });
	$("#homeSliderTabs").mouseout(function() {
    	tabScroll=1;
    	console.log(tabScroll);
    });
	
});

function scrollTo(obj){
	$("#homeSliderTabs .homeSliderArea .heroNAV ul li a").removeClass("activeHERO"); 
	
	$(obj).addClass("activeHERO"); 

//	console.log($(obj).parent().index());
	
//	$('#homeSliderTabs .homeSliderArea .heroIMG ul').css("margin-left", $(this).parent().index() * -$("div.heroIMG").width() +"px");
	currItem = $(obj).parent().index();
	scrollAmount = currItem * -$("div.heroIMG ul li a img").width();
	$('#homeSliderTabs .homeSliderArea .heroIMG ul').animate({
		marginLeft: scrollAmount+"px"
	},500,"","easeInOutQuart");
	
	scrollAmount = $(obj).parent().index() * -$("div.heroDES ul li").width();
	$('#homeSliderTabs .homeSliderArea .heroDES ul').animate({
		marginLeft: scrollAmount+"px"
	},500,"","easeInOutQuart");
}

function tabSlider(){
	if(tabScroll==1){
		aItems = $('div.homeSliderArea:visible div.heroNAV li a');
	//	 = $(selectedTab).children("")
		iItems = (aItems.length)-1;
		nextItem = currItem+1;
		if(nextItem>iItems){
			nextItem = 0;
			aTabs = $('#homeSliderTabs ul.sliderTabs li');
			tabobj = $('#homeSliderTabs ul.sliderTabs li.ui-tabs-selected');
			currTab = $(tabobj).index();
			nextTab = currTab+1;
	//		console.log("currTab -"+currTab+":aTabs.length-"+aTabs.length);
			if(nextTab>(aTabs.length-1)){
				nextTab = 0;
			}
			$('#homeSliderTabs').tabs("select",nextTab);
			$("#homeSliderTabs .homeSliderArea .heroNAV ul li:first-child a").addClass("activeHERO");
		}
		scrollTo($(aItems[nextItem]));
		currItem = nextItem;
	}
	var s = setTimeout(tabSlider,5000); 
}

$(function() {
    $(window).focus(function() {
    	tabScroll=1;
    	console.log(tabScroll);
    });

    $(window).blur(function() {
    	tabScroll=0;
    	console.log(tabScroll);
    });
    
    
});
