/**
 * Bauglir JS Core
 *
 * @author Bronislav Klucka <Bronislav.Klucka@bauglir.com>
 * @copyright Copyright (c) 2009+, Bronislav Klučka
 * @license http://licence.bauglir.com/bsd.php BSD License
 * @version $Id: bauglir.core.js,v 1.7 2010-01-25 22:48:29 root Exp $
 * @package BauglirWebCore
 * @subpackage Javascript
 */

/*global window, document, Bauglir, navigator, jQuery, BauglirConstructor */
(function () {

  var winWidth = 0,
  winHeight = 0,
  resizeTimer,
  anchors,
  BauglirConstructor = function () {};
  
  BauglirConstructor.prototype =
  {

    /**
     * determine whether variable is defined (not null and not undefined)
     * @param mixed param
     * @return bool
     */
    defined: function (param)
    {
      return param !== null && param !== 'undefined' && param !== undefined;
    },

    /**
    * returns float value from string,
    *
    * @param mixed x
    * @param integer defaultValue default value if parameter is not integer
    * @return integer
    */
    number: function (x, defaultValue)
    {
      if (defaultValue === null)
      {
        defaultValue = 0;
      }
      if (x !== null)
      {
        x = parseFloat(x);
        if (isNaN(x))
        {
          x = defaultValue;
        }
        defaultValue = x;
      }
      return defaultValue;
    },

    /**
    * returns integer value from string,
    *
    * @param mixed x
    * @param integer defaultValue default value if parameter is not integer
    * @return integer
    */
    integer: function (x, defaultValue)
    {
      if (defaultValue === null)
      {
        defaultValue = 0;
      }
      if (x !== null)
      {
        x = parseInt(x, 10);
        if (isNaN(x))
        {
          x = defaultValue;
        }
        defaultValue = x;
      }
      return defaultValue;
    },

    /**
     * log data to browser's cosole
     * @param string text
     * @param bool printTime whether print timestamp
     */
    log: function (text, printTime)
    {
      if (Bauglir.defined(printTime))
      {
        text = new Date() + ": " + text;
      }
      try 
      {
        window.opera.postError(text);
      }
      catch (e)
      {
        try
        {
          window.console.log(text);
        }
        catch (e1) {}
      }
      
    },

    Browser:
    {
      Application: 'unknown',
      Version: 'unknown',
      MajorVersion: 'unknown',
      MinorVersion: 'unknown',
      ScrollBarWidth: 0,

      Msie6: false,
      Msie7: false,
      Msie8: false,
      Msie: false,
      Opera: false,
      Firefox: false,
      Safari: false,
      Chrome: false,
      Konqueror: false,

      /**
       * function loads general browser information
       */
      load: function ()
      {
        try
        {
          var arVersion = [];

          //BROWSER IDENTIFICATION
          if (window.opera)
          {
            this.Application = 'Opera';
          }
          else if ((navigator.appName.toLowerCase() === 'netscape') && (navigator.userAgent.toLowerCase().indexOf("firefox") >= 0))
          {
            this.Application = 'Firefox';
          }
          else if ((navigator.appName.toLowerCase() === 'netscape') && (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0))
          {
            this.Application = 'Chrome';
          }
          else if ((navigator.appName.toLowerCase() === 'netscape') && (navigator.userAgent.toLowerCase().indexOf("safari") >= 0))
          {
            this.Application = 'Safari';
          }
          else if ((navigator.appName.toLowerCase() === 'microsoft internet explorer') && (navigator.userAgent.toLowerCase().indexOf("msie") >= 0))
          {
            this.Application = 'MSIE';
          }
          else if ((navigator.appName.toLowerCase() === 'netscape') && (navigator.userAgent.toLowerCase().indexOf("konqueror") >= 0))
          {
            this.Application = 'Konqueror';
          }


          switch (this.Application)
          {
          case "Opera":
            this.Opera = true;
            arVersion = navigator.appVersion.split(" ");
            this.Version = (arVersion[0]);
            break;
          case "Firefox":
            this.Firefox = true;
            arVersion = navigator.userAgent.split("Firefox/");
            this.Version = (arVersion[1]);
            break;
          case "Chrome":
            this.Chrome = true;
            arVersion = navigator.userAgent.split("Chrome/");
            arVersion = arVersion[1].split(" ");
            this.Version = (arVersion[0]);
            break;
          case "Safari":
            this.Safari = true;
            arVersion = navigator.userAgent.split("Version/");
            arVersion = arVersion[1].split(" ");
            this.Version = (arVersion[0]);
            break;
          case "MSIE":
            arVersion = navigator.userAgent.split("MSIE");
            arVersion = arVersion[1].split(";");
            this.Version = (arVersion[0]);
            break;
          case "Konqueror":
            this.Konqueror = true;
            arVersion = navigator.userAgent.split("Konqueror/");
            arVersion = arVersion[1].split(";");
            this.Version = (arVersion[0]);
            break;
          }

          this.Version = this.Version.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "");
          this.Application = this.Application.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "");

          arVersion = this.Version.split(".");
          if (arVersion.length > 0)
          {
            this.MajorVersion = arVersion[0];
          }
          else
          {
            this.MajorVersion = 0;
          }
          if (arVersion.length > 1)
          {
            this.MinorVersion = arVersion[1];
          }
          else
          {
            this.MinorVersion = 0;
          }

          Bauglir.Browser.Msie6 = (Bauglir.Browser.Application === 'MSIE') && (Bauglir.Browser.Version === '6.0');
          Bauglir.Browser.Msie7 = (Bauglir.Browser.Application === 'MSIE') && (Bauglir.Browser.Version === '7.0');
          Bauglir.Browser.Msie8 = (Bauglir.Browser.Application === 'MSIE') && (Bauglir.Browser.Version === '8.0');
          Bauglir.Browser.Msie = (Bauglir.Browser.Application === 'MSIE');



        }
        catch (ex)
        {
          
        }


        

      },

      /**
       * get viewport height
       * @return int
       */
      getViewportHeight: function ()
      {
        try
        {
          if (this.Msie)
          {
            return document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
          }
          else
          {
            return window.innerHeight;
          }
        }
        catch (e)
        {

        }
      },

      /**
       * get viewport height
       * @param object doc window
       * @return int
       */
      getViewportHeightEx: function (doc)
      {
        try
        {
          if (this.Msie)
          {
            return doc.document.documentElement.clientHeight ? doc.document.documentElement.clientHeight : doc.document.body.clientHeight;
          }
          else
          {
            return doc.innerHeight;
          }
        }
        catch (e)
        {

        }
      },



      /**
       * get viewport width
       * @return int
       */
      getViewportWidth: function ()
      {
        try
        {
          if (this.Msie)
          {
            return document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
          }
          else
          {
            return window.innerWidth;
          }
        }
        catch (e)
        {
          
        }
      },

      /**
       * get viewport width
       * @param object doc window
       * @return int
       */
      getViewportWidthEx: function (doc)
      {
        try
        {
          if (this.Msie)
          {
            return doc.document.documentElement.clientWidth ? doc.document.documentElement.clientWidth : doc.document.body.clientWidth;
          }
          else
          {
            return doc.window.innerWidth;
          }
        }
        catch (e)
        {

        }
      },


      //just to be able to end every pair with comma (easier to copy/paste)
      qwerty: 'a'
    },


    //OBJECTS
    /**
     * object working with web/www related issues
     */
    Web: {


      /**
       * drop cookie
       * @param string name name of the cookie
       */
      dropCookie: function (name)
      {
        var date = new Date(), expires;
        date.setTime(date.getTime() + (-1000 * 1000));
        expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + expires + "; path=/";
      },

      /**
       * read cookie
       * @param string name name of the cookie
       * @param string defaultValue default value if cookie does not exists
       */
      getCookie: function (name, defaultValue)
      {
        if (defaultValue === null)
        {
          defaultValue = '';
        }
        var nameEQ = name + "=",
        ca = document.cookie.split(';'),
        i, c, j;
        for (i = 0, j = ca.length; i < j; i++)
        {
          c = ca[i];
          while (c.charAt(0) === ' ')
          {
            c = c.substring(1, c.length);
          }
          if (c.indexOf(nameEQ) === 0)
          {
            return window.unescape(c.substring(nameEQ.length, c.length));
          }
        }
        return defaultValue;
      },

      


      /**
       *creates cookie
       * @param string name naem of the cookie
       * @param string value value of the cookie
       * @param int seconds seconds cookie should be valid
       */
      setCookie: function (name, value, seconds)
      {
        var expires = "",
        date;
        if (seconds !== null)
        {
          date = new Date();
          date.setTime(date.getTime() + (seconds * 1000));
          expires = "; expires=" + date.toGMTString();
        }
        if (Bauglir.Browser.Msie)
          document.cookie = name + "=" + window.escape(value) + expires;
	else    
          document.cookie = name + "=" + window.escape(value) + expires + "; path=/";
        //alert(document.cookie);
      },


      //just to be able to end every pair with comma (easier to copy/paste)
      qwerty: 'a'
    },
 
    //just to be able to end every pair with comma (easier to copy/paste)
    qwerty: 'a'
  };

  Bauglir = window.Bauglir = window.$$ = new BauglirConstructor();
  Bauglir.Browser.load();
  jQuery(function ()
  {

    var scr = null,
      inn = null,
      wNoScroll = 0,
      wScroll = 0,
      i, j;

    //SCROLLBAR
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    scr.style.overflow = 'hidden';
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';
    scr.appendChild(inn);
    document.body.appendChild(scr);
    wNoScroll = inn.offsetWidth;
    if (Bauglir.Browser.Msie)
    {
      scr.style.overflow = 'scroll';
    }
    else
    {
      scr.style.overflow = 'auto';
    }
    wScroll = inn.offsetWidth;
    document.body.removeChild(document.body.lastChild);
    Bauglir.Browser.ScrollBarWidth = (wNoScroll - wScroll);



    if (Bauglir.Browser.Msie)
    {
      anchors = document.getElementsByTagName('A');
      for (i = 0, j = anchors.length; i < j; i++)
      {
        anchors[i].hideFocus = true;
      }
    }
    jQuery(window).resize(function (e) {
      window.clearTimeout(resizeTimer);
      resizeTimer = window.setTimeout(function () {
        if ((winWidth !== Bauglir.Browser.getViewportWidth()) || (winHeight !== Bauglir.Browser.getViewportHeight()))
        {
          winWidth = Bauglir.Browser.getViewportWidth();
          winHeight = Bauglir.Browser.getViewportHeight();
          jQuery(window).triggerHandler('resized');
        }
      }, 50);
    });
    jQuery(window).triggerHandler('resize');
  });


  //alert(Bauglir.Web.html('<div>'));
}());




