var arrayCounter = 0;
var videos = new Array();
var flashVersion = "9";
var intv;
var currentVid = 2;
var dummyVar = false;

function getCookie(name) {
   var i=0; 
   var suche = name+"=";
   while (i<document.cookie.length){
      if (document.cookie.substring(i, i+suche.length)==suche){
         var ende = document.cookie.indexOf(";", i+suche.length);
         ende = (ende>-1) ? ende : document.cookie.length;
         var cook = document.cookie.substring(i+suche.length, ende);
         return unescape(cook);
      }
      i++;
   }
   return null;
}

function showThirdElement(animated) {
	listElements = $('.imageSlider li');

	if (animated == true) {
		animationSpeed = "slow";
	} else {
		animationSpeed = 200;
	}
	
	listElements.find('img').css('opacity', '0.6');
	listElements.find('img').css('filter', 'alpha(opacity = 60);');
	$('#nonverblaster').css('opacity', '0');
	$('#nonverblaster').css('filter', 'alpha(opacity = 0);');

	listElements.eq(2).find('img').css('opacity', '1');
	listElements.eq(2).find('img').css('filter', 'alpha(opacity = 100);');

	$('#nonverblaster').css('left',' 342px');
	listElements.eq(2).find('.subText').stop(true, true).slideDown(animationSpeed);
}

function hideThirdElement(animated) {
	
	if (animated == true) {
		animationSpeed = "slow";
	} else {
		animationSpeed = 200;
	}
	
	listElements = $('.imageSlider li');
	listElements.eq(2).find('.subText').stop(true, true).slideUp(animationSpeed);
	listElements.eq(2).find('img').css('opacity', '0.6');
	listElements.eq(2).find('img').css('filter', 'alpha(opacity = 60);');
}

function initFlashPlayer() {
	if( $.trim( $('.imageSlider ul li').eq(2).find('div.media-file').text() ).length == 0 ) {
		var mediaUrl = globalAssetPath+"flash/dummy.mp4";
		dummyVar = true;
	} else {
		var mediaUrl = $.trim( $('.imageSlider ul li').eq(2).find('div.media-file').text() );
	}
	
	var flashvars = {};
		flashvars.mediaURL = mediaUrl;
		flashvars.teaserURL  = "";
		flashvars.allowSmoothing = "false";
		flashvars.autoPlay = "true";
		flashvars.buffer = "3";
		flashvars.showTimecode = "false";
		flashvars.loop = "false";
		flashvars.controlColor = "0x3fd2a3";
		flashvars.controlBackColor = "0x000000";
		//flashvars.playerBackColor = "0xFFFFFF";
		flashvars.scaleIfFullScreen = "false";
		flashvars.showScalingButton = "false";
		flashvars.defaultVolume = "100";
		flashvars.crop = "true";
		flashvars.controlsEnabled = "false";

	var params = {};
		params.menu = "false";
		params.allowFullScreen = "false";
		params.allowScriptAccess = "always";
		params.wmode = "transparent";

	var attributes = {};
		attributes.id = "nonverblaster";
		//attributes.bgcolor = "#FF0000";

	swfobject.embedSWF(globalAssetPath + "flash/NonverBlaster.swf", "videoPlayer", "276", "150", flashVersion, false, flashvars, params, attributes);

	registerForJavaScriptCommunication("nonverblaster");
	
}

function computeEnd() {
	if( mouseOverFlag !== true ) {
		hideThirdElement(true);
		imageSlide('left', true);
	} else {
		sendToNonverBlaster('play');
	}

	intv = resetInterval();
}

function computeStart() {
	// computeStart() fires when the movie was loaded/buffered and playing starts.
	if( dummyVar == false ) {
		if( !$('.imageSlider .subText').is(':animated') &&
			!$('.imageSlider li').filter(':animated').is(':animated') ) {
			$('#nonverblaster').css('opacity', '1');
			$('#nonverblaster').css('filter', 'alpha(opacity = 100);');
			listElements = $('.imageSlider li');
			listElements.eq(currentVid).find('img').css('filter', 'alpha(opacity = 0);');
			listElements.eq(currentVid).find('img').css('opacity', '0');
		} else {
			$('#nonverblaster').css('opacity', '0').delay(300).css('opacity', '1');
			$('#nonverblaster').css('filter', 'alpha(opacity = 0);').delay(300).css('filter', 'alpha(opacity = 100);');
			listElements = $('.imageSlider li');
			listElements.eq(currentVid).find('img').css('filter', 'alpha(opacity = 60);').delay(300).css('filter', 'alpha(opacity = 0);');
			listElements.eq(currentVid).find('img').css('opacity', '0.6').delay(300).css('opacity', '0');
		}
	}

	dummyVar = false;

}

