﻿var slideShowInterval = null;

function slideShow()
{
	// Image-Slide
	if($('#slideshow img').length > 1)
	{
		var $active = $('#slideshow img.active');
		
		if ( $active.length == 0 ) 
		{
			$active = $('#slideshow img:last');
		}

		var $next = ($active.next().length)? $active.next() : $('#slideshow img:first');

		$active.addClass('last-active');
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() 
			{
				$active.removeClass('active last-active');
		});
	}
	
	// News-Slide
	if($('.startpage #newscontainer .newsitem').length > 2)
	{
		var $activeNews = $('.startpage #newscontainer .active');
		
		if ( $activeNews.length == 0 )
		{
			$activeNews = $('.startpage #newscontainer .page:last');
		}
		
		var $nextNews = ($activeNews.next().length)? $activeNews.next() : $('.startpage #newscontainer .page:first');
	
		$activeNews.addClass('last-active');
		$nextNews.css({opacity: 0})
			.addClass('active')
			.animate({opacity: 1}, 500, function() 
			{
				$activeNews.removeClass('active last-active');
			});
	}
	
	// Product News-Slide
	if($('.startpage #productnewscontainer .product-news').length > 1)
	{
		var $activeNews2 = $('.startpage #productnewscontainer .active');
		
		if ( $activeNews2.length == 0 )
		{
			$activeNews2 = $('.startpage #productnewscontainer .product-news:last');
		}
		
		var $nextNews2 = ($activeNews2.next().length)? $activeNews2.next() : $('.startpage #productnewscontainer .product-news:first');
	
		$activeNews2.addClass('last-active');
		$nextNews2.css({opacity: 0})
			.addClass('active')
			.animate({opacity: 1}, 500, function() 
			{
				$activeNews2.removeClass('active last-active');
		});
	}
}

function addTooltip(element)
{
	element.each(function()
	{
		$(this).find('li').each(function()
		{
			var $li = $(this); 
			var $a = $li.find('a');
			var iconUrl = $a.attr('title');
			var labelUrl = $a.attr('rel');
			var $label = $('<img class="label" />').attr('src',labelUrl).prependTo($li);
			var lHeight;
			var lwidth;

			$a.removeAttr('title')
				.css({
						backgroundImage:'url(' + iconUrl + ')'
			});
			
			// Image-load-event fix: http://www.nabble.com/Image-load-event---some-progress-td22243817s27240.html
			if (this.complete || this.readyState == 'complete')
			{
				lHeight = $label.height();
				lwidth = $label.width();
				
				$label.css(
				{
					display		:	'none',
					position 	:	'absolute',
					left		:	'-' + Math.floor((lwidth/2) - 14) + 'px',
					top			:	'-' + (lHeight) + 'px'
				});
			}
			else
			{	
				$label.bind('load readystatechange', function(e) 
				{
					if (this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) 
					{
						lHeight = $label.height();
						lwidth = $label.width();
						
						$label.css(
						{
							display		:	'none',
							position 	:	'absolute',
							left		:	'-' + Math.floor((lwidth/2) - 14) + 'px',
							top			:	'-' + (lHeight) + 'px'
						});
					}
				});
			}
				
			$li.bind('mouseover',function()
			{
				$label.css({
					display:'block',
					opacity:0
				}).animate({
					opacity:1
				}, 400);
			});
			$li.bind('mouseout',function(){$label.css({display:'none'})});

		});
	});
}

function brandHover()
{
	$('.brands a').each(function()
	{
		var aElement = $(this);
		var external = aElement.hasClass('external');
		var aRel = aElement.removeAttr('title').find('img').attr('rel');
		
		if(external)
		{
			aElement.attr('target','_blank');
		}
		
		aElement.prepend('<span class="mask" /><span class="mask-text"><img src="'+aRel+'" /></span>');
		aElement.find('.mask-text, .mask').css({visibility:'hidden'});
	});
	
	$('.mask-text img').load(function()
	{
		var thisImg = $(this);
		var thisH = thisImg.height();
		
		var marginTop = ($.browser.msie)? Math.floor((67 - thisH)/2)-3 : Math.floor((67 - thisH)/2);
		
		thisImg.css(
		{
			marginTop:marginTop+'px'
		});
		
	});
	
	$('.brands a').hover(function()
	{
		var aElement = $(this);
		var external = aElement.hasClass('external');
		var mask = aElement.find('.mask');
		var text = aElement.find('.mask-text');
		var myLink = aElement.attr('href');
		var imageLink = text.find('img');
		
		$('.mask, .mask-text').css({visibility:'hidden'});
		
		mask.css({visibility:'visible',opacity:0});
		text.css({visibility:'visible',opacity:0});
		
		imageLink.click(function()
		{
			if(external)
			{
				window.open(myLink);
			}
			else
			{
				location.href(myLink);
			}
			return false;
		});
		
		mask.animate({opacity: 0.7}, 200, function()
		{
			text.animate({opacity: 1}, 100);
		});
		
	},
	function()
	{	
		$('.mask-text, .mask').css({visibility:'hidden',opacity:0});
	});
	
	$('.startpage .wide .brands li:nth-child(2n)').css('marginRight','0');
	$('.startpage .last .brands li:nth-child(2n)').css('marginRight','0');
	
}

jQuery(function()
{
	
	(function pageInit()
	{
		// Fix Menu Width
		var menuWidth = 0;
		
		$('#navigation li:last').css('marginRight','0');
		$('#navigation li').each(function()
		{
			menuWidth += $(this).width() + 10;
		});
		
		menuWidth -= 10;
		
		$('#navigation').css('width',menuWidth+'px');
		
		// Add subMenuArrow
		if($('.active.hasSubMenu').length)
		{
			var width = $('.active.hasSubMenu').width();
			$('.active.hasSubMenu').append('<div style="width:' + width + 'px;"></div>');
		}
	})();
	
	if($('.retailers').length)
	{
		(function retailerMap()
		{
			$('.retailers .puff-image a')
			.bind('mouseover',function()
			{
				$('.retailers .puff-image').append('<div class="mapHolder ' + $(this).attr('rel') + '"></div>');
			})
			.bind('mouseout',function()
			{
				$('.mapHolder').remove();
			});
		})();
	}
	
	if($('.content .brands').length)
	{
			$('.content .brands a[href^="http://"]').addClass('external');
			brandHover();
	}
	
	if($('.retailers .retailer-brands').length)
	{
		addTooltip($('.retailers .retailer-brands'));
	}
	
	$('.newsitem:last').css({
		backgroundImage:'none',
		margin:'0',
		padding:'0'
	});

	$('table.striped tbody tr:even').addClass('even');
	$('#newscontainer .newsitem:last').addClass('last');
	
	if( ( $('#slideshow img').length > 1 ) || ( $('.startpage #newscontainer .newsitem').length > 2 ) )
	{
		// Wrap items in a .page-div
		$('.startpage #newscontainer .newsitem:nth-child(2n+1)').each(function()
		{
			var thisItem = $(this);
			thisItem
				.add(thisItem.next())
				.wrapAll('<div class="page"></div>');
		});
		
		$('#slideshow img:first').addClass('active');
		$('.startpage #newscontainer .page:first').addClass('active');
		$('#productnewscontainer').find('.news-item:first').addClass('active');
		
		slideShowInterval = setInterval(slideShow, 7000);
	}
	
});