/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision Base-10):
 * <phk@FreeBSD.ORG> inspired this file. As long as you retain this notice you
 * can do whatever you want with this source code, as long as you don't claim 
 * we wrote the modified version. If you think this source code is worth it,
 * you can buy us a beer or more in return. Base-10 Design & Development Inc.
 * ----------------------------------------------------------------------------
 */

(function($) {
    $.fn.baseshift = function (settings) {        
        $.fn.baseshift.settings = {			
			prev: 		'.prev',			
			next: 		'.next',        
            inbox : '0',            
            step: '-1'
        };
        var options = $.extend($.fn.baseshift.settings, settings);
        var currentPage=1;        
        var obj=$(this);
        var slider = obj.find('ul');
        var items = slider.find('li');
        var maxitems = items.size();
        var single = items.filter(':first');            
        var singleWidth = single.outerWidth();        
        var pages = Math.ceil(items.length / options.visible);        
        
        this.each(function () {
            doShift();
            if (!options.first) $(options.prev).click(function () { return jump(currentPage - 1); });        
            $(options.next).click(function () { return jump(currentPage + 1); });

            if (options.inbox != '0' ){
               $(options.prev).addClass("prev_inbox");
               $(options.next).addClass("next_inbox");               
               $(options.prev).fadeTo(0,0.1); 
               $(options.next).fadeTo(0,0.1);            
               //if (currentPage<=1) 
               //if (currentPage>=pages) 
               
               $(options.prev).hover(                   
                   function(){                     
                     var showit=1;                     
                     if (currentPage<=1) if (options.first) {
                        $(options.prev).unbind('click');
                        options.first();
                        showit=0;
                     }
                     if (showit==1) $(options.prev).fadeTo("fast",1);
                   },
                   function(){$(options.prev).fadeTo("fast",0.01); }                   
               );               
               $(options.next).hover(
                   function(){
                     var showit=1;
                     if (currentPage>=pages) if (options.last) {
                        $(options.next).unbind('click');                                                          
                        options.last();
                        showit=0;
                     }
                     if (showit==1) $(options.next).fadeTo("fast",1);
                    },
                   function(){$(options.next).fadeTo("fast",0.01); }                   
               );                            
            }
            
            function doShift(){
				//slider.css({ width: 9999+'px'});
			//	alert(singleWidth);
                obj.css({overflow: 'hidden', width: singleWidth * options.visible+'px'});				
                if ((items.length % options.visible) != 0) {
                    var toadd= options.visible - (items.length % options.visible);                         
                    if (options.step!=-1) var toadd= (items.length - options.visible) % options.step;                                                                 
                    for (i=0;i<toadd;i++){ slider.append('<li class="empty" >&nbsp;</li>'); }                        
                    items = slider.find('li');                 
                }
                pages=items.length / options.visible;               
                if (options.step!=-1) pages = Math.ceil((items.length-options.visible)/options.step)+1;
                
                items.filter(':first').before(items.slice(- options.visible).clone().addClass('cloned'));
                items.filter(':last').after(items.slice(0, options.visible).clone().addClass('cloned'));
                items = slider.find('li');
                obj.scrollLeft(singleWidth * options.visible);                
                if (options.first) options.first();                
            }
            
            function jump(page){
                var maxitems = items.size();
                var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * options.visible * n;
                if (options.step!=-1) left = singleWidth * dir * options.step * n;                    
                
                $(options.prev).unbind('click');
                $(options.next).unbind('click');                                                          
                
                obj.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {                    
                    if (!options.std){
                        $(options.prev).click(function () { return jump(currentPage - 1); });        
                        $(options.next).click(function () { return jump(currentPage + 1); });                    
                    }                    
                    var e=0;
                    if ((page==1)||(page>pages)) if (options.first) {
                        e=1;
                        options.first();
                        $(options.next).click(function () { return jump(currentPage + 1); });                    
                    }
                    if ((page<1)||page==pages) if (options.last){
                        e=1;
                        options.last();
                        $(options.prev).click(function () { return jump(currentPage - 1); });        
                    }
                    if (e==0) if (options.std) {
                        options.std();
                        $(options.prev).click(function () { return jump(currentPage - 1); });        
                        $(options.next).click(function () { return jump(currentPage + 1); });                    
                    }
                    
                    if (page <= 0) {            
                        if (options.step!=-1) {                             
                            if (page <= 1-options.visible/options.step) {              
                                obj.scrollLeft(maxitems * singleWidth);
                                page = pages;                              
                            }                           
                        } else {                           
                            obj.scrollLeft(singleWidth * options.visible * pages);
                            page = pages;
                            page = pages;
                        }                        
                    } else if (page > pages) {
                        if (options.step!=-1) {                           
                           var max=Math.ceil((options.visible/options.step+1))+pages-1;
                           if (page >= max) {
                              obj.scrollLeft(singleWidth * options.visible);                              
                              page = 1;
                           }
                        } else {
                            obj.scrollLeft(singleWidth * options.visible);                            
                            page = 1;
                        }                        
                    } 
                    currentPage = page;                                      
                });
            }            
        });
    }
    
})(jQuery);