/*
 * Umsetzung: WDR mediagroup digital GmbH, Koeln
 * Ralf Cremer <ralf.cremer@wdr-mediagroup.com>
 * (c) 2011
 */

/* globale Flashplayer Hilfsfunktionen */
function getScriptState() { return true; }

function getReferer() { return escape(location.href); }


var globalObj = {
	userAgent: navigator.userAgent.toLowerCase(),
	/*
	 * Supported UA Opera >= 9.0 (Opera 10 ~ 9.80) Gecko >= 1.8 (1.8 FF >= 1.5 /
	 * 1.9 FF >= 3.0) WebKitVersion >= 530 ( ~ Safari >= 3.0, Chrome >= 2.0)
	 */
  supportedUA: { opera: 9.80, gecko: 1.9, webkit: 530 },	
	checkIEVersion: function() {
  	var tmpBool = true;
  	/* check IE < 8 with Conditional Comments */
    /* @cc_on @ */
    /*
	 * @ @if(@_jscript_version < 5.8) tmpBool = false; @elif(@_jscript_version >
	 * 5.7) // check IE 8 / 9 Compatibility Mode tmpBool =
	 * (document.documentMode && document.documentMode >= 8); @else //check IE 6
	 * on XP SP 3 tmpBool = (navigator.userAgent.toLowerCase().indexOf('msie 6') ==
	 * -1) @end @
	 */

    return tmpBool;
  },
  checkWebKitVersion: function(pVersion) {
		var tmpWebKitVersion = NaN;		
		var tmpVer;
		if(this.userAgent.indexOf('version/') != -1) {
			 /* Mobile Safari Version 3.0 WebKit 420 !!! */ 
		   tmpVer = parseFloat(this.userAgent.substr(this.userAgent.indexOf('version\/') + 8,3));
		   tmpWebKitVersion = (!isNaN(tmpVer) && tmpVer >= 3.0)?(pVersion + 1):NaN;
	  }
	  else if(this.userAgent.indexOf('chrome/') != -1) {
	  	 tmpVer = parseFloat(this.userAgent.substr(this.userAgent.indexOf('chrome\/') + 7,3));
		   tmpWebKitVersion = (!isNaN(tmpVer) && tmpVer >= 2.0)?(pVersion + 1):NaN;
	  }
	  else {
		   tmpWebKitVersion = parseFloat(this.userAgent.substr(this.userAgent.indexOf('safari\/') + 7,5));		   
    }
    
    // if(this.userAgent.indexOf('iphone') != -1 && iPhone) { pVersion -= 1; }
    
    return (!isNaN(tmpWebKitVersion) && tmpWebKitVersion >= pVersion);
  },
	checkGeckoVersion: function(pVersion) {
		var tmpGeckoVersion = parseFloat(this.userAgent.substr(this.userAgent.indexOf('rv:') + 3,3));
    return (isNaN(tmpGeckoVersion) || tmpGeckoVersion >= pVersion);
  },
	checkOperaVersion: function(pVersion) {
		var tmpOperaVersion = parseFloat(this.userAgent.substr(this.userAgent.indexOf((this.userAgent.indexOf('opera\/') != -1)?'opera\/':'opera ') + 6,5));
    // tmpOperaVersion = (this.userAgent.indexOf('opera mobi') != -1 ||
	// this.userAgent.indexOf('opera mini') != -1)?(pVersion -
	// 1):tmpOperaVersion;
    return (isNaN(tmpOperaVersion) || tmpOperaVersion >= pVersion);
  },
  
  checkSupportedUA: function() {
		var tmpBool = true;		
		/* check IE >= 8 */
		tmpBool = (/* @cc_on ! @ */false)?this.checkIEVersion():tmpBool;
		
    /* check Opera >= 10 */
    tmpBool = (this.userAgent.indexOf('opera') != -1)?this.checkOperaVersion(this.supportedUA.opera):tmpBool;

    /* check Gecko >= 1.9 */
    tmpBool = (this.userAgent.indexOf('gecko') != -1)?this.checkGeckoVersion(this.supportedUA.gecko):tmpBool;
    
    /* check Safari >= 3.0 */
    tmpBool = (this.userAgent.indexOf('safari') != -1)?this.checkWebKitVersion(this.supportedUA.webkit):tmpBool;
    
    /* check Playstation 3 'n disable it */
    /* Layout disabled through CSS */
    tmpBool = (this.userAgent.indexOf('playstation 3') != -1)?false:tmpBool;
    
    return tmpBool;
	}
};
/** ********************************************************************************************** */


