﻿/**
 * jQuery YouTube On The Fly Plugin
 * This jQuery plugin created by Tohid Golkar http://tohidgolkar.com
 * @author Tohid Golkar http://tohidgolkar.com
 * @version 1.0
 * @date Jan 11, 2010
 * @category jQuery plugin
 * @copyright (c) 2010 byASP.net http://www.byasp.net
 * @demo http://floaty.byasp.net/ for more informations about this jQuery plugin
 */

(function(window, $, undefined) {
  
  $.fn.youfloat = function(opt) {
    var config = $.extend({
      width:	         340,
      height:	         245,
      thumbSize:		   'small',
      thumbVersion:    2,
      bgColor:			   '#000',
      bgOpacity:        0.6,
      borderColor:      '#fff',
      borderOpacity:    0.9,
      imgPlay:          'images/play.png',
      borderShow:	      false,
      showRelatedVideo: false
    }, opt);

    var youtube = {
      getId: function(url) {
        var result = undefined;
        if (url) {
          var match = url.match("[\\?&]v=([^&#]*)");
          result = (match !== undefined) ? match[1] : url;
        }
        return result;
      },
      getThumbnail: function(id, size, version) {
        if (id) {
          size = size || 'small';
          version =  (size === 'small') ? (version || '0') : '0';

          return ['http://img.youtube.com/vi/', id, '/', version, '.jpg'].join('')
        } else {
          return undefined;
        }
      }
    };

    $(this).each(function() {
      var $that = $(this),
      youtubeId = youtube.getId($that.attr('href'));

      $that.css({
        'background': ['url(', youtube.getThumbnail(youtubeId, config.thumbSize, config.thumbVersion) ,') no-repeat center center'].join(''),
        'line-height': '90px',
        'height': '90px',
        'width': '120px',
        'display': 'block'
      });
      //console.log(config.imgPlay);
      $(['<img src="', config.imgPlay, '" class="img-png" alt="Video abspielen" />'].join('')).appendTo($that);


      var size = $(this).attr('rel').split(',');
      var str = 'version=3&color1=0xb1b1b1&;color2=0xcfcfcf&fs=1&hd=1&feature=player_embedded';
      if (config.showRelatedVideo){
        str+='&rel=0'
      }
      if (config.borderShow){
        str+='&border=1'
      }

      $that.fancybox({
        cyclic: false,
        scrolling: false,
        type: 'swf',
        href: ['http://www.youtube.com/v/', youtubeId, '&hl=de_DE', str].join(''),
        width: parseInt(size[0]),
        height: parseInt(size[1]),
        swf: {
          wmode: 'transparent',
          width: parseInt(size[0]),
          height: parseInt(size[1]),
          allowFullScreen: 'true',
          allowscriptaccess: 'always'
        }
      });
    });
  };
})(window, jQuery);
