/**
 * initialization of the search engine.
 */
function initSearchEngine(){

  var field = jQuery("#destinationField"),
      slt = field.find("select"),
      sel = slt.find("option:selected"),
      cpt = jQuery("#cpt_csDestination"),
      ul = cpt.children("ul"),
      ulItem = [],
      i;

  if(typeof document.body.style.maxHeight === "undefined"){ // if IE6

    cpt.hide();
    slt.show();

  } else { // Other

    slt.hide();
    cpt.hide();

    for (i = 0; i < ul.length; i += 1) {
      ulItem[i] = ul.eq(i);
      if(i !== 0){
        ulItem[i].hide();
      }
    }

    /**
     * activates the button.
     */
    function activateButton(button){
      button.
        removeClass("disable").
        addClass("active");
    }

    /**
     * disables the button.
     */
    function disableButton(button){
      button.
        removeClass("active").
        addClass("disable");
    }

    /**
     * disable the 'next' button if there is just one list.
     */
    function nextButtonState(){
      var listsNumber = jQuery(".cpt_list").size();
      var nextButton = jQuery(".nextList").find("a");

      if (listsNumber <= 1){
        disableButton(nextButton);
      } else {
        activateButton(nextButton);
      }
    }

    /**
     * slides up the destinations selectbox.
     */
    function slideUpList(){
      cpt.slideUp(250, function(){
        cpt.
          removeClass("open").
          addClass("close");
        jQuery(document).unbind("click");
      });
    }

  /**
   * slides up the destinations selectbox.
   */
    function slideDownList(){
      cpt.slideDown(250, function(){
        cpt.
          removeClass("close").
          addClass("open");

        jQuery(document).bind("click", function(evt){
          var t = jQuery(evt.target),
              nav = t.closest("a").parent();

          if (nav.hasClass("cpt_nav")) {
            var method = nav.hasClass("prevList") ? "prev" : "next",
                prevButton = jQuery(".prevList").find("a"),
                nextButton = jQuery(".nextList").find("a"),
                currentList = jQuery("#cpt_csDestination").find(".cpt_list:visible"),
                followingList = currentList[method](".cpt_list"),
                followingfollowingList = followingList[method](".cpt_list");
            if(followingList.size() > 0){
              activateButton(prevButton);
              activateButton(nextButton);
              if(followingfollowingList.size() == 0){
                if(method == "prev"){
                  disableButton(prevButton);
                }else if(method == "next"){
                  disableButton(nextButton);
                }
              }
              currentList.hide();
              followingList.show();
            }
          } else if (t.parents(".cpt_list").hasClass("cpt_list")) {
            jQuery("#ipt_csDestination").val(t.text());
            jQuery("#destinationOption").val(t.attr("href"));
            jQuery("#destinationOptionText").val(t.text());
            slideUpList();
          } else {
            slideUpList();
          }

          return false;
        });
      });
    }

    function inputClickHandler(){
      var name = jQuery(this).attr("name"),
          ipt = jQuery("#ipt_csDestination"),
          iptOffset = ipt.offset(),
          iptTop = (iptOffset.top + ipt.outerHeight()) + "px",
          iptLeft = iptOffset.left + "px";

      cpt.css({top: iptTop, left: iptLeft});

      if (name == "ipt_csdestination"){
        if (cpt.hasClass("open")){
          slideUpList();
        } else {
          slideDownList();
        }
      } else {
        slideDownList();
      }
      return false;
    }
    nextButtonState();

    var ipt = jQuery("#ipt_csDestination").val();
    if (ipt == null){
      jQuery("<input />", {
        id: "ipt_csDestination",
        name: "ipt_csdestination",
        readonly: true,
        value: sel.text(),
        click: inputClickHandler
      }).insertAfter("#destinationField select");
    }

  }

}

jQuery(function() {
    initSearchEngine();
});


