var wb = {
    autoupload : function(action, fn_complete)
    {
        if(this.tagName != "INPUT" || this.value == ""){
            return;
        }

        $(this).wrap('<div style="display: inline;"></div>');

        var frame_id = Math.round(Math.random() * 1000000);
        var $self    = $(this);
        var $parent  = $self.parent();
        var code     = $parent.html();
        
        $("body").append(
            "<div class=\"wb_autoupload\">" +
              "<iframe name=\"wb_autoupload_"+ frame_id +"\" style=\"display: none;\" src=\"javascript:\"></iframe>" +
              "<form style=\"display:none;\" method=\"post\" enctype=\"multipart/form-data\" target=\"wb_autoupload_"+ frame_id +"\" action=\""+ action +"\">" +
                "<input type=\"hidden\" name=\"wb_autoupload\" value=\"1\" />"+
                "<input type=\"hidden\" name=\""+ $self.attr("name") +"\" value=\"1\" />"+
              "</form>" +
            "</div>"
        );
        $("body > div.wb_autoupload:last > form").append($self);

        $parent.html(code);
        
        $("body > div.wb_autoupload:last > iframe").load(function(){
            var res = $(this).contents().find("body").html();
            if(res != ""){
                if(typeof(fn_complete) != "undefined"){
                    fn_complete(res);
                }
            }
        });

        $("body > div.wb_autoupload:last > form").submit();
    },
    
    wnd_modal : function(link)
    {
        var $box = $("#wb_wnd_modal_box");

        if($box.size() == 0){
            $('body').append(
                '<div id="wb_wnd_modal_box" style="display: none; position: fixed; left: 0px; top: 0px; width: 100%; height: 100%;">'+
                  '<div id="wb_wnd_modal_box_content" style="z-index: 110; position: relative; float: left; background: #FFFFFF; padding: 10px; border: 1px solid #CCCCCC;">'+
                  '</div>'+
                  '<div id="wb_wnd_modal_box_overlay" style="z-index: 100; position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; background: #000000; opacity: 0.2;" onclick="wb.wnd_close()"></div>'+
                '</div>'
            );
            $box = $("#wb_wnd_modal_box");
        }

        jQuery.get(
            link,
            function(data){
                var $cnt = $("#wb_wnd_modal_box_content", $box);
                $cnt.html(data);
                $box.show();
                $cnt.css("margin-left", (($box.width() - $cnt.width()) / 2) + "px");
                $cnt.css("margin-top",  (($box.height() - $cnt.height()) / 4) + "px");
            }
        );

        return false;
    },
    
    wnd_close : function()
    {
        $("#wb_wnd_modal_box").hide();
    },
    
    yandex_map : function(pid, x, y, z)
    {
        // Карта
        var map     = new YMaps.Map(document.getElementById(pid));
        var center  = new YMaps.GeoPoint(x, y);
        var zoom    = z;
        var point   = new YMaps.Placemark(new YMaps.GeoPoint(x, y), {style: "default#shopIcon"});
        
        point.name        = 'Магазин &laquo;Автодиск&raquo;';
        point.description = '<div>'+
                              'г. Киев, ул. Гарматная 8,<br/>2-й этаж, офис №4<br/>(044) 228-89-79'+
                            '</div>';

        map.setCenter(center, zoom);
        map.addOverlay(point);
        point.openBalloon();
        map.addControl(new YMaps.Zoom());            
    },
    
    scroll : function() {
        this.shift = function(step)
        {
            var $obj     = $(this);
            var $frm     = $obj.parent().find(".scroll_frame");
            var $scr     = $obj.parent().find(".scroll_line");
            var $lt      = $obj.parent().find(".scroll_left");
            var $rt      = $obj.parent().find(".scroll_right");
            var maxw     = $frm.width();
            var scrw     = $scr.width();
            var left     = parseInt($scr.css("margin-left"));
            var new_left = left + step;
            
            new_left = new_left < -(scrw - maxw)  ? -(scrw - maxw) : new_left;
            new_left = new_left > 0 ? 0 : new_left;
            
            $lt.css("opacity", "1.0");
            $rt.css("opacity", "1.0");
            if(new_left >= 0){
                $rt.css("opacity", "0.5");
            }
            if(new_left + scrw <= maxw){
                $lt.css("opacity", "0.5");
            }
            
            $scr.css("margin-left", new_left + "px");
            
            return false;
        }
    },
    
    minmax : function(context, min, max)
    {
        var butmin          = $(".minmax_but_min", context).get(0);
        var butmax          = $(".minmax_but_max", context).get(0);
        var txtmin          = $(".minmax_val_min", context).get(0);
        var txtmax          = $(".minmax_val_max", context).get(0);
        var ctr_width       = $(context).width();
        var but_width       = $(".minmax_but_min", context).width();
        var val_min         = min;
        var val_max         = max;
        var val2px          = (ctr_width - but_width) / (val_max - val_min);
        var px2val          = (val_max - val_min) / (ctr_width - but_width);
        var mitem           = null;
        
        butmin.onmousedown  = mouse_down
        butmax.onmousedown  = mouse_down
        
        function mouse_down(e)
        {
            mitem                   = e.target;
            mitem.move_state        = 1;
            mitem.mouse_x           = e.clientX;
            
            document.onmousemove    = mouse_move
            document.onmouseup      = mouse_up      
            
            return false
        };

        function mouse_up(e)
        {
            mitem                   = null
            document.onmousemove    = null
            document.onmouseup      = null
        };

        function mouse_move(e)
        {
            var cx = e.clientX;
            
            if(mitem && mitem.move_state == 1){
                var dx = cx - mitem.mouse_x;
                var ml = parseInt(mitem.style.marginLeft) + dx;
                
                ml = ml < 0 ? 0 : ml;
                ml = ml > ctr_width - but_width ? ctr_width - but_width : ml;

                mitem.style.marginLeft = ml + "px";
                mitem.mouse_x = cx;
                
                txtmin.value  = Math.round(parseInt(butmin.style.marginLeft) * px2val + val_min);
                txtmax.value  = Math.round(parseInt(butmax.style.marginLeft) * px2val + val_min);
            }
        };

        return {
            set_val : function(min, max)
            {
                if(typeof(min) == "undefined"){
                    min = val_min;
                }
                if(typeof(max) == "undefined"){
                    max = val_max;
                }
                
                butmin.style.marginLeft = parseInt((min - val_min) * val2px) + "px";
                butmax.style.marginLeft = parseInt((max - val_min) * val2px) + "px";
                txtmin.value = Math.round(min);
                txtmax.value = Math.round(max);
            }
        }
    },
}



