/**
 * Jahia fix for cutting of second level navitation if it is too long if a user is logged in
 */
jQuery(window).load(function () {
	var doLevel2NavResize = function () {
		if (jQuery('body.dragdrop-dropTarget').length && jQuery('#header_nav ul#level2 li').length > 6) {
	    var first = jQuery('#header_nav ul#level2 li:first');
	    var last = jQuery('#header_nav ul#level2 li:last');
	    if (last.offset().top > first.offset().top + 5) {
				jQuery('#header_nav').css('height', '102px');
				jQuery('#header_nav ul#level2').css('height', '60px');
			}
		}
	};
  setTimeout(doLevel2NavResize, 1000);
  setTimeout(doLevel2NavResize, 3000);
});

/**
 * Browser fixes
 */
jQuery(function () {
	if (jQuery.browser.msie) {
	  // concernated unordered lists do have an unwanted space at top
			jQuery('#content #area2 table ul ul').css('margin-top', "-16px");
		// numbers within short tables starting with a MINUS do have an unwanted linebreak
			jQuery('td:contains(-)').each(function () {
				if (jQuery(this).text().charAt(0) == "-") jQuery(this).html('<span style="white-space:nowrap;">' + jQuery(this).html() + '</span>');
			});
	}
});

/**
 * Layout issues (both on all browsers and even on specific browsers)
 */
jQuery(function () {
  // align all "brown buttons" bottom
    // richtext built html stucture with tables
	    jQuery('table .assetOvBox span').each(function () {
				var me = jQuery(this);
				me.css('height', 'auto');
				if (me.height() < 20) {
					me.addClass('singleLineText').css('height', '');
					var h = parseInt(me.css('height').replace("px", "")) - parseInt(me.css('padding-top').replace("px", ""));
					me.css('height', h + "px");
				} else me.css('height', '');
			});
 		// bottom aligned landingpage buttons (the grey ones in area2 and area3)
 		  if (jQuery.browser.msie) jQuery('#content_type_311, #content_type_41').find('#area2, #area3').find('.box_link span.box_title').addClass('bottomAlignedLandingPageButton');
});

/**
 * List corrections
 */
jQuery(function () {
	jQuery('#content #area2 li ul:only-child').parent().css('background-image', 'none');
});

/**
 * Sortable tables with zebra stripes
 */
jQuery(function () {
	var getFieldData = function (node) {
	  var txt = jQuery(node).text();
	  if (txt.indexOf(",") > 0) {
			var num = txt.replace(",", ".").replace("%", "");
			if (!isNaN(num)) txt = num;
		}
		return txt;
	};
	jQuery(".jsSortableTable").tablesorter({
		cssHeader: 'jsSortableTh',
		cssAsc: 'ascSortedTableHeader',
		cssDesc: 'descSortedTableHeader',
		widgets: ['zebra'],
		widgetZebra: { css: ['odd', 'even'] },
    textExtraction: getFieldData
	});
	jQuery(".jsSortableTable th:first").trigger('click'); // sort first column at start
});

/**
 * Search Box
 */
function emptySearchBox () {
	$('#header #header_logo input.text').attr('value', '');
	$('#header #header_logo input.text').unbind('click', emptySearchBox);
}
jQuery(function () {
	$('#header #header_logo input.text').click(emptySearchBox);
});

/**
 * Redirect current page immediately to the parameter url-path
 * @param url absolute URL to target-page.
 * @param timeout in millieseconds
 * @return void
 */
function redirectTo(url, timeout) {
	if (timeout) setTimer('redirectTo("' + url + '")', timeout);
	else window.location = url;
}

/**
 * Zebra striped tables
 */
jQuery(function () {
	jQuery('table.jsStipedTable tbody tr').removeClass('even').removeClass('odd');
	jQuery('table.jsStipedTable tbody')
		.find('tr:even').addClass('odd').end()
		.find('tr:odd').addClass('even').end()
		.parent().find('th:first-child, td:first-child').addClass('firstCol');
});

/**
 * Home page product selector popup utility
 */
