
(function($){
$.fn.extend({
	combobox: function() {
		return this.each(function(i,obj){
		var selectId = (this.name||this.id)+'__jQSelect'+i||'__jQSelect'+i;
		if(obj.style.display != 'none' && $(this).parents()[0].id.indexOf('__jQSelect')<0){
		var tabindex = this.tabIndex||0;
		$(this).before("<div class='dropdown' id="+selectId+" tabIndex="+tabindex+"></div>").prependTo($("#"+selectId));
		var selectZindex = $(this).css('z-index'),selectIndex = $('#'+selectId+' option').index($('#'+selectId+' option:selected')[0]);
		$('#'+selectId).append('<div class="dropselectbox"><h4><span></span></h4><ul></ul></div>');
		$('#'+selectId+' h4 span').empty().append($('#'+selectId+' option:selected').text());
		//var selectWidth=$('#'+selectId+' select').width();
		var selectWidth=138;
		//if($.browser.safari){selectWidth = selectWidth+15}
		$('#'+selectId+' h4').css({width:selectWidth});
		var selectUlwidth = selectWidth /*+ parseInt($('#'+selectId+' h4').css("padding-left")) + parseInt($('#'+selectId+' h4').css("padding-right"))*/;
		//$('#'+selectId+' ul').css({width:selectUlwidth+'px'});
		//$('#'+selectId+' ul').css({'min-width':selectUlwidth+'px','width':'auto !important','width':selectUlwidth+'px'});
		$('#'+selectId+' select').hide();
		
		$('#'+selectId+' div').hover(function(){
			$('#'+selectId+' h4').addClass("over");
		},function(){
			$('#'+selectId+' h4').removeClass("over");
		});
		
		$('#'+selectId).hover(function(){
			
		},function(){
			if($('#'+selectId+' ul').css("display") == 'block'){
				$.fn.clearSelectMenu(selectId,selectZindex);
				return false;
			}
		});
		
		$('#'+selectId)
		.bind("click",function(e){
			if($('#'+selectId+' ul').css("display") == 'block'){
				$.fn.clearSelectMenu(selectId,selectZindex);
				return false;
			}else{
				$('#'+selectId+' h4').addClass("current");
				$('#'+selectId+' ul').show();
				var selectZindex = $(this).css('z-index');
				if ($.browser.msie || $.browser.opera){$('.dropdown').css({'position':'relative','z-index':'0'});}
				$('#'+selectId).css({'position':'relative','z-index':'999'});
				$.fn.setSelectValue(selectId);
				selectIndex = $('#'+selectId+' li').index($('.selectedli')[0]);
				/*var windowspace = ($(window).scrollTop() + document.documentElement.clientHeight) - $(this).offset().top;
				var ulspace = $('#'+selectId+' ul').outerHeight(true);
				var windowspace2 = $(this).offset().top - $(window).scrollTop() - ulspace;
				windowspace < ulspace && windowspace2 > 0?$('#'+selectId+' ul').css({top:-ulspace}):$('#'+selectId+' ul').css({top:$('#'+selectId+' h4').outerHeight(true)});
				$(window).scroll(function(){
					windowspace = ($(window).scrollTop() + document.documentElement.clientHeight) - $('#'+selectId).offset().top;
					windowspace < ulspace?$('#'+selectId+' ul').css({top:-ulspace}):$('#'+selectId+' ul').css({top:$('#'+selectId+' h4').outerHeight(true)});
				});	*/
				$('#'+selectId+' li').click(function(e){
						selectIndex = $('#'+selectId+' li').index(this);
						$.fn.keyDown(selectId,selectIndex);
						$('#'+selectId+' h4 span').empty().append($('#'+selectId+' option:selected').text());
						$.fn.clearSelectMenu(selectId,selectZindex);
						e.stopPropagation();
						e.cancelbubble = true;
				})
				.hover(
					   function(){
							$('#'+selectId+' li').removeClass("over");
							$(this).addClass("over").addClass("selectedli");
							selectIndex = $('#'+selectId+' li').index(this);
						},
						function(){
							$(this).removeClass("over");
						}
				);
			};
			e.stopPropagation();
		})
		.bind("selectstart",function(){
				return false;
		});
	}else if($(this).parents()[0].id.indexOf('__jQSelect')>0){
		selectId = $(this).parents()[0].id;
		$.fn.setSelectValue(selectId);
		var selectWidth=$('#'+selectId+' select').width();
		//if($.browser.safari){selectWidth = selectWidth+15}
		$('#'+selectId+' h4').css({width:selectWidth});
		var selectUlwidth = selectWidth /*+ parseInt($('#'+selectId+' h4').css("padding-left")) + parseInt($('#'+selectId+' h4').css("padding-right"))*/;
		//$('#'+selectId+' ul').css({width:selectUlwidth+'px'});
		//$('#'+selectId+' ul').css({'min-width':selectUlwidth+'px','width':'auto !important','width':selectUlwidth+'px'});
		if(this.style.display != 'none'){$(this).hide();}
	}})},
	clearSelectMenu:function(selectId,selectZindex){
		if(selectId != undefined){
			selectZindex = selectZindex || 5;
			$('#'+selectId+' ul').empty().hide();
			$('#'+selectId+' h4').removeClass("over").removeClass("current");
			$('#'+selectId).css({'z-index':selectZindex});
		}
	},
	setSelectValue:function(sID){
		var content = [];
		
		$.each($('#'+sID+' option'), function(i){
			content.push("<li class='FixSelectBrowser'>"+$(this).text()+"</li>");
		});
		content = content.join('');
		$('#'+sID+' ul').html(content);
		$('#'+sID+' h4 span').html($('#'+sID+' option:selected').text());
		$('#'+sID+' li').eq($('#'+sID+' select')[0].selectedIndex).addClass("over").addClass("selectedli");
		var w=$('#'+sID+' ul').width();
		$('#'+sID+' li').css({width:w+'px'});
	},
	keyDown:function(sID,selectIndex){
		var $obj = $('#'+sID+' select');
		$obj[0].selectedIndex = selectIndex;
		$obj.change();
		$('#'+sID+' li:eq('+selectIndex+')').toggleClass("over");
		$('#'+sID+' h4 span').html($('#'+sID+' option:selected').text());
	}
	
});

})(jQuery);