var Menu = {

	timer: null,
	timeout: 500,
	speed: 'slow',
	current: null,

	initialize: function()
	{
		jQuery.each($('ul.navigation a.tinyFont'), function() {
			Menu.register($(this))
		});
	},

	register: function(item)
	{
		item.one('mouseover', Menu.itemFocus);
	},

	itemFocus: function(event)
	{
		var $item = $(event.target);
		var identifier = '#' +$item.parents('li').attr('id');

		if (Menu.current != null && Menu.current != identifier)
		{
			window.clearTimeout(Menu.timer);
			Menu.hide();
		}
		if (Menu.current != identifier && $(identifier+'-linklist').length > 0)
		{
			Menu.current = '#' +$item.parents('li').attr('id'); 
			Menu.getCurrentList().fadeIn('def');

			$item.one('mouseout', Menu.itemBlur);
			$(identifier+'-linklist').bind('mousemove', Menu.move);
			$(identifier+'-linklist').bind('mouseout', Menu.itemBlur);
		}
	},
	
	itemBlur: function(event)
	{
		var $item = $(event.target);
		Menu.timer = window.setTimeout(Menu.hide, Menu.timeout);
	},

	move: function()
	{
		window.clearTimeout(Menu.timer);
	},
	
	getCurrentItem: function()
	{
		return $(Menu.current);
	},
	
	getCurrentList: function()
	{
		return $(Menu.current+ '-linklist');
	},

	hide: function ()
	{
		Menu.getCurrentList().fadeOut('def');
		Menu.register(Menu.getCurrentItem().find('a.tinyFont'));
		Menu.current = null;
	}
};



var Carousel = {

	current: null,
	list: null,
	event: null,
	target: null,

	setList: function(list)
	{
		this.list = list;
		list.slideUp(1);
	},

	openSingleItem: function(event)
	{
		event.preventDefault();
		
		if (Carousel.current != null)
		{
			Carousel.closeSingleItem(event);
		}
		else
		{
			var $this = $(event.target).parent();
			var identifier = $this.attr('id');
			var $target = $('#' +identifier+ '-record'); 
		
			$target.show();
			$target.parents('li').show();
			$target.parent().find('h2').show();
		
			Carousel.current = identifier;
			Carousel.list.slideDown(1000);
		}
	},

	closeSingleItem: function(event)
	{
		Carousel.event = event;

		Carousel.list.slideUp(500, function(){
			var $target = $('#' +Carousel.current+ '-record'); 

			$('li,h2,.address', Carousel.list).hide();

			Carousel.current = null;
			if (Carousel.target == 'all')
			{
				Carousel.openAll(Carousel.event);
				Carousel.target = null;
			}
			else
			{
				Carousel.openSingleItem(Carousel.event);
			}
		});
	},

	openAll: function(event)
	{
		event.preventDefault();
		if (Carousel.current == 'all')
		{
			Carousel.closeAll(event);
		}
		else if (Carousel.current != null)
		{
			Carousel.target = 'all';
			Carousel.closeSingleItem(event)
		}
		else
		{
			Carousel.current = 'all';
			$('li,h2,.address', Carousel.list).show();
			Carousel.list.slideDown(3000);
		}
	},

	closeAll: function(event)
	{
		Carousel.event = event;

		Carousel.list.slideUp(2000, function(){
			var $target = $('#' +Carousel.current+ '-record');
			$('li,h2,.address', Carousel.list).hide();

			if (Carousel.current != 'all')
			{
				Carousel.current = null;
				Carousel.openAll(Carousel.event);
			}
			else
			{
				Carousel.current = null;
			}
		});
	}
};



var ImageReload = {
		
	loadImages: function(images)
	{
		$('img:regex(class,~hash-[a-z0-9]{32})').each(function(index){
			if ($(this).attr('class').match(/hash-([0-9a-fA-F]{32})/))
			{
				var hash = RegExp.$1;
				if (imageHash[hash])
				{
					$(this).attr('src', GLOBALS.webBasepath + imageHash[hash]);

					// remove hashKey from class
					$(this).removeClass('hash-'+hash);
				}
			}
		});
	}
}


