/******************************************************************************
 * 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");
  }, function() {
    /* Remark the currently active item. */
    jQuery(selectedSubmenuItem).toggleClass("active");
  });

  /* **************************************************************************
   * 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");
  }, function() {
    /* Remark the currently active item. */
    jQuery(selPageMzMenuItem).toggleClass("active");
  });
  
  /* **************************************************************************
   * EMISSIONS ONLINE NAVIGATION                                              *
   ****************************************************************************/

  /* Store the currently active item. */
  var selPageEoMenuItem = jQuery("#emEoMenu").find(".active");

  /* Show the menu of the current active item. */
  jQuery("#emEoMenu div li:has(.active)").parent().show();

  /* Show the menu of the current active item. */
  jQuery("#emEoMenu div li:has(.active) > ul").show();

  /*
   * 1st Level
   */
  jQuery("#emEoMenu > ul > div > li").hover(function() {
    /* Unmark the currently active item. */
    jQuery(selPageEoMenuItem).toggleClass("active");
  }, function() {
    /* Remark the currently active item. */
    jQuery(selPageEoMenuItem).toggleClass("active");
  });

  /* **************************************************************************
   * 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);
  });
  
  /* **************************************************************************
   * MZ SEARCH                                                                *
   ****************************************************************************/
  
  /* Checkboxes of the document template selection */
  jQuery("#DocumentTemplates\\:0").change(function() {
    if (this.value == 0) {
      if (this.checked) {
        jQuery("#DocumentTemplates\\:0").each(function() {
          if (this.value != 0) {
            this.checked = false;
          }
        });
      } else {
        this.checked = true;
      }
    } else {
      jQuery("#DocumentTemplates\\:0").each(function() {
        if (this.value == 0) {
          this.checked = false;
          return;
        }
      });
    }
	});
});

/******************************************************************************
 * 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(url) {
  if (url.indexOf("?") > -1) {
	url = url + "&print=true";
  } else {
	url = url + "?print=true";
  }

  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 win = window.open(url, 'Euromot', 
	        "top=" + posy + ", left=" + posx 
	      + ", width=" + winw + ", innerWidth=" + winw 
	      + ", height=" + winh + ", innerHeight=" + winh
	      +	", menubar=yes, locationbar=no, statusbar=no, resizable=yes" 
	      + ", toolbar=yes, dependent=no, scrollbars=yes");
  
  win.focus();
}

/**
 * Opens a popup window.
 *
 * @param url - the url.
 * @param properties - the window properties.
 */
function OpenWindow(url, properties) {
  var win = window.open(url, 'Euromot', properties);
  win.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;
    }
  }
}

/**
 * This method unchecks all currently selected checkboxes.
 */
function toggleDocumentTemplates(id) {
	id = id.substr(0, id.lastIndexOf(':'));
	var checkboxes = document.getElementById(id).getElementsByTagName('input');

	for (var i = checkboxes.length - 1; i >= 0; i--) {
		var checkbox = checkboxes[i];

		if (checkbox.getAttribute('type') == 'checkbox' 
			&& checkbox.value != 'all'
			&& checkbox.checked == true) {
			checkboxes[0].checked = false; // uncheck 'all'
			return;
		} else if (checkbox.getAttribute('type') == 'checkbox' 
			&& checkbox.value == 'all'
			&& checkbox.checked == true) {
			
			// if 'all' is checked, uncheck all others
			for (var k = 0; k <= checkboxes.length; k++) {
				tmpCheckBox = checkboxes[k];
	
				if (tmpCheckBox.getAttribute('type') == 'checkbox'
					&& tmpCheckBox.value != 'all') {
					tmpCheckBox.checked = false;
				}
			}
			
			return;
		}
	}
}

// -- 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);
  }
}

// -- PIWIK TRACKING ----------------------------------------------------------

jQuery(window).load(function(){
	jQuery.getScript('http://www.cinenow.de/piwik/piwik.js', function() {
		try {
			var piwikTracker = Piwik.getTracker("http://www.cinenow.de/piwik/piwik.php", 2);
			piwikTracker.trackPageView();
			piwikTracker.enableLinkTracking();
		} catch( err ) {}
    });
});

