/*******************************************************************************
 jquery.mb.components
 Copyright (c) 2001-2009. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
 email: info@pupunzi.com
 site: http://pupunzi.com
 Licences: MIT, GPL
 ******************************************************************************/

/*
 * jQuery.mb.components: jquery.mb.flipV
 * version: 1.0
 * © 2001 - 2009 Matteo Bicocchi (pupunzi), Open Lab
 *
 */


(function(jQuery) {
  var isIE=jQuery.browser.msie;
  jQuery.fn.encHTML = function() {
    return this.each(function(){
      var me   = jQuery(this);
      var html = me.text();
      me.text(html.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/'/g, escape("'")).replace(/"/g,escape('"')));
    });
  };
  jQuery.mbflipText= {
    author:"Matteo Bicocchi",
    version:"1.0",
    flipText:function(tb){
      return this.each(function(){

        var h=jQuery(this).getFlipTextDim(true)[1]+"px";
        var w=jQuery(this).getFlipTextDim(true)[0]+"px";

       if(!isIE) jQuery(this).encHTML();
        var txt= jQuery(this).html();
        var label="";
        var bgcol=jQuery(this).css("background-color")?jQuery(this).css("background-color"):"#fff";
        var fontsize= parseInt(jQuery(this).css('font-size'))>0?parseInt(jQuery(this).css('font-size')):14;
        var fontfamily=jQuery(this).css('font-family')?jQuery(this).css('font-family').replace(/\'/g, '').replace(/"/g,''):"Arial";
        var fontcolor=jQuery(this).css('color')?jQuery(this).css('color'):"#000";

        if (jQuery.browser.msie){
          if(!tb) jQuery(this).css({'writing-mode': 'tb-rl', height:h, filter: 'fliph() flipv("") ', whiteSpace:"nowrap"}).css('font-weight', 'normal');
          label=jQuery("<span style='writing-mode: tb-rl; whiteSpace:nowrap; height:"+h+"; width:"+w+"'>"+txt+"</span>");
        }else{
          var rot="-90";
          var ta="end";
          var xFix=0;
          var yFix=jQuery.browser.opera ?parseInt(w)-(parseInt(w)/4): jQuery.browser.safari?5:"0";
          if (tb){
            xFix= jQuery.browser.safari?5:0;
            rot="90, "+((parseInt(w)/2)-xFix)+", "+parseInt(w)/2;
            ta="start";
          }
//          var  SafariFix= jQuery.browser.safari? "<rect x=\"0\" y=\"0\" width=\""+w+"\" height=\""+h+"\" fill=\""+bgcol+"\" stroke=\"none\"/>":"";
          label=jQuery("<object class='flip_label' style='height:"+h+"; width:"+w+";' type='image/svg+xml' data='data:image/svg+xml," +
                  "<svg xmlns=\"http://www.w3.org/2000/svg\">" +
                  "<rect x=\"0\" y=\"0\" width=\""+w+"\" height=\""+h+"\" fill=\""+bgcol+"\" stroke=\"none\"/>"+ //safari Fix
                  "<text  x=\"-"+xFix+"\" y=\""+(yFix)+"\" font-family=\""+fontfamily+"\"  fill=\""+fontcolor+"\" font-size=\""+fontsize+"\"  style=\"text-anchor: "+ta+"; " +
                  "dominant-baseline: hanging\" transform=\"rotate("+rot+")\" text-rendering=\"optimizeSpeed\">"+txt+"</text></svg>'></object>" +
                  "<div class='pointer' style='position:absolute;top:0;left:0;width:100%;height:100%;background:transparent'/>" +
                  "");
        }
        var cssPos= jQuery(this).css("position")!="absolute" || jQuery(this).css("position")!="fixed"  ?"relative" : jQuery(this).css("position");
        jQuery(this).html(label).css({position:cssPos, width:w});
      });
    },
    getFlipTextDim:function(enc){
      if(!enc && !isIE) jQuery(this).encHTML();
      var txt= jQuery(this).html();
      var fontsize= parseInt(jQuery(this).css('font-size'));
      var fontfamily=jQuery(this).css('font-family').replace(/'/g, '').replace(/"/g,'');
      if (fontfamily==undefined) fontfamily="Arial";
      var placeHolder=jQuery("<span/>").css({position:"absolute",top:-100, whiteSpace:"noWrap", fontSize:fontsize, fontFamily: fontfamily});
      placeHolder.html(txt);
      jQuery("body").append(placeHolder);
      var h = (placeHolder.outerWidth()!=0?placeHolder.outerWidth():(16+txt.length*fontsize*.60));
      var w = (placeHolder.outerHeight()!=0?placeHolder.outerHeight()+5:50);
      placeHolder.remove();
      return [w,h];
    }
  };
  jQuery.fn.mbFlipText=jQuery.mbflipText.flipText;
  jQuery.fn.getFlipTextDim=jQuery.mbflipText.getFlipTextDim;

})(jQuery);