/*!
 *
 * iSlideshow v1.1.1 ~ Created by Maros Takac, http://marostakac.co.uk
 *
 */

/*var iSlideshow = (function($)
{
	
	function iSlideshow(el, options)
	{
		// default options				
		this.options  = 
		{
			intervalTime : 3000,
			fadeInTime :  2000,
			fadeOutTime : 2000,
			easing : 'swing',
			slideElement : 'img',
			selectedIndex : 0,
			goRound : false 
		}
		
		// user defined options
		for(var item in options) this.options[item] = options[item];
		
		// store a reference to the wrapper 
		this.target = typeof el == 'string' ? $(el) : el;
		
		// find slides to be animated through
		this.slides = this.target.find(this.options.slideElement);
		this.slides.eq(this.options.selectedIndex).css({'z-index': 2}).siblings().css({'opacity': 0, 'z-index': 1})							
	}
	
	iSlideshow.prototype = 
	{
		target : null,
		slides : {},
		selectedIndex : 0,
		__interval : 0,
		__isPaused : false,
		
		goToPage : function(index)
		{			
			if(this.selectedIndex == index){ return false;}
			
			var that = this;
									
			clearTimeout(this.__interval);									
		
			if(index == null)
			{	
				index = (this.selectedIndex + 1) % this.slides.length;
						
			} else if(index < 0){
				
				index = 0;
							
			} else if(index > this.slides.length){
				
				index = (this.slides.length - 1)
			}
				
			this.slides.eq(this.selectedIndex).css('z-index', 1).stop().animate({opacity: 0}, this.options.fadeOutTime, this.options.easing)
			
			this.slides.eq(index).css({'z-index': 2, 'opacity': 0})
				.stop()
				.animate({opacity: 1}, this.options.fadeInTime, this.options.easing, function(){ 
				if(!that.__isPaused)
				{ 
					clearTimeout(that.__interval);
					that.__interval = setTimeout(function(){ that.next()}, that.options.intervalTime);			
				}});
			
			this.selectedIndex = index;	
		},
		
		next : function()
		{
			if(this.selectedIndex < (this.slides.length - 1))
			{ 
				this.goToPage(this.selectedIndex + 1)
			} else {
				if(this.options.goRound) this.goToPage(0);
			}
		},
		
		prev : function()
		{
			if(this.selectedIndex > 0)
			{ 
				this.goToPage(this.selectedIndex - 1)
			} else {
				if(this.options.goRound) this.goToPage((this.slides.length - 1));
			}
		},
		
		pause : function()
		{
			if(this.__isPaused == false)
			{
				this.__isPaused = true;				
				
			} else {
				
				this.__isPaused = false;
				this.next();							
			}
		},
		
		isPaused : function()
		{
			return this.__isPaused;
		},
		
		getCurrentSlide : function()
		{
			return this.slides.eq(this.selectedIndex);	
		},
		
		getSlides : function()
		{
			return this.slides;	
		}
	}
	
	return iSlideshow;
		
})(jQuery);*/



function Slideshow(target, options)
{	
	  if(!target || !options) return false;
	
	  if(typeof target == 'string')
	  {
		  this.slideshowArea =  $(target);
		  
	  } else if(typeof target == 'object' && target.length > 0)
	  {
		  this.slideshowArea =  target;		
	  }	
	  	
	this.slides        =  options.element ? this.slideshowArea.find(options.element) : this.slideshowArea.find('img');
	
	this.actualSlide   =  (options.actualSlide >= 0 && options.actualSlide < this.slides.length && options.actualSlide != null) ? options.actualSlide : 0;
	this.timeInterval  =  options.interval || 3000;  
	this.fadeIn        =  options.fadeIn || 2000;
	this.fadeOut	   =  options.fadeOut || 3000 
	this.autoRun	   =  options.autoRun == null ? true : options.autoRun ;
	this.intervalId    =  0;
	this.easing = options.animation || 'swing';
		
	this.slides.eq(this.actualSlide).css('z-index', 2).siblings().css({'opacity': 0, 'z-index': 1}) 	
}	

Slideshow.prototype.run = function()
{				
	var scope = this;
	
	if(this.slides.length > 0) this.intervalId = setTimeout(function(){ scope.slide()}, this.timeInterval);	
}

Slideshow.prototype.next = function(b)
{
	if(this.actualSlide < (this.slides.length - 1))
	{ 
		this.slide(this.actualSlide + 1)
	} else {
		if(b) this.slide(0);
	}
}

Slideshow.prototype.prev = function(b)
{
	if(this.actualSlide > 0)
	{ 
		this.slide(this.actualSlide - 1)
	} else {
		if(b) this.slide((this.slides.length - 1));
	}
}

Slideshow.prototype.pause = function()
{				
	this.autoRun = !this.autoRun;	
}

Slideshow.prototype.slide = function(slideNumber)
{	
	if(this.actualSlide == slideNumber){ return false;}
	
	clearInterval(this.intervalId);
	var scope = this;

	if(slideNumber == null)
	{	
		slideNumber = 	(this.actualSlide + 1) % this.slides.length;
				
	} else if(slideNumber < 0){
		
		slideNumber = 0;
					
	} else if(slideNumber >  this.slides.length){
		
		slideNumber = (this.slides.length - 1)
	}
		
	this.slides.eq(this.actualSlide).css('z-index', 1).stop(true).animate({opacity: 0}, this.fadeOut, this.easing)
	
	this.slides.eq(slideNumber).css({'z-index': 2, 'opacity': 0}).stop(true).animate({opacity: 1}, this.fadeIn, this.easing, function(){ 
			if(scope.autoRun)
			{ 
				clearInterval(scope.intervalId);
				scope.intervalId = setTimeout(function(){ scope.slide()}, scope.timeInterval);			
			}});
	
	this.actualSlide = slideNumber;	
}

// GETTERS

Slideshow.prototype.selectedItem = function()
{
	return $(this.slides.eq(this.actualSlide));
}

Slideshow.prototype.isPaused = function()
{
	return !this.autoRun;
}

Slideshow.prototype.children = function()
{	
	return this.slides;		
}

