onYouTubePlayerReady = function (playerId) {
	 var videoOject = eval ('video_object'+playerId);
	 videoOject.ytplayer = document.getElementById('its_video_inner_'+playerId);
	 
	 videoOject.ytplayer.addEventListener('onStateChange', '(function(state) { var videoOject = video_object'+playerId+'; return videoOject.playerState2(state, "' + playerId + '"); })' );
	
	
}
function its_youtube_jq (contentid,title,text,videoid,Width,Height,onload,onunload) {
	var self2 = this;
	this.contentid = contentid;
    var params = { allowScriptAccess: "always" };
    var atts = { id: 'its_video_inner_'+contentid };
    var IE = document.all&&!window.opera;
    this.ytplayer =0;
    var _its_video = jQuery('<div id="its_video_'+contentid+'"></div>');
    var _its_video_inner = jQuery ('<div id="its_video_inner_'+contentid+'"></div>');
    var _its_video_title = jQuery ('<div id="its_video_title_'+contentid+'"></div>');
    var _its_video_close = jQuery ('<div id="its_video_close_'+contentid+'"></div>');    
    var _fade = jQuery ('<div id="fade'+contentid+'" style="opacity: 0.01;  height: 4361px; display: none;"></div>');
    var _close_pic1 = jQuery ('<img src="typo3conf/ext/its_youtube_v2/Resources/Public/Images/close.png" width="30" heigth="30" alt="close" />');
    var _close_pic2 = jQuery ('<img src="typo3conf/ext/its_youtube_v2/Resources/Public/Images/fileclose.png" width="20" heigth="20" alt="close" />' );
    var _close_pic = jQuery ('<div id="close_pic_"'+contentid+'" ></div>');
    this.running = false;
    this.ready = true;
    this.isiPad = navigator.userAgent.match(/iPad/i) != null;
	if ( ! this.isiPad) {
		this.isiPad = navigator.userAgent.match(/Win64; x64/i) != null;
	}
	if ( ! this.isiPad) {
		this.isiPad = navigator.userAgent.match(/obile/i) != null;
	}
	
    _its_video.addClass('its_video');
    _close_pic.addClass('closepic');
    _close_pic.append(_close_pic1);
    _its_video.append(_close_pic);    
    _close_pic.css ('cursor', 'pointer');
    _its_video_close.css ('cursor' , 'pointer');
    var _content = jQuery('#video_'+contentid);
   
    _content.css ('cursor',  'pointer');
    _close_pic.hide();
    _its_video_close.hide();
    _its_video.hide();
    _fade.addClass ( "black_overlay");
    jQuery("body").append(_fade );
    _fade.css('opacity', .8001);
	
	_its_video.append(_its_video_inner);
	_its_video_close.innerHTML=text[0];
	_its_video_title.addClass ('its_videotitle');
	_its_video_close.addClass ('its_videotitle_close');
	_close_pic2.addClass('its_videotitle_close');
	_close_pic.css ('cursor' , 'pointer');
	_its_video.append(_its_video_title);
	_its_video.append(_close_pic2);
	_its_video.append(_its_video_close);
	_its_video_close[0].innerHTML=text[0];
	_close_pic2.hide();
	jQuery('body').append(_its_video );
	
	jQuery(_content).bind('click',function(event) {
	    	self2.start2();
	});
	jQuery(_its_video_close).bind('click',function(event) {
    	self2.close();
    	});
	jQuery(_fade).bind('click',function(event) {
    	self2.close();
    	});
	jQuery(_close_pic2).bind('click',function(event) {
    	self2.close();
    	});
	jQuery(_close_pic).bind('click',function(event) {
    	self2.close();
    	});
	
 	this.getDocHeight = function(){
	    var height = Math.max(
	    	jQuery(document).height(),
	    	jQuery(window).height(),
	        /* For opera: */
	        document.documentElement.clientHeight
	        
	    );
	    var b = jQuery.browser;
	    //alert(jQuery.browser.firefox);
	    if (b.mozilla && b.version=='6.0') {
	    	height = window.scrollMaxY+window.outerHeight;
	    } 
	    return height;
	}
 	this.start = function () {
 		var a = jQuery('#its_video_inner_'+contentid)[0];
		_close_pic.show();
		if (typeof a=='undefined') {

			jQuery(_its_video_inner).insertAfter(_close_pic);

		}
		if (this.isiPad) {			
			jQuery('#its_video_inner_'+self2.contentid)[0].innerHTML
			='<iframe width="'+Width+'" height="'+Height+'" frameborder="0" src="http://www.youtube.com/embed/'+videoid+'?autoplay=1&border=0&hd=1" frameborder="0" allowfullscreen type="text/html" class="youtube-player"></iframe>';
			var a =jQuery('#its_video_inner_'+self2.contentid);
			//.html( 'hallo');
			
		} else {
			
		swfobject.embedSWF("http://www.youtube.com/v/"+  videoid + "?enablejsapi=1&hd=1&iv_load_policy=3&playerapiid="+contentid, 
				'its_video_inner_'+self2.contentid, Width,  Height, "8", null, null, params, atts);
		} 		
 	}
 	
 	this.playerState2 = function (state, playerId) {
 		
    	if (typeof this.ytplayer=='undefined') {
    		this.ytplayer = document.getElementById('its_video_inner_'+self2.contentid);
    	} 
        if (state==5) {
        	if (self2.running == false ) {        		
        		self2.running =true;
        		
            	self2.updateDetails(_its_video);
            	this.ytplayer.playVideo();
            	
        	}
        }
        if (state==0) {
        	self2.close();
        }
    }
 	
	this.start2 = function () {
		if (! this.ready) {
			return;
		}
		
		try
	  	  {
	  		eval(onload);
	  	

	  	  } 
	  	catch(e) 
	  	  {

	  	  }
	  	
		this.ready = false;
		_fade.css('opacity' , 0.8 );
		_fade.hide();
		
		_its_video_close.hide();
		_its_video_title.hide();	
		_its_video_close.hide();
		_close_pic2.hide();
		_its_video.css ('width','100px');
		_its_video.css ('height' ,'10px');
		if(IE) {
			_fade.css('filter' , "alpha(opacity=80)");
		}
		var bodyheight = this.getDocHeight();

		var windowsheight =0;
		if (window.innerHeight) {
			windowsheight = window.innerHeight;
		} else if (document.documentElement.clientHeight){
			
			windowsheight = document.documentElement.clientHeight;
		} else if (document.body.clientHeight) {
			windowsheight = document.body.clientHeight;
		}
		if (windowsheight>bodyheight) {
			bodyheight=windowsheight; 
		}

		_fade.height(bodyheight+'px');
		_its_video.css('zIndex' ,2202);
		_its_video.css ('position' ,'absolute');
		this.myPopupRelocate2(_its_video);
		jQuery(_its_video).fadeIn('slow', function() {
	        // Animation complete
	      });
		jQuery(_fade).fadeIn('slow', function() {
	        // Animation complete
	      });
		this.resizeVideoContainer(Width,Height,_its_video);
	}
		

	
	this.close = function () {
    	
    	if (this.isiPad == false) {
    		if (typeof this.ytplayer=='undefined') {
	    		this.ytplayer = document.getElementById('its_video_inner_'+self2.contentid);
	    	} 
	    	if (this.ytplayer) {
	    		this.ytplayer.stopVideo();
	    	}
	    	delete this.ytplayer;
	    	swfobject.removeSWF('its_video_inner_'+contentid);
    	}
    	jQuery(_its_video).fadeOut('slow', function() {
	        // Animation complete
	      });
		jQuery(_fade).fadeOut('slow', function() {
			self2.ready = true;
	      });
		try {
			_its_video_inner.remove();
		} catch (e) {}
    	if (this.running) {
    		try
    		{
    			this.running = false;
    			eval(onunload);
    		}
    		catch(e)
    		{
    		}
    	}
    	this.running = false;
//    	try{this.effect1.cancel(); }  catch (e) {}
//    	try{this.effect2.cancel(); }  catch (e) {}
    	_its_video.stop(true, true)
    	_fade.stop(true, true)
    	
    	_its_video.hide();
    	_close_pic.hide();
    }
	
	this.resizeVideoContainer = function (imgWidth,imgHeight,ele) {
		var widthCurrent  = ele.width();
		var heightCurrent = ele.height();
		var widthNew  = (imgWidth  + 0 * 2);
		var heightNew = (imgHeight + 0 * 2); //4 = Bordersize
		var xScale = (widthNew  / widthCurrent)  * 100;
		var yScale = (heightNew / heightCurrent) * 100;
		var wDiff = widthCurrent - widthNew;
		var hDiff = heightCurrent - heightNew;
		
		if (this.isiPad) {
			this.currentHeight-=23;
		}
		if (hDiff != 0) {
			jQuery(ele).animate({height: imgHeight,top:'+='+hDiff/2}, 1000);
		}
		if (wDiff != 0) {
			jQuery(ele).animate({width: imgWidth,left:'+='+wDiff/2}, 1000,
				function() {
			    self2.start();
			  });
		}
		
	}

	this.myPopupRelocate2 = function(ele) {
		 var scrolledX, scrolledY;
		 
		 if( self.pageYOffset ) {
		   scrolledX = self.pageXOffset;
		   scrolledY = self.pageYOffset;
		 } else if( document.documentElement && document.documentElement.scrollTop ) {
		   scrolledX = document.documentElement.scrollLeft;
		   scrolledY = document.documentElement.scrollTop;
		 } else if( document.body ) {
		   scrolledX = document.body.scrollLeft;
		   scrolledY = document.body.scrollTop;
		 }

		 var centerX, centerY;
		 if( self.innerHeight ) {
		   centerX = self.innerWidth;
		   centerY = self.innerHeight;
		 } else if( document.documentElement && document.documentElement.clientHeight ) {
		   centerX = document.documentElement.clientWidth;
		   centerY = document.documentElement.clientHeight;
		 } else if( document.body ) {
		   centerX = document.body.clientWidth;
		   centerY = document.body.clientHeight;
		 }
		 //var width = ele.getDimensions();
		 width = ele.width() ;
		 height =  ele.height() ;

		 var leftOffset = scrolledX + (centerX - width ) / 2;
		 var topOffset = scrolledY + (centerY - height) / 2;
		 
		 ele.css('top', topOffset + 'px');
		 ele.css('left', leftOffset + 'px');
		
		 
	}
	
	this.updateHeight = function(ele,height,target) {
		height++;
		if (height<target) {
			ele.delay(20).queue(function(){ 
				 ele.css({height:height});				 
				 jQuery(this).dequeue();
				 self2.updateHeight(ele,height,target);
				});
		} else {
			this.updateDetails2(ele)
		}
	}
	this.updateDetails2 = function(ele) {
		_its_video_title[0].innerHTML=title;
		_its_video_title.show();
		_its_video_close.show();
		
		_close_pic2.show();
	}
	this.updateDetails = function (ele) {
		
		var widthCurrent  = ele.width();
 		var heightCurrent = ele.height();
 		var heightNew = heightCurrent + 25; 		
 		var yScale = (heightNew / heightCurrent) * 100; 		
 		var hDiff = heightCurrent - heightNew; 	
 		var i=0;
 		this.updateHeight(ele,heightCurrent,heightNew);
 	
	}
	
}
