/**
 * scroll box
 * 
 * @dependency jQuery.js
 * @dependency jQuery.ui.js
 */
;(function($) {
var i3 = (typeof i3 == 'undefined') ? {} : i3;

i3.scloll_box = function(o) {
    var def_options = {
        scrollSpeed: 10,
        scrollInterval: 10
    }
    var o = $.extend(def_options, o);
    
    // initialize
    var id   = this.attr('id');
    var _target_id       = id + '_target';
    var _left_button_id  = id + '_left';
    var _right_button_id = id + '_right';
    var html = this
        .addClass('js')
        .prepend('<p id="' + _left_button_id + '"><img src="user_assets/images/home/btn_scrl_left.gif" alt="" width="19" height="186" /></p>')
        .append('<p id="' + _right_button_id + '"><img src="user_assets/images/home/btn_scrl_right.gif" alt="" width="19" height="186" /></p>')
        .html();
    this.html(html);
    
    var target = $('#'+_target_id);
    
    // ul li
    var ul_width = 0;
    $(this).find('ul > li').each(function() {
        ul_width += parseInt($(this).width());
        ul_width += parseInt($(this).css('margin-left').replace('px', ''));
        ul_width += parseInt($(this).css('margin-right').replace('px', ''));
    });
    var scrollRange = Math.min(0, target.width() - ul_width);
    
    // scroll function
    var scroll = function(dx) {
        var _scroll = target.find('ul');
        var ox = parseInt(_scroll.css('margin-left'));
        var vx = ox + dx;
        if (vx > 0) {
            vx = 0;
        } else if(vx < scrollRange) {
            vx = scrollRange;
        }
        _scroll.css('margin-left', vx + 'px');
    }
    
    scroll(0);
    
    // left bar
    this.find('#'+_left_button_id).find('img').hover(
        function() {
            scroll(o.scrollSpeed);
            this.interval = setInterval(function(){scroll(o.scrollSpeed)}, o.scrollInterval);
        },
        function() {
            clearInterval(this.interval);
        }
    );
    this.find('#'+_right_button_id).find('img').hover(
        function() {
            scroll(o.scrollSpeed);
            this.interval = setInterval(function(){scroll(-o.scrollSpeed)}, o.scrollInterval);
        },
        function() {
            clearInterval(this.interval);
        }
    );
}

$.fn.i3scloll_box = i3.scloll_box;
})(jQuery);
