/*  
* 		jCombox 1.0b | 2008.08.25
*		jQuery: HTML Select Tag Replacement Plugin 1.0b
*		Licensed under a Creative Commons Attribution 3.0 License
*
*		Tested to work on Firefox2/3, IE6/7, Safari3.1(Win) and Opera9.5 versions
*		Limitations: no keyboard usage + no multiple selections > checkout next version
*		_______________________________________________
* 		http://jquerylab.com | Sponsored by http://12tb.com
*		ruvanepa@gmail.com
*
*		How-to use: $('.select').jcombox();
*			or replce all <select> tags: $('select').jcombox();
*/
(function ($)
{
    $.fn.setOffCss = function (a)
    {
        var b = this.parent();
        b.css('position', 'relative');
        return this.css({
        	'postion' : 'relative',
        	'left'    : -1+'px',
        	'width'   : b.width()
        });
    };
    $.fn.fadesTo = function (o)
    {
        return this.animate(
        {
            opacity : o
        },
        0, function ()
        {
            if (o == 1 && $.browser.msie) {
                this.style.removeAttribute("filter")
            }
        })
    };
    $.fn.jcombox = function (o)
    {
        o = $.extend(
        {
            fn : null, fx : "toggle", fxType : "easeOutBounce", fxDelay : 0, set : false, theme : "default", 
            linkUrl : null
        },
        o || {});
        return this.each(function ()
        {
            var b = $(this), _7 = "", _8 = b.attr("name"), _9 = $("option", b).eq(0).text(), _a = $("option", 
            b).eq(0).val(), _b = "#" + _8, _c = b.attr("size"), _d = b.attr("rows") || 0, _e = b.attr("width") || 0, 
            _f = b.attr("disabled") || 0, _10 = b.attr("onchange") || b.attr("onChange") || 0;
            $("option", b).each(function ()
            {
                if ($(this).attr("selected")) {
                    _9 = $(this).text();
                    _a = $(this).val()
                }
                _7 += ("<a href='" + (o.linkUrl != null ? "?" + _8 + "=" + $(this).val() : "#") + "' rel='" + $(this).val() + "' >" + $(this).text() + "</a>")
            });
            if (_c > 0)
            {
                b.replaceWith("<div class='jcombox-m " + o.theme + "' id='" + _8 + "'><input type='hidden' name='" + _8 + "' value='" + _a + "' /><div>" + _7 + "</div></div>");
                $(_b).css("background-image", "none");
                if (!$("div a", _b).eq(0).text()) {
                    $("div a", _b).eq(0).text("-")
                }
                $(_b).width($("div", _b).width() + 40 + "px");
                $(_b).height($("div a", _b).height() * _c + ($.browser.msie ? 4 : 2) + "px")
            }
            else
            {
                b.replaceWith("<div class='jcombox " + o.theme + "' id='" + _8 + "'><span>" + _9 + "</span><input type='hidden' name='" + _8 + "' value='" + _a + "' /><div class='menu'>" + _7 + "</div></div>");
                var c = $(_b);
                if ($("span", _b).width() > $("div", _b).width()) {
                    $(_b).width($("span", _b).width() + 35 + "px")
                }
                else {
                    $(_b).width($("div", _b).width() + 40 + "px")
                }
                if (_d > 1) {
                	$("div", _b).height($("span", _b).height() * _d + ($.browser.msie ? 2 : 0) + "px")
                }
                $(document).click(function (e)
                {
                    if ($("div", _b).css("display") == "block") {
                        $("div", _b).hide()
                    }
                });
                c.hover(function ()
                {
                    clearTimeout(this.timeout)
                },
                function ()
                {
                    this.timeout = setTimeout(function ()
                    {
                        if ($("div", _b).css("display") == "block") {
                            c.trigger("click")
                        }
                    }, 750)
                });
                if (!$("span", _b).text()) {
                    $("span", _b).text("-")
                }
                if (o.set)
                {
                    c.css({
                        "border" : "transparent", "background-color" : "transparent", "padding-right" : "2px"
                    });
                    $("span", _b).css(
                    {
                        "border" : "transparent", "background-image" : c.css("background-image"), "background-position" : "left center", 
                        "padding" : "0 0px 3px 5px"
                    })
                }
            }
            $("div", _b).find("a[rel='" + $(":hidden", _b).val() + "']").addClass("current");
            $(_b).bind("jcDisable", function ()
            {
                $(":hidden", _b).val("");
                if ($("div", _b).css("display") == "block" && !_c > 0) {
                    $("div", _b).hide()
                }
                $(_b).fadesTo(0.3).unbind("click");
                $("div a", _b).unbind("click").bind("click", function ()
                {
                    return false;
                })
            });
            $(_b).bind("jcEnable", function ()
            {
                $(this).fadesTo(1);
                $(":hidden", this).val($(".current", this).attr("rel"));
                if (!_c > 0)
                {
                	$(_b).unbind("click").bind("click", function (e)
                    {
                    	var a = $("div", _b);
                    	switch (o.fx)
                        {
                            case "toggle":
                            	a.setOffCss().toggle();
                                break;
                            case "slide":
                                a.setOffCss().slideToggle(o.fxDelay);
                                break;
                            case "fade":
                                a.setOffCss().animate({
                                    opacity : "toggle"
                                },
                                o.fxDelay).fadesTo(1);
                                break;
                            case "slideFade":
                                a.setOffCss().animate({
                                    opacity : "toggle", height : "toggle"
                                },
                                o.fxDelay).fadesTo(1);
                                break;
                            case "easing":
                                a.setOffCss().slideToggle(o.fxDelay, o.fxType);
                                break
                        }
                        e.stopPropagation()
                    })
                }
                $("a", _b).unbind("click").bind("click", function ()
                {
                    if ($(this).hasClass("disabled")) {
                        return false
                    }
                    $("a", _b).removeClass("current");
                    $(this).addClass("current");
                    if ($.browser.safari) {
                        $(this).fadesTo(0.99).fadesTo(1)
                    }
                    if (!_c > 0) {
                        $("span", _b).text($(this).text());
                        $(_b).trigger("click")
                    }
                    $(":hidden", _b).val($(this).attr("rel"));
                    if (o.fn) {
                        o.fn()
                    }
                    if ($.browser.msie && _10) {
                        eval(_10())
                    }
                    else {
                        if (_10) {
                            eval(_10)
                        }
                    }
                    return false;
                });
                $("a", this).css("color", $("." + o.theme).css("color"));
                $("span", this).css("overflow", "hidden")
            });
            if (b.attr("disabled")) {
                $(_b).trigger("jcDisable")
            }
            else {
                $(_b).trigger("jcEnable")
            }
            $(_b).bind("selectstart mousedown", function ()
            {
                return false;
            }).css("MozUserSelect", "none");
            $(_b).hover(function ()
            {
                $(this).addClass(o.theme + "-hover");
                if (o.set) {
                    $("span", _b).css("background-image", c.css("background-image"))
                }
            },
            function ()
            {
                $(this).removeClass(o.theme + "-hover");
                if (o.set) {
                    $("span", _b).css("background-image", $("." + o.theme).css("background-image"))
                }
            });
            if (_e) {
                $(_b).width(_e + "px")
            }
        })
    };
    var d = ["jcEnable", "jcDisable"];
    for (var i = 0; i < d.length; i++) {
        $.fn[d[i]] = (function (a)
        {
            return function ()
            {
                this.trigger(a)
            }
        })(d[i])
    }
    $.fn.jcSelected = function (a)
    {
        if (a) {
            return $(".current", this).text()
        }
        else {
            return $(":hidden", this).val();
        }
    };
    $.fn.jcAdd = function (a, b, c)
    {
        if (c) {
            $("a.current", this).removeClass()
        }
        $("div", this).append("<a " + (c ? "class='current'" : "") + " href='#' rel='" + a + "' >" + b + "</a>");
        if (c) {
            $("span", this).text($(".current", this).text())
        }
        this.jcEnable()
    };
    $.fn.jcAddAll = function (a, c, n_a, n_b)
    {
        if (c) {
            $("a.current", this).removeClass()
        }
        var appendsNode = new Array();
        var count       = 0;
        var max_length  = 1;
        $.each( a, function(){
        	var text_length = E1gp.commons.mb_strlen(this[n_b]);
        	if ( max_length < text_length ) max_length = text_length;
        	appendsNode.push("<a " + (c == count ? "class='current'" : "") + " href='#' rel='" + this[n_a] + "' >" + this[n_b] + "</a>")
        	count++;
        });
        $("div", this).append(appendsNode.join(''));
        if (c) {
            $("span", this).text($(".current", this).text())
        }
        this.jcEnable();
       	return {
       		count : count,
       		max_length : max_length
       	}
    };
    $.fn.jcEdit = function (a, b)
    {
        $(".current", this).replaceWith("<a class='current' href='#' rel='" + a + "' >" + b + "</a>");
        this.jcEnable();
        if ($(".current", this).text()) {
            $("span", this).text($(".current", this).text())
        }
    };
    $.fn.jcClear = function (a)
    {
        if (a) {
            $("a.current", this).removeClass()
        }
        else {
            $(".current", this).slideUp(function ()
            {
                $(this).remove()
            })
        }
        this.jcEnable();
        $("span", this).text("-");
        $(":hidden", this).val("")
    }
    $.fn.jcClearAll = function (a)
    {
        if (a) {
            $("a.current", this).removeClass()
        }
        else {
            $("a", this).slideUp(function ()
            {
                $(this).remove()
            })
        }
        this.jcEnable();
        $("span", this).text("-");
        $(":hidden", this).val("")
    }
})(jQuery);