/*******************************************************************************
 jquery.mb.components
 Copyright (c) 2001-2010. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
 email: info@pupunzi.com
 site: http://pupunzi.com
 Licences: MIT, GPL
 ******************************************************************************/

/*
 * jQuery.mb.components: mb.mediaEmbedder
 * version: 1.0- 23-gen-2010 - 55
 * © 2001 - 2010 Matteo Bicocchi (pupunzi), Open Lab
 *
 */

(function($){

  $.mb_videoEmbedder={
    name:"jquery.mb.videoEmbedder",
    version:1.0,
    author:"Matteo Bicocchi",
    defaults:{
      width:450,
      youtube:{
        showTitle:false
      }
    },
    regEx:/\[(.*?)\]/g,
    mb_setMovie:function(context,pat,string){
      function findMovieAndEmbed(node, pat,string) {
        var skip = 0;
        if(pat.indexOf("&AMP;")!=-1) pat=pat.replace(/&AMP;/g,"&");
        if (node.nodeType == 3) {
          var pos = node.data.toUpperCase().indexOf(pat);
          if (pos >= 0) {
            // teoros
            //var embed = $('<span id="test" style="display:none;"/>').addClass("mb_video");
            var embed = $('<span/>').addClass("mb_video");
            var middlebit = node.splitText(pos);
            middlebit.splitText(pat.length);
            embed.append(string);
            middlebit.parentNode.replaceChild(embed.get(0), middlebit);
            skip = 1;
          }
        }
        else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
          for (var i = 0; i < node.childNodes.length; ++i) {
            i += findMovieAndEmbed(node.childNodes[i], pat.toUpperCase(),string);
          }
        }
        return skip;
      }
      return context.each(function() {
        findMovieAndEmbed(this, pat.toUpperCase(), string);
      });
    },
    mb_embedMovies:function(opt) {
      var context=$(this);
      this.options = {};
      $.extend (this.options, $.mb_videoEmbedder.defaults, opt);

      var movies= $(this).html().match($.mb_videoEmbedder.regEx);

      if(!movies) return;
      $(movies).each(function(i){
        var pat=movies[i];
        var isYoutube= pat.indexOf("youtube=http://")!=-1;
        var isYoutubeChannel= pat.indexOf("youtube=http://www.youtube.com/user/")!=-1;
        var isVimeo= pat.indexOf("vimeo=http://")!=-1;
        var isUstream= pat.indexOf("ustream=http://")!=-1;
        var isLivestream= pat.indexOf("livestream=http://")!=-1;
        var stringToParse=pat.replace(/\[/g,"").replace(/\]/g,"");
        var showTitle= !$.mb_videoEmbedder.defaults.youtube.showTitle? 0:1;

        var vidId = isYoutubeChannel ? stringToParse.split("/")[stringToParse.split("/").length-1] :
          isYoutube?(stringToParse.match( /[\\?&]v=([^&#]*)/) )[1]:
            isVimeo? (stringToParse.match(/\d+/))[0]:
              isUstream?(stringToParse.match(/\d+/))[0]:
                isLivestream?(stringToParse.replace("livestream=http://www.livestream.com/","").toLowerCase()):
                  null;

        //var ratio=isYoutube?(showTitle==1?80.5:68) : (isVimeo ?  57.5 : 65);
        var ratio = 65;

        var width=$.mb_videoEmbedder.defaults.width;
        var height= Math.ceil((width*ratio)/100);

        var path= isYoutube?"http://www.youtube.com/v/":
          isVimeo?"http://vimeo.com/moogaloop.swf?clip_id=":
            isUstream?"http://www.ustream.tv/flash/video/":
              isLivestream?"http://cdn.livestream.com/grid/LSPlayer.swf?channel=":
                null;

        var param=isYoutube?"&fs=1&rel=0&hd=1&showsearch=0&showinfo="+showTitle :
          isVimeo?"&amp;server=vimeo.com&amp;show_title="+showTitle+"&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1":
            isUstream?"?disabledComment=true&amp;autoplay=false":
              isLivestream?"&amp;color=0xe8e8e8&amp;autoPlay=false&amp;mute=false":
                "";

        var embedstring= '<object width="'+width+'" height="'+height+'" style="position:relative;">' +
          '<param name="movie" value="'+path+vidId+param+'">' +
          '<param name="allowFullScreen" value="true">' +
          '<param name="wmode" value="transparent">' +
          '<param name="allowscriptaccess" value="always">' +
          '<param name="flashvars" value="autoplay=false">' +
          '<embed src="'+path+vidId+param+'"' +
          ' type="application/x-shockwave-flash" ' +
          'wmode="transparent" allowscriptaccess="always" ' +
          'flashvars="autoplay=false"' +
          'allowfullscreen="true" width="'+width+'" height="'+height+'">' +
          '</embed>' +
          '</object>';

        if(isYoutube || isVimeo || isUstream || isLivestream)
          $.mb_videoEmbedder.mb_setMovie(context,pat,embedstring);
      });
    }
  };

  $.mb_audioEmbedder={
    name:"jquery.mb.videoEmbedder",
    version:1.0,
    author:"Matteo Bicocchi",
    playerPath:"media/player.swf",
    defaults:{
      width:300
    },
    regEx:/\[audio=(.*?)\]/g,
    mb_setAudio:function(context,pat,string){
      function findAudioAndEmbed(node, pat,string) {
        var skip = 0;
        if(pat.indexOf("&AMP;")!=-1) pat=pat.replace(/&AMP;/g,"&");
        if (node.nodeType == 3) {
          var pos = node.data.toUpperCase().indexOf(pat);
          if (pos >= 0) {

            var embed = $('<span/>').addClass("mb_audio");
            var middlebit = node.splitText(pos);
            middlebit.splitText(pat.length);
            embed.append(string);
            middlebit.parentNode.replaceChild(embed.get(0), middlebit);
            skip = 1;
          }
        }
        else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
          for (var i = 0; i < node.childNodes.length; ++i) {
            i += findAudioAndEmbed(node.childNodes[i], pat.toUpperCase(),string);
          }
        }
        return skip;
      }
      return context.each(function() {
        findAudioAndEmbed(this, pat.toUpperCase(), string);
      });
    },
    mb_embedAudio:function(opt){
      var context=$(this);
      this.options = {};
      $.extend (this.options, $.mb_audioEmbedder.defaults, opt);

      var audiofiles= $(this).html().match($.mb_audioEmbedder.regEx);
      if(!audiofiles) return;
      $(audiofiles).each(function(i){
        var pat=audiofiles[i];
        var isAudio= pat.indexOf("audio=")!=-1;
        var params=pat.replace(/\[audio=/g,"").replace(/\]/g,"");
        params = params.beginsWith('http://') || params.beginsWith('https://') || params.beginsWith('/') ? params : '/' +params;

        var width=$.mb_audioEmbedder.defaults.width;
        /*
         var embedstring= '<object codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+width+'" height="24" type="application/x-shockwave-flash"' +
         'style="outline: medium 2px; visibility: visible;" ' +
         'data="'+$.mb_audioEmbedder.playerPath+'" type="application/x-oleobject">' +
         '<param name="Movie" value="'+$.mb_audioEmbedder.playerPath+'">'+
         //                         '<param name="src" value="'+$.mb_audioEmbedder.playerPath+'" >'+
         '<param name="menu" value="false">'+
         '<param name="flashvars" value="soundFile='+params+'">'+
         '<param name="wmode" value="transparent">' +
         '<param name="bgcolor" value="#000">' +
         '<param name="allowscriptaccess" value="always">' +
         '<embed src="'+$.mb_audioEmbedder.playerPath+'" type="application/x-shockwave-flash" ' +
         'wmode="transparent" allowscriptaccess="always" ' +
         'width="'+width+'" height="24"' +
         'flashvars="soundFile='+params+'">' +
         '</embed>' +
         '</object>';
         */
        var fileName = params.split("/")[params.split("/").length-1];
        if(fileName.split(".").length>2)
          fileName = fileName.split(".")[1];
        var embedstring = "<a href='"+params+"' class='mb_mini_audio_player {autoPlay:false, width:120}'>"+fileName+"</a>";

        if(isAudio)
          $.mb_audioEmbedder.mb_setAudio(context,pat,embedstring);
      });
      
    }
  };

  $.fn.mb_embedMovies=$.mb_videoEmbedder.mb_embedMovies;
  $.fn.mb_embedAudio=$.mb_audioEmbedder.mb_embedAudio;

})(jQuery);
