var slideSpeed = 10; // Number of Seconds between slide auto-rotation
var slidetotal = 6; // Total number of slides
var slideSpeedAfterClick = 30; // Number of Seconds between slide auto-rotation after manual slide selection


var slidewidth;
var currentslide = 0;
var autoSwitch;
var autoWait;
var autoSwitching = true;
$(document).ready(function() {
	
	$('a[href*="http"]').attr('target','_blank');
	
	
	slidewidth = $('.js_slide').width();
	$('#slides').width(slidewidth*slidetotal);
	$(function(){
		if(autoSwitching) {
			autoSwitch = setInterval( "autotab()", slideSpeed*1000 /* converting to milleseconds */ );
		}
	})
	$('#welcome_nav a').click(function(event) {
		event.preventDefault();
		switchTab('#welcome_slide',0);
		$(this).parent().addClass('active');
		changeTimer();
	})
	$('#industries_nav a').click(function(event) {
		event.preventDefault();
		switchTab('#industries_slide',1);
		$(this).parent().addClass('active');
		changeTimer();
	})
	$('#training_nav a').click(function(event) {
		event.preventDefault();
		switchTab('#training_slide',2);
		$(this).parent().addClass('active');
		changeTimer();
	})
	$('#services_nav a').click(function(event) {
		event.preventDefault();
		switchTab('#services_slide',3);
		$(this).parent().addClass('active');
		changeTimer();
	})
	$('#careers_nav_2 a').click(function(event) {
		event.preventDefault();
		switchTab('#careers_slide',4);
		$(this).parent().addClass('active');
		changeTimer();
	})
	$('#innovations_nav a').click(function(event) {
		event.preventDefault();
		switchTab('#innovations_slide',5);
		$(this).parent().addClass('active');
		changeTimer();
	})


// Homepage location switcher
    $("#locationselect .button").hide();
    $("#locationselect select").change(function() {
        window.location.href = 'locations.php#'+$("#locationselect option:selected").val();
    })

// Locations page switchers
	$('#locations #locationslist p').hide();
	$("#stateselect").change(function() {
		$('#locations #locationslist h4').removeClass('active').next('p').slideUp('fast');
		var locationselect = $("#stateselect option:selected").val();
        window.location.hash = locationselect;
		$('h4#'+locationselect).addClass('active').next('p').slideDown('fast');
    })
	$('#locations #locationslist h4').toggle(function(){
		$(this).addClass('active').next('p').slideDown('fast');
	}, function() {
		$(this).removeClass('active').next('p').slideUp('fast');
	}).addClass('hasjs');
	$('#map_sub a').click(function(){
		$('#locations #locationslist h4').removeClass('active').next('p').slideUp('fast');
		$('#stateselect option').removeAttr('selected');
		var locationselect = $(this).attr('href').replace('#','');
		$('#stateselect option.'+locationselect).attr('selected','selected');
		$('h4#'+locationselect).addClass('active').next('p').slideDown('fast');
	})
	checkHash();
	
//services page
	$('#services #sub_content p').hide();
	$('#services #sub_content h3').addClass('hasjs').toggle(function(){
		$(this).addClass('active').next('p').slideDown('fast');
	}, function(){
		$(this).removeClass('active').next('p').slideUp('fast');	
	});
	$('#industries #sub_content div').hide();
	$('#industries #sub_content h3').addClass('hasjs').toggle(function(){
		$(this).addClass('active').next('div').slideDown('fast');
	}, function(){
		$(this).removeClass('active').next('div').slideUp('fast');	
	});
	$('#innovations #sub_content div').hide();
	$('#innovations #sub_content h3').addClass('hasjs').toggle(function(){
		$(this).addClass('active').next('div').slideDown('fast');
	}, function(){
		$(this).removeClass('active').next('div').slideUp('fast');	
	});
	
//	login popup
	var login_open=false;
	function define_close(){
		$('#close_popup').click(function(e){
			$('#login_popup').remove();
			login_open=false;
			e.preventDefault();
		})
	}
	$("#login_nav a").click(function(e){
		e.preventDefault();
		if(!login_open){
			login_open=true;
			$("body").append('<div id="login_popup"><div id="bg_div"><div id="relative"><div><a href="http://private.hoistcrane.com/apps">Member Login</a><a href="#" id="emp_login">Employee Login</a></div><a href="#" id="close_popup">Close</a></div></div></div>');
			define_close();
			$('#login_popup').click(function(e){
				
			})
			$('#emp_login').click(function(e){
				e.preventDefault();
				$('#login_popup #relative').html('<div><a href="https://hoistcrane.megameeting.com/">Web Conferencing</a><a href="http://private.hoistcrane.com/intranet">Corporate Intranet</a><a href="http://po.hoistcrane.com/exchange">Corporate WebMail</a></div><a href="#" id="close_popup">Close</a>');
				define_close();
			})
		}
	})
	
}) // close document ready
function checkHash() {
	if($('body').attr('id')=='locations') {
		var locationselect = window.location.hash.replace('#','');
		if(locationselect!='') {
			$('h4#'+locationselect).addClass('active').next('p').show();
			$('#stateselect option.'+locationselect).attr('selected','selected');
		}
	}
}
function changeTimer() {
	autoSwitching = false;
	clearInterval(autoSwitch);
	clearTimeout(autoWait);
	
}
function switchTab(slide, number) {
	currentslide = number;
	$('#js_nav .active').removeClass('active');
	if(slide=="auto") {
		$('#second_nav li:nth-child('+(currentslide+1)+')').addClass('active');
	}
	$('#slides').animate({
		left: -slidewidth*number
	}, 1000);
}
function autotab(){
	if (!autoSwitching) {
		autoSwitch = setInterval( "autotab()", slideSpeed*1000 /* converting to milleseconds */ );
		autoSwitching = true;
	}
	currentslide++;
	if (currentslide >= slidetotal) {
		currentslide = 0;
	}
	switchTab("auto",currentslide);
}
