var absPath = '/';
var ie = getInternetExplorerVersion();
var navigationActive = 0;
var navigationClicked = 0;

$(document).ready(function() {
	// IE fix z-index
	$(function() {
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});	
	
	// Navigation
	if (ie < 0 || ie > 6) {
		$('#navigation .hsubsCSS').addClass('hsubs');
		$('#navigation .hsubsCSS').removeClass('hsubsCSS');
	}

	$('<li class="dropdownClose"><a href="javascript:dropdownClose();">x</a></li>').insertBefore('#mmnmic336');
	$('<li class="dropdownOverview"><a href="/Modulbau/Modulgebaeude-11.html">Zur Übersicht Modulbau</a></li>').insertBefore('#mmnmic336');
	$('<li class="dropdownClose"><a href="javascript:dropdownClose();">x</a></li>').insertBefore('#mmnmic37');
	$('<li class="dropdownOverview"><a href="/Raum-und-Lagertechnik-10.html">Zur Übersicht Raum- und Lagertechnik</a></li>').insertBefore('#mmnmic37');
	$('<li class="dropdownClose"><a href="javascript:dropdownClose();">x</a></li>').insertBefore('#mmnmic3');
	$('<li class="dropdownOverview"><a href="/Umwelt-Lagertechnik/Gefahrstofflagerung-2.html">Zur Übersicht Umwelt-Lagertechnik</a></li>').insertBefore('#mmnmic3');
	
	$('li.hsubs a.l1').click(function(){		
		navigationActive = 1;
		thisId = $(this).attr('id');
		
		if ($('#dropdown').css('display') == 'block') {
			dropdownHide();
			$('.navL2').slideUp();
			navigationActive = 0;
		}
		if ($('#dropdown').css('display') == 'none') {
			dropdownShow(thisId);
		}
		
		if ($('#dropdown').css('display') == 'block' && navigationActive == 1) $('#' + thisId.replace('a', '')).find('.navL2').stop(true,true).slideDown();		
		navigationClicked = $(this).attr('id');
		
		return false;
	});
	
	$('li.hsubs a.l1').mouseover(function(){
		if (navigationActive == 1) {
			thisId = $(this).attr('id');
			
			if ($('#' + thisId.replace('a', 's')).css('display') == 'none') {
				$('.navL2').slideUp();
				if ($('#' + thisId.replace('a', '')).hasClass('subModulbau')) $('#dropdown').addClass('modulbau');
				else $('#dropdown').removeClass('modulbau');
				
				$('#' + thisId.replace('a', '')).find('.navL2').stop(true,true).slideDown();				
			}
		}
	});
	
	$('.navL1 li').mouseover(function(){
		thisId = $(this).attr('id');		
		if (thisId == 'mmnmi1' || thisId == 'mmnmi12' || thisId == 'mmnmi13' || thisId == 'mmnmi400' || thisId == 'mmnmi405' || thisId == 'mmnmi406') {
			$('.navL2').slideUp();
			dropdownHide();
			navigationActive = 0;
		}
	});	
		
	$('#dropdownBackground').click(function(){
		navigationActive = 0;
		$('.navL2').slideUp();
		dropdownHide();
	});
	
	$('#header').click(function(){
		navigationActive = 0;
		$('.navL2').slideUp();
		dropdownHide();
	});	
		
	//documentHeight = $(document).height() - 646;
	documentHeight = $(document).height() - 111;
	$('#dropdownBackground').css('height', documentHeight + 'px');
	
	contentbarNavigationHeight = $('#contentbarNavigation').height();
	if (contentbarNavigationHeight > 300) {
		if(!$('body').hasClass('modulbau')) $('#contentbarMedia .content').css('min-height', contentbarNavigationHeight - 50 + 'px');
	}
	
	// Gallery
	$('#imageBig').hide().fadeIn();
	$("#imageBigLink").lightBox({
		overlayBgColor: '#fff',
		imageLoading: absPath + 'images/lightbox-ico-loading.gif',
		imageBtnClose: absPath + 'images/lightbox-btn-close.gif',
		imageBtnPrev: absPath + 'images/lightbox-btn-prev.gif',
		imageBtnNext: absPath + 'images/lightbox-btn-next.gif'	
	});
	$(".lb").lightBox({
		overlayBgColor: '#fff',
		imageLoading: absPath + 'images/lightbox-ico-loading.gif',
		imageBtnClose: absPath + 'images/lightbox-btn-close.gif',
		imageBtnPrev: absPath + 'images/lightbox-btn-prev.gif',
		imageBtnNext: absPath + 'images/lightbox-btn-next.gif'	
	});	
	
	// Tooltip
    $('.productTeaserTooltip[title]').tooltip({ opacity: 1.0, position: 'top center', offset: [1,20], layout: '<div><div class="tooltip-inner-wo-bottom"></div><div class="tooltip-bottom"></div></div>', tipInner: 'tooltip-inner-wo-bottom'});
    $('.modulbau #contentbarNavigationContent li a[title]').tooltip({ opacity: 1.0, position: 'top center', offset: [1,20], layout: '<div><div class="tooltip-inner-modulbau-bottom"></div><div class="tooltip-modulbau"></div></div>', tipInner: 'tooltip-inner-modulbau-bottom'});
    
    // Search
    $('#q').click(function(){
    	if ($('#q').attr('value') == 'Suche' )
    		$('#q').attr('value', '');
    });
    
    $('#q').blur(function(){
    	if ($('#q').attr('value') == '' )
    		$('#q').attr('value', 'Suche');
    });    
});