function resetInterval() {
	clearInterval( intv );
	intv = setInterval(function() {
		if( mouseOverFlag == false && ( videos[2] == "" || $('.imageSlider').hasClass('imageSlider-novideo') ) ) {
			hideThirdElement(true);
			imageSlide('left', true);
		}
	}, 4500);

	return intv;
}

function initSlider() {
	$('.imageSlider').css('overflow', 'hidden');
	$('.imageSlider ul').css('margin-left', '-220px');
	$('.subText').hide();
	
	soundClass= "soundOn";
	if (document.cookie.length>0) {
		cookie = getCookie('sound');

		if(cookie == 'off') {
			soundClass = "soundOff";			
		}
		
	}
	
	$('.imageSlide-wrapper').append('<a id="toggleSliderSound" class="'+soundClass+' png_bg" href="#"></a>');
	$('.imageSlider ul').prepend('<a href="#" id="slidePrev" class="png_bg"> <span> previous </span></a>');
	$('.imageSlider ul').prepend('<a href="#" id="slideNext" class="png_bg"> <span> next </span></a>');
	$('#toggleSliderSound').css('display','block');
	
	$('.imageSlider').mouseenter(function() {
		$('#slideNext, #slidePrev').stop(true,true).fadeIn();
	}).mouseleave(function() {
		$('#slideNext, #slidePrev').stop(true,true).fadeOut();
	});

	var i = 0;

	$('.imageSlider ul li div.media-file').each(function() {
		$(this).after('<div class="eventOverlay"></div>');
		videos[i] = $.trim( $(this).text() );
		i++;
	});

	var vidsize = videos.length;

	if( vidsize <= 5 && vidsize > 0 ) {
		do {
			for ( var x = 0; x < vidsize; x++ ) {
				$('.imageSlider li').eq(x).clone().css("margin-left","0px").appendTo( $('.imageSlider ul') );
				videos.push(videos[x]);
			}
		} while(videos.length < 5);
	}
	showThirdElement(true);
}

function imageSlide(direction, animated) {
	$('#nonverblaster').css('opacity', '0');
	$('#nonverblaster').css('filter', 'alpha(opacity = 0);');
	$('.imageSlider li').eq(2).find('img').css('opacity', '0.6');
	$('.imageSlider li').eq(2).find('img').css('filter', 'alpha(opacity = 60);');
	//listElements.find('img').css('opacity', '0.6');
	//listElements.find('img').css('filter', 'alpha(opacity = 60);');

	if(animated == true) {
		animationSpeed = "slow";
	} else {
		animationSpeed = 300;
	}
	
	if(direction != "right") {
		$('.imageSlider li:first').clone().css("margin-left","0px").appendTo( $('.imageSlider ul') );
		$('.imageSlider li:first').stop(true,true).animate({"margin-left": "-=277px"}, animationSpeed, function() {
			$('.imageSlider li:first').remove();

			videos.push( videos[0] );
			videos.shift();

			if( !$('.imageSlider').hasClass('imageSlider-novideo') ) {
				if( videos[2] !== "" )  {
					sendToNonverBlaster("switch:" + videos[2]);
					sendToNonverBlaster('play');
				} else {
					sendToNonverBlaster('pause');
				}
			}
			showThirdElement(animated);			
		});
	} else {
		$('.imageSlider li:last').clone().css("margin-left","-277px").prependTo( $('.imageSlider ul') );
		$('.imageSlider li:first').stop(true,true).animate({"margin-left": "0px"}, animationSpeed, function() {
			$('.imageSlider li:last').remove();

			videos.unshift(videos[videos.length-1]);
			videos.pop();

			if( !$('.imageSlider').hasClass('imageSlider-novideo') ) {
				if( videos[2] !== "" )  {
					sendToNonverBlaster("switch:" + videos[2]);
					sendToNonverBlaster('play');
				} else {
					sendToNonverBlaster('pause');
				}				
			}
			showThirdElement(animated);
		});
	}
}

