/******************************************************************************
 * JQUERY JAVASCRIPT CODE                                                     *
 ******************************************************************************/
jQuery(document).ready(function() {

  /* **************************************************************************
   * MAIN NAVIGATION                                                          *
   ****************************************************************************/

  /* hide all submenus on page load. */
  jQuery("#emMainMenu > ul > li > ul").hide();

  /* remove the arrow image of all menu items
   * which do not have a submenu */
  jQuery("ul > li").not(":has(ul)").each(function() {
    jQuery("a", jQuery(this)).css("background-image", "none");
  });

  /* save the currently active menu item */
  var selMMItem = jQuery("#emMainMenu > ul > li:has(.active)").find(":first");

  /* show submenu on mouse over mainmenu item */
  jQuery("#emMainMenu > ul > li:has(a)").hover(function() {
    /* activate the selected mainmenu item */
    jQuery(":first", jQuery(this)).toggleClass("hover");
    /* deselect the current mainmenu item */
    selMMItem.toggleClass("active");
    /* hide the current submenu */
    jQuery("#emMainMenu > ul > li:has(.active) > ul").hide();

    if (jQuery(this).children().size() > 0) {
      /* hide all other submenus */
      jQuery("> ul", jQuery(this).siblings()).hide();
      /* fade in the submenu of the current hovered mainmenu item */
      jQuery("> ul", jQuery(this)).fadeIn("normal");
    }
  }, function() {
    if (!jQuery(":first", jQuery(this)).hasClass("active")) {
      /* fade out the selected submenu */
      jQuery("> ul", jQuery(this)).fadeOut("slow");
      /* reselect the current mainmenu item */
      selMMItem.toggleClass("active");
    }

    /* activate the selected mainmenu item */
    jQuery(":first", jQuery(this)).toggleClass("hover");
  });

  /* the activated submenu item should be temporary deselected
   * when navigating on other items on the same level. */
  var selMMSubItem = jQuery("#emMainMenu > ul > li > ul > li:has(.active)").find(":first");
  jQuery("#emMainMenu > ul > li > ul > li:has(a)").hover(function() {
    /* deselect the current menu subitem */
    selMMSubItem.toggleClass("active");
  }, function() {
    /* reselect the current menu subitem */
    selMMSubItem.toggleClass("active");
  });

  jQuery("#emMainMenu > ul > li > ul").hover(function() {
    /* deselect the current menu subitem */
    //jQuery(":first", jQuery(this)).toggleClass("active");
    jQuery(this).siblings().toggleClass("active");
  }, function() {
    /* reselect the current menu subitem */
    //jQuery(":first", jQuery(this)).toggleClass("active");
    jQuery(this).siblings().toggleClass("active");
  });

  /* **************************************************************************
   * SUB NAVIGATION                                                           *
   ****************************************************************************/

  /* if a page menu item is selected, deselect the current active item. */
  var selectedSubmenuItem = jQuery("#emSubMenu").find(".active");

  /* Show the menu of the current active item. */
  jQuery("#emSubMenu li:has(.active)").parent().show();

  /* Show the menu of the current active item. */
  jQuery("#emSubMenu li:has(.active) > ul").show();

  jQuery("#emSubMenu > ul > li").hover(function() {
    /* Unmark the currently active item. */
    jQuery(selectedSubmenuItem).toggleClass("active");

    /* Show the sub navigation of the selected item. */
    jQuery("> ul", jQuery(this)).show();
  }, function() {
    /* Remark the currently active item. */
    jQuery(selectedSubmenuItem).toggleClass("active");

    /* Hide the sub navigation of the selected item. */
    jQuery("> ul", jQuery(this)).hide();

    /* show the currently active sub sub navigation */
    jQuery("#emSubMenu > ul > li > ul > li:has(.active)").parent().show();

    /* show the submenu of the currently active menu item. */
    jQuery("#emSubMenu > ul > li:has(.active) > ul").show();
  });

  /* **************************************************************************
   * MEMBER NAVIGATION                                                        *
   ****************************************************************************/

  /* Store the currently active item. */
  var selPageMzMenuItem = jQuery("#emMzMenu").find(".active");

  /* Show the menu of the current active item. */
  jQuery("#emMzMenu div li:has(.active)").parent().show();

  /* Show the menu of the current active item. */
  jQuery("#emMzMenu div li:has(.active) > ul").show();

  /*
   * 1st Level
   */
  jQuery("#emMzMenu > ul > div > li").hover(function() {
    /* Unmark the currently active item. */
    jQuery(selPageMzMenuItem).toggleClass("active");
//    /* Show the sub navigation of the selected item. */
//    jQuery("> ul", jQuery(this)).show();
  }, function() {
    /* Remark the currently active item. */
    jQuery(selPageMzMenuItem).toggleClass("active");
//    /* Hide the sub navigation of the selected item. */
//    jQuery("> ul", jQuery(this)).hide();
//
//    /* show the currently active sub sub navigation */
//    jQuery("#emMzMenu > ul > div > li > ul > div > li:has(.active)").parent().show();
//
//    /* show the submenu of the currently active menu item. */
//    jQuery("#emMzMenu > ul > div > li:has(.active) > ul").show();
  });


  /* **************************************************************************
   * PAGE AREAS (HOME)                                                        *
   ****************************************************************************/

  /* hide all page area navigations on startup. */
  jQuery("#pageAreas .padesc").hide();

  /* hover functionality of the area pages */
  jQuery("#pageAreas .paheader").hover(function() {
    /* on mouse over, show the current navigation */
    jQuery(this).next().fadeIn("normal");
  }, function() {
    /* on mouse out, hide the navigation */
    jQuery(this).next().fadeOut("normal");
  });


  /* hover the location panels and hightlight them. */
  jQuery("div:has(.LocationGrid)").hover(function() {
    jQuery(this).effect("highlight", {}, 3000);
  });

  /* **************************************************************************/

//  /* Toggle the listing details */
//  jQuery('input[type="image"]').click(function() {
//    if (jQuery(this).attr("class") == 'DetailsButton') {
//      var detailPanel = jQuery(this).parent().parent().next();
//      jQuery("> td > div", detailPanel).toggleClass("visible");
//    }
//  });

  /* **************************************************************************
   * FORM FIELDS                                                              *
   ****************************************************************************/
  /*
  jQuery('input[type="text"]').addClass("idleField");
  jQuery('input[type="text"]').focus(function() {
    jQuery(this).removeClass("idleField").addClass("focusField");
    if (this.value == this.defaultValue){
      this.value = '';
    }
    if(this.value != this.defaultValue){
      this.select();
    }
  });
  jQuery('input[type="text"]').blur(function() {
    jQuery(this).removeClass("focusField").addClass("idleField");
    if (jQuery.trim(this.value == '')){
      this.value = (this.defaultValue ? this.defaultValue : '');
    }
  });

  jQuery('input[type="password"]').addClass("idleField");
  jQuery('input[type="password"]').focus(function() {
    jQuery(this).removeClass("idleField").addClass("focusField");
    if (this.value == this.defaultValue){
      this.value = '';
    }
    if(this.value != this.defaultValue){
      this.select();
    }
  });
  jQuery('input[type="password"]').blur(function() {
    jQuery(this).removeClass("focusField").addClass("idleField");
    if (jQuery.trim(this.value == '')){
      this.value = (this.defaultValue ? this.defaultValue : '');
    }
  });

  jQuery('textarea').addClass("idleTextarea");
  jQuery('textarea').focus(function() {
    jQuery(this).removeClass("idleTextarea").addClass("focusTextarea");
    if (this.value == this.defaultValue){
      this.value = '';
    }
    if(this.value != this.defaultValue){
      this.select();
    }
  });
  jQuery('textarea').blur(function() {
    jQuery(this).removeClass("focusTextarea").addClass("idleTextarea");
    if (jQuery.trim(this.value == '')){
      this.value = (this.defaultValue ? this.defaultValue : '');
    }
  });*/
});

