var initShowSelect = function(){
   //The mask
   var div = '<div id="themask">&nbsp;</div>';
   $("#outerPage").before(div);
   
   //Hide the list of options
   $(".holder").css("visibility", "hidden");
   
   //Empty all input value
   $(".advancedSearch input:hidden").attr("value", "");
   
   //When click on a select title
   $(".select a").click( function () {
      $(this).blur();
      var theParent = $(this).parent();
      /* If it was not opened, open it */
      if (theParent.next(".holder").css("visibility")== "hidden") {
         hideOpenedSelect();
         theParent.next(".holder").css("visibility", "visible");
         theParent.addClass("selectOpened");
         $("#themask").show();
      }
      /* If it was opened, close it */
      else {
         theParent.next(".holder").css("visibility", "hidden");
         theParent.removeClass("selectOpened");
         $("#themask").hide();
      }
      return false;
   });
   
   //Click on the mask to hide the opened list
   $("#themask").click( function () {
      hideOpenedSelect();
      $(this).hide();
   });
}

//Hide the opened list
var hideOpenedSelect = function(){
   var selectOpened = $(".selectOpened");
   selectOpened.next(".holder").css("visibility", "hidden");
   selectOpened.removeClass("selectOpened");
}

//Reset the selected value
var resetSelect = function(){
   $(".wasSelected").find("a span").text($(".wasSelected").find("a span").attr("title"));
   $(".wasSelected").removeClass("wasSelected");
   
   $("input.selectedValue").attr("value", "");
   $("input.selectedValue").removeClass("selectedValue");
}


var tmpHeight;
//Select an option
var selectValue = function(){
	$(".selectValue li a").click( function (){
      //resetSelect();
		$(".listOfOptions li ul").css("display", "none");
      var theParent = $(this).parent();
      while(theParent.attr("id")==undefined){
         theParent = theParent.parent();
      }
      var id = theParent.attr("id");
      var theInput = $("#" + id + "Field");
      theInput.val($(this).text());
      theInput.addClass("selectedValue");
      $("#" + id + "Holder").css("visibility", "hidden");
	   $("#" + id + "Select").find("a span").text($(this).text());
      $("#" + id + "Select").addClass("wasSelected");
	   $("#" + id + "Select").removeClass("selectOpened");   
      return false;
   });
	
	/* MODIFIED on 03/30/2009 */
   $(".selectValue li").mouseover( function (){
      $(this).find("ul").show();
   });
   $(".selectValue li").mouseout( function (){
      $(this).find("ul").hide();
   });
	/* END MODIFIED on 03/30/2009 */
}

var selectSubMenu = function(){
	$(".selectSubMenu li a").click( function (){
      resetSelect();
		$(".listOfOptions li ul").css("display", "none");
      var theParent = $(this).parent();
      while(theParent.attr("id")==undefined){
         theParent = theParent.parent();
      }
      var id = theParent.attr("id");
      var theInput = $("#" + id + "Field");
      theInput.attr("value", $(this).text());
      theInput.addClass("selectedValue");
      $("#" + id + "Holder").css("visibility", "hidden");
	   $("#" + id + "Select").find("a span").text($(this).text());
      $("#" + id + "Select").addClass("wasSelected");
	   $("#" + id + "Select").removeClass("selectOpened");
		
		var listId = $(this).attr("class");
		$("#pdtSubFamily").empty();
		$("#pdtSubFamily").html($("#"+listId).html());
		selectValue();
		reinitialiseSelect();
      return false;
   });	
}

var reinitialiseSelect = function(){
	if($('.scroll-pane').is("div")) $('.scroll-pane').jScrollPane({showArrows:true , scrollbarWidth: 18, dragMinHeight : 17, dragMaxHeight : 17});
}

//onload
$(document).ready(function (){
   initShowSelect();
   selectValue();
	if($(".selectSubMenu li a") != "undefined") selectSubMenu();
});
