(function($){
	jQuery.fn.center = function () {
		this.css("position","absolute");
		// alert( ($(window).height() - this.height()) / 2 + "+" + $(window).scrollTop());
		this.css("top", ( $(window).height() - this.height() - ($(this).parent().height() / 2) ) / 2+$(window).scrollTop() + "px");
		// this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	}

})(jQuery);

jQuery(document).ready(function($) {

	if (window.location.href.indexOf("promos", 0) > 0)
	{
		$("#menu-main-navigation li").removeClass("current_page_parent").removeClass("current_page_item");
		$("#menu-item-404").addClass("current_page_parent");
	}
		
	/* lte IE7 Fixes */
	if($.browser.msie && parseInt($.browser.version) <= 7)
	{
		$(window).resize(function(){
			$("#navigation .sub-menu-wrap").each(function(){
				var offset = $(this).parent().offset();
				var offsetparent = $(this).parent().parent().parent().offset();
				$(this).css({ 'position':'absolute', 'margin-top': '15px', 'left': parseInt(offset.left - offsetparent.left) + 'px', 'top': parseInt(offset.top - offsetparent.top) + 'px'});
			});
		});
		
		$("#navigation .sub-menu-wrap").each(function(){
			var offset = $(this).parent().offset();
			var offsetparent = $(this).parent().parent().parent().offset();
			$(this).css({ 'position':'absolute', 'margin-top': '15px', 'left': parseInt(offset.left - offsetparent.left) + 'px', 'top': parseInt(offset.top - offsetparent.top) + 'px'});
		});
	}

	/* all browsers > menu styling */
	// jQuery('.sub-menu-wrap').css({'position':'absolute', 'opacity':'0'});
	$("#navigation li").hover(
		function () {
			// li has sub-menu
			if ($(this).children(".sub-menu-wrap").html() != null)
			{
				$(".sub-menu-left", this).height($("ul", this).height());
				$(".sub-menu-right", this).height($("ul", this).height());
				if($.browser.msie)
				{
					$("#navigation .sub-menu-wrap").stop('true','true').hide();
				}
				else
				{
					$("#navigation .sub-menu-wrap").stop('true', 'true').fadeTo(10, 0, function() {
						$(this).hide();
					});
				}
			}
			
			if($.browser.msie)
				$(this).find(".sub-menu-wrap").eq(0).stop('true', 'true').delay(100).show();
			else
				$(this).find(".sub-menu-wrap").eq(0).stop('true', 'true').delay(100).fadeTo(100, 1).show();
		},
		function () {
		
			if($.browser.msie)
			{
				setTimeout($(this).find(".sub-menu-wrap").eq(0).delay(1000).hide(), 1000);
			}
			else
			{
				$(this).find(".sub-menu-wrap").eq(0).delay(1000).fadeTo(200, 0, function() {
				  $(this).hide(); 
				});
			}
		}
	);

	/* header images fade effect */
	$("#header span").cycle({
		fx:    'fade', 
		pause:  1 
	});
	
	/* Grille functions */
	$("#popup").draggable();
	
	$("#grille a").click(function(){
		$("#popup-bg").show();
		
		$("#popup h4").html($(this).find(".image").html());
		// $(this).find(".content").html($(this).find(".content").text()).text();
		
		$("#popup .content").html($(this).find(".content").text()).text();
		
		
		// alert($(this).find(".content").html());
		
		// alert(($(this).height()+ "- "  + $(this).offset().top));
		
		if($.browser.msie)
			$($(this).attr('href')).show().center().css({"left": parseInt($(this).offset().left + $(this).width()) + "px", "top": $(this).offset().top + "px" });
		else
			$($(this).attr('href')).fadeTo(200,1).center().css({"left": parseInt($(this).offset().left + $(this).width()) + "px",  "top": $(this).offset().top + "px" });
		
		attachLightbox();
		
		return false;
	});
	
	$("#popup-bg, #popup .close").click(function(){
		if($.browser.msie)
		{
			$("#popup").hide();
			$("#popup-bg").hide();
		}
		else
			$("#popup").fadeTo(500,0, function(){ $("#popup-bg").hide(); $("#popup").hide(); });
		
		return false;
	});
	
	/* Personnel */
	
	$("#personnel a").click(function(){return false;});
	
	$("#personnel a").hover(function(){
		$(this).children(".text").fadeTo(1000,1);
		$(this).children(".overlay").stop().fadeTo(1000,0.75);
	},
	function(){
		$(this).children(".text").stop().fadeTo(500,0);
		$(this).children(".overlay").stop().fadeTo(500,0);
	});
	
	/* Services lightbox */
	function attachLightbox() {
		$('#gallery a, .ngg-galleryoverview a').lightBox({
		overlayBgColor: '#FFF',
		overlayOpacity: 0.6,
		imageLoading: templateUrl + '/images/lightbox-ico-loading.gif',
		imageBtnClose: templateUrl + '/images/lightbox-btn-close.gif',
		imageBtnPrev: templateUrl + '/images/lightbox-btn-prev.gif',
		imageBtnNext: templateUrl + '/images/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'de',
		maxHeight: parseInt($(window).height() - 400),
		maxWidth: parseInt($(window).width() - 400)
		});
		}
	attachLightbox();
});
