$(document).ready(function(){
	//CSS properties only for active javascript	
	$("body").addClass("javascriptTrue");	
	
	//Höhe des Objects neu setzen zur Vermeidung der schwarzen Ränder
	$(".podcast .player object").attr("height","228");
	
	$(".noJS").removeClass("noJS");
	
	//Browserdetection
	var IE = (navigator.appVersion.indexOf("MSIE")==-1) ? false : true;
	var IE6 = (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
	var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	var IE8 = (navigator.appVersion.indexOf("MSIE 8.")==-1) ? false : true;
	
	/* Fancybox */
	if($("a.magnify").length > 0) {		
		var prevLink = "<a class=\"prevLink\" onclick=\"$.fancybox.prev();\">&nbsp;< zur&#x00FC;ck</a>";
        var nextLink = "<a class=\"nextLink\" onclick=\"$.fancybox.next();\">&nbsp;vor ></a>";
		var closeLink = "<a class=\"closeLink\" onclick=\"$.fancybox.close();\">&nbsp;<strong>Fenster schlie&#x00DF;en</strong></a>";
  	   
		/* Speziell f?r externe Videos mit Link */		
		$("a.externVideoLink").fancybox( {
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'outside',
				'overlayOpacity'	: 0.6,
	    		'overlayColor'		: '#000',
				'showCloseButton'	: false,
				'showNavArrows'		: false,							
				'padding'			: 31,
				'href'				: '#externVideoBig',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {													
					return '<div class="externalVidBu"><p>' + $(".bu .date").eq(currentIndex).text() + '</p><p>' + $(".bu strong").eq(currentIndex).text() + '</p><p>' + $(".bu .quelle").eq(currentIndex).text() + '</p>' + closeLink + ' </div>';														
				}					
			}			
		);	
		
		$("a.mainImageLink").fancybox( {
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'outside',
				'overlayOpacity'	: 0.6,
	    		'overlayColor'		: '#000',
				'showCloseButton'	: false,
				'showNavArrows'		: false,							
				'padding'			: 31,				
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {													
					return '<div class="externalVidBu"><p>' + $(".bu .desc").eq(currentIndex).text() + '<br /><span class="quelle" style="display:block;margin-top:0.5em">' + $(".bu .quelle").eq(currentIndex).text() + '</span></p>' + closeLink + ' </div>';														
				},
						
						'padding'			: 31,
						'cyclic'			: true,
						'type'				: 'image'
					});	
		

			function myFocus() {
			
				$("#fancybox-inner").prepend(nextLink).css("font-weight","bold");
				$("#fancybox-inner").prepend(prevLink).css("font-weight","bold");						
				$("#fancybox-title .nextLink").detach();
				$("#fancybox-title .prevLink").detach();
				$("#fancybox-inner").prepend($(".galleryCounter"));
				$("#fancybox-title-over .galleryCounter").detach();
				if(! IE) {
					$("#fancybox-inner").prepend($("span.counter"));
					$("#fancybox-title-over span.counter").detach();
				}				
								
				setTimeout(function() {
					$("#fancybox-close").focus();
				}, 100);				
			}
			
			$('.magnify').click(function(event) {				
				if($(this).hasClass("single")) {								
					$("#fancybox-inner").addClass("noNavigation");
				} else {					
					$("#fancybox-inner").removeClass("noNavigation");				
				}
				
				if($(this).hasClass("gallery")) {								
					var counter = "counter galleryCounter";
				} else {					
					var counter = "nocounter";				
				}
				
				if($(this).hasClass("internVideoLink")) {													
					var noTitle = 'class="noTitle"';
					var isInternVideoLink = true;					
				} else {
					var noTitle = ' ';
					var isInternVideoLink = false;
				}				
																
				var hrf = $(this).attr("href");
				var thisClick = $(this);
				
				if (thisClick.attr("rel") != "inline") {					
					$('<div class="singleimgGallery"></div>').insertAfter($(this).parents('.singleimg'));
				}
				
				if (!$(this).hasClass("mainImageLink")) {					
					$('.singleimgGallery').load(hrf, function(){
						var hasVidCaption = $(".videoCaption").length > 0;						
						var hasVidDate = $(".videoDate").length > 0;
						if (hasVidCaption) {
							var vidCaption = $('.videoCaption').text();
						}
						else {
							var vidCaption = "";
						}
						if (hasVidDate) {
							var vidDate = $('.videoDate').text();							
						}
						else {
							var vidDate = "";
						}						
					
						$("li a[rel=example_group]").fancybox({
							'transitionIn': 'none',
							'transitionOut': 'none',
							'titlePosition': 'outside',
							'overlayOpacity': 0.6,
							'overlayColor': '#000',
							'showCloseButton': false,
							'showNavArrows': false,
							'onClosed': function(){
								$(".singleimgGallery").detach()
							},
							'titleFormat': function(title, currentArray, currentIndex, currentOpts){
								if(isInternVideoLink) {
									return prevLink + '<p class="videoDate">' + vidDate + '</p><p class="videoCaption">' + vidCaption + '</p><p>' + closeLink + '</p><p id="fancybox-title-over" ' + noTitle + '><span class="' + counter + '">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '</span>' + $('.caption').eq(currentIndex).text() : '') + '<br /><span class="quelle">' + $(".imgSource").eq(currentIndex).text() + '</span></p>' + nextLink;
								} else {
									return prevLink + '<p id="fancybox-title-over" ' + noTitle + '><span class="' + counter + '">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '</span>' + $('.caption').eq(currentIndex).text() : '') + '<br /><span class="quelle">' + $(".imgSource").eq(currentIndex).text() + '</span></p>' + nextLink + '<p class="videoDate">' + vidDate + '</p><p class="videoCaption">' + vidCaption + '</p><p>' + closeLink + '</p>';
								}								
							},
							'onComplete': myFocus,
							'padding': 31,
							'cyclic': true,
							'type': thisClick.attr("rel"),
							'swf'   : {'allowfullscreen':'true' }


						}).css("display", "none");
						
						$("li a[rel=example_group]:first").trigger('click');
						$("ul.noscriptGallery").hide();
						$(".noscriptGalleryWrapper h5").hide();
						
					});
				}		
				event.preventDefault();				
			});
  }
	
	/* Karussell-Slider */
	$(".carousel").dualSlider({
		auto:true,
		autoDelay: 7500,
		easingCarousel: "swing",
		easingDetails: "swing",
		durationCarousel: 1300,
		durationDetails: 700
	});		
	

	$('div[id^=ytapiplayer]').each(function(){
		var $this = $(this);			
		var url = $this.attr('url');		
		var id = $this.attr('id');	
	
		var params = { 
			allowScriptAccess: "always", 
			allowfullscreen: true,			
			wmode: "transparent" 
		};
		
		swfobject.embedSWF(url + '&playerapiid=' + id, id, "560", "315", "8", null, null, params, { id: id });
		
	});	
	
	/* Interaktive Karte */
	$(".map-control .up").hide();
	$(".map-control .down").hide();
	$(".map-control .left").hide();
	$(".map-control .right").hide();
    $("#viewport").mapbox({layerSplit:1}); 
    
    $(".pin").hover(function () {
    	$(this).addClass("active1");
	  }, 
	  function () {
	    $(this).removeClass("active1");
	});
	
	$(".pin").click(function(){
		$(this).addClass("active2");
	});
	
	$(".pin2").hover(function () {
    	$(this).addClass("active1");
	  }, 
	  function () {
	    $(this).removeClass("active1");
	});
	
	$(".pin2").click(function(){
		$(this).addClass("active2");
	});	
	
	
	$(".pin").qtip({
	   suppress: false,
	   content: {
	      text: function(api) {	         
	         return $(this).next(".tooltipContent").clone(true);
	      }
	   },
   	   show: {
      	  event: 'mousedown',
      	  solo: true
   	   },
   	   hide: {
	      event: 'click',	      
	      target: $('span.closeTooltip')
	   },
	   events: {
	      hide: function(event, api) {
	         $(".pin").removeClass("active2");
	      }
	   },
	   overwrite: false
	});
	
	$(".pin2").qtip({
	   suppress: false,
	   content: {
	      text: function(api) {	    
	      	 var title = $(this).attr("title");	         		         		
	         return $(".pin[title = '" + title + "']").next(".tooltipContent").clone(true);			
	      }
	   },
   	   show: {
      	  event: 'mousedown',
      	  solo: true
   	   },
   	   hide: {
	      event: 'click',	      
	      target: $('span.closeTooltip')
	   },
	   events: {
	      hide: function(event, api) {
	         $(".pin2").removeClass("active2");
	      }
	   },
	   overwrite: false
	});
	           
        jQuery(".map-control a").click(function() {        	
            var viewport = $("#viewport"); 
            //this.className is same as method to be called 
            if(this.className == "zoom" || this.className == "back") { 
                viewport.mapbox(this.className, 1);//step twice 
            } 
            else { 
                viewport.mapbox(this.className); 
            } 
            
            if(this.className == "zoom") {
            	$(".map-control .up").show();
            	$(".map-control .down").show();
            	$(".map-control .left").show();
            	$(".map-control .right").show();
            	$("#viewport").css("cursor","move");
            }
            if(this.className == "back") {
            	$(".map-control .up").hide();
				$(".map-control .down").hide();
				$(".map-control .left").hide();
				$(".map-control .right").hide();
				$("#viewport").css("cursor","default");
            }            
            return false; 
        }); 

});

function onYouTubePlayerReady(playerId) {
	$(".stageWrapper .item object").attr("width","560").attr("height","312");
	$.each(document.getElementsByTagName("object"),function(index) {
		document.getElementsByTagName("object")[index].addEventListener("onStateChange", "onytplayerStateChange");
				
	});	
	
}



function onytplayerStateChange(newState) {
	if (newState == 1 || newState == 3) {
		stopScroll();					
	}	
		
	var url;
	
	//if ended cued for restart
	if ( newState == "0") {
		$('[id^=ytapiplayer]').each(function(){
			if ( this.getCurrentTime() > 1 ) {
				if(this.data != "") {
					url = this.data;					
				} else {
					url = this.movie;					
				}
				this.cueVideoByUrl(url);
			}
		});
	
	} 	

	$("#numbers a").empty();
	
	$("#numbers a").click(function() {			
		if(newState == 1 || newState == 3) {
			pauseMovie(newState);
		} 				
	});	
	
	$(".carousel .panel .play").click(function() {			
		if(newState == 1 || newState == 3) {
			pauseMovie(newState);			
		} 		
	});
		
}

function stopScroll() {
	$("body").stopTime("autoScroll");	
	$(".carousel .panel .pause").hide();
	$(".carousel .panel .play").show();
}

function pauseMovie(newState) {	
	$.each(document.getElementsByTagName("object"),function(index) {
		document.getElementsByTagName("object")[index].pauseVideo();		
	});	
}