/******************************************************************************
 * OTHER JAVASCRIPT CODE                                                      *
 ******************************************************************************/

/**
 * This method limits the amount of characters for a given textfield.
 *
 * @param textarea - the textarea reference.
 * @param max - the maximum amount of allowed characters.
 */
function limit(textarea, max) {
  if (textarea.value.length > max) {
    textarea.value = textarea.value.substring(0, max);
  }
}

/**
 * Opens a popup window for printing.
 *
 * @param uri - the request URI.
 */
function print(uri) {
  if (uri.indexOf("?") > -1) {
    uri = uri + "&print=true";
  } else {
    uri = uri + "?print=true";
  }

  //var url = window.location + "/print";
  var padding = (navigator.appName == "Microsoft Internet Explorer") ? 10 : 0;
  var screenw = screen.availWidth;
  var screenh = screen.availHeight;
  var width = 800;
  var height = 600;
  var winw = (width + 15 + padding);
  var winh = (height + 15 + padding);
  var posx = (screenw / 2) - (winw / 2);
  var posy = (screenh / 2) - (winh / 2);

  var popup = window.open(uri, 'Euromot', "top=" + posy + ", left=" + posx
      + ", width=" + winw + ", height=" + winh + ", menubar=yes, locationbar=no, "
      + "statusbar=no, resizable=no, toolbar=yes, dependent=no, scrollbars=yes");
  popup.focus();
}