$(document).ready(function()
{
	try
	{
		GLOBALS.isIE == true;
	}
	catch (exc)
	{
		GLOBALS.isIE = GLOBALS.isIE6 = GLOBALS.isIE7 = false;
	}


	// fix transparent png's for best pal IE6
	if(GLOBALS.isIE6)
	{
		$.ifixpng(GLOBALS.webBasepath+ '/resources/gfx/alpha.gif');
		$('img.alpha').ifixpng();
		$('.breadcrumbs').css('display', 'block');
	}
	
	if(GLOBALS.isIE7)
	{
		$('.breadcrumbs').css('display', 'block');
	}
	

	// jquery selectboxes
	try
	{
		$('.pulldown select').selectbox();
	}
	catch (e) {}


	// external links
	$('a.ext').click(function() {
		try
		{
			if ($(this).attr('href') != '')
			{
				var ext = window.open($(this).attr('href'));
				ext.focus();
			}
		}
		catch (e) {}

		return false;
	});


	// external links
	$('form.redirect .pulldown select').change(function() {
		if ($(this).val() != '')
		{
			window.location.href = $(this).val();
		}
	});

	
	// keyvisuals
	try
	{
		if ($('.keyvisual-rotator img.sweep').length > 0)
		{
			var $images = $('.keyvisual-rotator img.sweep').hide().removeClass('sweep');
			$('.keyvisual-rotator').everyTime(5000, function(index) {
		        var crntImage = $('.keyvisual-rotator img:visible');
		        var nextImage = (index % $('.keyvisual-rotator img').length == 0)
		        	? $('.keyvisual-rotator img:first')
		        	: crntImage.next();
	
		        crntImage.fadeOut(2000);
		        nextImage.fadeIn(2000);
			});
		}
	}
	catch (exc) {}
	
	// keyvisuals project page
	try
	{
		if ($('.keyvisual-rotator-project-top img.rotatingImg.sweep').length > 0 &&
			$('.keyvisual-rotator-project-main img.rotatingImg.sweep').length > 0)
		{
			
			var imagesMain = $('.keyvisual-rotator-project-main img.rotatingImg.sweep').hide().removeClass('sweep');
			var imagesTop = $('.keyvisual-rotator-project-top img.rotatingImg.sweep').hide().removeClass('sweep');
			var intervall = 7500;
			var fadeTime = 1400;
			
			$('.keyvisual-rotator-project-main').
				everyTime(
					intervall, 
					function(index) {
						
						var crntImageMain = ($('.keyvisual-rotator-project-main img.rotatingImg:visible'));
						var crntImageTop  = ($('.keyvisual-rotator-project-top img.rotatingImg:visible'));
						
						var nextImageMain = ((index % $('.keyvisual-rotator-project-main img.rotatingImg').length) == 0) 
										? $('.keyvisual-rotator-project-main img.rotatingImg:first')
										: crntImageMain.next();
										
						var nextImageTop = ((index % $('.keyvisual-rotator-project-top img.rotatingImg').length) == 0) 
										? $('.keyvisual-rotator-project-top img.rotatingImg:first')
										: crntImageTop.next();				
			
						crntImageMain.fadeOut(fadeTime);
						nextImageMain.fadeIn(fadeTime);
						
						crntImageTop.fadeOut(fadeTime);
						nextImageTop.fadeIn(fadeTime);
						
					}
				);
			
		}
	}
	catch (exc) {}
	
	// rotating project teasers
	try
	{	
		if($('div.teasers div.image-text').length > 1)
		{
			// set the wrapping elements height equal to the largest boxes height
			height = 0;
			$('div.teasers div.box').each(function() {
				if(height < $(this).height())
				{
					height = $(this).height();
				}
			});
			height = (height + 15) / 11;
			$('div.teasers').css('height', height + 'em');
			
			// get the first teasers left offset
			var left = $('div.teasers div.image-text:first').position().left;
					
			// hide all but the first project teaser, position them 
			// absolute on the first teasers left edge.
			$('div.teasers div.image-text:not(:first)').hide()
													   .css('position', 'absolute')
													   .css('left', left + 'px');
			
			// rotate teasers
			$('div.teasers').everyTime(6000, function(index) {
				var crntItem = $('div.teasers div.image-text:visible');
		        var nextItem = (index % $('div.teasers div.image-text').length == 0)
		        	? $('div.teasers div.image-text:first')
		        	: crntItem.next();
		        	
		        crntItem.fadeOut(1500);
		        nextItem.fadeIn(1500);
			});
		}
	}
	catch (exc) {}


	// address carousel
	try
	{
		var $addressCarousel = $('div.address-carousel div.clip');
		var $addressList     = $('div.addresses > ul');

		if ($addressList.length > 0 && $addressCarousel.length > 0)
		{
			Carousel.setList($addressList);
			$addressCarousel.jCarouselLite({
				auto: 400,
				speed: 1000,
				visible: 3,
			    btnPrev: "div.carousel-left",
				btnNext: "div.carousel-right"
			});
			$('li,h2,.address',$addressList).hide();
			$('a', $addressCarousel).click(Carousel.openSingleItem);
			$('.carousel-showall a').click(Carousel.openAll);

			$('div.carousel-right,div.carousel-left').show();
		}
	}
	catch (exc) {}


	// panorama carousel
	try
	{
		var $panoramaCarousel  = $('div.panorama-carousel');

		var visibleItems = 4;
		if ($panoramaCarousel.get(0).className.match(/split-([1-9])/))
		{
			visibleItems = parseInt(RegExp.$1);
		}

		if ($('li.carousel-item', $panoramaCarousel).length >= visibleItems)
		{
			$panoramaCarousel.jCarouselLite({
				speed: 1000,
				visible: visibleItems,
			    btnPrev: "div.carousel-left",
				btnNext: "div.carousel-right"
			});
			$('div.carousel-right,div.carousel-left').show();
			$('li.special', $panoramaCarousel).css('width', '40.909em');
			$('li.split', $panoramaCarousel).addClass('border');
			$panoramaCarousel.css('height', '10.727em');
			
			if (GLOBALS.isIE6)
			{
				$panoramaCarousel.css({
					'height': '11.727em',
					'width': ($panoramaCarousel.width()+4)+'px'
				});
				$('li.carousel-item', $panoramaCarousel).css({
					'height': '11em',
					'width': '21.273em'
				});
				$('li.special', $panoramaCarousel).css('width', '42.546em');
			}
		}
	}
	catch (exc) {}


	// print view
	if (window.location.href.match(/(\?|&)print/))
	{
		print();
	}

	
	// tooltips in the certificate menu
	$('.certificates img').tooltip({		 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - ", 
		fade: 250 
	});

	
	// search field
	$("#searchQuery").bind("focus", function() {
		if ($(this).attr('rel') == undefined || $(this).attr('rel') == '')
		{
			$(this).attr("rel", $(this).val());
			$(this).val('');
		}
		else if ($(this).attr('rel') != '')
		{
			$(this).select();
		}
	});

	$("#searchQuery").bind("blur", function() {
		if ($(this).val() == '')
		{
			$(this).val($(this).attr("rel"));
			$(this).attr('rel', '');
		}
	});
	
	
	try
	{
		Menu.initialize();
	}
	catch (exc) {}
});