jQuery('document').ready(function($) {
	
	

	if(globalObj.checkSupportedUA()) {
		
		/* Medieneinbindung */
	    /* Check Medialinks */
	    var $tmpVideo = $('.videoLink').length;
	    var $tmpAudio = $('.audioLink').length;    
	    if($tmpVideo > 0 || $tmpAudio > 0) {	    	
	    	var $activePlayer = $('<a></a>'); /*
												 * Temporary LinkObject for
												 * CSS-Reset
												 */    	
	
	    	// Audioplayer direkt in Seite
			var $openAudioPlayer = function(pNode) {
				$activePlayer.css({ visibility: 'visible' })
	  	    	.parent().removeClass('noClick') // .css({ zIndex: 'auto' })
	  	    	$('#audioPlayer').next().show();
	  	    	$('#videoPlayer').prev().show();				  	    	
	  	    	$('#videoPlayer').prev().prev(".wsIMG310b").show();
	  	    	$('#videoPlayer').prev().prev(".wsIMG512b").show();	  	    	
	  	    	$('#audioPlayer').remove();
	  	    	$('#videoPlayer').remove();
			  	$('#featureVideo').remove();	  	    	
	  	    	
	  	    	$activePlayer = $(pNode);
	  	      
	  	    	$(pNode) // .css({ cursor: 'wait' })
	  	    	.before('<span id="audioPlayer">Loading Player ...</span>')
	  	    	.parent().addClass('noClick') // .css({ zIndex: '110' });
 	   
	  	  
	  	      $('#audioPlayer')
	  	      .css({ opacity: '0' })
	  	      .load($(pNode).attr('href') + ' #insideContainer')
	  	      .ajaxStop(function(){
	  	        $(this).unbind('ajaxStop')
  	          	$activePlayer.css({ visibility: 'hidden'});
	  	      });	
				  	      
			};
			
			// Audioplayer in Layer
			var $openPopupAudioPlayer = function(pNode) {
	  	    	$activePlayer.css({ visibility: 'visible' })
	  	    	.parent().removeClass('noClick') // .css({ zIndex: 'auto' })
	  	    	$('#audioPlayer').next().show();
	  	    	$('#videoPlayer').prev().show();				  	    	
	  	    	$('#videoPlayer').prev().prev(".wsIMG310b").show();
	  	    	$('#videoPlayer').prev().prev(".wsIMG512b").show();	  	    		  	    	
	  	    	$('#audioPlayer').remove();
	  	    	$('#videoPlayer').remove();
			  	$('#featureVideo').remove();	  	    	
	  	    	
	  	    	$activePlayer = $(pNode);
	  	      
	  	    	$(pNode) // .css({ cursor: 'wait' })
	  	    	.before('<span id="audioPlayer">Loading Player ...</span>')
	  	    	.parent().addClass('noClick') // .css({ zIndex: '110' });
	  	      
	
		  	
		  	      $('#audioPlayer')
		  	      .css({ opacity: '0' })
		  	      .load($(pNode).attr('href') + ' #insideContainer')
		  	      .ajaxStop(function(){
		  	        $(this).unbind('ajaxStop')			  	    
		  	        .prepend($('<span class="audioLink"></span>').html($activePlayer.html()))
		  	        .append($('#audioPlayer')
		  	        .parent()
		  	        .find('.audioMeta').clone().show())
		  	        .prepend('<a href="#" id="audioClose"><span class="hidden">Audioplayer schlie&szlig;en: </span>X</a>')
		  	        .find('#audioClose')
		  	        .click(function(){		  	        	
		  	          $activePlayer.css({ visibility: 'visible' })
		  	          .parent().removeClass('noClick') // .css({ zIndex: 'auto' })
		  	          $('#audioPlayer').animate({ opacity: '0' }, 'slow', function() {
		  	        	  $('#audioPlayer').remove();
		  	          });	
		  	          $('#audioPlayer').next().show();
		  	          return false;	
		  	        })
		  	        
		  	        $('#audioPlayer').animate({ opacity: '1.0' }, 'slow', function() {
		  	          	$activePlayer.css({ visibility: 'hidden'});
		  	        });
		  	      });			  	        	
	  	      	      
			};			
			    	
			var $mediaEmbed = function() {   
				
			    /* Videoeinbindung per AJAX */
			  	if($tmpVideo > 0) { 
			  		$('.videoLink a:not(.caption .videoLink a)').click(function() {			  			
			  	    if(navigator.userAgent.toLowerCase().indexOf('(iphone\;') != -1
			  	    || navigator.userAgent.toLowerCase().indexOf('(ipod\;')   != -1
			  	    || navigator.userAgent.toLowerCase().indexOf('(ipad\;')   != -1
			  	    || !($hasRequiredFlashVersion)) {
			  	    	return true;
			  	    }
			  	    else {
			  	    	//Link in Linkliste
			  	    	if($(this).parent().parent().hasClass("linkList")) {			  	    		
			  	    		$activePlayer.css({ visibility: 'visible'}).parent() // .css({ zIndex: 'auto' });	
					  	    $('#audioPlayer').next().show();					  	    				  	    					  	    	
					  	    $('#videoPlayer').prev().show();				  	    	
					  	    $('#videoPlayer').prev().prev(".wsIMG310b").show();		
				  	    	$('#videoPlayer').prev().prev(".wsIMG512b").show();					  	    
					  	    $('#audioPlayer').remove();
						  	$('#videoPlayer').remove();
						  	$('#featureVideo').remove();						  	
					  	    
					  	    	
						  	$activePlayer = $(this);
						  	
					  	      
				  	    	$(this) // .css({ cursor: 'wait' })
				  	    	.before('<span id="videoPlayer">Loading Player ...</span>')
				  	    	.parent().addClass('noClick') // .css({ zIndex: '110' });
				  	      
				
					  	
					  	     $('#videoPlayer')
					  	      .css({ opacity: '0' })
					  	      .load($(this).attr('href') + ' #insideContainer')
					  	      .ajaxStop(function(){
					  	        $(this).unbind('ajaxStop')			  	    
					  	        .prepend($('<span class="videoLink"></span>').html($activePlayer.html()))
					  	        .append($('#videoPlayer')
					  	        .parent()
					  	        .find('.videoMeta').clone().show())
					  	        .prepend('<a href="#" id="videoClose"><span class="hidden">Videoplayer schlie&szlig;en: </span>X</a>')
					  	        .find('#videoClose')
					  	        .click(function(){		  	        	
					  	          $activePlayer.css({ visibility: 'visible' })
					  	          .parent().removeClass('noClick') // .css({ zIndex: 'auto' })
					  	          $('#videoPlayer').animate({ opacity: '0' }, 'slow', function() {
					  	        	  $('#videoPlayer').remove();
					  	          });	
					  	          $('#videoPlayer').next().show();
					  	          return false;	
					  	        })
					  	        
					  	        $('#videoPlayer').animate({ opacity: '1.0' }, 'slow', function() {
					  	          	$activePlayer.css({ visibility: 'hidden'});
					  	        });
					  	      });	
			  	    	}
			  	    	//Link in Teaser
			  	    	else {
			  	    		$activePlayer.css({ visibility: 'visible'}).parent() // .css({ zIndex: 'auto' });	
					  	    $('#audioPlayer').next().show();					  	    				  	    					  	    	
					  	    $('#videoPlayer').prev().show();				  	    	
					  	    $('#videoPlayer').prev().prev(".wsIMG310b").show();		
				  	    	$('#videoPlayer').prev().prev(".wsIMG512b").show();					  	    
					  	    $('#audioPlayer').remove();
						  	$('#videoPlayer').remove();
						  	$('#featureVideo').remove();						  	
					  	    
					  	    	
					  	    $activePlayer = $(this);
					  	    $(this).hide();
					  	    $(this).prev(".wsIMG310b").hide();
					  	    $(this).prev(".wsIMG512b").hide();					  	    
					  	      
					  	    
					  	    $(this).after('<span id="videoPlayer">Loading Player ...</span>');
					  	    $('#videoPlayer').load($(this).attr('href') + ' #insideContainer');
			  	    		$(this).hide();
			  	    	}
			  	    	return false;    
			  	    } 
			  	  }); // End of videoLink a
			  		
			  		// Videolink in Feature Teaser
			  		$('.caption .videoLink a').live('click', function() {			  		

			  			$activePlayer.css({ visibility: 'visible'}).parent() // .css({ zIndex: 'auto' });	
				  	    $('#audioPlayer').next().show();					  	    				  	    					  	    	
				  	    $('#videoPlayer').prev().show();				  	    	
				  	    $('#videoPlayer').prev().prev(".wsIMG310b").show();
				  	    $('#videoPlayer').prev().prev(".wsIMG512b").show();						  	    
				  	    $('#audioPlayer').remove();
					  	$('#videoPlayer').remove();	
					  	$('#featureVideo').remove();
				  	    	
					  	$activePlayer = $(this);
					  	
				  	      
			  	    	$('#featured') // .css({ cursor: 'wait' })
			  	    	.after('<div id="featureVideo"><span id="videoPlayer">Loading Player ...</span></div>')
			  	    	.parent().addClass('noClick') // .css({ zIndex: '110' });
			  	      
			
				  	
				  	     $('#videoPlayer')
				  	      .css({ opacity: '0' })
				  	      .load($(this).attr('href') + ' #insideContainer')
				  	      .ajaxStop(function(){
				  	        $(this).unbind('ajaxStop')			  	    
				  	        .prepend($('<span class="videoLink"></span>').html($activePlayer.html()))
				  	        .append($('#videoPlayer')
				  	        .parent()
				  	        .prev()
				  	        .find('.caption .videoMeta').clone().show())
				  	        .prepend('<a href="#" id="videoClose"><span class="hidden">Videoplayer schlie&szlig;en: </span>X</a>')
				  	        .find('#videoClose')
				  	        .click(function(){		  	        	
				  	          $activePlayer.css({ visibility: 'visible' })
				  	          .parent().removeClass('noClick') // .css({ zIndex: 'auto' })
				  	          $('#videoPlayer').animate({ opacity: '0' }, 'slow', function() {
				  	        	  $('#videoPlayer').remove();
				  	          });	
				  	          $('#videoPlayer').next().show();
				  	          return false;	
				  	        })		  	        
				  	         
				  	        
				  	        $('#videoPlayer').animate({ opacity: '1.0' }, 'slow', function() {
				  	          	$activePlayer.css({ visibility: 'hidden'});
				  	        });
				  	      });	
			  			return false;			  	    	
			  		});			  		
			  	} // End of Videolink
			  	
			  	/* Audioeinbindung per AJAX */
			  	if($tmpAudio > 0) { 
			  		$('.audioMeta').hide();
			  		
			  		$('.audioLink a').click(function() {
			  	    if($(this).hasClass('textPageLink')
			  	    || navigator.userAgent.toLowerCase().indexOf('(iphone\;') != -1
			  	    || navigator.userAgent.toLowerCase().indexOf('(ipod\;')   != -1
			  	    || navigator.userAgent.toLowerCase().indexOf('(ipad\;')   != -1
			  	    || !($hasRequiredFlashVersion)) {
			  	    	return true;
			  	    }
			  	    else {	
			  	    	//Link in Linkliste
			  	    	if($(this).parent().parent().hasClass("linkList")) {
			  	    		$openPopupAudioPlayer($(this));
			  	    		$(this).hide();
			  	    	}
			  	    	//Link in Teaser
			  	    	else {
			  	    		$openAudioPlayer($(this));
			  	    		$(this).hide();
			  	    	}
			  	    	return false;
			  	    } 
			  	  });  	  
			  	} // End of Audiolink
			};
  	
		$.requireScript('/codebase/js/plugins/detect_flash.min.js',function() { $mediaEmbed(); });    	
		
	    }
	}

}); // End jQuery('document').ready

