(function($){

    $.fn.bbSliderTabs = function(options){

		var options = $.extend($.fn.bbSliderTabs.defaults, options);
        
        return this.each(function(){

            var $this = $(this);

            var $tabs = null,
                sel_index = 0,
                item_width = 0,
                item_count = 0,
                dsp_count = 0,
                max_index = 0,
                sel_index = 0,
                dsp_index = 0,
                $controls = $this.find(">.content-controls");

            function moveTo(index, dir) {

                var ni = Math.max(0, index);
                ni = Math.min(max_index, ni);
                sel_index = ni;
                
                if ( (sel_index + dsp_index >= dsp_count) || (sel_index < dsp_index) ) {
                    dsp_index = Math.min(ni, max_index);
                    var l = $tabs.find("li:eq("+dsp_index+")").position().left;
                    $tabs.stop().animate({"left": -l+"px"}, options.interval);
                    options.onMove.call(this, dir, sel_index, $this);
                }
                initButtons();
                $tabs.find("li:eq("+ni+")").trigger("click");
            }

            function moveBy(dir) {

                var t = $tabs.position().top + dir * options.inc * 10;

                if ( (dir>0 && t<=0) || (dir<0 && $tabs.height()>$tabs.parent().height()-t ) ) {
                    $tabs.css({"top": t+"px"});
                    clearTimeout($.fn.bbSliderTabs.timer);
                    $.fn.bbSliderTabs.timer = setTimeout(function(){
                        moveBy(dir);
                    }, 20);
                    options.onMove.call(this, dir, null, $this);
                }
            }

            function calc(item, event) {
                var p = event.layerY + $(event.currentTarget).find('ul').position().top;
                var h = $(event.currentTarget).height();
                var o = (p / (h/2)) - 1;
                o *= 3;
                moveBy(-o);
            }

            function move(dir) {
                moveTo(sel_index + dir, dir);
            }

            function initButtons() {
                $controls.find(".oc-arrow-left").css("opacity", (sel_index>0)?1:0);
                $controls.find(".oc-arrow-right").css("opacity", (sel_index<max_index)?1:0);
            }

            function init() {
            
                $this.css({"position": "relative", "overflow": "hidden"});
                $tabs = $this.find('>'+options.contentSlider+' ul');
                $tabs.css({"position": "absolute"});

                var $firstli = $tabs.find("li:first");
                item_count = $tabs.find("li:visible").length;
                
                $controls.hide();

                if ( options.direction == 'horizontal' ) {
                
                    var tabs = $this.attr("rel");
                    if ( tabs && tabs!="" ) {
                        // Tabs, so hide them
                        $this.addClass("tabs");
                        var h = 0;
                        $this.find("."+tabs+">div").each(function(){
                            h = Math.max(h, $(this).height() );
                        }).hide();
                        if ( options.fixedHeight || options.minHeight ) {
                            if ( options.minHeight )
                                h = options.minHeight;
                            $this.find("."+tabs).css({"min-height": h});
                        }
                    }
                    tabs = null;

                    var _item_width = $firstli.outerWidth(true)
                                                + parseInt($firstli.css("margin-left"))
                                                + parseInt($firstli.css("margin-right"));
                    var conw = $this.find('>'+options.contentSlider).width();
                    max_index = item_count;
                    var fits = (_item_width * item_count <= conw);

                    item_width = _item_width;
                    
                    if ( options.autoResize ) {
                    
                        if ( fits ) {
                            // genişlet
                            item_width = conw / item_count ;
                        } else {
                            // daralt
                            var cw = $controls.outerWidth(true);
                            var nc = conw - cw;
                            item_width = nc  / item_count;
                            if ( item_width<options.minWidth ) {
                                dsp_count = Math.floor(nc / options.minWidth);
                                item_width = parseInt(nc / dsp_count)
                                                - parseInt($firstli.css("margin-left"))
                                                - parseInt($firstli.css("margin-right"));
                                max_index = item_count - dsp_count;
                                $this.find('>'+options.contentSlider).width(dsp_count*item_width+dsp_count);
                                $controls.css("left", conw-cw).show();
                            }
                        }
                    } else {
                        item_width = ( options.width == false ) ? _item_width:options.width;
                        max_index = Math.max(0, item_count - Math.max(0, parseInt(conw / item_width)));
                        if ( item_count * item_width >= conw )
                            $controls.show();
                    }
                    
                    $controls.find('.oc-arrow-left').bind("click", function(){
                        moveTo(sel_index-1);
                    });
                    $controls.find('.oc-arrow-right').bind("click", function(){
                        moveTo(sel_index+1);
                    });
                    $tabs.find("li").width(item_width).bind("click", function(){
                        sel_index = $(this).index();
                        options.onClick.call(this, $(this), $this);
                    });
                    $tabs.find("li").bind("mouseenter", function(){
                        $(this).addClass("hover");
                    }).bind("mouseleave", function(){
                        $(this).removeClass("hover");
                    });

                    width_ul = (item_count+1) * item_width;
                    $tabs.width(width_ul);
                    moveTo(options.activeTab);
                } else {
                    // Vertical Scroll
                    $controls.find('.oc-arrow-left').bind("mousedown", function(){
                        moveBy(1);
                    }).bind("mouseup", function(){
                        clearTimeout($.fn.bbSliderTabs.timer);
                        $.fn.bbSliderTabs.timer = null;
                    });
                    $controls.find('.oc-arrow-right').bind("mousedown", function(){
                        moveBy(-1);
                    }).bind("mouseup", function(){
                        clearTimeout($.fn.bbSliderTabs.timer);
                        $.fn.bbSliderTabs.timer = null;
                    });
                    $tabs.parent().bind("mouseenter", function(e){
                        calc(this, e);
                    }).bind("mouseleave", function(){
                        clearTimeout($.fn.bbSliderTabs.timer);
                        $.fn.bbSliderTabs.timer = null;
                    });
                }
            }

            $.fn.bbSlider = function(method) {
                /* TEST */
            }

            init();
        });
    }

    $.fn.bbSliderTabs.defaults = {
        direction: 'horizontal',
        interval: false,
        width: false,
        autoResize: true,
        activeTab: 0,
        minWidth: 100,
        minHeight: 400,
        interval: 500,
        inc: 3,
        fixedHeight: true,
        contentSlider: ".content-slider",
        onMove: function(dir, index, slider) {},
        onClick: function(item, slider) {}
    }
    $.fn.bbSliderTabs.timer = null;
    $.fn.bbSliderTabsStop = function(){
        clearTimeout($.fn.bbSliderTabs.timer);
        $.fn.bbSliderTabs.timer = null;
        return this;
    };
})(jQuery)

