(function($){  
  
	$.fn.iSlider = function(options){
		$.fn.iSlider.defaults = {
			animSpeed:1000,
			autoStart:false,
			pauseTime:3000,
			arrowsNav:true,
			arrowsNavHide:true,
			listNav:true,
			listNavThumbs:false,
			pauseOnHover:true,
			easingEfect:'swing',
			direction:'horizontal' // horizontal, vertical
		};
		var settings = $.extend({
			
			
		}, $.fn.iSlider.defaults, options);
		
		var blocks = {
			frame: '<div class="i-slider-frame">',
			tape: '<div class="i-slider-sections">',
			section: '<div class="i-slider-section">',
			switcher_wrapper: '<div class="i-slider-switcher-wrapper">'
		};
		
		var $previous = $('<div class="i-slider-arrows previous"></div>');
		var $next = $('<div class="i-slider-arrows next"></div>');
		var $switcher = $('<ul class="switcher"></ul>');
		var $switcher_wrap = $('<div class="i-slider-switcher-wrap"></div>');
		
        var $this = $(this);
        
        var init = {
		height: $this.height(),
		width: $this.width(),
		images_total: $this.find('div.i-slider-section').length,
		images_amount: $this.find('div.i-slider-section').length
        };
        
        var move = false;
        
	var make = function() {
			
			$this.addClass('i-slider');
			
			// get slider height and width
			var height = $this.height();
			var width = $this.width();
			
			$this.wrapInner(blocks.frame);
			$('div.i-slider-frame',$this).wrapInner(blocks.tape).height(height).width(width);
           
			// set equal height and width for all sections and the frame
			$('div.i-slider-section',$this).height(height).width(width);
			
			var i = 1;
			$('div.i-slider-section', $this).each(function(){
				$(this).attr('data-section', i++ );
			});

			$('.i-slider-captions',  $this).wrapInner('<div class="inner"/>');
			
			// clone the first and last and set them before and after all images
			var first_last = $('div.i-slider-section:last', $this).clone().data('section','last');
			var last_first = $('div.i-slider-section:first', $this).clone().data('section','first');
			
			// Since these are cloned, remove the id's
			first_last.find("*").removeAttr("id");
			last_first.find("*").removeAttr("id");
			
			$('div.i-slider-sections', $this).prepend(first_last).append(last_first);
			
			// check direction and set init width of tape and start position
			if(settings.direction == 'vertical'){
				$('div.i-slider-sections', $this).css('top','-'+height+'px');
			}
			if(settings.direction == 'horizontal'){
				$('div.i-slider-sections', $this).height(height).width( ($('div.i-slider-section',$this).length)*width ).css('left','-'+width+'px');
			}
				
			if(settings.arrowsNav){
				$this.append($previous).append($next);
				if(settings.arrowsNavHide){
					$('div.i-slider-arrows',$this).hide();
				}
			}
			
            if(settings.listNav){
            	$this.append($switcher);
            	if(settings.listNavThumbs){
            		$switcher.addClass('i-slider-listNavThumbs');
            		
            		var j = 1;
            		
            		$('div.i-slider-section', $this).each(function(){
            			if($(this).data('section') != 'first' && $(this).data('section') != 'last') {
            				thumb_src = $(this).data('thumb');
                			$switcher.append('<li data-element="'+ j++ +'"><img src="'+thumb_src+'" /></li>');
                			j++;
            			}
            		});
            	}
            	else {
            		$switcher.addClass('i-slider-listNav');
            		
            		var j = 1;
            		
            		$('div.i-slider-section', $this).each(function(){
            			if($(this).data('section') != 'first' && $(this).data('section') != 'last') {
            				thumb_src = $(this).data('thumb');
                			$switcher.append('<li data-element="'+ j +'"></li>');
                			j++;
            			}
            		});
            		
            	}
            	$switcher.wrap($switcher_wrap);
            	$switcher.find('li:first').addClass('active');
            }
        };
        
        $('li', $this).live('click', function(){
        	move = true;
        	var element = $(this).data('element');
        	var sections = $('div.i-slider-sections', $this);
        	var section = $('div.i-slider-section', $this);
        	
        	$("ul li",$this).removeClass('active');
	   		$(this).addClass('active');
        	
		$this.trigger("slideSwitch");
        	if(settings.direction == 'horizontal'){
        		sections.animate({
 			   		left: -element*init.width
 		   		}, settings.animSpeed, settings.easingEfect, function(){
    		   		if( sections.css('left') == '0px' ){
    		   			sections.css('left','-'+( (section.length - 2) * section.width() )+'px');
    		   		}

    		   		move = false;
    	   		});
        		
        	}
        	if(settings.direction == 'vertical'){
        		sections.animate({
 			   		top: -element*init.height
 		   		}, settings.animSpeed, settings.easingEfect, function(){
    		   		if( sections.css('top') == '0px' ){
    		   			sections.css('top','-'+( (section.length - 2) * section.height() )+'px');
    		   		}
    		   		
    		   		var p = sections.position();
    		   		var element = Math.abs(p.top/init.height);
    		   		
    		   		move = false;
    	   		});
        	}
        });
        
        $previous.bind('click', function(){
        	if(move == true) return;
        	move = true;
        	
        	var sections = $('div.i-slider-sections', $this);
        	var section = $('div.i-slider-section', $this);
        	var p = sections.position();
        	
        	if(settings.direction == 'horizontal') var element = Math.abs((p.left + init.width)/init.width);
        	if(settings.direction == 'vertical') var element = Math.abs((p.top + init.height)/init.height);
        		
	   		//if(Math.abs(p.left) == init.width) element = $('div.i-slider-section', $this).length - 2;
	   		if(element == 0) element = section.length - 2;;
	   		
	   		$("ul li",$this).removeClass('active');
	   		$("ul",$this).find("[data-element='" + element +"']").addClass('active');
        	
		$this.trigger("slideSwitch");
        	if(settings.direction == 'horizontal'){
        		sections.animate({
 			   		left: '+='+init.width
 		   		}, settings.animSpeed, settings.easingEfect, function(){
    		   		if( sections.css('left') == '0px' ){
    		   			sections.css('left','-'+( (section.length - 2) * section.width() )+'px');
    		   		}

    		   		move = false;
    	   		});
        	}
        	if(settings.direction == 'vertical'){
        		sections.animate({
 			   		top: '+='+init.height
 		   		}, settings.animSpeed, settings.easingEfect, function(){
    		   		if( sections.css('top') == '0px' ){
    		   			sections.css('top','-'+( (section.length - 2) * section.height() )+'px');
    		   		}
    		   	
    		   		
    		   		move = false;
    	   		});
        	}
        });
        
        $next.bind('click', function(e, data){
        	if(move == true) return;
        	move = true;
        	
        	var sections = $('div.i-slider-sections', $this);
        	var section = $('div.i-slider-section', $this);
        	var p = sections.position();
	   		
	   		if(settings.direction == 'horizontal') var element = Math.abs((p.left-init.width)/init.width);
	   		if(settings.direction == 'vertical') var element = Math.abs((p.top-init.height)/init.height);

	   		if((section.length - 1) == element) element = 1;
	   			
	   		$("ul li",$this).removeClass('active');
	   		$("ul",$this).find("[data-element='" + element +"']").addClass('active');
        	
        	$this.trigger("slideSwitch");
        	if(settings.direction == 'horizontal'){
        		sections.animate({
        			left: '-='+init.width
        		}, settings.animSpeed, settings.easingEfect, function(){
        			if( sections.css('left') == '-'+( (section.length -1) * section.width() )+'px' ){
        				sections.css('left','-'+ section.width() +'px');
        			}

    		   		move = false;
        		});
        	}
        	if(settings.direction == 'vertical'){
        		sections.animate({
        			top: '-='+init.height
        		}, settings.animSpeed, settings.easingEfect, function(){
        			if( sections.css('top') == '-'+( (section.length -1) * section.height() )+'px' ){
        				sections.css('top','-'+ section.height() +'px');
        			}
        			
        			move = false;
        		});
        	}
        });
        
        var timer = '';
	
	var skip = false;
	
	this.skipAutoAdvance = function() {
		skip = true;
	}
	
	$this.bind('startAutoAdvance', function() {
		if(timer == ''){
			timer = setInterval( function(){ 
				$this.trigger('autoAdvance');
				if(!skip) {
					$next.trigger('click', { autoAdvance: true });
				} else {
					skip = false;
				}
			}, settings.pauseTime );
		}
	});
	
	$this.bind('stopAutoAdvance', function() {
		clearInterval(timer);
		timer = '';
	});
        
        if(settings.autoStart){
		$this.trigger('startAutoAdvance');
        }
        if(settings.pauseOnHover && settings.autoStart){
		$this.hover( function(){
			$this.trigger('stopAutoAdvance');
		}, function(){
			$this.trigger('startAutoAdvance');
		});
	}
        
        if(settings.arrowsNav && settings.arrowsNavHide){
        	var timeout = '';
        	
        	$this.hover( function(){
        		if(timeout){
        			clearTimeout( timeout );
					timeout = '';
        		}
				$('div.i-slider-arrows',$this).show().hover( function(){
					clearTimeout( timeout );
					timeout = '';
				}, function(){
					$('div.i-slider-arrows', $this).hide(); 
				});
			}, function(){
				timeout = setTimeout( function(){ $('div.i-slider-arrows', $this).hide(); }, 1000);
			});
        }
        
        this.each(make);
	
	return this;
       
    };
    
})(jQuery);  

