var sui = {
    options: {
        webroot: WEBROOT
    },
    event_to_function: function(e, d, o) {
        eval("(" + d + ")");
    },
    shader: {
        options: {
            css: {
                opacity: "0.7"
            }
        },
        create: function() {
            $("body").append($('<div id="sui_shader" class="sui_shader"></div><div id="sui_shader_content" class="sui_shader_content"></div>'));
        },
        show: function(selector) {
            if($.browser.opera || $.browser.firefox) {
                $.support.opacity = true;
            }
            if (!selector) {
                selector = "*";
            }
            $("#sui_shader.sui_shader").css({width: $(document).width(), height: $(document).height()}).css(sui.shader.options.css).show();
            $("#sui_shader_content.sui_shader_content").show().children(selector).show().each(function() {
                var t = $(this);
                var o = t.data("sui_shader_content_options");
                if (!o) {
                    o = {};
                }
                if (!o.offset_x) {
                    o.offset_x = "center";
                }
                if (!o.offset_y) {
                    o.offset_y = "center";
                }
                var w = $(window);
                if (o.offset_x == "center") {
                    o.offset_x = (w.width() / 2) - (t.width() / 2);
                }
                if (o.offset_y == "center") {
                    o.offset_y = (w.height() / 2) - (t.height() / 2);
                }
                t.move(o.offset_x, o.offset_y);
            });
        },
        hide: function() {
            $("#sui_shader.sui_shader,#sui_shader_content.sui_shader_content").hide();
        },
        destroy: function() {
            $("#sui_shader.sui_shader,#sui_shader_content.sui_shader_content").remove();
        },
        content: {
            all: function() {
                return $("#sui_shader_content > *");
            },
            get: function(selector) {
                return $("#sui_shader_content > " + selector);
            },
            add: function(content, o) {
                var s = $("#sui_shader.sui_shader");
                if (s.length == 0) {
                    sui.shader.create();
                }
                var t = $(content);
                t.data("sui_shader_content_options", o);
                $("#sui_shader_content").append(t);
            },
            remove: function(selector) {
                $("#sui_shader_content > " + selector).remove();
            }
        },
        remove_and_hide: function(selector) {
            sui.shader.content.remove(selector);
            sui.shader.hide();
        }
    },
    ajax: {
        options: {
            login_url: "login"
        },
        login: function(success_cb, cancel_cb) {
            if (success_cb) {
                sui.ajax.login_success_cb = success_cb;
            }
            if (cancel_cb) {
                sui.ajax.login_cancel_cb = cancel_cb;
            }
            $.get(sui.options.webroot + sui.ajax.options.login_url, null, sui.ajax.login_response_handler, "html");
        },
        login_response_handler: function(d, s) {
            if (s == "success") {
                var t = $(d);
                if (t.hasClass("ajax_login")) {
                    t.sui_shader_load({offset_x: "center", offset_y: "center"});
                }
            }
        },
        login_success_cb: function() {
            window.location.reload(true);
        },
        login_cancel_cb: function() {
            sui.shader.remove_and_hide('.ajax_login');
        }
    },
    tooltip: {
        remove_all: function() {
            for (var i = 0; i < sui.tooltip.active_list.length; i++) {
                if (sui.tooltip.active_list[i].target.data("sui_tooltip_delayed_hide_timer_id")) {
                    clearTimeout(sui.tooltip.active_list[i].target.data("sui_tooltip_delayed_hide_timer_id"));
                    sui.tooltip.active_list[i].target.removeData("sui_tooltip_delayed_hide_timer_id");
                }
                sui.tooltip.active_list[i].content.stop().sui_moveable("remove").remove();
                sui.tooltip.active_list[i].content = null;
                sui.tooltip.active_list[i].target.removeData("sui_tooltip_options").removeData("sui_tooltip_stuck").removeData("sui_tooltip_content").removeData("sui_tooltip").unbind("click").unbind("mousemove");
                sui.tooltip.active_list[i].target = null;
            }
            sui.tooltip.active_list = [];
        },
        active_list: []
    }
};

