(function($){
    $.fn.setOffCss = function(a){
		var b = this.parent();
		var c = this.parent().parent();
        if ($(window).height() + (document.documentElement.scrollTop || document.body.scrollTop) < b.offset().top + this.height() + b.height()) {
            objTop = b.offset().top - ($.browser.msie ? 3 : 1) - this.height()
        }
        else {
            objTop = b.offset().top + ($.browser.msie ? -1 : 1) + b.height()
        }
		//alert(b.offset().left)
        return this.css({
            left: b.offset().left - ($.browser.msie ? 0 : 0),
            top: objTop + ($.browser.msie ? 2 : 0),
			//left: b.offset().left - ($.browser.msie ? 2 : 0),
            //top: c.height(),
            width: b.width() + ($.browser.msie ? 0 : 0)
        })
    };
    $.fn.fadesTo = function(o,t){
		if(typeof(t)=='undefined'){
			t=0;
		}
		if(this.css('opacity')==o){
			return this;
		}
		/*
		return this.fadeTo(t,o,function(){
            if (o == 1 && $.browser.msie) {
                this.style.removeAttribute("filter")
            }
        });
        */
		
		return this.animate({
            opacity: o
        }, t, function(){
            if (o == 1 && $.browser.msie) {
                this.style.removeAttribute("filter")
            }
        })
        
    };
    $.fn.jcombox = function(o){
       
		o = $.extend({
            fn: null,
            fx: "toggle",
            fxType: "easeOutBounce",
            fxDelay: 200,
            theme: "default",
            linkUrl: null,
			afterSelect:null,
			associateSelect:null,
			rows:6,
			height:28,
			lastword:false,
			isfloat:true
        }, o ||
        {});
        return this.each(function(){
            
			var b = $(this), _7 = "", 
				_8   = b.attr("name"), 
				_9   = $("option", b).eq(0).html(),
				_a   = $("option", b).eq(0).val(), 
				_b   = "#" + _8, 
				_c   = b.attr("size"), 
				_d   = o.rows || 0, 
				_e   = o.width || $(this).css('width').split('px')[0], 
				_f   = b.attr("disabled") || 0, 
				_10  = b.attr("onchange") || b.attr("onChange") || 0;
				_mul = b.hasClass('multiple') ? true : false;
			
			if(b.find('option[value=0]').length==1){
				_ti  = b.find('option[value=0]').html();
				_val = b.find('option[value=0]').val();
			}else{
				_ti  = b.find('option:first').html();
				_val = b.find('option:first').val();
			}
			
			if(b.hasClass('nofloat')){
				o.isfloat=false;
			}
			
            var cpt=0;

			$("option", b).each(function(){
				if ($(this).attr("selected")) {
                    _9 = $(this).html();
                    _a = $(this).val()
					//alert($(this).html())
                }
				if(!_mul){
                	_7 += ("<a href='" + (o.linkUrl != null ? "?" + _8 + "=" + $(this).val() : "#") + "' rel='" + $(this).val() + "' >" + $(this).html() + "</a>");
				}else{
					if(cpt>0 || _val!='0'){
						var mystyle='';
						var myclass='';
						if($(this).hasClass('checked')){
							myclass='checked="checked"';
						}
						
						if(o.isfloat){
							mystyle = ' style="float:left;width:50%;"';
						}

						_7 += ('<li'+mystyle+'><input '+myclass+' type="checkbox" value="' + $(this).val() + '" id="input-'+_8+'-'+cpt+'" /><label for="input-'+_8+'-'+cpt+'">'+ $(this).html() + '</label></li>')
					}
				}
				cpt++;
            });

			if(_mul && cpt>0){
				//_7 = '<ul>'+_7+'<li'+mystyle+'><button type="button" class="normal">Terminer</button></li></ul>';
				if($('body').attr('id')=='lang-fr'){
					mytexte = 'Terminer';
				}else{
					mytexte = 'Close';
				}
				if(o.isfloat){
					mystyle = ' style="float:left;width:50%;"';
				}else{
					mystyle='';
				}
				_7 = '<ul>'+_7+'<li'+mystyle+'><input type="button" value="'+mytexte+'" class="button_normal" /></li></ul>';
				if(o.isfloat){
					_d = (cpt/2)+1;
				}else{
					_d = cpt+2;
				}
			}
			
			if(_b.length<2){
				alert('no name attr for your select');
			}
			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).html()) {
                    $("div a", _b).eq(0).html("-")
                }
                $(_b).width($("div", _b).width() + 40 + "px");
                
				$(_b).height($("div a", _b).height() * _c + ($.browser.msie ? 4 : 2) + "px")
            } else {
                if(o.lastword){
					classname = 'lastword ';	
				}else{
					classname = '';	
				}
				b.replaceWith("<div class='jcombox " + classname+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(o.height * _d + ($.browser.msie ? 5 : 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).html()) {
                    $("span", _b).html("-")
                }
            }
		
            $("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.5,0).unbind("click");
				$(_b).addClass(o.theme+'-disable');
                $("div a", _b).unbind("click").bind("click", function(){
                    return false
                })
            });
			
            $(_b).bind("jcEnable", function(){
				$(this).fadesTo(1,500);
                $(this).removeClass(o.theme+'-disable');
				$(":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()
                    })
                }
				$("input:checkbox, label",_b).bind('click',function(e){
					var str = '';
					var strLabel='';
					$('input:checkbox:checked',_b).each(function(){
						strLabel += $(this).parent().find('label').text()+', '
						str += $(this).val()+'-';
					})
					strLabel = strLabel.substr(0,strLabel.length-2);
					
					if(strLabel.length>38){
						strLabel = strLabel.substr(0,38)+'...';
					}
					if(strLabel.length==0){
						strLabel = _ti;
						$('input:hidden',_b).val(_val);
					}else{
						//update du input
						$(':hidden',_b).val(str);
					}
					
					$('span:first',_b).html(strLabel);
					
					if ($.browser.msie && _10) {
						eval(_10())
                    } else {
                        if (_10) {
							_10()
                        }
                    }
					
					e.stopPropagation();
				})
				$("label:first",_b).trigger('click');
				//$("input:checkbox",_b).unbind('click');
                $("button",_b).unbind('click').bind('click',function(){
					$(_b).trigger("click");
					return false;
				})
				$("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).html($(this).html());
                        $(_b).trigger("click")
                    }
                    $(":hidden", _b).val($(this).attr("rel"));
                    if (o.fn) {
                        o.fn()
                    }
                    if ($.browser.msie && _10) {
						eval(_10())
                    } else {
                        if (_10) {
							_10()
                        }
                    }
					if(typeof(blur_focus_action)=='function'){
						blur_focus_action($(_b),$(this).parents('form').attr('id'));
					}
					if(typeof(o.afterSelect)=='function'){
						o.afterSelect($(this),o.associateSelect);
					}
                    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(){
				if(!$(this).hasClass(o.theme+'-disable')){
                	$(this).addClass(o.theme + "-hover");
				}
            }, function(){
                $(this).removeClass(o.theme + "-hover");
            });
			if($.browser.msie){
				//alert(_e)
				//return;
			}
            if (_e) {
				if(_e=='auto'){
					alert('no size is set')	
					return;
				}
				$(_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).html()
        }
        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).html($(".current", this).html())
        }
        //this.jcEnable()
    };
    $.fn.jcEdit = function(a, b){
        $(".current", this).replaceWith("<a class='current' href='#' rel='" + a + "' >" + b + "</a>");
        this.jcEnable();
        if ($(".current", this).html()) {
            $("span", this).html($(".current", this).html())
        }
    };
    $.fn.jcClear = function(a){
        if (a) {
            $("a.current", this).removeClass()
        }
        else {
            $(".current", this).slideUp(function(){
                $(this).remove()
            })
        }
        this.jcEnable();
        $("span", this).html("-");
        $(":hidden", this).val("")
    }
	$.fn.jcAllClear = function(){
    	$("a:not([rel='0'])",this).remove();
    };
	$.fn.jcSelect = function(a){
		value = $("a[rel='"+a+"']",this).attr('rel');
		text  = $("a[rel='"+a+"']",this).html();
		$("span",this).html(text);
		$(":hidden", this).val(value);
	}
})(jQuery);
