﻿//Adapted from
//www.ilovecolors.com.ar/rotating-jquery-tabs/

//array to store IDs of our tabs
var tabs = [];
//index for array
var ind = 0;
//store setInterval reference
var inter;

//change tab and highlight current tab title
function change(stringref){
	//hide the other tabs
	jQuery('.tab:not(#' + stringref + ')').hide();
	//show proper tab, catch IE6 bug
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0")
		jQuery('.tab#' + stringref).show();
	else 
		jQuery('.tab#' + stringref).fadeIn(1000);
		
	// test

	//clear highlight from previous tab title
    jQuery('#slidenav h3').removeClass('selectParent');
    jQuery('#slidenav a:not(#' + stringref + 't)').removeClass('select');
	//highlight currenttab title
    jQuery('#slidenav a[href=#' + stringref + ']').addClass('select');
    jQuery('#slidenav a[href=#' + stringref + ']').parent().parent().prev().addClass('selectParent');
}
function next(){
	//call change to display next tab
	change(tabs[ind++]);
	//if it's the last tab, clear the index
	if(ind >= tabs.length)
		ind = 0;
}

function startslide(){
	//store all tabs in array
	jQuery(".tab").map(function(){
		tabs[ind++] = jQuery(this).attr("id");
    })
	//set index to next element to fade
	ind = 1;
	//initialize tabs, display the current tab
	jQuery(".tab:not(:first)").hide();
	jQuery(".tab:first").show();
	//highlight the current tab title
	jQuery('#' + tabs[0] + 't').addClass('select');
	//handler for clicking on tabs
	jQuery("#slidenav a").click(function() {
		//if tab is clicked, stop rotating 
		clearInterval(inter);
		//store reference to clicked tab
		stringref = jQuery(this).attr("href").split('#')[1];
		//display referenced tab
		change(stringref);
//		inter=setInterval("startslide()",10000);
		return false;
	});
	//handler for hovering over tabs
	jQuery("#slidenav a").mouseenter(function() {
	    //if tab is entered, stop rotating 
	    clearInterval(inter);
	    //store reference to hovered tab
	    stringref = jQuery(this).attr("href").split('#')[1];
	    //display referenced tab
	    change(stringref);
	    return false;
	});
	//handler for hovering over slides
	jQuery("#mainslide").mouseenter(function() {
	    //if slide is entered, stop rotating 
	    clearInterval(inter);
	    return false;
	});
	//	//handler for restarting slideshow when leaving hover
	jQuery("#slideshow").mouseleave(function() {
	    ind = 0;
	    clearInterval(inter);
	    inter = setInterval("next()", 7500);
	    return false;
	});
	//	jQuery(".htabs a").mouseleave(function() {
//	    //if tab is clicked, stop rotating 
//	    clearInterval(inter);
//	    //store reference to clicked tab
//	    stringref = jQuery(this).attr("href").split('#')[1];
//	    //display referenced tab
//	    change(stringref);
//	    inter = setInterval("startslide()", 10000);
//	    return false;
//	});


	//start rotating tabs
	clearInterval(inter);
	inter = setInterval("next()", 7500);
}

jQuery(document).ready(function(){
	startslide();
});
