// Headline
(function ($) {
	$.fn.playHeadline = function() {
		var currentVisible = jQuery('li:visible', this);

		if (currentVisible.is(':last-child')) currentVisible.fadeOut(function() { jQuery('li:first-child', jQuery(this).parent()).fadeIn() });
		else currentVisible.fadeOut(function() { jQuery(this).next().fadeIn() });
	}
})(jQuery);

$(function() {
	$('.layer-showcase').each(function() {
		var oStage = $(this);
		var iLeft = -1900;
		var isAnimated = false;
		
		$('.layer-showcasecontenttext', this).hide();
		
		$('.layer-showcasecontentwrapper', this).each(function() {
			iLeft += 950;
			$(this).css('left', iLeft+'px').css('opacity', '.4').css('filter', 'alpha(opacity=40)');
 			if (iLeft == 0) {
 				$('.layer-showcasecontenttext', $(this).css('opacity','1').css('filter', 'alpha(opacity=100)')).fadeIn('normal');
 			}
		});
		
		$('a[href=#next]', this).click(function() {
			if (!isAnimated) {
				var oOnStage = $('.layer-showcasecontenttext:visible', oStage).parent().parent();
				var oPrev = oOnStage.prevAll();
				
				if (oPrev.length > 1) {
					iLeft = parseInt($('.layer-showcasecontentwrapper:first', oStage).css('left'));
					
					if (oPrev.length == 2) {
						iLeft -= 950;
						var oLastObject = $('.layer-showcasecontentwrapper:last', oStage);
						var oClonedObject = oLastObject.clone().css('left', iLeft);
						$('.layer-showcasecontenttext', oClonedObject).hide();
						$('.panel-previous', oStage).after(oClonedObject);
						oLastObject.remove();
					}
					
					isAnimated = true;
					
					$('.layer-showcasecontentwrapper', oStage).each(function() {
						iLeft += 950;
						$(this).animate({ 'left': iLeft+'px', 'opacity': '.4' }).css('filter', 'alpha(opacity=40)');
					});
					
					$('.layer-showcasecontenttext', oOnStage).fadeOut('normal', function() { 
						$('.layer-showcasecontenttext', oOnStage.prev().css('opacity', '1').css('filter', 'alpha(opacity=100)')).fadeIn('normal', function() { 
							isAnimated = false;
						}); 
					});
				}
			}
			
			return false;
		});
		
		$('a[href=#previous]', this).click(function() {
			if (!isAnimated) {
				var oOnStage = $('.layer-showcasecontenttext:visible', oStage).parent().parent();
				var oNext = oOnStage.nextAll();
				
				if (oNext.length > 1) {
					iLeft = parseInt($('.layer-showcasecontentwrapper:first', oStage).css('left'));
					iLeft += -1900;
					
					if (oNext.length == 2) {
						iLeft += 950;
						var oFirstObject = $('.layer-showcasecontentwrapper:first', oStage);
						var oClonedObject = oFirstObject.clone().css('left', 1900);
						$('.layer-showcasecontenttext', oClonedObject).hide();
						$('.panel-next', oStage).before(oClonedObject);
						oFirstObject.remove();
					}
					
					isAnimated = true;
					
					$('.layer-showcasecontentwrapper', oStage).each(function() {
						iLeft = parseInt(iLeft)+950;
						$(this).animate({ 'left': iLeft+'px', 'opacity': '.4' }).css('filter', 'alpha(opacity=40)');
					});
					
					$('.layer-showcasecontenttext', oOnStage).fadeOut('normal', function() { 
						$('.layer-showcasecontenttext', oOnStage.next().css('opacity', '1').css('filter', 'alpha(opacity=100)')).fadeIn('normal', function() {
							isAnimated = false; 
						}); 
					});
					
				}
			}
			
			return false;
		});
	});
	
	$('.layer-latestnews li:gt(0)').hide();
	
	if ($('.layer-latestnews')) setInterval(function() { $('.layer-latestnews').playHeadline() }, 5000);
	
	if ($('.layer-showcasewrapper')) setInterval(function() { $('.layer-showcasewrapper a[href=#next]').trigger('click'); }, 7000);
	
	
	// Partner logo
	$('.layer-partnercontent li:gt(0)').hide();
	
	if ($('.layer-partnercontent li').length > 1) {
		setInterval(function() {
			if ($('.layer-partnercontent li:visible').is(':last-child')) {
				$('.layer-partnercontent li:visible').fadeOut(function() {
					$('.layer-partnercontent li:first').fadeIn();
				});
			}
			else {
				$('.layer-partnercontent li:visible').fadeOut(function() {
					$(this).next().show();
				});
			}
		
		}, 3000);
	};
	
	
	// Related Item
	$('.layer-relateditem a.link-section').click(function() {
		var target = $(this).attr('href');
		target = '#' + 'listing-' + target.substr(1, target.length);
		
		if ($(this).hasClass('link-active')) $(this).removeClass('link-active').parent().removeClass('item-titleactive').addClass('item-title');
		else $(this).addClass('link-active').parent().addClass('item-titleactive');
		
		$(target).slideToggle('fast');
		
		return false;
	});
	
	$('.layer-relateditem ul li ul li a').click(function() {
		var targetURL = $(this).attr('href').substr(0, $(this).attr('href').indexOf('#'));
		var targetDIV = $(this).attr('href').substr($(this).attr('href').indexOf('#')+1);
		var currentURL = location.href.substr(0, location.href.indexOf('#'));
		
		if (currentURL.indexOf(targetURL) > -1) {
			$('.layer-articlelisting a[rel='+targetDIV+']').trigger('click');
			return false;
		}
	});
	
	$('.layer-relateditem a.link-section:gt(0)').trigger('click');
});

$(document).ready(function() {
	$('.layer-articlelisting').each(function() {
		var oListing = $(this);

		$('a', oListing).click(function() {
			if (!$(this).parent().hasClass('item-active')) {
				var aClickedItem = $(this);
				$('li.item-active a', oListing).each(function() {
					$(this).parent().removeClass('item-active');
					$('#layer-'+$(this).attr('rel')).slideUp('fast', function() {
						aClickedItem.parent().addClass('item-active');
						$('#layer-'+aClickedItem.attr('rel')).slideDown('fast');
					});
				});
			}

			return false;
		});
	});
	
	var articleListingTarget = location.href.substr(location.href.indexOf('#')+1);
	
	if (articleListingTarget != '') $('a[rel='+articleListingTarget+']').trigger('click');
});