jQuery(function(){
	slider();
	reSlide();
	startTimer();
	theRotator();
	activeMenu();
});
	
function activeMenu(){

		
		jQuery(".cat-post-item, .cat-item").each(function () {
												
		url = window.location;
		current = jQuery("a",this).attr("href");	
		
        if (current == url) {
          jQuery("a",this).addClass("active");
        } 
		});

}
	
var myTimer;
var timerbis;

function startTimer() {
	if (maxLimit >0){
    myTimer = window.setInterval( function() {
        maxLimit=jQuery('.page-home .single-slide').size()-1;
		if(i<maxLimit) {i = i+1} else {i=0}
		callSlide(i);
    }, 4000);
	}
};

function stopTimer(){
    window.clearInterval(myTimer);
	clearTimeout(timerbis);
}


	
var i = 0;
function slider(){
		
	/* init */
	
	
	maxLimit=jQuery('.single-slide').size()-1;
	jQuery('.single-slide').hide();
	jQuery('.single-slide').eq(0).show();
	
	if (maxLimit >0){
		/* apro solo la prima promo*/
		
		
		
		/* costruisco il counter a pallini */
		
		jQuery.each(jQuery('.single-slide'), function(i, val) {
		  jQuery('.spots ul').append('<li><a href="javascript:void(0)"></a></li>');
		});

		jQuery('.spots li a:eq(0)').addClass('active');
		
		
		
		/*costruisco le azioni delle frecce */
		jQuery('#homeSlider .nav-arrows a.next').click(function(){
			stopTimer();
			timerbis = setTimeout( "startTimer()", 4000);
			if(i<maxLimit) {i = i+1} else {i=0}
			callSlide(i);
		});
		
		jQuery('#homeSlider .nav-arrows a.prev').click(function(){
			stopTimer();
			timerbis = setTimeout( "startTimer()", 4000);
			if(i>0) {i = i-1} else {i=maxLimit}
			callSlide(i);
		});
		}
}



function callSlide(i){
	jQuery('.single-slide').hide();
	jQuery('.single-slide:eq('+i+')').fadeIn();
	jQuery('.spots li a').removeClass('active');
	jQuery('.spots li a:eq('+i+')').addClass('active');
}

function reSlide(){
	jQuery('.spots a').click(function(){
		stopTimer();
		timerbis = setTimeout( "startTimer()", 7000);
		
		var indiceLi = jQuery(this).parent().index();
		
		i = indiceLi;
		jQuery('.spots a').removeClass("active");
		jQuery(this).addClass("active");
		jQuery('.single-slide').hide();
		jQuery('.single-slide').eq(indiceLi).fadeIn();
	});
}


/*function footerRes(){
	var dh = jQuery("#content").height();
	if (dh < 300) {
		jQuery("#footer").css("position","absolute");
		jQuery("#footer").css("bottom","0");
		}
	else {
		jQuery("#footer").css("position","static");
		}
}*/
	

function theRotator() {
	//Set the opacity of all images to 0
	jQuery('#internal-slider .main-img').eq(0).addClass("show");
	jQuery('#internal-slider .main-img').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	jQuery('#internal-slider .main-img:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',8000);
	
}

function rotate() {	
	//Get the first image
	var current = (jQuery('#internal-slider .main-img.show')?  jQuery('#internal-slider .main-img.show') : jQuery('#internal-slider .main-img:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? jQuery('#internal-slider .main-img:first') :current.next()) : jQuery('#internal-slider .main-img:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000).removeClass('show');
	
};
