/* !loop function */

function Loop(index, maxIndex)
{
    
    // public
    
    this.next = function(set) // set default true
    {
        if(this.isOk())
        {
            var set = (set == false)? false : true;
            var nextIndex = null;
            
            if(this.direction == 1)
            {
                // incremental
                nextIndex = ((this.index + 1) > this.maxIndex)? 0 : this.index + 1;
            }
            else
            {
                // decremental
                nextIndex = ((this.index - 1) < 0)? this.maxIndex : this.index - 1;
            }
            
            if(set)
            {
                this.index = nextIndex;
            }
            return nextIndex;
        }
        return false;
    }
    
    this.last = function()
    {
        if(this.isOk())
        {
            if(this.direction == 1)
            {
                // incremental
                lastIndex = ((this.index - 1) < 0)? this.maxIndex : this.index - 1;
            }
            else
            {
                // decremental
                lastIndex = ((this.index + 1) > this.maxIndex)? 0 : this.index + 1;
            }
            return lastIndex;
        }
        return false;
    };
    
    // private
    
    this.isOk = function()
    {
        if((isFinite(this.index) && isFinite(this.maxIndex)) && (this.index <= this.maxIndex))
        {
            return true;
        }
        
        return false;
    }
    
    this.setInt = function(value)
    {
        value = parseInt(value);
        if(isNaN(value))
        {
            return undefined;
        }
        return value;
    }
    
    // vars
    
    this.index = this.setInt(index);
    this.maxIndex = this.setInt(maxIndex);
    this.direction = 1; // 0: decremental, 1: incremental
    
}

/* !slideshow */

var slideshow = {

	elements 	: {},
	vars		: {},
	
	init		: function()
	{
		if(document.getElementById('slider'))
		{
			this.elements.images 	= $("#slider .slider-image");
			this.elements.boxes		= $("#slider .slider-box-content");
			
			if(this.elements.images.length === this.elements.boxes.length)
			{
				this.vars.totSteps 	= this.elements.images.length;
				this.vars.loop		= new Loop(0, this.vars.totSteps - 1);
				this.vars.index		= 0;
				this.vars.stop		= false;
				this.setTimeout();
				
				for(var i = 0; i < this.elements.images.length; i++)
				{
					var strClass = (i == 0)? 'slider-step sel' : 'slider-step';
					$("#slider-step-container").append('<a href="javascript:void(0)" id="slider-step-'+ i +'" class="'+ strClass +'"></a>');
				}
				
			}
			
			$("#slider-step-container a").click(function(){
				var index = parseInt(this.id.replace(/slider-step-/, ''));
				clearTimeout(slideshow.vars.timeout);
				slideshow.animate(index);
			});
			
		}
	},
	
	animate		: function(index)
	{
		
		if(typeof(index) === "number")
		{
			if(index === slideshow.vars.loop.index)
			{
				this.vars.stop = true;
				slideshow.setTimeout();
			}
		}
		
		if(!this.vars.stop)
		{
			this.vars.stop		= true;
			var nextIndex		= (index >= 0)? index : this.vars.loop.next(false);
			var currentIndex 	= slideshow.vars.loop.index;
			
			$("#slider-step-"+currentIndex).removeClass("sel");
			$("#slider-step-"+nextIndex).addClass("sel");
			
			$(slideshow.elements.images[currentIndex]).css({
				"z-index"	: 0
			});
			$(slideshow.elements.boxes[currentIndex]).css({
				"z-index"	: 0
			});
			$(slideshow.elements.images[nextIndex]).css({
				"position" 	: "absolute",
				"top"		: 0,
				"left"		: 0,
				"opacity"	: 0,
				"z-index"	: 1
			});
			$(slideshow.elements.boxes[nextIndex]).css({
				"position" 	: "absolute",
				"top"		: 0,
				"left"		: 0,
				"opacity"	: 0,
				"z-index"	: 1
			});
			$(slideshow.elements.images[nextIndex]).animate({
				"opacity"	: 1
			},{
				duration : 500,
				easing : "swing",
				complete : function()
				{
					$(slideshow.elements.images[currentIndex]).css({
						"opacity" : 0
					});
				}
			});
			$(slideshow.elements.boxes[nextIndex]).animate({
				"opacity"	: 1
			},{
				duration : 500,
				easing : "swing"
			});
			$(slideshow.elements.boxes[currentIndex]).animate({
				"opacity"	: 0
			},{
				duration : 500,
				easing : "swing",
				complete : function()
				{
					slideshow.vars.loop.index = nextIndex;
					slideshow.setTimeout();
					slideshow.vars.stop = false;
				}
			});
		};
		
		slideshow.vars.stop = false;
		
	},
	
	setTimeout	: function()
	{
		this.vars.timeout = setTimeout(function(){slideshow.animate()}, 12000);
	}

}

var page = {
	
	vars : {},
	elements : {},
	
	init : function()
	{
		
		// search
		
		this.vars.search_default_values = ['cerca nel sito', 'search', 'pesquisa'];
		
		$("#field").click(function(){
			var value = this.value.toLowerCase();
			if(page.vars.search_default_values.indexOf(value) >= 0)
			{
				this.value = "";
			}
		});
		
	}

}

/* !events */

$(document).ready(function(){
	
	// navmenu
	
	$('#navMenu').navMenu();
	
	// preload
	
	$("img.preload").luglio7ImagePreload();
	
	// shadowbox
	
	if(Shadowbox)
	{
		Shadowbox.init();
	}
	
	slideshow.init();
	page.init();
	
});