/**
 * Event for the pulldown of contacts in the probuerger product detail area to switch between the contacts.
 */
$(document).ready(function()
{
	try
	{
		$('div#contactspulldown_container').click(function(){  
			var selectedContact = $(this).find('ul li.selected');
			var contactName = $(selectedContact).text();
			var contactsRightMargin = $('div.contactRightMargin');

			$(contactsRightMargin).each(function(index, value){ 
				if (true)
				{
					if ($(value).attr('title') == contactName)
					{
						$(value).removeClass('sweep');
					}
					else
					{
						if (!$(value).hasClass('sweep'))
						{
							$(value).addClass('sweep');
						}
					}
				}
			});

			var hiddenContacts = $('div.contactRightMargin.sweep');
			var contacts = $('div.contactRightMargin');

			// check if all contacts are hidden
			// in this case set the first contact active          
			if (hiddenContacts.length == contacts.length)   
			{
				$(contacts[0]).removeClass('sweep');
			}
		});
	}
	catch (exc) {}
});

/**
 * Map tooltips for infopoints. 
 */
$(document).ready(

	function () {
		try
		{
			var offsetLeft = 40;
			var offsetTop = 100;
			var tooltips = $('#tooltips');
			$('body').append(tooltips);
			$('#map ul li').mouseover(show).mousemove(reposition).mouseout(wait);
			
		}
		catch (exc) {}
        
		function show (e) {
			
			var currentObjectId = $(this).attr('id');
			var id = getNumberPart(currentObjectId, 'infopoint');
			var tooltip = '#tooltip' + id;
			$('img', this).removeAttr('title');
			
			var positionLeft = e.pageX + offsetLeft;
			var positionTop = e.pageY - offsetTop;
			
			$(tooltip).fadeIn('slow').css("top", positionTop).css("left", positionLeft);
		}
		
		function wait () {
			var currentObjectId = $(this).attr('id');
			window.setTimeout("hide('"+currentObjectId+"')", 1000);
		}
		
		function reposition (e) {
			var currentObjectId = $(this).attr('id');
			var id = getNumberPart(currentObjectId, 'infopoint');
			var tooltip = '#tooltip' + id;
			
			var positionLeft = e.pageX + offsetLeft;
			var positionTop = e.pageY - offsetTop;
			
			$(tooltip).css("top", positionTop).css("left", positionLeft);
		}
	}
	
);