function showSubText() {
	listElements = $('.imageSlider li');

	$(".imageSlider ul li").live( 'mouseenter', function () {
		if( $(this).get(0) !== $('.imageSlider li:first').get(0) && $(this).get(0) !== listElements.eq(4).get(0) ) {
			if( $(this).get(0) !== listElements.eq(2).get(0)  ) {
				hideThirdElement(true);
			}

			var video = playerLeft = false;

			if( $(this).get(0) === listElements.eq(1).get(0) ) {
				video = videos[1];
				playerLeft = '66px';
				currentVid = 1;
			}
			if( $(this).get(0) === listElements.eq(3).get(0) ) {
				video = videos[3];
				playerLeft = '618px';
				currentVid = 3;
			}
			if( $(this).get(0) === listElements.eq(2).get(0) ) {
				video = videos[2];
				currentVid = 2;
			}

			$('#nonverblaster').css('opacity', '0');
			$('#nonverblaster').css('filter', 'alpha(opacity = 0);');
			$(this).find('img').css('opacity', '1');
			$(this).find('img').css('filter', 'alpha(opacity = 100);');

			if( video !== false && video !== "" && !$('.imageSlider').hasClass('imageSlider-novideo') )  {
				if( currentVid !== 2 ) {
					sendToNonverBlaster("switch:" + video);
				} else {
					sendToNonverBlaster('play');	
					$('#nonverblaster').css('opacity', '1');
					$('#nonverblaster').css('filter', 'alpha(opacity = 100);');
					$(this).find('img').css('opacity', '0');
					$(this).find('img').css('filter', 'alpha(opacity = 0);');					
				}
				
				// nonverblaster is shown at computeStart()
			} else {
				if( !$('.imageSlider').hasClass('imageSlider-novideo') ) {
					sendToNonverBlaster('pause');
				}
			}

			if( playerLeft !== false ) {
				$('#nonverblaster').css('left', playerLeft);
			}

			if( $(this).get(0) !== listElements.eq(2).get(0) ) {
				$(this).find('.subText').stop(true, true).slideDown("slow");
			}
		}
	}).live('mouseleave', function () {

		currentVid = 2;


		if( $(this).get(0) !== listElements.eq(2).get(0) ) {
			$(this).find('.subText').stop(true, true).slideUp("slow");
		}

		if( $(this).get(0) === listElements.eq(1).get(0) || $(this).get(0) === listElements.eq(3).get(0) ) {
			if( videos[2] !== "" && !$('.imageSlider').hasClass('imageSlider-novideo') )  {
				sendToNonverBlaster("switch:" + videos[2]);
			} else {
				if( !$('.imageSlider').hasClass('imageSlider-novideo') ) {
					sendToNonverBlaster('pause');
				}
			}
			$(this).find('img').css('opacity', '0.6');
			$(this).find('img').css('filter', 'alpha(opacity = 60);');

			showThirdElement(true);
		}

		//$(this).find('img').css('opacity', '0.6');
		//$(this).find('img').css('filter', 'alpha(opacity = 60);');
	});
}

function setLinks() {
	$(".imageSlider ul li .eventOverlay").live('click', function() {
		window.location= $(this).parent().find('a').attr('href');
	});
}

$(window).load( function() {
	if ( $('.imageSlide-wrapper').size() > 0  
		&& document.cookie.length>0 && getCookie('sound') == 'off' ) {
		sendToNonverBlaster('toggleSound');	
	}
});