(function($) {
    $.fn.extend({
        sui_shader_load: function(o) {
            this.each(function() {
                sui.shader.content.add(this, o);
            });
            var t = $(this);
            if (t.length > 0) {
                sui.shader.show();
            }
            return t;
        },
        sui_button: function(onclick) {
            this.each(function() {
                var t = $(this);
                var d = t.attr("data");
                t.removeAttr("data");
                var oc = onclick;
                if (oc == null || oc == undefined) {
                    oc = t.attr("whenclick");
                }
                t.removeAttr("whenclick");
                if (oc == null || oc == undefined) {
                    oc = function() {
                        return false;
                    }
                }
                if (typeof(oc) == "string") {
                    oc = eval(oc);
                }
                var dcb = t.attr("dcb");
                if (typeof(dcb) == "string") {
                    dcb = eval(dcb);
                }
                t.removeAttr("dcb");
                t.click(function(e) {
                    if (!$(this).hasClass("disabled")) {
                        if (d != null && d != undefined) {
                            oc(e, d, this);
                        } else {
                            oc(e, null, this);
                        }
                    } else {
                        if (typeof(dcb) == "function") {
                            if (d != null && d != undefined) {
                                dcb(e, d, this);
                            } else {
                                dcb(e, null, this);
                            }
                        }
                    }
                })
                .sui_hover()
                .sui_press()
                .sui_focus()
                .select(function() {
                    return false;
                });
            });
            return $(this);
        },
        sui_tab: function(o) {
            this.each(function() {
                var t = $(this);
                t.find("ul.sui_tab_tabs li").click(function(){$(this).find("a").click()})
                .sui_hover()
                .sui_press()
                .mousedown(function(){return false;})
                .find("a").click(function(e) {
                    var tt = $(this);
                    t
                    .find("li").removeClass("active").end()
                    .children(".sui_tab_content").hide().end()
                    .find(tt.attr("href")).show();
                    tt.parents("li:first").addClass("active");
                    e.preventDefault();
                    e.stopPropagation();
                    if (o && o.onclick) {
                        o.onclick(e, this);
                    }
                    return false;
                })
                .select(function(){return false});
                var y = t.find("ul.sui_tab_tabs li.sui_tab_default");
                if (y.length > 0) {
                    y.click();
                } else {
                    t.find("ul.sui_tab_tabs li:first").click();
                }
            });
            return $(this);
        },
        sui_accordion: function(o) {
            this.each(function() {
                var t = $(this);
                t.find(".sui_accordion_key").click(function() {
                    var z = $(this);
                    z.siblings(".sui_accordion_chord").slideToggle("fast");
                    if (!o || o.keep_open == undefined || o.keep_open == null || o.keep_open == false) {
                        z.parents(".sui_accordion_plate:first").siblings(".sui_accordion_plate").find(".sui_accordion_chord").slideUp("fast");
                    }
                }).sui_hover().sui_press()
                .filter(":not(:first)").siblings(".sui_accordion_chord").slideUp("fast");
            });
            return $(this);
        },
        sui_scroll: function(o) {
            this.each(function() {
                var t = $(this);
                t
                .change(function() {$(this).sui_scroll_reset()})
                .wrapInner("<div class='sui_scroll'><div class='sui_scroll_contents'></div></div>");
                t.find(".sui_scroll").css({position: "relative"}).width(t.width()).height(t.height());
                if (o && o.vertical && o.vertical == true) {
                    t.find(".sui_scroll").append("<div class='sui_scroll_bar vertical'><div class='sui_scroll_bar_dongle'></div></div>")
                    .find(".sui_scroll_bar.vertical")
                    .click($.sui_scroll)
                    .mousedown(function(){return false;})
                    .select(function(){return false;})
                    .children(".sui_scroll_bar_dongle")
                    .mousedrag($.sui_scroll)
                    .mousedown(function(){return false;})
                    .select(function(){return false;});
                }
                if (o && o.horizontal && o.horizontal == true) {
                    t.find(".sui_scroll").append("<div class='sui_scroll_bar horizontal'><div class='sui_scroll_bar_dongle'></div></div>")
                    .find(".sui_scroll_bar.horizontal")
                    .click($.sui_scroll)
                    .mousedown(function(){return false;})
                    .select(function(){return false;})
                    .children(".sui_scroll_bar_dongle")
                    .mousedrag($.sui_scroll)
                    .mousedown(function(){return false;})
                    .select(function(){return false;});
                }
                t.sui_scroll_reset();
            });
            return $(this);
        },
        sui_scroll_reset: function() {
            this.each(function() {
                var t = $(this);
                var h = t.find(".sui_scroll > div").height();
                var sh = t.find(".sui_scroll").height();
                var mv = ((h - (h - sh)) / h);
                var nh = Math.round(sh * mv);
                if (nh > sh || nh < 0) {
                    nh = sh;
                }
                var w = t.find(".sui_scroll > div").width();
                var sw = t.find(".sui_scroll").width();
                var mw = ((w - (w - sw)) / w);
                var nw = Math.round(sw * mw);
                if (nw > sw || nw < 0) {
                    nw = sw;
                }
                t.find(".sui_scroll_bar.vertical").data("r", mv).data("rm", 0).data("m", t.find(".sui_scroll_bar_dongle").offset().top).find(".sui_scroll_bar_dongle").height(nh);
                t.find(".sui_scroll_bar.horizontal").data("r", mw).data("rm", 0).data("m", t.find(".sui_scroll_bar_dongle").offset().left).find(".sui_scroll_bar_dongle").width(nw);
            });
            return $(this);
        },
        sui_tooltip: function(c, o, e) {
            o = $.extend({}, $.fn.sui_tooltip.defaults, o);
            this.each(function() {
                var t = $(this);
                if (!c || c == "create") {
                    t.data("sui_tooltip_options", o);
                    t.data("sui_tooltip", true);
                    t.hover(function(e){t.sui_tooltip("before_show", null, e);t.sui_tooltip("show", null, e)},function(e){t.sui_tooltip("hide", null, e)});
                    if (o.parent_fade == true) {
                        t.parent().hover(function(){t.fadeIn("fast")},function(){t.fadeOut("fast")});
                        t.hide();
                    }
                    if (o.sticky_on_click == true) {
                        t.click(function(e){
                            var q = $(this);
                            if (q.data("sui_tooltip_content")) {
                                if (q.data("sui_tooltip_content").data("sui_tooltip_stuck") == true) {
                                    q.data("sui_tooltip_content").removeData("sui_tooltip_stuck");
                                    if (o.sticky_has_close == true) {
                                        q.data("sui_tooltip_content").find("div.sui_tooltip_sticky_handler").animate(o.sticky_handler_hide_animation, o.sticky_handler_hide_animation_length, "linear", function(){$(this).hide()});
                                        q.data("sui_tooltip_content").sui_moveable("remove");
                                    }
                                } else {
                                    q.data("sui_tooltip_content").data("sui_tooltip_stuck", true);
                                    if (o.sticky_has_close == true) {
                                        q.data("sui_tooltip_content")
                                        .find("div.sui_tooltip_sticky_handler").sui_hover().sui_press()
                                        .find("div.sui_tooltip_close_button").click(function(){q.data("sui_tooltip_content").sui_moveable("remove").sui_tooltip("force_hide")}).end()
                                        .css(o.sticky_handler_hide_animation).show()
                                        .animate(o.sticky_handler_show_animation, o.sticky_handler_show_animation_length, "linear", null);
                                    }
                                    q.data("sui_tooltip_content").sui_moveable();
                                }
                            }
                            e.preventDefault();
                        });
                    }
                } else if (c == "show") {
                    var d = t.data("sui_tooltip_options");
                    var a = null;
                    if (d.content_cb) {
                        a = d.content_cb(t);
                        if (typeof(a) != "number") {
                            $("body").append(a);
                            var ok = true;
                            for (var i = 0; i < sui.tooltip.active_list.length; i++) {
                                if (sui.tooltip.active_list[i].target.get(0) == t.get(0)) {
                                    ok = false;
                                }
                            }
                            if (ok == true) {
                                sui.tooltip.active_list.push({content: a, target: t});
                            }
                        } else {
                            if (sui.tooltip.active_list[a]) {
                                if (sui.tooltip.active_list[a].target.data("sui_tooltip_delayed_hide_timer_id")) {
                                    clearTimeout(sui.tooltip.active_list[a].target.data("sui_tooltip_delayed_hide_timer_id"));
                                    sui.tooltip.active_list[a].target.removeData("sui_tooltip_delayed_hide_timer_id");
                                }
                                sui.tooltip.active_list[a].content.stop().css(d.default_css);
                                sui.tooltip.active_list[a].target = t;
                                a = sui.tooltip.active_list[a].content;
                            }
                        }
                        t.data("sui_tooltip_content", a);
                    }
                    if (d.move_with_mouse == true) {
                        t.mousemove(function(e) {
                            if (!a.data("sui_tooltip_stuck")) {
                                a.move((d.restrict_x == true ? null : e.pageX + d.x_offset), (d.restrict_y == true ? null : e.pageY + d.y_offset), {window_edge_bound:d.window_edge_bound, left_edge_offset:20, top_edge_offset:20});
                            }
                        });
                        if (!a.data("sui_tooltip_stuck")) {
                            a.move(e.pageX + d.x_offset, e.pageY + d.y_offset, {window_edge_bound:d.window_edge_bound, left_edge_offset:20, top_edge_offset:20}).show();
                        }
                    } else {
                        if (!a.data("sui_tooltip_stuck")) {
                            a.move(t.offset().left + d.x_offset, t.offset().top + d.y_offset, {window_edge_bound:d.window_edge_bound, left_edge_offset:20, top_edge_offset:20}).show();
                        }
                    }
                } else if (c == "before_show") {
                    var d = t.data("sui_tooltip_options");
                    if (d.delayed_hide == false && t.data("sui_tooltip_content") && !t.data("sui_tooltip_content").data("sui_tooltip_stuck")) {
                        t.sui_tooltip("try_hide", null, e);
                    }
                } else if (c == "hide") {
                    var d = t.data("sui_tooltip_options");
                    if (t.data("sui_tooltip_content") && !t.data("sui_tooltip_content").data("sui_tooltip_stuck")) {
                        if (d.delayed_hide == false) {
                            t.sui_tooltip("try_hide", null, e);
                        } else {
                            t.data("sui_tooltip_delayed_hide_timer_id", setTimeout(function(){t.sui_tooltip("try_hide")}, d.delayed_hide));
                            t.data("sui_tooltip_content").animate(d.delayed_hide_animation, d.delayed_hide, 'linear', null);
                        }
                    }
                } else if (c == "try_hide") {
                    var d = t.data("sui_tooltip_options");
                    var ri = null;
                    var oe = false;
                    for (var i = 0; i < sui.tooltip.active_list.length; i++) {
                        if (sui.tooltip.active_list[i].target.get(0) == t.get(0)) {
                            ri = i;
                        } else if (sui.tooltip.active_list[i].content.get(0) == t.data("sui_tooltip_content").get(0)) {
                            oe = true;
                        }
                    }
                    if (ri != null && oe == false) {
                       sui.tooltip.active_list.splice(ri, 1);
                    }
                    if (t.data("sui_tooltip_delayed_hide_timer_id")) {
                        clearTimeout(t.data("sui_tooltip_delayed_hide_timer_id"));
                        t.removeData("sui_tooltip_delayed_hide_timer_id");
                    }
                    if (d.move_with_mouse == true) {
                        t.unbind("mousemove");
                    }
                    if (t.data("sui_tooltip_content")) {
                        t.data("sui_tooltip_content").remove()
                        t.removeData("sui_tooltip_content");
                    }
                } else if (c == "force_hide") {
                    var a = null;
                    for (var i = 0; i < sui.tooltip.active_list.length; i++) {
                        if (sui.tooltip.active_list[i].target.get(0) == t.get(0)) {
                            sui.tooltip.active_list.splice(i, 1);
                            if (t.data("sui_content_options").move_with_mouse == true) {
                                t.unbind("mousemove");
                                a = t.data("sui_tooltip_content");
                                t.removeData("sui_tooltip_content");
                                if (t.data("sui_tooltip_delayed_hide_timer_id")) {
                                    clearTimeout(t.data("sui_tooltip_delayed_hide_timer_id"));
                                    t.removeData("sui_tooltip_delayed_hide_timer_id");
                                }
                            }
                        } else if (sui.tooltip.active_list[i].content.get(0) == t.get(0)) {
                            if (sui.tooltip.active_list[i].target.data("sui_tooltip_delayed_hide_timer_id")) {
                                clearTimeout(sui.tooltip.active_list[i].target.data("sui_tooltip_delayed_hide_timer_id"));
                                sui.tooltip.active_list[i].target.removeData("sui_tooltip_delayed_hide_timer_id");
                            }
                            sui.tooltip.active_list[i].target.unbind("mousemove").removeData("sui_tooltip_content");
                            sui.tooltip.active_list.splice(i, 1);
                            a = t;
                        }
                    }
                    if (a != null) {
                        a.remove();
                    }
                } else if (c == "remove") {
                    t.removeData("sui_tooltip_options");
                    if (t.data("sui_tooltip_stuck") && t.data("sui_tooltip_content")) {
                        t.data("sui_tooltip_content").sui_moveable("remove");
                    }
                    t.removeData("sui_tooltip_stuck");
                    t.removeData("sui_tooltip_content");
                    t.removeData("sui_tooltip");
                    t.unbind("click");
                }
            });
            return $(this);
        },
        sui_hover: function() {
            this.each(function() {
                var t = $(this);
                if (!t.data("sui_hover")) {
                    t.hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")});
                    t.data("sui_hover", true);
                }
            });
            return $(this);
        },
        sui_press: function() {
            this.each(function() {
                var t = $(this);
                if (!t.data("sui_press")) {
                    t.mousedown(function(){$(this).addClass("pressed");return false;})
                    .mouseup(function(){$(this).removeClass("pressed")})
                    .mouseout(function(){$(this).removeClass("pressed")})
                    t.data("sui_press", true);
                }
            });
            return $(this);
        },
        sui_focus: function() {
            this.each(function() {
                var t = $(this);
                if (!t.data("sui_focus")) {
                    t.focus(function(){$(this).addClass("focused")})
                    .blur(function(){$(this).removeClass("focused")})
                    .data("sui_focus", true);
                }
            });
            return $(this);
        },
        sui_moveable: function(c) {
            var o = {
                handle_selector: ".sui_moveable_handle",
                window_edge_bound: false
            };
            if (c && typeof(c) == "object") {
                o = $.extend({}, o, c);
            }
            this.each(function() {
                var t = $(this);
                if (c && typeof(c) == "string" && c == "remove" && t.data("sui_moveable") === true) {
                    var z = t.find(t.data("sui_moveable_handle_selector"));
                    if (z && z.length > 0) {
                        z.mousedrag("unbind");
                        z.removeData("sui_moveable_handle");
                        z.removeData("sui_moveable_target");
                    } else {
                        t.mousedrag("unbind");
                        t.removeData("sui_moveable_handle");
                        t.removeData("sui_moveable_target");
                    }
                    t.removeData("sui_moveable_handle_selector");
                    t.removeData("sui_moveable_options");
                    t.removeData("sui_moveable");
                } else if (t.data("sui_moveable") == undefined) {
                    var z = t.find(o.handle_selector);
                    t.data("sui_moveable_handle_selector", o.handle_selector);
                    t.data("sui_moveable_options", o);
                    if (z && z.length > 0) {
                        z.data("sui_moveable_handle", true);
                        z.data("sui_moveable_target", t);
                        z.mousedrag({cb:$.sui_moveable_move,ignore_mouseout:true,ondragable:function(m){
                            m.addClass("dragable").data("sui_moveable_target").addClass("dragable")
                        },onundragable:function(m){
                            m.removeClass("dragable").data("sui_moveable_target").removeClass("dragable")
                        }}).sui_hover().sui_press();
                    } else {
                        t.data("sui_moveable_handle", true);
                        t.data("sui_moveable_target", t);
                        t.mousedrag({cb:$.sui_moveable_move,ignore_mouseout:true,ondragable:function(m){m.addClass("dragable").data("sui_moveable_target").addClass("dragable")},onundragable:function(m){m.removeClass("dragable").data("sui_moveable_target").removeClass("dragable")}}).sui_hover().sui_press();
                    }
                    t.data("sui_moveable", true);
                }
            });
            return $(this);
        }
    });
    $.fn.sui_tooltip.defaults = {
        sticky: false,
        sticky_on_click: false,
        sticky_has_close: false,
        sticky_handler_show_animation: {top: 0, height: 20},
        sticky_handler_show_animation_length: 100,
        sticky_handler_hide_animation: {top: 0, height: 20},
        sticky_handler_hide_animation_length: 100,
        move_with_mouse: false,
        window_edge_bound: true,
        restrict_x: false,
        restrict_y: true,
        default_css: {opacity: 1},
        delayed_show: false,
        delayed_show_animation: {opacity: 1},
        delayed_hide: false,
        delayed_hide_animation: {opacity: 1},
        x_offset: 0,
        y_offset: 10,
        parent_fade: false
    }
    $.fn.sui_tooltip.active_list = new Array();
    $.sui_scroll = function(e) {
        if ($.sui_scroll_is_scrolling == true) {
            return false;
        }
        $.sui_scroll_is_scrolling = true;
        var t = $(this);
        var p  = t.parents(".sui_scroll_bar:first");
        if (p.hasClass("vertical")) {
            var c = p.siblings(".sui_scroll_contents")
            t.move(null, t.offset().top - (t.lastPageY - e.pageY));
        } else if (p.hasClass("horizontal")) {
            t.move(t.offset().left - (t.lastPageX - e.pageX), null);
        }
        e.stopPropagation();
        $.sui_scroll_is_scrolling = false;
    }
    $.sui_scroll_is_scrolling = false;
    $.sui_moveable_move = function (e) {
        var t = $(this);
        if (t.data("sui_moveable_handle")) {
            var z = t.data("sui_moveable_target");
            var o = z.data("sui_moveable_options");
            if (z.css("position") == "absolute") {
                z.move(z.offset().left - (e.lastPageX - e.pageX), z.offset().top - (e.lastPageY - e.pageY), {window_edge_bound:o.window_edge_bound, left_edge_offset:20, top_edge_offset:20});
            } else if (z.css("position") == "fixed") {
                z.move(z.offset().left - $(document).scrollLeft() - (e.lastClientX - e.clientX), z.offset().top - $(document).scrollTop() - (e.lastClientY - e.clientY), {window_edge_bound:o.window_edge_bound, left_edge_offset:20, top_edge_offset:20});
            }
            e.preventDefault();
            e.stopPropagation();
        }
    }
})(jQuery);

$(document).ready(function() {
    $(".sui_button").sui_button();
    $(".sui_tab_container").sui_tab({onclick:function(e, o){$($(o).attr("href")).sui_scroll_reset()}});
    //$(".sui_scroll_container").sui_scroll({vertical: true});
    $(".sui_accordion_container").sui_accordion({keep_open:true});
});