function dropdownShow(thisId) {
	if ($('#' + thisId).hasClass('subModulbau')) $('#dropdown').addClass('modulbau');
	else $('#dropdown').removeClass('modulbau');
	
	dropdownBackgroundShow();	
	$(this).find('.navL2').stop(true,true).slideDown();
	$('#dropdown').addClass('visible');
}

function dropdownHide() {
	$(this).find('.navL2').stop(true,true).slideUp();
	$('#dropdown').removeClass('visible');
	dropdownBackgroundHide();
}

function dropdownClose() {
	$('.navL2').stop(true,true).slideUp();
	$('#dropdown').removeClass('visible');
	dropdownBackgroundHide();	
	navigationActive = 0;
}

function dropdownBackgroundShow() {
	if (ie <= 8&&ie != -1) {		
		$('#dropdown').stop(true,true).show();
		$('#dropdownBackground').stop(true,true).show();		
	}
	if (ie > 8||ie == -1) {		
		$('#dropdown').stop(true,true).fadeIn();
		$('#dropdownBackground').stop(true,true).fadeIn();
	}
}

function dropdownBackgroundHide() {
	//$('#dropdown').removeClass('modulbau');	
	$(this).doTimeout(250, function(){
		if (!$('#dropdown').hasClass('visible')) {		
			if (ie <= 8&&ie != -1) {
				$('#dropdown').stop(true,true).hide();
				$('#dropdownBackground').stop(true,true).hide();				
			}
			if (ie > 8||ie == -1) {
				$('#dropdown').stop(true,true).fadeOut();
				$('#dropdownBackground').stop(true,true).fadeOut();				
			}

		}		
	});	
}

function showImage(id) {
	w = $('#contentMediaImage').width();
	h = $('#contentMediaImage').height();
	
	$('#contentMediaImage').addClass('loading');
	
	$('#contentMediaImageContainer').fadeOut(function(){
		$.get(absPath + 'products/index/image/id/' + id + '/w/' + w + '/h/' + h, function(data) {
			  	rdata = data.split('|');

					var _image = new Image();
					_image.src = rdata[0];
					_image.id = 'imageBig';
					$(_image).load( function(){
						$("#temp").html( _image );
						$('#imageBig').attr('src', $("#temp img").attr('src'));
						$('#imageBig').attr('width', rdata[1]);
						$('#imageBig').attr('height', rdata[2]);
						$('#imageBig').attr('alt', rdata[4]);
						$('#imageBig').attr('title', rdata[4]);
						$('#imageBigLink').attr('href', absPath + 'modules/files/' + id + '.jpg');
						$('#contentMediaImageContainer').fadeIn(function(){
								$('#contentMediaImage').removeClass('loading');
							});
					});
			});
	});
	
	$('#contentMediaGalleryNavigation a').removeClass('active');
	$('#cmgn' + id).addClass('active');		
		
}

function showGallery(gid,start,limit,mod) {
	$.get(absPath + 'products/index/galleryShow/g/' + gid + '/s/' + start + '/l/' + limit + '/m/' + mod, function(data) {
		  //size = data.split('|');
		html = data;
		});
	
	$('#contentbarMediaGalleryBox').fadeOut(function(){
		$('#contentbarMediaGalleryBox').html(html);
		$(this).fadeIn();
	});	
}

function contentHistory(id) {
	  $('.historyActive').removeClass('historyActive');
	  $('#history' + id).addClass('historyActive');	
	  $.post(absPath + 'content/index/historylive/', {id: id}, function(data) {
		  split = data.split('%spacer%');
		  
		  $('#historyImage').fadeOut(function(){			  
			  $('#historyImage').html(split[0]);
			  $('#historyContent').html(split[1]);
			  
			  $('#historyImage').delay(200).fadeIn();
			  
		  });
		});
}

function newsList(limit) {
	last = $('#listLastItem').attr('value');
	$('#listMoreButton').before('<div id="listAnchor"><img src="' + absPath + 'images/lightbox-ico-loading.gif" width="32" height="32" /></div>');
	$('#listMoreButton').remove();
	$('#listLastItem').attr('value', parseInt(last) + parseInt(limit));

	$(this).doTimeout(500, function(){
		$('#listAnchor').fadeOut(function(){
		  $.post(absPath + 'news/index/listload/', {fid: 1, start: last, limit: limit}, function(data) {
			  $('#listAnchor').before(data);
			  $('#listAnchor').remove();
			});
		});
	});	
}

function getInternetExplorerVersion()
//Returns the version of Windows Internet Explorer or a -1
//(indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
   var ua = navigator.userAgent;
   var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
   if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkIEVersion()
{
var msg = "You're not using Windows Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver> -1 )
{
   if ( ver>= 8.0 )
      msg = "You're using Windows Internet Explorer 8.";
   else if ( ver == 7.0 )
 	  msg = "You're using Windows Internet Explorer 7.";
   else if ( ver == 6.0 )
 	  msg = "You're using Windows Internet Explorer 6.";
   else
 	  msg = "You should upgrade your copy of Windows Internet Explorer";
 }
alert( msg );
}

function search(){
	$('#search').submit();
}