function initilizeProductSelectorListeners () {
	if (!$('.items_group .item .tooltip').length) return;
	$('.items_group .item .tooltip').find('.item_tooltip').addClass('item_tooltip_with_arrow').each(function () {
		var el = $(this).find('.tooltip_text');
		$(this).show();
		if (el.width() < 150) el.width(150);
		$(this).hide();
	}).end().append('<div class="tooltip_arrow"></div>');
	$('.items_group .item .name, .items_group .item .digits').bind('mouseenter', function () {
		$('.item_tooltip, .tooltip_arrow', $(this).parent()).css('display', 'block');
	}).bind('mouseleave', function () {
		$('.items_group .item .item_tooltip, .items_group .item .tooltip_arrow').css('display', 'none');
	});
}

jQuery(initilizeProductSelectorListeners);

/**
 * Disclaimer
 */
jQuery(function () {
	var disclaimerEl = jQuery('#disclaimer_over');
	if (!disclaimerEl.length) return;
	disclaimerEl.parents().css('z-index', '999999'); // ie z-index fix
	var disclaimerOverlay = jQuery('#disclaimer_overlay');
	disclaimerEl.css('margin-left', '0');
	var adjustDisclaimerPosition = function () {
		disclaimerEl.css({
			'left': ((disclaimerOverlay.width() - disclaimerEl.width()) / 2) + "px",
			'top': ((disclaimerOverlay.height() - disclaimerEl.height()) / 2) + "px"
		});
	};
	jQuery(window).bind('resize', adjustDisclaimerPosition);
	adjustDisclaimerPosition();
});


/**
 * Automatically change visual on page "Anlageprodukte" on product-mouseover
 */
var changeAnlageprodukteHomePageVisualTimeout;
var changeAnlageprodukteHomePageVisualLastSection;
function changeAnlageprodukteHomePageVisual (section) {
	if (changeAnlageprodukteHomePageVisualLastSection == section) return;
	//changeAnlageprodukteHomePageVisualLastSection = section;
	window.clearTimeout(changeAnlageprodukteHomePageVisualTimeout);
	var sections = {
		healthcare: {
			path: '/repository/default/content/sites/bellevuegoupSite/files/Images/visuals_bam/anlageprodukte/healthcare/',
			images: ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg']
		},
		new_markets: {
			path: '/repository/default/content/sites/bellevuegoupSite/files/Images/visuals_bam/anlageprodukte/new_markets/',
			images: ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg']
		},
		selected_niches: {
			path: '/repository/default/content/sites/bellevuegoupSite/files/Images/visuals_bam/anlageprodukte/selected_niches/',
			images: ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg']
		}
	};
	var images = sections[section].images;
	var rndNum = Math.floor(Math.random() * images.length);
	changeAnlageprodukteHomePageVisualTimeout = window.setTimeout(function () {
		var oldImg = jQuery('#header_img img:first');
		oldImg.css({
			'position': 'absolute',
			'z-index': jQuery('#header_img img').length + 1
		});
		newImg = jQuery('<img src="' + sections[section].path + images[rndNum] + '" style="position:absolute; z-index:1; display: none;" alt="" />');
		newImg.bind('load', function () {
			oldImg.fadeOut(400, function () {
				jQuery(this).remove();
			});
			newImg.fadeIn(400, function() {
				changeAnlageprodukteHomePageVisualLastSection = section;
			});
		});
		jQuery('#header_img').prepend(newImg);
	}, 180);
}
function stopChangeAnlageprodukteHomePageVisual () {
	window.clearTimeout(changeAnlageprodukteHomePageVisualTimeout);
}


/**
 * LINK TEASER
 */
jQuery(function () {
	jQuery('.linkTeaser a.teaserBottomArrow').each(function () {
		var me = jQuery(this);
		var myLink = me.attr('href');
		me.attr('href', "javascript:;");
		if (jQuery.trim(myLink) != "") {
			me.parents('.linkTeaser:first').bind('click', function () {
				window.location.href = myLink;
			}).css('cursor', 'hand').css('cursor', 'pointer');
		} else {
			me.css('cursor', 'default');
		}
	});
});
