//-------------------------------------------------
//          youtube playlist jquery plugin
//          Created by dan@geckonm.com
//          www.geckonewmedia.com
//
//          v1.1 - updated to allow fullscreen 
//                - thanks Ashraf for the request
//-------------------------------------------------

(function ($) {
jQuery.fn.ytplaylist = function(options) {
 
  // default settings
  var options = jQuery.extend( {
    holderId: 'ytvideo',
     playerHeight: '300',
     playerWidth: '450',
     children_selector: 'li',
     addThumbs: false,
     thumbSize: 'small',
     showInline: false,
     autoPlay: true,
     showRelated: true,
     initial_start: true,
     replaceText: true,
     allowFullScreen: false
  },options);
 
  return this.each(function() {
                                   
             var selector = $(this);
          var children_selector = options.children_selector;
          var autoPlay = "";
          var showRelated = "&rel=0";
          var fullScreen = "";
          if(options.autoPlay) autoPlay = "&autoplay=1"; 
          if(options.showRelated) showRelated = "&rel=1"; 
          if(options.allowFullScreen) fullScreen = "&fs=1"; 
          
          //throw a youtube player in
          function play(id)
          {
           // var html  = '';
           //   
           // html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
           // html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
           // html += '<param name="wmode" value="transparent"> </param>';
           // if(options.allowFullScreen) { 
           //       html += '<param name="allowfullscreen" value="true"> </param>'; 
           // }
           // html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
           // if(options.allowFullScreen) { 
           //       html += ' allowfullscreen="true" '; 
           //   }
           // html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></embed>';
           // html += '</object>';
           //           
           // return html;
             
           var s1 = new SWFObject("http://www.youtube.com/v/"+id+autoPlay+showRelated+fullScreen,"prodotti",options.playerWidth,options.playerHeight,"7");
           s1.addVariable("width",options.playerWidth);
           s1.addVariable("height",options.playerHeight);
           if(options.allowFullScreen) { 
                s1.addParam("wmode","transparent");
           }
           s1.addParam("wmode","transparent");
           s1.addVariable("transition","fade");
           s1.write(options.holderId);
           
          };
          
          
          //grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
          function youtubeid(url) {
               var ytid = url.match("[\\?&]v=([^&#]*)");
               ytid = ytid[1];
               return ytid;
          };
          
          
          //load inital video
          if (options.initial_start){
              var firstVid = selector.children(children_selector+":first-child").addClass("currentvideo").children("a").attr("href");
              play(youtubeid(firstVid));
          }
          
          //load video on request
          selector.children(children_selector).children("a").click(function() {
               
               if(options.showInline) {
                    $(children_selector+".currentvideo").removeClass("currentvideo");
                    $(this).parent(children_selector).addClass("currentvideo")
                    play(youtubeid($(this).attr("href")));
               }
               else {
                    play(youtubeid($(this).attr("href")));
                    $(this).closest(selector).find(children_selector+".currentvideo").removeClass("currentvideo");
                    $(this).parent(children_selector).addClass("currentvideo");
               }
               
               return false;
          });
          
          //do we want thumns with that?
          if(options.addThumbs) {
               
               selector.children().each(function(i){
                                                         
                    var replacedText = $(this).text();
                    
                    if(options.thumbSize == 'small') {
                         var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/2.jpg";
                    }
                    else {
                         var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/0.jpg";
                    }
                    
                    if (options.replaceText)
                        $(this).children("a").empty().html("<img src='"+thumbUrl+"' alt='"+replacedText+"' />"+replacedText).attr("title", replacedText);
                    else
                        $(this).children("a").empty().html("<img src='"+thumbUrl+"' alt='"+replacedText+"' />").attr("title", replacedText);
                    
               });     
               
          }
               
          
   
  });
 
};
}(jQuery));
