/** TO FLASH PLUGIN **/

$.fn.toflash = function(options) {
    var opts = $.extend({}, $.fn.toflash.defaults, options);
    return this.each(function() {
      $(this).hide();
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      var html  = $(this).getFlashHtml(o.htmlConfig);
	    var img   = $(this).attr('class');

      if(!o.asSwfObject) { 
          var flash = '<object type="application/x-shockwave-flash" menu="false" data="'+o.flashfile+'" FlashVars="c='+html+'&bg_img='+img+'"  width="'+o.flashWidth+'" height="'+o.flashHeight+'">';
          flash+='<param name="movie" value="'+o.flashfile+'" />';
          flash+='<param name="menu" value="false" />';
          flash+='<param name="wmode" value="transparent" />';
          flash+='<param name="FlashVars" value="c='+html+'&bg_img='+img+'" />';
          flash+='</object>';
          $(this).html(flash);
          $(this).show();
      } else {
          var FO = new SWFObject(o.flashfile, "flashobject", o.flashWidth, o.flashHeight, "8", "#ffffff","","high","","","");
          FO.addParam("menu", "false");
		  FO.addParam("wmode","transparent");
          FO.addParam("FlashVars", "c="+html+'&bg_img='+img);
          FO.write("maincontent");
          $(this).show();
      }
      
      

  });
};

/** TO FLASH DEFAULT CONFIG **/

$.fn.toflash.defaults = {
  asSwfObject : false,
  flashfile  : 'content.swf',
  flashWidth : 900,
  flashHeight: 400,
  htmlConfig : {
                  '<h1>([^<]*)</h1>'      : '<font size="16">$1</font><br>',
                  '<h2>([^<]*)</h2>'      : '<font size="13">$1</font><br>',
                  '<div>(.*)</div>'       : '$1<br>',
                  '<p>(.*)</p>'           : '$1<br>'
              }
    
};

/** CONVER HTML PLUGIN **/
$.fn.getFlashHtml = function(o) {
    var s = $(this).html();
    // rm - wrong attributes  (for ie)
    s = s.replace(new RegExp("\\s([^\\s]*)=([^\\s|>\"]*)(\\s|>)", "gi"), ' $1="$2"$3 ');
    
    // trim - start/end spacer
    s = s.replace(new RegExp("^[\\s]+", "g"), "");
    s = s.replace(new RegExp("[\\s]+$", "g"), "");
    // rm - double spacer
    s = s.replace(new RegExp("  +", "g"), "");
    // rm - special chars
    s = s.replace(new RegExp("\r+", "g"), "");
    s = s.replace(new RegExp("\n+", "g"), "");
    s = s.replace(new RegExp("\t", "g"), "");
    // format
    for(i in o) {
      s = s.replace(new RegExp(i, "gi"), o[i]);
    }
    // urlencode
    s = encodeURIComponent(s);
    s = s.replace(new RegExp('&amp;', "gi"), '%26');
    return s;
};




