//*******************************
//  Slider Plugin
//  Version 1.3
//*******************************
(function($) {
    $.fn.slider = function(options) {
        var opt = $.extend({}, $.fn.slider.defaults, options);
        return this.each(function(){
            $this = $(this);
            var total_elements = 0;
            var elm_width = 0;
            $(opt.container + " *").each(function() {
                if("#" + this.parentNode.id == opt.container) {
                    elm_width = $(this).width();
                    total_elements ++;
                }
            });
            $(opt.container).width(total_elements * 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) > 0) {
                                    $(opt.container).animate({left : newPos });
                                    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;
                        }
                    }
                }());
            }
        });
    };
    
    $.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 : 300,
        yStep : 150
    };
})(jQuery);
