//*******************************
//  Slider Plugin
//  Version 1.3
//*******************************
//  10.06.2010 
//  Version 1.3.1 - Поправка при моестенето в дясно. Условието беше променено на " if((contDim.width + newPos - parentWidth) >= 0) " от  " if((contDim.width + newPos - parentWidth) > 0) "
//*******************************
(function($) {
    $.fn.slider = function(options) {
        var opt = $.extend({}, $.fn.slider.defaults, options);
        return this.each(function(){
            $this = $(this);
            var parentWidth = $this.width();
            var total_elements = 0;
            var elm_width = 0;
            
            $(opt.container + " *").each(function() {                
                if("#" + this.parentNode.id == opt.container) {
                    elm_width = $(this).outerWidth();                    
                    total_elements ++;
                }
            });
			
            $(opt.container).width((total_elements+1) * opt.xStep);
            var newPos = 0;
            var contDim = {width : $(opt.container).width(), height : $(opt.container).height()};
            var curr_pos = {x : parseInt($(opt.container).css("left"), 10), y : parseInt($(opt.container).css("top"), 10)};
            curr_pos.x = isNaN(curr_pos.x) ? 0 : curr_pos.x;
            curr_pos.y = isNaN(curr_pos.y) ? 0 : curr_pos.y;
            
            for(var i in opt.controls) {
                $(opt.controls[i]).click(function() {
                    var newIdx = i;
                    return function() {
                        switch( newIdx ) {
                            case 'left' :
                                newPos = curr_pos.x + opt.xStep;
                                if(curr_pos.x < 0) {
                                    $(opt.container).animate({left : newPos});
                                    curr_pos.x += opt.xStep;
                                }
                                break;
                            case 'right' :
                                newPos = curr_pos.x - opt.xStep;								
                                if((contDim.width + newPos - parentWidth) >= 0) {								
                                    $(opt.container).animate({left : newPos });
                                    curr_pos.x -= opt.xStep;
                                }
								if((contDim.width + newPos - parentWidth) < opt.xStep){
									curr_pos.x = opt.xStep;
								}
                                break;
                            case 'top' :
                                newPos = curr_pos.y + opt.yStep;
                                if((curr_pos.y < 0)) {
                                    $(opt.container).animate({top : newPos });
                                    curr_pos.y += opt.yStep;
                                }
                                coordProperty = "top";
                                break;
                            case 'bottom' :
                                newPos = curr_pos.y - opt.yStep;
                                if( contDim.height + newPos > 0 ) {
                                    $(opt.container).animate({top : newPos });
                                    curr_pos.y -= opt.yStep;
                                }
                                break;
                            case 'top_left' :
                                newPos = curr_pos.x + opt.xStep;
                                var newYPos = curr_pos.y + opt.yStep;
                                if((curr_pos.x < 0) && (curr_pos.y < 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y += opt.yStep;
                                    curr_pos.x += opt.xStep;
                                }
                                break;
                            case 'top_right' :
                                newPos = curr_pos.x - opt.xStep;
                                var newYPos = curr_pos.y + opt.yStep;
                                if( ((contDim.width + newPos) > 0) && (curr_pos.y < 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y += opt.yStep;
                                    curr_pos.x -= opt.xStep;
                                }
                                break;
                            case 'bottom_left' :
                                newPos = curr_pos.x + opt.xStep;
                                var newYPos = curr_pos.y - opt.yStep;
                                if((curr_pos.x < 0) && (contDim.height + newYPos > 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y -= opt.yStep;
                                    curr_pos.x += opt.xStep;
                                }
                                break;
                            case 'bottom_right' :
                                newPos = curr_pos.x - opt.xStep;
                                var newYPos = curr_pos.y - opt.yStep;
                                if( ((contDim.width + newPos) > 0) && (contDim.height + newYPos > 0)) {
                                    $(opt.container).animate({left : newPos, top : newYPos});
                                    curr_pos.y -= opt.yStep;
                                    curr_pos.x -= opt.xStep;
                                }
                                break;
                            case 'reset' :
                                $(opt.container).animate({top : "0px", left : "0px"});
                                break;
                        }
                    }
                }());
            }
            
            var animates = true;
            $(opt.container).mouseover(function(){animates = false;})
            $(opt.container).mouseout(function(){animates = true;})
            
            if(opt.animate) {
                intervalID = setInterval(
                function() {
                    if(animates){
                        var movedElm = $(opt.container);
                        var old_left = parseInt(movedElm.css("left"), 10);
                        var new_left = old_left - opt.xStep;
                        var newsum_left = 0;
                        var moveElmWidth = (movedElm - opt.xStep);
                        
                        if((movedElm.width() + new_left) > $this.width()){
                            movedElm.animate({"left" : new_left + "px"});
                        }
                        else{
                            movedElm.animate({"left" : newsum_left + "px"});
                        }
                    }
                    
                }, opt.setTimemove);
            }
        });
    };
    
    $.fn.slider.defaults = {
        controls : {
            left : "#left_btn",
            right : "#right_btn",
            top : "#top_btn",
            bottom : "#bottom_btn",
            top_left : "#top_left_btn",
            top_right : "#top_right_btn",
            bottom_left : "#bottom_left_btn",
            bottom_right : "#bottom_right_btn",
            reset: "#reset_btn"
        },
        container : "#container",
        xStep : 154,
        yStep : 140,
        setTimemove : 3000,
        animate : false
    };
})(jQuery);
