$(document).ready(function()
{
  if (listObject.length > 1)
  {
	  $('.box-light').pdMotiveRotator({ 
	    rowTimeOut: 6000,
	    list: listObject,
	    random: false
	  });
  }
});

(function($){
    
    $.fn.pdMotiveRotator = function(options)
        {
            /* OPTION */
            var o = $.extend({
                rowTimeOut: 3000,
                list:[],
                random: true
            }, options);
            
            /* HELP RULES */
            var time = null;
            var element = $(this);
            $(element).addClass('js-motive');
            var paging = '';
            var arrayLength = o.list.length
            var counter = 1;
            if( o.random == true)
                {
                    var actualNum = Math.round(Math.random()*(arrayLength-1));
                }
            else
                {
                    var actualNum = 0;    
                }
            
            
            /* CREATE PAGING */
            for(var i=0; i < arrayLength; i++){
                paging = paging + '<li class="tab-'+ (i+1) +'">'+ (i+1) +'</li>';
            }
            paging = '<div class="pager-home-object"><div class="prev"></div><ul>' + paging + '</ul><div class="next"></div></div>';
            $('.object', this).after(paging);
            
            /* ITEM */
            var actualUnit = function(){
                $('.pager-home-object li').removeClass('active');
                $('.pager-home-object li:eq('+actualNum+')').addClass('active');                    
            };
            actualUnit();
            
            /* ROTATE DATA */
            var rotateData = function(){
                var imgTmp = document.createElement('img');
                $(imgTmp).bind('load', function(){

                                $('.top', element).replaceWith( o.list[actualNum].top);
                                $('.object', element).replaceWith( o.list[actualNum].object);
                                $('.bottom', element).replaceWith( o.list[actualNum].bottom);
                                $('.bottom2', element).replaceWith( o.list[actualNum].bottom2);           
                    counter++;
                }).attr('src', o.list[actualNum].src)
                
            }
            
            var clickUnit = function(el){
                time = clearTimeout(time);
                actualNum = $('.pager-home-object li').index(el);
                rotateData();
                actualUnit();
                time = setTimeout(function(){rotate()}, o.rowTimeOut);
            }
            
            /* ROTATE */
            var rotate = function(){
                if( actualNum < arrayLength -1 ){
                    actualNum++;
                }
                else{
                    actualNum  = 0;  
                }
                rotateData();
                actualUnit();
                time = setTimeout(function(){rotate()}, o.rowTimeOut);
            }
            
            /* NEXT and PREV */
            $('div.next, div.prev', element).bind('click', function(){
                time = clearTimeout(time);
                if( actualNum < arrayLength -1 && $(this).is('.next') == true){  actualNum++;  }
                else if( actualNum == arrayLength -1 && $(this).is('.next') == true){  actualNum  = 0;  }
                else if( actualNum > 0 && $(this).is('.prev') == true){  actualNum--;  }
                else if( actualNum == 0 && $(this).is('.prev') == true){  actualNum  = arrayLength -1;  }
                rotateData();
                actualUnit();
                time = setTimeout(function(){rotate()}, o.rowTimeOut);
                return false;
            });
            
            /* UNIT CLICK */
            $('.pager-home-object li').bind('click', function(){clickUnit($(this)); return false;});
            $('.pager-home-object li a').bind('click', function(){ return false;});
            

            time = setTimeout(function(){rotate()}, o.rowTimeOut); 
	   };
})(jQuery);

