/* !loop */

/*  Loop
	requires: nothing
	manage a loop index, useful for sliders
*/

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
    
}

/* !fading slider */

/*  fadingSlider
	requires: Loopè
	manage a simple fading slider
*/

function fadingSlider(selector, params)
{
	
	var self = this;
	this.vars = {
		selector : selector,
		delay : (params.delay)? params.delay : 5000,
		duration : (params.duration)? params.duration : 1000
	};
	this.elements = {};
	
	// animate 
	
	this.animate = function(){
		var currentIndex = this.vars.loop.index;
		var nextIndex = this.vars.loop.next(false);
		
		$(this.elements.images[currentIndex]).css({
			"z-index" : 1
		});
		$(this.elements.images[nextIndex]).css({
			"z-index" : 2,
			"opacity" : 0
		});
		$(this.elements.images[nextIndex]).animate({
			"opacity" : 1
		}, {
			"easing" : "swing",
			"duration" : this.vars.duration,
			"complete" : function(){
				$(self.elements.images[self.vars.loop.index]).css({"z-index": 0, "opacity" : 0});
				$(self.elements.images[self.vars.loop.next(false)]).css("z-index", 1);
				self.vars.loop.next();
				self.setTimeout();
			}
		});
	};
	
	// set timeout
	
	this.setTimeout = function(){
		this.vars.timeout = setTimeout(function(){
			self.animate()
		}, self.vars.delay);
	};
	
	// init 
	
	(function(self, selector){
		self.elements.container = $(selector);
		if(self.elements.container[0])
		{
			self.elements.images = $(selector + " img");
			if(self.elements.images.length > 1)
			{
				self.vars.loop = new Loop(0, self.elements.images.length - 1);
				$(self.elements.images[self.vars.loop.index]).css("z-index", 1);
				self.setTimeout(self);
			}
		}
	})(self, selector);
	
}

/* !init */


$(document).ready(function(){
	if(document.getElementById("slider"))
	{
		fadingSlider = new fadingSlider("#slider", {duration: 2000, delay: 5000});
	}
});

function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) 
	{
		return false;		
	}
	return true;
}

