(function($) {
      
 
  // Define the hello button
  $.cleditor.buttons.youtube = {
    name: "youtube",
    image: "youtube.png",
    title: "Inserisci un video YouTube",
    command: "inserthtml",
    popupName: "youtube",
    popupClass: "cleditorPrompt",
    //Width: <input type='text' size='3' value='480' id='width' /><br /><br /> Height: <input type='text' size='3' id='height' value='385' />
    popupContent: "Copiare il codice iframe presente sul sito di youtube:<br><input type='text' size='100' id='ybid'><br /><br /><br /><input type='button' value='Inserisci' class='bottoneCLE'>",
    buttonClick: youtubeClick
  };
      
 
 
  // Handle the hello button click event
  function youtubeClick(e, data) {
      
 
    // Wire up the submit button click event
    $(data.popup).children(":button")
      .unbind("click")
      .bind("click", function(e) {
      
 
        // Get the editor
        var editor = data.editor;
      
 
        // Get the entered name
        var ybId = $(data.popup).find("#ybid").val();
        //var width = $(data.popup).find("#width").val();
        //var height = $(data.popup).find("#height").val();
        //alert(ybId);
 
        // Insert some html into the document
        //var html = '<object width="'+width+'" height="'+height+'"><param name="movie" value="'+ybId+'"></param><param name="allowFullScreen" value="true"></param><param name="autoplay" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+ybId+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" play="true" width="'+width+'" height="'+height+'"></embed></object>';        
        //var html = '<iframe class="youtube-player" frameborder="0" scrolling="no" type="text/html" width="'+width+'" height="'+height+'" src="'+ybId+'"></iframe>';        
        
        //var html = "<br/><iframe width=\"560\" height=\"315\" src=\"" + ybId + "\" frameborder=\"0\" allowfullscreen></iframe><br/>";
        var html = '<br/>{£ Inizio oggetto video youtube £}<br/>' + ybId + '<br/>{£ Fine oggetto video youtube £}<br/>';
        //alert(html);        
        
        if ((html.indexOf('<iframe') != -1) && (html.indexOf('</iframe>') != -1))
        {
//            html = html.replace('<iframe', '££lt;iframe');
//            html = html.replace('></iframe>', '££gt;££lt;/iframe££gt;');
//            html = html.replace('</iframe>', '££lt;/iframe££gt;');
            html = html.replace('<iframe', '§iframe');
            html = html.replace('</iframe>', '§/iframe>');            
            html = html.replace('" frameborder', '?rel=0" frameborder');            
//            html = "<br/>" + ybId + "<br/>";
//            alert(html);   
            editor.execCommand(data.command, html, null, data.button);
            // Check for firefox
//            if ($.browser.mozilla) {

//              // Recreate the iframe in a disabled state
//              editor.disabled = true;
//              editor.refresh();

//              // Wait for the YouTube iframe to finish loading
//              $(editor.$frame[0].contentWindow).load(function() {

//                // Enable the editor
//                editor.disable(false);

//              });

//            } 
        }
        else
        {
            $('body').append('<div id="youtube_message" style="position: fixed; top: 38%; left: 38%; width: 260px; height: 80px; border: 3px solid lightgray; background-color: #CCC;"><b>Attenzione!</b><br />Il codice inserito non č corretto <br/><br/><input type="button" class="bottoneCLE" onclick="$(\'#youtube_message\').remove();" value="Ok"></input></div>');
        }
        
 
        // Hide the popup and set focus back to the editor
        editor.hidePopups();
        editor.focus();
      
 
      });
      
 
  }
      
 
})(jQuery);

