// JavaScript Document
(function($) {   // Compliant with jquery.noConflict()
$.fn.slideshow = function(options) {
    var defaults = $.extend({
      	startItem: 0,
		duration: 10,
		interval: 1000,	
		but_prev: '.slide-prev',
		but_next: '.slide-next',
		but_play: '.slide-play',
		but_stop: '.slide-stop',
		but_playback: '.slide-playback',
		overlap: 0,
		navigation: '',
		animation: 'fade',
		autoPlay: false,
		items: '.slide-item',
		thumbs_handles: '.slide-thumbs-handles'	
    }, options || {});
	
	var options = $.extend(defaults, options); 
	
	// get all elements
	var items = $($(this).find(options.items));
	var thumbs_handles =  $($(this).find(options.thumbs_handles));	
	
	// reset vars
	var timeOutFn = null;
	var timeAfterClick = null;
	var timeStart = null;
	var timeCurrent = null;	
	var inc = 0;
	var currentItem = null;
	var lastNo = options.startItem;
	var currNo = options.startItem;
	var isClick = false;	
	var next = true;
	var typePlay = 'next';
	var conWidth = options.overlap ? '100%' :  options.mainWidth;
	
	// start anim from selected item
	currentItem = options.startItem ? items[options.startItem] : null;

	// display first item
	$(items[options.startItem]).css('display', 'block');
	
	var fadeElement = function() {
		 if (options.autoPlay == true) {
			if(items.length > 1) {			
				timeOutFn = setInterval(makeAnim, options.duration);				
			} else {
				if (items.length=1) makeAnim(); 
			}
		}
	}
	
	// thumbs	
	$(thumbs_handles).find('span').each(function(k) {
		$(this).click (function (){	
			if (currNo != k ) {
				next = false;
				clearInterval(timeOutFn);
				click(k);
			} else {
				return false;	
			}
		});	

		/*$(this).hover (function() {  
			$(this).css("background-image","hover.png"); 
		}, function() {  
			$(this).css("background-image","normal.png");  
		});*/  
	 });
	
	// prev
	if ($($(this).find(options.but_prev))) {
		$($(this).find(options.but_prev)).click (function() {
				prev();
		});
	}
	
	// next
	if ($($(this).find(options.but_next))) {
		$($(this).find(options.but_next)).click (function() {
				next();
		});
	}
	
	// play	
	if ($($(this).find(options.but_play))) {
		$($(this).find(options.but_play)).click (function() {
				play();
		});
	}
	
	// playback
	if ($($(this).find(options.but_playback))) {
		$($(this).find(options.but_playback)).click (function() {				
				playback();
		});
	}
	
	// pause
	if ($($(this).find(options.but_stop))) {
		$($(this).find(options.but_stop)).click (function() {
				pause();
		});
	}
	
	// next
	var next = function () {
			clearInterval(timeOutFn);			
			currNo      = currNo + 1;						
			if (currNo > (items.length - 1)) {	
				currNo = 0;				
			}			
			next = false;			
			click(currNo);
	}
	
	// prev
	var prev = function () {		
			clearInterval(timeOutFn);			
			currNo  =  currNo - 1;			
			if (currNo < 0) {
				currNo = items.length - 1;				
			} 			
			next = false;			
			click(currNo);
	}
	
	// pause
	var pause = function () {
		options.autoPlay = false;
		clearInterval(timeOutFn);
	}
	
	// play
	var play = function () {
		options.autoPlay = true;
		typePlay = 'next';
		clickNext(); //Play the next element
	}
	
	// playback
	var playback = function () {
		options.autoPlay = true;
		typePlay = 'back';
		clickPrev(); //Play the previous element
	}
	
	// thumb
	var click = function(k) {		
		currNo = k;					
		currentItem = items[currNo];
		
		$(items[lastNo]).hide();
		$(items[currNo]).show();
				
		lastNo = currNo;
		isClick = true;
		timeStart = new Date();
		animateAfterClick();
	}
	
	//Make an animation
	var makeAnim = function() {
		if (typePlay == 'next') {
			currNo = jQuery.inArray(currentItem, items) + 1;
			currNo = (currNo > items.length) ? 0 : (currNo - 1);
			if (currNo < 0 ) {
				currNo = 0;
			}				
			lastIdx = (currNo <= 0 ) ? items.length - 1 : (currNo - 1);
		} else {
			currNo = jQuery.inArray(currentItem, items) - 1;
			currNo = (currNo > items.length) ? 0 : (currNo + 1);
			if (currNo < 0 ) {
				currNo = items.length - 1;
			}
			lastIdx = currNo + 1;
		}
			
		lastNo = currNo;
		if (isClick && inc == 0) {
			inc++;				
		}
			
		if(next == true) {	
            next = false;                    
			//$(items[currNo]).animate({opacity: 1},'slow');					
			currentItem = items[currNo];
					

			$(items[lastIdx]).hide();
			$(items[currNo]).show();

				//$(items[currNo]).fadeIn(options.duration / 2);
				/*$(items[currNo]).show(options.interval, function() {								
					$(items[lastIdx]).hide(options.duration / 2);
				});*/

			
					
        } else {				
			next = true;	 
			if (typePlay == 'next') { 
				currentItem = items[currNo + 1];
			} else {
				currentItem = items[currNo - 1];	
			}			
		}
			
		if (options.autoPlay == false) {
			clearInterval(timeOutFn);
		}
    }
        
	var animateAfterClick = function() {
		clearInterval(timeAfterClick);
		timeAfterClick = setInterval(function() { 
			timeCurrent = new Date();
			if (timeCurrent.getSeconds() - timeStart.getSeconds() > 2) {
				clearInterval(timeAfterClick);
				fadeElement();
			}
		}, 1000);
	}
	
	// start animation		
	fadeElement();
}
})(jQuery);
