/* IMPORTANT NOTE: 
This file has been substantially modified from the originally obtained "Dropdown Select" 
jQuery plug-in. The file should not be upgraded to a newer version of the plug-in or the 
custom coded functionality will be lost and the dropdowns may not render properly.
*/
  
  function createDropDown(origSelName, hideOtherElements, truncateAtChar, callbackOnSelect){
    
    var source = $("#" + origSelName);
    var selected = source.find("option[selected]");  // get selected <option>
    var options = $("option", source);  // get all <option> elements

    // create <dl> and <dt> with selected value inside it
    $("#" + origSelName + "Wrapper").append('<dl id="' + origSelName + '_dropdown" class="dropdown"></dl>')
    $("#" + origSelName + "_dropdown").append('<dt><a href="javascript:void(0)">' + $("#" + origSelName + " option:selected").text() + 
        '<span class="value">' + $('#' +origSelName).val() + 
        '</span></a></dt>')
    $("#" + origSelName + "_dropdown").append('<dd><ul></ul></dd>')
    
    // iterate through all the <option> elements and create UL
    options.each(function(){
      cVal = $(this).val();
      if (cVal != "") $("#" + origSelName + "_dropdown dd ul").append('<li><a href="javascript:void(0)">' + 
          $(this).text() + '<span class="value">' + 
          $(this).val() + '</span></a></li>');
    });
      
		$("#" + origSelName + "_dropdown dt a").click(function() {
					dropdownFound = false;
					state = $("#" + origSelName + "_dropdown dd ul").css("display")
					$("#" + origSelName + "_dropdown dd ul").toggle();
					
					$(".dropdown").each(function(){
						if (state == "block") {
							if (hideOtherElements) $("#" + this.id).parent().removeClass("selectWrapperHidden");
						} else {
							if (dropdownFound) {

								if (hideOtherElements) $("#" + this.id).parent().addClass("selectWrapperHidden");
							}
							if(this.id == (origSelName + "_dropdown")) dropdownFound = true;
						}
						if(this.id != (origSelName + "_dropdown")) $("#" + this.id + " dd ul").hide();
					});
		});

    $(document).bind('click', function(e) {
      var $clicked = $(e.target);
      if (! $clicked.parents().hasClass("dropdown")) {
				$(".dropdown dd ul").hide();
				if (hideOtherElements) $(".dropdown").parent().removeClass("selectWrapperHidden");
			}
    });
		
    $("#" + origSelName + "_dropdown dd ul li a").click(function() {
			
      var text = $(this).html();
			if (truncateAtChar) {
				parts = text.split("<span");
				text = parts[0];
				if (text.length>truncateAtChar) text = text.substring(0, truncateAtChar) + "...";
			}
      $("#" + origSelName + "_dropdown dt a").html(text);
      $("#" + origSelName + "_dropdown dd ul").hide();
      $(".dropdown dd ul").hide();
			if (hideOtherElements) $(".dropdown").parent().removeClass("selectWrapperHidden");
      var source = $("#" + origSelName);
      source.val($(this).find("span.value").html())
			
			if (callbackOnSelect) eval(callbackOnSelect);
			
    });
    
    // hide the original select element
    $("#" + origSelName).css("visibility", "hidden");
  }