/**
 * Opens a popup window.
 *
 * @param url - the url to show.
 * @param width - the width size.
 * @param height - the height.
 */
function openWindow(url, width, height) {
  var padding = (navigator.appName == "Microsoft Internet Explorer") ? 10 : 0;
  var screenw = screen.availWidth;
  var screenh = screen.availHeight;
  var winw = (width + 15 + padding);
  var winh = (height + 15 + padding);
  var posx = (screenw / 2) - (winw / 2);
  var posy = (screenh / 2) - (winh / 2);

  var popup = window.open(url, 'Euromot', "top=" + posy + ", left=" + posx
      + ", width=" + winw + ", height=" + winh + ", menubar=no, locationbar=no, "
      + "statusbar=no, resizable=no, toolbar=no, dependent=no, "
      + "scrollbars=no");
  popup.focus();
}

/**
 * Opens a popup window.
 *
 * @param url - the url to show.
 * @param width - the width size.
 * @param height - the height.
 * @param scrollbars - true, if with scrollbars.
 * @param resizable - true, if resizable.
 */
function openWindow(url, width, height, scrollbars, resizable) {
  var padding = (navigator.appName == "Microsoft Internet Explorer") ? 10 : 0;
  var screenw = screen.availWidth;
  var screenh = screen.availHeight;
  var winw = (width + 15 + padding);
  var winh = (height + 15 + padding);
  var posx = (screenw / 2) - (winw / 2);
  var posy = (screenh / 2) - (winh / 2);

  var popup = window.open(url, 'Euromot', "top=" + posy + ", left=" + posx
      + ", width=" + winw + ", height=" + winh + ", menubar=no, locationbar=no, "
      + "statusbar=no, resizable=" + resizable + ", toolbar=no, dependent=no, "
      + "scrollbars=" + scrollbars);
  popup.focus();
}

/**
 * This method resizes the current window.
 *
 * @param width - the width.
 * @param height - the height.
 */
function resizeWindow(width, height) {
  if (window.outerWidth) {
    window.outerWidth = width;
    window.outerHeight = height;
  } else if (window.resizeTo) {
    window.resizeTo(width,height);
  }
}

/**
 * Redirects the current view to a given URL.
 *
 * @param url - the url.
 */
function redirect(url) {
  window.location = url;
}

/**
 * This method unchecks all currently selected checkboxes.
 *
 * @param id - the data table identifier.
 */
function uncheckAllCheckboxes(id){
  var commonIdPart = id.substr(0, id.lastIndexOf(':'));
  var tableId = commonIdPart;
  var tableElement = document.getElementById(tableId);
  var checkboxes = tableElement.getElementsByTagName('input');

  for (var i = 0; i <= checkboxes.length; i++){
    var checkbox = checkboxes[i];
    if (checkbox.getAttribute('type') == 'checkbox') {
      checkbox.checked = false;
    }
  }
}

// -- GOOGLE MAPS -------------------------------------------------------------

var map;

/**
 * Initialize the google maps api.
 */
function initGoogleMaps(locations, view, zoom) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("GoogleMap"), { } );
    var point = null;

    for (var i = 0; i < locations.length; ++i) {
      point = new GLatLng(locations[i][0], locations[i][1]);
      if (locations[i].length == 3) {
        map.addOverlay(new GMarker(point, {title: locations[i][2]}));
      } else {
        map.addOverlay(new GMarker(point));
      }
    }

    var customUI = map.getDefaultUI();
    map.setUI(customUI);
    map.setCenter(point, zoom, view);
  }
}

/**
 * Sets the center point of the current google map.
 */
function setMapCenter(latitude, longitude, zoom) {
  if (GBrowserIsCompatible()) {
    var point = new GLatLng(latitude, longitude);
    map.setCenter(point, zoom);
  }
}
