(function($){  

$.fn.youplay = function(options) {       
	
	 var defaults = {  
   	videoid: "",
   	xdim:"200px",
   	ydim:"200px"
   };  
            
   var options = $.extend(defaults, options);  
	 var script  = document.createElement('script');
	      script.type = 'text/javascript';
	      script.text  = 'function closeme() {$("div#videoplayer").css("display","none");}';        	      
	      document.documentElement.firstChild.appendChild(script);	
     		var html='';	      	      
	      html += '<div style="clear:both; width:'+options.xdim+';" align="right"><img src="../application/views/img/closebox.png" onclick="javascript:closeme();"/></div>';	     
		    html += '<div><object >';
		    html += '<param name="movie" value="http://www.youtube.com/v/'+options.videoid+'"></param>';
		    html += '<param name="autoplay" value="1">';
		    html += '<param name="wmode" value="transparent"></param>';
		    html += '<embed src="http://www.youtube.com/v/'+options.videoid+'&autoplay=1" type="application/x-shockwave-flash" wmode="transparent"  width="'+options.xdim+'" height="'+options.ydim+'" ></embed>';
		    html += '</object></div>';
		    		    
		    $('div#videoplayer').html(html);
		    $('div#videoplayer').show();
		    
};  

/**
* Generazione del Thumb
**/

$.fn.youthumb = function(options) {       
	 var defaults = {  
   	videoid: "",
   	imgwidth: "100",
   	xdim:"320px",
   	ydim:"265px"
   };  
   var options = $.extend(defaults, options);  
   return this.each(function() {     	  
      obj = $(this);  
      
      var myid=obj.attr('id');      
      
      if(options.videoid!="") {      	
	      var html='';
	      var html='<img width="'+options.imgwidth+'" alt="" onclick="javascript:$.fn.youplay({\'videoid\':\''+options.videoid+'\',\'xdim\':\''+options.xdim+'\',\'ydim\':\''+options.ydim+'\'})" id="youtubethumb" src="http://i.ytimg.com/vi/'+options.videoid+'/1.jpg"/>'	      
	      // la ficco dentro
				var body = obj.html(); 
				obj.html(html);
				
				// Devo appendere il MegaDiv,Ma solo uno											
				var div_adder=document.createElement('div');
				div_adder.id='videoplayer';												
				if(!document.getElementById('videoplayer')) {
					document.body.appendChild(div_adder);	
				}
				$('div#videoplayer').addClass('videoplayer');
				  //$('div#videoplayer').attr("style","position: absolute;top: 140px;left: 350px;");
				  
  
    

      }
   });  
};  


})(jQuery);  
