var AHEAD = {

	// Tab switch effect
	//
	// When a tab is clicked...
	// - set the tab's CSS class to "active" (and remove that class from all
	// other tabs)
	// - create a hidden white "overlay" div with the same position and
	// dimension as the containing content div
	// - fade in the white overlay div
	// - change the content within the containing div
	// - fade out the overlay div
	//
	// Assumed HTML structure:
	//
	// <ul id="ulSelector">
	// <li class="active">Tab 1</li>
	// <li>Tab 2</li>
	// <li>Tab 3</li>
	// </ul>
	// <div id="containerSelector">
	// <div>Content 1</div>
	// <div style="display:none>Content 2</div>
	// <div style="display:none>Content 3</div>
	// </div>

	tabClicked : function(ulSelector, containerSelector, num) {
		var ulList = jQuery(ulSelector);
		var containerDiv = jQuery(containerSelector);
		var overlaySelector = containerSelector + "-overlay";

		// Create overlay div (or re-use existing)
		var overlay = jQuery(overlaySelector);
		if (overlay.length <= 0) {
			overlay = jQuery("<div id=\"" + overlaySelector.substr(1) + "\" />");
			overlay.width(containerDiv.width());
			overlay.css("position", "absolute");
			overlay.css(containerDiv.offset());
			overlay.css("z-index", 22);
			overlay.css("background-color", "#fff");
			overlay.appendTo('body');
			overlay.hide();
		}
		overlay.height(containerDiv.height());
		
		if(containerSelector.indexOf('#agf') >= 0){
			overlay.width("745px");
			overlay.css("margin-left", "-9px");
		}

		// Update tab CSS classes
		ulList.children().each(function(i) {
			if (i + 1 == num) {
				jQuery(this).find("a").addClass("active");
			} else {
				jQuery(this).find("a").removeClass("active");
			}
		});

		// Fade to white, change contents, fade out
		overlay.fadeIn(400, function() {
			containerDiv.children().each(function(i) {
				if (i + 1 == num) {
					jQuery(this).show();
				} else {
					jQuery(this).hide();
				}
			});
			overlay.height(containerDiv.height());
			overlay.fadeOut(800);
		});
	},

	displayMenu : function(currentNavItem) {
		jQuery('#subnaviTopMenu').each(
				function(index, elem) {
					jQuery(this).find('> li > ul').each(function(i, e) {
						jQuery(this).find('> li > ul').each(function(i, e) {
							var innerDisplay = jQuery(this).css("display");
							if (innerDisplay == "block") {
								jQuery(this).css("display", "none");
							}
						});
						var display = jQuery(this).css("display");
						if (display == "block") {
							jQuery(this).css("display", "none");
						}
					});
					jQuery(this).find('#' + currentNavItem).each(
							function(i, e) {
								// if(jQuery(this).find('> a').css("background")
								// != "none"); {
								// jQuery(this).find('> a').css("background",
								// "url('../img/bulletpoint-subnavi-hi.png')
								// no-repeat left 10px");
								// }
								if (jQuery(this).hasClass("deepestElement")) {
									jQuery(this).find('> a').addClass(
											"subnaviactiveBulletPoint");
									// jQuery(this).find('>
									// a').css("background",
									// "url('../img/bulletpoint-subnavi-hi.png')
									// no-repeat left 10px");
								} else {
									jQuery(this).find('> a').addClass(
											"subnaviactive");
									// jQuery(this).find('>
									// a').css("background", "none");
								}
								// jQuery(this).find('>
								// a').css("background-color", "#e53434");
								jQuery(this).parent().parent().css(
										"font-weight", "bold");
								jQuery(this).parent().parent().parent()
										.parent().css("font-weight", "bold");
								jQuery(this).parent('ul').css("display",
										"block");
								jQuery(this).parent('ul').parent().parent()
										.css("display", "block");
								jQuery(this).children('ul').css("display",
										"block");
							});
				});
		// var topMenu = document.getElementById("subnaviTopMenu");
		// topMenu.getElementsByTagName("li").style.display = "none";
		// alert("Funzt");
	},

	bookmark : function(url, title) {
		// ist keine Url angegeben, wird der aktuelle Hostname genutzt
		// var url = url || location.protocol + '//' + location.host;
		// um die aktuelle Url zu bookmarken, die vorherige Zeile
		// auskommentieren und die nächste einkommentieren.
		var url = url || location.href;

		// kein Titel angeg. wird der aktuelle Hostname benutzt
		var title = title || location.host;
		// um den Titel aktuelle Url zu bookmarken, die vorherige Zeile
		// auskommentieren und die nächste einkommentieren.
		// var title = document.title;

		// Internet Explorer
		if (document.all) {
			window.external.AddFavorite(url, title);
		}
		// Firefox
		else if (window.sidebar) {
			window.sidebar.addPanel(title, url, '');
		}
		// false zurückliefern, fall die Funktion
		// über das "onlick"-Event des <a>-Tags
		// aufgerufen wurde
		return false;
	},

	createMailto : function(betreff, body) {
		location.href = 'mailto:?SUBJECT=' + betreff + '&BODY=' + escape(body)
				+ ' ' + escape(location.href);
		return false;
	},

	showHidePortlet : function(id, hide) {
		var portletIdOnPage = "#p_p_id_" + id + "_";
		var container = jQuery(portletIdOnPage);
		if (hide) {
			container.fadeOut(300, function() {
				container.hide();
			});
			// Uncheck the start page config checkbox; see:
			// portletCheckboxes.jsp in the standardahead-portlet
			jQuery("input#checkbox-" + id).removeAttr("checked");
			jQuery.ajax({
				'url' : themeDisplay.getPathMain() + '/portal/userpage',
				'data' : {
					'p_p_id' : id,
					'hide_show' : 'hide',
					'p_l_id' : themeDisplay.getPlid()
				}
			});
		} else {
			// When showing a portlet, we can fade in the portlet if it is still
			// on the page
			if (container && container.length > 0) {
				container.fadeIn(300, function() {
					jQuery("input#checkbox-" + id).attr("checked", "checked");
				});
				jQuery.ajax({
					'url' : themeDisplay.getPathMain() + '/portal/userpage',
					'data' : {
						'p_p_id' : id,
						'hide_show' : 'show',
						'p_l_id' : themeDisplay.getPlid()
					}
				});
			}
			// Otherwise, we need to send an AJAX request and re-load the
			// current page
			else {
				jQuery("#user-startpage-config input").attr("disabled", true);
				jQuery.ajax({
					'url' : themeDisplay.getPathMain() + '/portal/userpage',
					'data' : {
						'p_p_id' : id,
						'hide_show' : 'show',
						'p_l_id' : themeDisplay.getPlid()
					},
					'success' : function() {
						window.location = window.location + "?"
								+ new Date().getMilliseconds();
					}
				});
			}
		}
	},

	// Sidebar portlet management
	//
	// - Read the user's preferences and adjust the "slid up"/"slid down" states
	// accordingly when the page is ready
	// - Monitor "slide up"/"slide down" actions and save the state via AJAX

	initializeSidebarPortlets : function() {
		if (!Liferay.ThemeDisplay.slidUpPortlets) {
			return;
		}
		for ( var i = Liferay.ThemeDisplay.slidUpPortlets.length - 1; i >= 0; i--) {
			var portletId = Liferay.ThemeDisplay.slidUpPortlets[i];

			// Selector of the container we need to hide:
			// div#portlet-wrapper-{PORTLET_ID}.main-box-right
			// div.portlet-content
			jQuery(
					"div#portlet-wrapper-" + portletId
							+ ".main-box-right div.portlet-content").hide();

			// Also adjust the arrow image at:
			// div#portlet-wrapper-startpageconfig_WAR_standardaheadportlet_INSTANCE_2xoe.main-box-right
			// div.portlet-topper a.link-arrow-down
			var a = jQuery("div#portlet-wrapper-" + portletId
					+ ".main-box-right div.portlet-topper a.link-arrow-down");
			if (a && a.css("background-image")) {
				var bg = a.css("background-image").replace(/arrow.\w+/,
						"arrow-right");
				a.css("background-image", bg);
			}
		}
	},

	slideUpDownPortlet : function() {
		var arrowLink = jQuery(this);
		var contentbox = arrowLink.parent().next();
		var bg = jQuery(this).css("background-image");
		var hide = contentbox.is(":visible");
		if (bg != undefined) {
			if (hide) {
				contentbox.slideUp();
				bg = bg.replace(/arrow.\w+/, "arrow-right");
			} else {
				contentbox.slideDown();
				bg = bg.replace(/arrow.\w+/, "arrow-down");
			}
			arrowLink.css('background-image', bg);
		}

		var container = arrowLink.parent().parent();
		if (!container.attr("id")) {
			return;
		}
		var match = /portlet\-wrapper\-(.*)/.exec(container.attr("id"));
		if (!match) {
			return;
		}
		var id = match[1];

		jQuery.ajax({
			'url' : themeDisplay.getPathMain() + '/portal/userpage',
			'data' : {
				'p_p_id' : id,
				'slide_up_down' : hide ? 'up' : 'down',
				'p_l_id' : themeDisplay.getPlid()
			}
		});
	}
};
