

function getYTVideoID(videoURL) {
   var videoId;
   var regex = /^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/;
   var res = regex.exec(videoURL);
   if(res) {
      videoId =res[5]
   } else {
      videoId = videoURL;
   }
   return videoId;
}


// Video-Boxen
var ytplayer;
function onYouTubePlayerReady(playerId) {
   ytplayer = document.getElementById(playerId);
}
var params = {
   allowScriptAccess: "always"
};



//$.noConflict();

$(document).ready(function() {


   // Video
   $(".infoBoxVideo").each(function() {
      // Video einbauen
      var elementId = $(this).parent('div').attr('id');
      var videoCanvasDomId = elementId + '-videoCanvas';
      var videoPlayerDomId = elementId + 'videoPlayer';
      var videoTitle = $(this).find('.videoTitle').html();
      var videoId = getYTVideoID($(this).find('.videoId').html());
      var videoUrl = 'http://www.youtube.com/v/'+videoId+'?enablejsapi=1&playerapiid='+videoPlayerDomId;
      var atts = {
         id: videoPlayerDomId
      };

      // Dialog einbauen
      var dlg = $(this).siblings(".videoCanvas").dialog({
         autoOpen: false,
         width: 500,
         height: 355,
         resizable: false,
         modal: true,
         title: videoTitle,
         open: function(event, ui) {
            swfobject.embedSWF(videoUrl, videoCanvasDomId, "500", "306", "8", null, null, params, atts);
         },
         close: function(event, ui) {
            if(ytplayer) {
               ytplayer.stopVideo();
            }
            $(this).empty();
         }
      });
      $(this).find('.videoTeaser').click(function() {
         dlg.dialog('open');
         return false;
      });

   });

});