$(document).ready(function() {

	if( $('.imageSlide-wrapper').size() > 0 ) {
		// no-flash version also for opera due to some bugs
		if( swfobject.hasFlashPlayerVersion( flashVersion ) == false || $.browser.opera == true ) {
			$('.imageSlider').addClass('imageSlider-novideo');
		} else {
			initFlashPlayer();
		}

		initSlider();
		showSubText();
		setLinks();

		mouseOverFlag = false;
		$('.imageSlider').mouseenter(function() {
			mouseOverFlag = true;
		}).mouseleave(function(){
			mouseOverFlag = false;
		});

		intv = setInterval(function() {
			if( mouseOverFlag == false && ( videos[2] == "" || $('.imageSlider').hasClass('imageSlider-novideo') ) ) {
				hideThirdElement(true);
				imageSlide('left', true);
			}
		}, 4500);

		$('#toggleSliderSound').click(function() {
			sendToNonverBlaster('toggleSound');
			$(this).toggleClass('soundOn');
			$(this).toggleClass('soundOff');
			
			if (document.cookie.length>0) {
				expires = new Date();
				expires = new Date(expires.getTime() +1000*60*60*24*365);
				
				if( $(this).hasClass('soundOff') ) {
					cookieVal = 'off';
				} else {
					cookieVal = 'on';
				}
				
				document.cookie = 'sound='+cookieVal+'; expires=Thu, 01-Jan-70 00:00:01 GMT;'; 
				document.cookie = 'sound='+cookieVal+'; expires='+expires.toGMTString()+';'; 
			}
			
			return false;
		});

		$('#slideNext').click(function() {
			if( !$('.imageSlider .subText').is(':animated') &&
					!$('.imageSlider li').filter(':animated').is(':animated') ) {
				intv = resetInterval();
				hideThirdElement(false);
				imageSlide('left', false);
			}
			return false;
		});

		$('#slidePrev').click(function() {
			if( !$('.imageSlider .subText').is(':animated') &&
					!$('.imageSlider li').filter(':animated').is(':animated') ) {
				intv = resetInterval();
				hideThirdElement(false);
				imageSlide('right', false);
			}
			return false;
		});
	}

	if( $('.references-overview').size() > 0 ) {
		referencesOverviewInit();
	}
	if( $('.references-detail').size() > 0 ) {
		referencesDetailInit();
	}
	if( $('.person-overview').size() > 0 ) {
		personOverviewInit();
	}
	if( $(' .article-page #map_canvas').size() > 0 ) {
		initGoogleMaps();
	}
});

