 /**
 * Las Venturas Playground
 *
 * Compatability helpers for the website's display. In most cases the visitor's
 * browser won't support one or more features of the things we'd like to implement,
 * like font-replacing, style hacks for certain browsers and more.
 * 
 * @author Badeend <badeend@sa-mp.nl>
 * @copyright Copyright (c) 2006-2009, Las Venturas Playground
 * @version 1.0
 *
 * @see http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/
 *
 * $Id: playground.js 196 2009-08-31 20:09:53Z Badeend $
 */

$(document).ready (function ()
{
        if (document.getElementById ('lvp-cool-fonts') != null || TypeHelpers.hasSmoothing () == true)
        {
                $ ('body').attr ('id', 'lvp-cool-fonts');
                return ;
        }
        
        $ ('h2').each (function () 
        {
                var pHeaderElement = $ (this);
                if (pHeaderElement.parent ().attr ('id') == 'content' || pHeaderElement.hasClass ('big-header'))
                {
                        pHeaderElement.html ('<img src="/images/headers/top-' + escape (pHeaderElement.text ()) + '.jpg" title="' + pHeaderElement.attr ('title') + '" />');
                }
                else
                {
                        pHeaderElement.html ('<img src="/images/headers/' + escape (pHeaderElement.text ()) + '.jpg" title="' + pHeaderElement.attr ('title') + '" />');
                }
        });
		
		if ($.browser.msie && parseInt($.browser.version,10) < 9) {
			(function (a) {
				while ("undefined" !== document.createElement (a.shift ()).tagName);
			})
			("abbr,article,aside,audio,canvas,datalist,details,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split (","));
		}
});

/**
 * Fix up HTML 5 entity rendering in Internet Explorer, it'd be nice to be able to use that,
 * which is exactly what we need this for. Silly IE.
 */

var TypeHelpers = new function(){

   // I use me instead of this.  For reasons why, please read:
   // http://w3future.com/html/stories/callbacks.xml
   var me = this;

   me.hasSmoothing = function(){
      if (typeof(sessionStorage) != "undefined" && typeof(sessionStorage.lvpFontSmoothing) != "undefined") {
        return (sessionStorage.lvpFontSmoothing == true || sessionStorage.lvpFontSmoothing == 'true');
      }
      if ($.cookie ('lvpFontSmoothing') !== false) {
        return ($.cookie ('lvpFontSmoothing') == 'true');
      }
      
      // IE has screen.fontSmoothingEnabled - sweet!
      if (typeof(screen.fontSmoothingEnabled) != "undefined") {
         return screen.fontSmoothingEnabled;
      } else {

         try {

            // Create a 35x35 Canvas block.
            var canvasNode = document.createElement('canvas');
            canvasNode.width = "35";
            canvasNode.height = "35"

            // We must put this node into the body, otherwise
            // Safari Windows does not report correctly.
            canvasNode.style.display = 'none';
            document.body.appendChild(canvasNode);
            var ctx = canvasNode.getContext('2d');

            // draw a black letter 'O', 32px Arial.
            ctx.textBaseline = "top";
            ctx.font = "32px Arial";
            ctx.fillStyle = "black";
            ctx.strokeStyle = "black";

            ctx.fillText("O", 0, 0);

            // start at (8,1) and search the canvas from left to right,
            // top to bottom to see if we can find a non-black pixel.  If
            // so we return true.
            for (var j = 8; j <= 32; j++) {
               for (var i = 1; i <= 32; i++) {

                  var imageData = ctx.getImageData(i, j, 1, 1).data;
                  var alpha = imageData[3];

                  if (alpha != 255 && alpha != 0) {
                     if (typeof(sessionStorage) != "undefined") {
                       sessionStorage.lvpFontSmoothing = true;
                     }
                     
                     $.cookie ('lvpFontSmoothing', true, { expires: 7 });
                     return true; // font-smoothing must be on.
                  }
               }

            }

            // didn't find any non-black pixels - return false.
            if (typeof(sessionStorage) != "undefined") {
              sessionStorage.lvpFontSmoothing = false;
            }
            
            $.cookie ('lvpFontSmoothing', false, { expires: 7 });
            return false;
         }
         catch (ex) {
            // Something went wrong (for example, Opera cannot use the
            // canvas fillText() method.  Return null (unknown).
            if (typeof(sessionStorage) != "undefined") {
              sessionStorage.lvpFontSmoothing = false;
            }
            
            $.cookie ('lvpFontSmoothing', false, { expires: 7 });
            return null;
         }
      }
   }
};

/**
 * The lvpCompat pseudo-class contains all methods which we will be using for
 * compatibility detection in browsers, in some easy tricks and methods.
 */

var lvpCompat = (function () 
{
    return {
        
        /**
         * In here we'll determine the most commonly used rules and pre-define them
         * for usage later on. Detection based on browser-type.
         */
        
        initialize : function () 
        {
                
        },
        
        /**
         * A simple method which will return whether the browser supports the @font-
         * face CSS rules, or not, determined in the initialize() method.
         */
        
        hasFontFace : function ()
        {
                return TypeHelpers.hasSmoothing ();
        },
        
        /**
         * If the browser supports the @font-face CSS rule, we will be able to
         * display custom web fonts. Quite cool considering the image-titles won't
         * be required anymore, since we'll be able to show normal text instead.
         */
        
        loadTitleFont : function ()
        {
        
        }
        
    };
    
})();
