
$(window).load(function() {
	var fadeInDelay;
	var fadeOutDelay;
	var slideNum=1;
	var slideLen=$('.home-slide-images').length;
	
	function fadeSlideOut(){
		//Reset after last slide
		$("#home-slide-images-"+ slideNum).fadeOut();
		$(".home-slide-selector-img").hide();
		slideNum++;	
		if (slideNum == slideLen + 1){
			slideNum=1;
		}
		$("#home-slide-images-selector-" + slideNum).fadeIn();
		fadeInDelay = setTimeout(fadeSlides,1000);
	}
	
	function fadeSlides(){
		//Reset after last slide
		$("#home-slide-images-"+ slideNum).fadeIn(800);
		fadeOutDelay = setTimeout(fadeSlideOut,4000);
	}
		
	function initClientSlides()
	{   
		var zIndex = 1;
		activeSlide = 1;
		   
		$('.home-slide-images').each(function()
		{
			zIndex = $('.home-slide-images').index(this) + 1;
			$(this).css({ 'z-index' : zIndex });           
			$(this).attr({ 'id' : 'home-slide-images-' + zIndex });
			$('#home-slides-controller').prepend('<div class="home-slide-selector" id="home-slide-selector-' + zIndex + '"><img src="images/global/selector-active.png" alt="&bull;" class="home-slide-selector-img" id="home-slide-images-selector-' + zIndex + '" /></div>');
		});
	   
		$('#home-slide-images-selector-1').fadeIn(1200);
		fadeSlides();   
	}

	initClientSlides();
	
	$(".home-slide-selector").click(function(){
		$(".home-slide-images,.home-slide-selector-img").hide();
		var newSlide =  $(this).attr('id').replace('home-slide-selector-','');
		slideNum = newSlide;
		clearTimeout(fadeInDelay);
		clearTimeout(fadeOutDelay);
		$("#home-slide-images-selector-" + slideNum).show();
		fadeSlides();
		
	});
	$('.go-to-link-external').css('cursor','pointer').click(function(){
		var linkURL = $(this).attr('alt');
		window.open(linkURL, '_blank');
	});
	$('.go-to-link-internal').css('cursor','pointer').click(function(){
		var linkURL = $(this).attr('alt');
		window.open(linkURL, '_self');
	});
		
});