function hide (objectId) {
	var id = getNumberPart(objectId, 'infopoint');
	var tooltip = '#tooltip' + id;
	$(tooltip).fadeOut('slow');
}	

/**
 * Get the number part of a string like 'tooltip12'.
 *
 * @param  string input
 * @param  string prefix
 * @return string
 */
function getNumberPart(input, prefix) {

	var number = '';
	if (typeof input == "string" && typeof prefix == "string")
	{
		if (input.substring(0, prefix.length) == prefix)
		{
			var numberFilter = /[0-9]+/;
			number = input.match(numberFilter);
		}
	}
	return number;
}

/**
 * Map tooltip taxonomy filter of infopoints. 
 */
$(document).ready(

	function () {
		try
		{
			$('#map_form input').click(filter);
			
		}
		catch (exc) {}
        
		function filter (e) {
			
			var inputElems = $('#map_form input:checked');
			
			if (inputElems.length > 0)
			{
				var tagsOut = new Array(inputElems.length);
				var tagsIn = new Array(inputElems.length);
				var selectorsOut = '';
				var selectorsIn = '';
				jQuery.each(inputElems, function(key, object) {
					var tag = $(object).attr('id');
					tagsOut[key] = '.' + tag;
					selectorsOut = tagsOut.join(", ");
					tagsIn[key] = 'li.' + tag;
					selectorsIn = tagsIn.join(", ");
				});
				/*
				var infoPointsIn = $('#map ul ' + selectorsIn);
				jQuery.each(infoPointsIn, function(key, object) {
					var point = $(object).attr('id');
					//console.log(key + ' :: ' + object + ' :: ' + point);
				});
				*/
				$('#map ul ' + selectorsIn).fadeIn('slow');
				$('#map ul ').find('li:not('+ selectorsOut +')').fadeOut('slow');
			}
			else
			{
				$('#map ul li').fadeOut('slow');
			}
		}
	}
);


$(document).ready(function()
{
	try
	{
		/**
		 * default tightbox behaviour
		 */
		$('.tightbox').tightbox(
		{			
			typeTemplates : {
		 		flash     : 'http://hoexter-abnahme.projects.tweety.kernarea.de/kreis-hoexter.de/public/page.php'
		 	}		 	
		});
	}
	catch (exc) {}
	
});