var currentPosition = -1;
var oldPosition = -1;
var numberOfSlides;
var slideInt;
var imageDuration = 5000;
var menuAnimation = false;

$(document).ready(function(){
	var browserName=navigator.appName; 

	
	/* SLIDESHOW IN BACKGROUND*/
	var slides = $('.slide');
	numberOfSlides = slides.length;
	
	if(numberOfSlides > 1)
	{
		updateSlides();
		$(function() {slideInt = setInterval(updateSlides, imageDuration);});
	}
	
	var len = 0;
	for(var i = 0; i < $('#slideshow_navi_items .navi_wrapper').children('img').length; i++)
	{
		len += $('#slideshow_navi_items .navi_wrapper').children('img:eq(' + i +')').outerWidth();
	}
	
	$('#slideshow_navi_items').css('width', len);
	$('#slideshow_navi_items .navi_wrapper').css('width', len);
	
	$('#slideshow_navi_items .leaf').mouseenter(function() {
		$(this).css('cursor', 'pointer');
	});

	$('#slideshow_navi_items .leaf').click(function() {
		currentPosition = Number($(this).attr('id'));
		
		clearInterval(slideInt);
		updateSlides('false');
	    return false; 
	});
	
	/* MENU */ 
	$('#headeroverlay').mouseenter(function() {
		$(this).css('cursor', 'pointer');
	});
	
	var pd = false;
	var isAnimating = false;
	var mouseOverHeader = false;
	var mouseOverSubmenu = null;
	
	$('#header, #category').mouseenter(function() {
		mouseOverHeader = true;
		
		if(! $('body').hasClass("front") && ! pd && ! isAnimating)
		{
			//console.log("mouseenter");
			isAnimating = true;
			$('#header').stop(true, false);
			$('#header').stop(true, false);
			$('#main-menu').stop(true, false);
			
			if($.browser.msie)
			{
				$('#projectdetail').css('border-bottom', 'none');
				$('#header').css('border-bottom', '1px solid #8F8F91');
			}
			
			$('#category').animate({opacity: '0'}, 250);
			$('#header').animate({height: "36"}, 500, function() {
				$('#projectdetail').hide();
				$('#header').css('height', 'auto');
				$('#main-menu').css('height', '0');
				$('#main-menu').animate({height: '327'}, 500, function() {
					isAnimating = false;
					
					if(mouseOverSubmenu)
					{
						$(mouseOverSubmenu).show();
					}
					else if(! mouseOverHeader)
					{
						if($('#projectdetail').length > 0)
						{
							isAnimating = true;
							$('#header').animate({height: "26"}, 500, function() {
								$('#main-menu').hide();
								$('#header').css('height', 'auto');
								$('#projectdetail').slideDown('slow', function() {
									isAnimating = false;
									if($.browser.msie)
									{
										$('#header').css('border-bottom', 'none');
										$('#projectdetail').css('border-bottom', '1px solid #8F8F91');
									}
									$('#category').animate({opacity: '1'}, 250);
								});
							});
						}
						else
						{
							/*
							 * doesn't work properly in IE7! the div disappears after sliding down
								$('#main-menu').slideDown('slow');
							 */
							isAnimating = true;
							$('#header').animate({height: "26"}, 500, function() {
								$('#category').animate({opacity: '1'}, 250);
								$('#main-menu').hide();
								$('#header').animate({height: "52"}, 250, function() {
									isAnimating = false;
								});
							});
						}
					}
					
				});
			});
		}
		else
		{
			//console.log("mouseenter ohne");
		}
	});
	
	$('#projectdetail').mouseenter(function() {
		pd = true;
		//console.log("enter pd");
	});
	
	$('#projectdetail').mouseleave(function() {
		pd = false;
		//console.log("leave pd");
	});
	
	$('#header').mouseleave(function() {
		mouseOverHeader = false;
		
		if(! $('body').hasClass("front") && ! isAnimating)
		{
			//console.log("leave pd " + $('#projectdetail').length + " ani " + isAnimating);
			
			if($('#projectdetail').length > 0)
			{
				if($('#projectdetail .show').css('display') == "block")
				{
					//console.log("eins");
					
					if($('#projectdetail').css('display') == "none" )
					{
						isAnimating = true;
						$('#header').animate({height: "26"}, 500, function() {
							$('#main-menu').hide();
							$('#header').css('height', 'auto');
							$('#projectdetail').slideDown('slow', function() {
								isAnimating = false;
								if($.browser.msie)
								{
									$('#header').css('border-bottom', 'none');
									$('#projectdetail').css('border-bottom', '1px solid #8F8F91');
								}
								$('#category').animate({opacity: '1'}, 250);
							});
						});
					}
				}
				else
				{
					if(! isAnimating && $('#projectdetail').css('display') == "none")
					{
						//console.log("zwei");
						isAnimating = true;
						$('#header').animate({height: "26"}, 500, function() {
							$('#main-menu').hide();
							$('#header').css('height', 'auto');
							$('#projectdetail').slideDown('slow', function() {
								isAnimating = false;
								if($.browser.msie)
								{
									$('#header').css('border-bottom', 'none');
									$('#projectdetail').css('border-bottom', '1px solid #8F8F91');
								}
								$('#category').animate({opacity: '1'}, 250);
							});
						});
					}
					else if(isAnimating)
					{
						//console.log("drei");
						
						$('#header').stop(true, false);
						$('#main-menu').stop(true, false);
						isAnimating = true;
						
						$('#header').animate({height: "26"}, 500, function() {
							$('#main-menu').hide();
							$('#header').css('height', 'auto');
							$('#projectdetail').slideDown('slow', function() {
								isAnimating = false;
								if($.browser.msie)
								{
									$('#header').css('border-bottom', 'none');
									$('#projectdetail').css('border-bottom', '1px solid #8F8F91');
								}
								$('#category').animate({opacity: '1'}, 250);
							});
						});
					}
					else
					{
						//console.log("vier");
					}
				}
			}
			else
			{
				//console.log("unten");
				
				$('#header').stop(true, false);
				$('#main-menu').stop(true, false);
				isAnimating = true;
				
				$('#header').animate({height: "26"}, 500, function() {
					$('#category').animate({opacity: '1'}, 250);
					$('#main-menu').hide();
					$('#header').animate({height: "52"}, 250, function() {
						isAnimating = false;
					});
				});
			}
		}
	});
	
	$('#block-menu-menu-mainnavigation ul li').mouseenter(function() {
		var pos = $(this).find('.submenu, .menu').parent().find("a:first").position();
		
		$(this).find('.submenu, .menu').css("left", $(this).find('.submenu, .menu').parent().find("a:first").width() + "px");
		$(this).find('.submenu, .menu').css("top", pos.top + 6 + "px");
	});
	
	$('#block-menu-menu-mainnavigation ul.menu li').hover(
	  function () {
		  if($(this).find('.menu').length > 0)
			  mouseOverSubmenu = $(this).find('.menu');
		  else if($(this).find('.submenu').length > 0)
			  mouseOverSubmenu = $(this).find('.submenu');
		  
		  if(! isAnimating)
		  {
			  $(this).find('.menu').show();
			  $(this).find('.submenu').show();
		  }
	  },
	  function () {
		  mouseOverSubmenu = null;
		  
		  $(this).find('.menu').hide();
		  $(this).find('.submenu').hide();
	  }
	);
	
	$('.block-search').mouseenter(function() {
		$(this).css('cursor', 'pointer');
	});
	
	$('.block-search').click(function() {
		if($(this).find('form').css('display') == "none")
		{
			$(this).find('form').show();
		}
		else if(document.activeElement.name == 'search_block_form')
		{
			// do nothing
		}
		else
		{
			$(this).find('form').hide();
		}
	});
	
	
	
	if($('#projectdetail').length == 0)
	{
		$('#header').css('padding-bottom', '17px');
		if($.browser.msie)
		{
			$('#header').css('border-bottom', '1px solid #8F8F91');
		}
	}
	
	
	
	/* NEWSLETTER */
	$('#nl-link').mouseenter(function() {
		$(this).css('cursor', 'pointer');
	});
	
	
	
	$('#nl-link').click(function() {
		if($('.block-simplenews form').css("top") == "500px")
		{
			$('.block-simplenews form').css("top", "-" + ($('.block-simplenews form').outerHeight() + 40) + "px");
		}
		else
		{
			$('.block-simplenews form').css("top", "500px");
		}
	});
	

	
	/* PROJECT PAGE */
	$('#projectdetail .shower, .more').mouseenter(function() {
		$(this).css('cursor', 'pointer');
	});
	
	$('#projectdetail .shower').click(function() {
		
		if($('#projectdetail .show').css('display') == "none")
		{
			$('#projectdetail .shower').addClass('close');
			$('#projectdetail .shower').removeClass('open');

			$('#projectdetail .show').slideDown('slow');
		}
		else
		{
			$('#projectdetail .shower').addClass('open');
			$('#projectdetail .shower').removeClass('close');
			
			$('#projectdetail .show').slideUp('slow');
		}
	});
});