function initGoogleMaps() {

	$('#map_alt').hide();

	var latCenter = new google.maps.LatLng(51.197246,10.213623);
	var latMaCenter = new google.maps.LatLng(49.508079,8.473005);
	var latMoCenter = new google.maps.LatLng(50.394359,7.614841);
	var latWuCenter = new google.maps.LatLng(49.777639,9.934702);

	var latlng = new google.maps.LatLng(49.529144,8.484571);
	var latlng2 = new google.maps.LatLng(50.928167,6.94268);
	var latlng3 = new google.maps.LatLng(50.937508,6.938117);
	var latlng4 = new google.maps.LatLng(50.435786,7.812302);
	var latlng5 = new google.maps.LatLng(48.149547,11.555378);
	var latlng6 = new google.maps.LatLng(49.750014,9.942345);

	var image = globalAssetPath+'images/marker.png';
	var shadow = globalAssetPath+'images/markerShadow.png';

	var contentString	= '<div id="infoBoxMa" style="height: 150px;">'+$('#ContainerMa').html()+'</div>';

	var infowindow = new google.maps.InfoWindow({
		content: contentString,
		maxWidth: 800
	});

	var contentString2	= '<div id="infoBoxK1" style="height: 150px;">'+$('#ContainerK1').html()+'</div>';
	var contentString3	= '<div id="infoBoxK2" style="height: 150px;">'+$('#ContainerK2').html()+'</div>';
	var contentString4	= '<div id="infoBoxMo" style="height: 150px;">'+$('#ContainerMo').html()+'</div>';
	var contentString5	= '<div id="infoBoxMu" style="height: 150px;">'+$('#ContainerMu').html()+'</div>';
	var contentString6	= '<div id="infoBoxWu" style="height: 150px;">'+$('#ContainerWu').html()+'</div>';

	var myOptions = {
	zoom: 6,
	center: latCenter,
	mapTypeId: google.maps.MapTypeId.TERRAIN
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	var marker = new google.maps.Marker({
	position: latlng,
	map: map,
	title:"trio-group Mannheim",
	icon: image,
	shadow: shadow
	});

	var marker2 = new google.maps.Marker({
	position: latlng2,
	map: map,
	title:"Köln (trio-kölner, trio-b.com)",
	icon: image,
	shadow: shadow
	});

	var marker3 = new google.maps.Marker({
	position: latlng3,
	map: map,
	title:"Köln (BIRCH COVE)",
	icon: image,
	shadow: shadow
	});

	var marker4 = new google.maps.Marker({
	position: latlng4,
	map: map,
	title:"trio-group Montabaur",
	icon: image,
	shadow: shadow
	});

	var marker5 = new google.maps.Marker({
	position: latlng5,
	map: map,
	title:"trio-group Muenchen",
	icon: image,
	shadow: shadow
	});

	var marker6 = new google.maps.Marker({
	position: latlng6,
	map: map,
	title:"buena la vista",
	icon: image,
	shadow: shadow
	});

	google.maps.event.addListener(marker, 'click', function() {
		infowindow.setContent(contentString);
		infowindow.open(map,marker);
	});

	google.maps.event.addListener(marker5, 'click', function() {
		infowindow.setContent(contentString5);
		infowindow.open(map,marker5);
	});

	google.maps.event.addListener(marker2, 'click', function() {
		infowindow.setContent(contentString2);
		infowindow.open(map,marker2);
	});

	google.maps.event.addListener(marker3, 'click', function() {
		infowindow.setContent(contentString3);
		infowindow.open(map,marker3);
	});

	google.maps.event.addListener(marker4, 'click', function() {
		infowindow.setContent(contentString4);
		infowindow.open(map,marker4);
	});

	google.maps.event.addListener(marker6, 'click', function() {
		infowindow.setContent(contentString6);
		infowindow.open(map,marker6);
	});

	$('#gmLinkMa').click(function() {
		map.setZoom(12);
		map.setCenter(latMaCenter);
		infowindow.setContent(contentString+'');
		infowindow.open(map,marker);
		return false;
	});

	$('#gmLinkK1').click(function() {
		map.setZoom(13);
		map.setCenter(latlng2);
		infowindow.setContent(contentString2);
		infowindow.open(map,marker2);
		return false;
	});

	$('#gmLinkK2').click(function() {
		map.setZoom(13);
		map.setCenter(latlng3);
		infowindow.setContent(contentString3);
		infowindow.open(map,marker3);
		return false;
	});

	$('#gmLinkMo').click(function() {
		map.setZoom(11);
		map.setCenter(latMoCenter);
		infowindow.setContent(contentString4);
		infowindow.open(map,marker4);
		return false;
	});

	$('#gmLinkMu').click(function() {
		map.setZoom(13);
		map.setCenter(latlng5);
		infowindow.setContent(contentString5);
		infowindow.open(map,marker5);
		return false;
	});

	$('#gmLinkWu').click(function() {
		map.setZoom(13);
		map.setCenter(latWuCenter);
		infowindow.setContent(contentString6);
		infowindow.open(map,marker6);
		return false;
	});

}

function referencesDetailInit() {
	$('.references-detail .galleryThumbs').css('overflow','hidden');

	$('.references-detail video, .references-detail audio').mediaelementplayer({
		defaultVideoWidth: 580, // if the <video width> is not specified, this is the default
		defaultVideoHeight: 380, // if the <video height> is not specified, this is the default
		videoWidth: 580, // if set, overrides <video width>
		videoHeight: 380, // if set, overrides <video height>
		audioWidth: 580, // width of audio player
		audioHeight: 30, // height of audio player
		pluginWidth: 580, // overrides <video width>
		pluginHeight: 380, // overrides <video height>
		startVolume: 0.5, // initial volume when the player starts
		loop: false, // useful for <audio> player loops
		enableAutosize: true, // enables Flash and Silverlight to resize to content size
		features: ['playpause','progress','current','duration','tracks','volume'/*,'fullscreen'*/]
	});

	if( $('.references-detail div.galleryThumbSlider').find('.galleryThumbs a').size() > 3 ) {
		$('.references-detail .galleryThumbSlider .galleryNext').css('cursor','pointer').click(function() {
			if( !$(this).parents('div.galleryThumbSlider').hasClass('galleryThumbSlider-locked') ) {
				$(this).parents('div.galleryThumbSlider').addClass('galleryThumbSlider-locked');
				$('.references-detail .galleryThumbs a:first').clone(true).css("margin-left","0px").appendTo( $('.references-detail .galleryThumbs') );
				$('.references-detail .galleryThumbs a:first').animate({"margin-left": "-=180px"}, "slow", function() {
					$('.references-detail .galleryThumbs a:first').remove();
					$('div.galleryThumbSlider').removeClass('galleryThumbSlider-locked');
				});
			}
		});

		$('.references-detail .galleryThumbSlider .galleryPrev').css('cursor','pointer').click(function() {
			if( !$(this).parents('div.galleryThumbSlider').hasClass('galleryThumbSlider-locked') ) {
				$(this).parents('div.galleryThumbSlider').addClass('galleryThumbSlider-locked');
				$('.references-detail .galleryThumbs a:last').clone(true).css("margin-left","-180px").prependTo( $('.references-detail .galleryThumbs') );
				$('.references-detail .galleryThumbs a:first').animate({"margin-left": "0px"}, "slow", function() {
					$('.references-detail .galleryThumbs a:last').remove();
					$('div.galleryThumbSlider').removeClass('galleryThumbSlider-locked');
				});
			}
		});
	} else {
		$('.references-detail .galleryThumbSlider .galleryNext, .references-detail .galleryThumbSlider .galleryPrev').hide();
	}

	$('.references-detail .galleryThumbs a:first img').addClass('thumb-active');

	$('.references-detail .galleryThumbs a').click(function() {
		var id = $(this).attr('id').replace('galleryThumb', 'galleryPresenterItem');
		var idIE = $(this).attr('id').replace('galleryThumb', 'reference-video-fallback');
		$('#'+id).hide().css({ left: '4px' });
		$('.references-detail .galleryPresenter .galleryPresenterItem:visible').fadeOut();

		$('#'+id).fadeIn();

		$("div.galleryPresenterItem:visible").find(".mejs-pause").trigger('click');

		$(this).blur();

		$('.references-detail .galleryThumbs a img').removeClass('thumb-active');
		$(this).find('img').addClass('thumb-active');

		// if clicked element is the first or third, center it.
		if( $(this).prevAll('a').size() == 0 ) {
			$('.references-detail .galleryThumbSlider .galleryPrev').click();
		}
		if( $(this).prevAll('a').size() == 2 ) {
			$('.references-detail .galleryThumbSlider .galleryNext').click();
		}

		return false;
	});

	$('.references-detail .gallery').addClass('galleryDynamic');
	// move the elements out of viewport, but don't hide() them because hiding leads to erroneous
	// dimension calculation of mediaelement.js skin!
	$('.references-detail .galleryPresenter .galleryPresenterItem').not(':first').css({ left: '-10000px' });

	// overwrite backlink functionality
	$('.references-detail a.backlink').click(function() {
		window.back();
		return false;
	});
}

function referencesOverviewInit() {
	$('.references-overview .teaserText').hide();

	$('.references-overview .referenceTeaserBox').mouseenter(function() {
		$(this).find('.teaserText').stop(true, true).fadeIn("fast");
		$(this).find('.teaserText').addClass('teaserTextMouseOver');
	}).mouseleave(function(){
		$(this).find('.teaserText').stop(true, true).fadeOut("normal",function() {
			$(this).removeClass('teaserTextMouseOver');
		});
	});
}

function personOverviewInit() {
	$('.person-overview').addClass('person-overview-js');
	$('.person-overview .content .contentBox-twoThird .person-detail').not(':last').hide();

	$('.person-overview-js ul.person-anchor-index li a').bind('mouseenter click', function() {
		$(this).parents('ul.person-anchor-index').find('a').removeClass('active');
		$(this).addClass('active');

		var contentContainer = $('.person-overview .content .contentBox-twoThird');
		contentContainer.find('.person-detail').hide();
		contentContainer.find($(this).attr('href')).show();

		return false;
	});
}
