$j(document).ready(function(){
  $j("select").replaceSelect(150);
});


$j(document).keyup(function(e){
	var obj_visible = $j(".repDD").css("display");
	if(e.keyCode == 27){
		switch (obj_visible){
			case 'block':
				$j(".repDD").hide();
				$j("#temp").remove();
				break;
			default:
				$j(".widgets").remove();
				break;

		}
	}else if( $j("#buffer").length != 0){
		try{
			clearTimeout(t);			
		}catch(err){

		}
		
		bufferVal = bufferVal + String.fromCharCode(e.keyCode);
			regString = '^'+bufferVal;
			regString = new RegExp(regString,'i')
		$j("#activeList li").each(function(){
			if($j(this).html().match(regString)){
				$j(this).css({"display":"block"});
			}else{
				$j(this).css({"display":"none"});
			}
		})
		t = setTimeout('clearBuffer()',1000);
	}
});

$j(document).keypress(function(e){
	if($j("#buffer").length != 0){
		if(e.keyCode === 8) {
			return false;
		}
		
	}
});

jQuery.fn.replaceSelect = function(x) {
  var speed = x;
  $j(this).each(function(){
  initialVal = $j(this).val();
  //construct new select's environment
      classs = $j(this).attr("class");
      $j(this) .bind("click", function(){return false;})
              .wrap('<span class="selectWrapper '+classs+'" style="display: inline-block; position: relative" />')
              .hide().parent()
              .append(
                '<span class="replaced" tabindex="1">'+initialVal+'</span><div class="repDD" style="display:none"><div><ul></ul></div></div>')
              .find("option").each(function(){
                  if($j(this).val() == 'Please select'){
        					  data ='';
			        	  }else{
                    data = '<li>'+$j(this).val()+'</li>';
        				  }				  
                  $j(this).parent().siblings(".repDD").find("ul").append(data);
              });
      if($j(this).find('option').length == 1){
        $j(this).siblings("span").addClass('inactive');
      }
    });

        
        //second part of select replacement script
        $j("span.replaced").live("click", function(e){
          //define list

          var list = $j(this).siblings(".repDD").find("ul");
          var dd = $j(this).siblings(".repDD")
          bufferVal = '';
          $j(list).find('li').show();


          if($j(list).find('li').length == 0){
        	return false;
        	}
          //handle addition of temporary div that is used to create overlay effect and/or help with closing dropdowns 
          if(($j("#activeList").css("display") == 'none')||($j("#activeList").length == 0)){
          $j("body").append('<input id="buffer" type="hidden" />');
           $j(this).parent().css({
              'z-index':'9999'
              });
            $j(this).before('<div id="temp">');
          }else{
           $j(this).parent().css({"z-index":""})
           $j("#temp").remove();
           $j("#buffer").remove();
          }
          //remove id class from all other dropdowns that are not siblings to clicked select button
          $j(".repDD").not(dd).attr({"id":''}).removeClass("active");
          // and sets the current list active
          dd.attr({"id":"activeList"}).addClass("active");
          //hide list that was previously opened
          if($j("#activeList").css("display") == 'none'){
            $j(".repDD").not("#activeList").hide();
          }else{
          }
          //set current list active, open it, and set proper z-index on it's select wrapper
          $j("#activeList")
            .stop()
            .animate({"height":"toggle"}, speed)
            .addClass("active");
        });

        $j(".repDD li").live("click", function(){
          value = $j(this).html();
          $j(this).parents(".selectWrapper").find("span.replaced").html(value).siblings("select").val(value)
          $j(this).closest(".repDD").animate({"height":"toggle"}, speed);
          $j("#temp").remove();
          $j(".selectWrapper").css({"z-index":""});
          var select = $j(this).parents(".repDD").siblings("select").trigger("change");
          $j("#buffer").remove();
        });

        $j("#temp").live('click',function(){
          $j("#temp").remove();
          $j(".selectWrapper").css({"z-index":""});
          $j("#activeList").animate({"height":"toggle"}, speed);
           $j("#buffer").remove();
        });
}  
