﻿(function($) {
    // jQuery plugin definition
    $.fn.simpleSlide = function(options) {
    
        var defaults = {  
            speed1: 20000,  
            speed2: 30000,
			orientation: 'horizontal'
        };  
        var options = $.extend(defaults, options);  
    
        // express a single node as a jQuery object
        var container = this;
		if(options.orientation == 'vertical') {
			container.find('ul.slide').addClass('tall');
		}

        // Find the buttons to trigger
        var prevButton = this.find('a.prev:first');
        var nextButton = this.find('a.next:first');

        //rotation speed and timer
        var speed = container.find('li div.panelContent').length > 2 ? options.speed1 : options.speed2;
        var run = setInterval(function() {rotate(nextButton.attr('id'));},speed);

        //grab the width and calculate left value
        var item_first = container.find('li div.panelContent:first');
        var item_width = (options.orientation == 'horizontal') ? item_first.outerWidth(true) : item_first.outerHeight(true);
		var left_value = item_width * (-1); 
            
        //move the last item before first item, just in case user click prev button
        container.find('li:first').before(container.find('li:last'));
    	
        //set the default item to the correct position 
		if(options.orientation == 'horizontal'){
			container.find('ul').css({'left' : left_value});
		}else{
			container.find('ul').css({'top' : left_value});
		}
        

        //if user clicked on prev button
        prevButton.click(function() {

            //get the right position            
            var left_indent = parseInt(container.find('ul').css('top')) + item_width;
			if(options.orientation == 'horizontal'){
				left_indent = parseInt(container.find('ul').css('left')) + item_width;
			}

            //slide the item         
			if(options.orientation == 'vertical'){
				container.find('ul').animate({'top' : left_indent}, 1000,function(){    

					//move the last item and put it as first item            	
					container.find('li:first').before(container.find('li:last'));           

					//set the default item to correct position
					container.find('ul').css({'top' : left_value});
				
				});
			}
			else{
				container.find('ul').animate({'left' : left_indent}, 1000,function(){    

					//move the last item and put it as first item            	
					container.find('li:first').before(container.find('li:last'));           

					//set the default item to correct position
					container.find('ul').css({'left' : left_value});
				
				});
			}

            //cancel the link behavior            
            return false;
                
        });
     
        //if user clicked on next button
        nextButton.click(function() {
    		
            //get the right position
			if(options.orientation == 'vertical'){
				var left_indent = parseInt(container.find('ul').css('top')) - item_width;
				
				//slide the item
				container.find('ul').animate({'top' : left_indent}, 1000, function () {
					
					//move the first item and put it as last item
					container.find('li:last').after(container.find('li:first'));                 	
					
					//set the default item to correct position
					container.find('ul').css({'top' : left_value});
				
				});
			}else{
				var left_indent = parseInt(container.find('ul').css('left')) - item_width;
				
				//slide the item
				container.find('ul').animate({'left' : left_indent}, 1000, function () {
					
					//move the first item and put it as last item
					container.find('li:last').after(container.find('li:first'));                 	
					
					//set the default item to correct position
					container.find('ul').css({'left' : left_value});
				
				});

			}
    		         
            //cancel the link behavior
            return false;
        });        
    	
        //if mouse hover, pause the auto rotation, otherwise rotate it
        container.hover(
            function() {
                clearInterval(run);
            }, 
            function() {
                run = setInterval(function() {rotate(nextButton.attr('id'));},speed); 
            }
        ); 
    }
})(jQuery);


//a simple function to click next link
//a timer will call this function, and the rotation will begin :)  
function rotate(nextId) {
    $('#'+nextId).click();
}