function updateSlides(auto)
{
	if(auto != 'false')
	{
		oldPosition = currentPosition;
		
		if(currentPosition == numberOfSlides-1)
		{
			currentPosition = 0;
		}
		else
		{
			currentPosition++;
		}
	}
	
	$('#slideshow').children('.slide:eq(' + currentPosition +')').hide();
	$('#slideshow').children('.slide:eq(' + currentPosition +')').css('z-index', '2');
	$('#slideshow').children('.slide:eq(' + currentPosition +')').fadeIn(800, function() {
		$('#slideshow').children('.slide:eq(' + oldPosition +')').hide();
	});
	
	for(var i = 0; i < $('#slideshow').children('.slide').length; i++)
	{
		if(i != currentPosition && i != oldPosition)
		{
			$('#slideshow').children('.slide:eq(' + i +')').hide();
			$('#slideshow').children('.slide:eq(' + i +')').css('z-index', '1');
		}
		else if(i == oldPosition)
		{
			$('#slideshow').children('.slide:eq(' + i +')').css('z-index', '1');
		}
	}
	
	$('#slideshow_navi_items .leaf').attr('src', Drupal.settings.basePath + 'sites/all/themes/inostudio/gfx/image_not_shown.png');
	$('#slideshow_navi_items #' + currentPosition).attr('src', Drupal.settings.basePath + 'sites/all/themes/inostudio/gfx/image_shown.png');
}

function adjustNews()
{
	var wHeight = $(window).height();
	var docHeight = $(document).height();
	var numItems = $('.view-Start .view-content, .view-News .view-content').children('div').length;
	var currHeight = 0;
	
	for(var i = 0; i < numItems; i++)
	{
		var currElem = $('.view-Start .view-content, .view-News .view-content').children('div:eq(' + i +')');
		var elemHeight = currElem.outerHeight();
		
		if(currHeight + elemHeight <= wHeight )
		{
			currHeight += elemHeight;
		}
		else
		{
			currElem.css("display", "none");
		}
	}
}

