/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


(function( $ ){

    var settings = {
        height:300,
        width:500,
        speed : 4000,
        freeze:-1
    };
    var rotator_local={
        current:0,
        li_count:0,
        root:null ,
        interval:null
    }
    
    var methods ={
        init:function(options) {
            if ( options ) { 
                $.extend( settings, options );
            }
            //get settings
            rotator_local.root = $(this);
            var _lis = rotator_local.root.find("li");
            rotator_local.li_count = _lis.length;
            var _ul = rotator_local.root.find("ul");
        
            //set W x H
            rotator_local.root.width(settings.width);
            rotator_local.root.height(settings.height);
            rotator_local.root.css('overflow','hidden');
            _ul.width(settings.width*rotator_local.li_count);
            _ul.height(settings.height);
            _lis.css('float','left');
            //handlers
            if(settings.freeze!=-1){
                $(rotator_local.root).hover(
                    function() {
                        clearInterval(rotator_local.interval);
                    }, 
                    function() {
                        methods.run();
                    });
                $(rotator_local.root).click(
                    function() {
                        methods.scrollNext();
                    });
                //run rotator
                methods.run();
            }
        },
        run:function(){
            rotator_local.interval = setInterval("$().mImageRotator('scrollNext')", settings.speed);   
        },
        scrollNext:function(){
            var index = rotator_local.current+1;
            if(index >= rotator_local.li_count)
                index = 0;
            rotator_local.current = index;
           
            //methods.log(index);
            var elem = $(rotator_local.root).find('li').get(index);
            $(rotator_local.root).scrollTo(elem, 500); 
        },
        log:function(line){
            $("#log").html($("#log").html()+"<br/>"+line);
        }
    }
    
    $.fn.mImageRotator= function(method) {
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.m.rotator' );
        }   
    };
    
})( jQuery );

