/* for google analytics */

var _gaq = _gaq || [];


/* trainer animation */

var height = 284; // height of each element
var width = 211; // width of each elements 
var offsetX = 237; // offset between to elements (identical points of the 2 elements, e.g. left top corner)
var offsetY = 310; // offset between to elements
var offsetTopY = 279; // offset of the list from parent element

function animateTrainer() {
	var containerHeight=0;
	var flag = 0; // flag to be subtracted from index to uncount fully display element (prevent gap)
	jQuery('div.trainer').each(function(i) {
		if(jQuery(this).hasClass('big')) { // fully displayed element
			jQuery(this).animate({height: '576px', width: '668px', padding: '16px', top: (offsetTopY+offsetY)+'px', left: '237px'},{ queue:false, duration:500, easing:'swing'});
			jQuery(this).children('.vita').fadeIn();
			containerHeight=(containerHeight>offsetTopY+3*offsetY) ? containerHeight : offsetTopY+3*offsetY;
			flag=1;
		}
		else { 
			jQuery(this).children('.vita').fadeOut();
			jQuery(this).animate({heigth: height+'px', width: width+'px', padding: '8px'},{ queue:false, duration:500, easing:'swing'});
			if (i-flag < 4) { // first row (4 elements) 
				jQuery(this).animate({top: offsetTopY+'px', left: (i-flag)*(offsetX)+'px'},{ queue:false, duration:500, easing:'swing'});
				containerHeight=(containerHeight>offsetTopY+offsetY) ? containerHeight : offsetTopY+offsetY;
			}
			else if (i-flag < 6) { // column next to fully displayed element, 2 elements
				jQuery(this).animate({top: ((i-flag-3)*offsetY+offsetTopY)+'px', left: '0'},{ queue:false, duration:500, easing:'swing'});
				containerHeight=(containerHeight>offsetTopY+(i-2)*offsetY) ? containerHeight : offsetTopY+(i-2)*offsetY;
			}
			else { // all other elements in grid view; 4 elements each row.
				var thisTop = Math.ceil((i-flag+3)/4)*offsetY+offsetTopY;
				var thisLeft = ((i-flag-2)%4)*offsetX;
				jQuery(this).animate({top: thisTop+'px', left: thisLeft+'px'},{ queue:false, duration:500, easing:'swing'});
				var thisHeight = thisTop+offsetY;
				containerHeight=(containerHeight>thisHeight) ? containerHeight : thisHeight;
			}
		}
	});
	if(!(parseInt(jQuery.browser.version)<8 && jQuery.browser.msie)) {
		jQuery('.col-main').css('height', containerHeight+'px');
	}
}






jQuery(document).ready(function($){
	
	/* product filter tooltip animation */
	if(!(jQuery.browser.msie)) {
		$('.productHead th ol').css('opacity','0');
		$('.productHead th div').hover(
			function(){
				$(this).children('ol').css('display','block');
				$(this).children('ol').animate({opacity: '1'},{ queue:false, duration:400, easing:'swing'});
			},
			function(){
				$(this).children('ol').animate({opacity: '0'},{ queue:false, duration:400, easing:'swing', complete: function() {
						$(this).css('display','none');
					}
				});
			}
		);
	}
	
	/* product description*/
	
	$('.product .description').prepend($('.product .shortDescription').html()).children('#moreDesc').remove();
	$('.product .description').append('<span class="close">close</span>');
	//$('.product .description #moreDesc').remove();
	$('#moreDesc').click(
		function() {
			$('.product .description').fadeIn().click(function(){$(this).fadeOut();});
			return false;
	});
	
	/* product vita*/
	
	$('li.trainer').append('<p>'+$('li.trainer .vita').text().substr(0,500)+'&nbsp;&nbsp; <a id="moreVita" href="#moreVita" onclick="return false;">mehr ...</a></p>');
	$('li.trainer .vita').append('<span class="close">close</span>');
	//$('.product .description #moreDesc').remove();
	$('#moreVita').click(
		function() {
			$('li.trainer .vita').fadeIn().click(function(){$(this).fadeOut();});
			return false;
	});
	
	
	/* product navi image animation */
	$('#productNavi li img, #productNavi li dfn').css({opacity: '0', display: 'block'});
	$('#productNavi li.active img, #productNavi li.home img, #productNavi li.active dfn, #productNavi li.home dfn').css({opacity: '1'});
	$('#productNavi li').hover(
		function() { // mouseover
			if (!($(this).hasClass("home") || $(this).hasClass("active"))) {
				$(this).children('img,dfn').css('display', 'block');
				$(this).children('img').css('z-index', '5');
				$(this).children('dfn').css('z-index', '6');
				$(this).children('img, dfn').animate({opacity: '1'},{ queue:false, duration:400, easing:'swing'});
				//$('#productNavi li img.home, #productNavi li img.active').animate({opacity: '0'},{ queue:false, duration:400, easing:'swing'});
			}
			else { 
				$(this).children('img').css('z-index', '3');
			}
		},
		function() { //mouseout
			if (!($(this).hasClass("home") || $(this).hasClass("active"))) {
				$(this).children('img, dfn').animate({opacity: '0', zIndex: '3'},{ queue:false, duration:400, easing:'swing', complete: function() {
					$(this).css('display','none');
				}
			});
				//$(this).children('img').css('z-index', '3');
				//$('#productNavi li img.home, #productNavi li img.active').animate({opacity: '1'},{ queue:false, duration:400, easing:'swing'});
			}
		}
	);
	
	
	/* partner */
//	var sliderWidth=0;
//	var slides = $('.block-partner .block-content a');
//	$(slides).wrapAll('<div id="slider">');
//	$(slides).each(function(){
//		sliderWidth+=$(this).width()+12;
//	});
//	$('#slider').css({width: sliderWidth+'px',position: 'absolute', top: 0, left: 0});
//	
//	
//	var run = setInterval(function() {
//		$('#slider').animate({left: -sliderWidth+'px'},10000,'linear',function(){$('#slider').css('left',0)})
//	}, 10000);
	
	
	
	/* init trainer */
	var trainerItems = $('.col1-layout div.trainer');
	if (trainerItems.length > 0) {
		animateTrainer();
		$(trainerItems).css({float: 'none', position:'absolute', margin: 0});
		// for debugging: add indexes
		/*
		$(trainerItems).each(function(j) {
			$(this).append('<span style="position: absolute; top: 0; right: 0; color: red; font-weight: bold;"># '+j+'</span>');
		});
		 */
		trainerItems.click(function(){
			$('div.trainer').removeClass('big');
			$(this).addClass('big');
			animateTrainer();
		});
	}
});
