// ================================================================
//  p6tbl-Lib.js ---- plus6th-table.com Function Library
//  Copyright 2007 Tatuto Gion <tatuto017@gmail.com>
//  Version 2007/06/04
// ================================================================
/******************************************************************/

if ( typeof P6TBL == 'undefined' ) P6TBL = function() {};

P6TBL.Lib = {
  isCompat : function() {
    return ( document.compatMode && document.compatMode == 'CSS1Compat' );
  },

  isIE : function() {
    return ( document.all &&
             window.ActiveXObject &&
             navigator.userAgent.toLowerCase().indexOf("msie") > -1 &&
             navigator.userAgent.toLowerCase().indexOf("opera") == -1 );
  },

  isFx : function() {
    return ( navigator.userAgent.indexOf('Firefox') > -1 );
  },

  isOpera : function() {
    return ( navigator.userAgent.toLowerCase().indexOf("opera") != -1 );
  },

  getCookie : function(cookieName) {
    var result  = document.cookie.split('; ').find(function(cookie) {
      var crumb = cookie.split('=');
      return ( crumb[0] ==  cookieName && crumb[1] != null );
    });

    if ( P6TBL.Lib.isOpera() && typeof result == 'null' ) {
      result = result.replace(/%22/g, '"');
    }

    return result;
  },

  setCookie : function(cookieName, cookieValue) {
    var LifeTime = ( arguments == 3 ) ? arguments[2] : 30;

    if ( P6TBL.Lib.isOpera() ) {
      cookieValue = cookieValue.replace(/"/g, "%22");
    }
    var date = new Date();
    date.setTime(date.getTime() + (LifeTime * 24*60*60*1000));
    var expires     = '; expires=' + date.toGMTString();
    document.cookie = cookieName + '=' + cookieValue + expires + '; path=/';
  },

  clearCookie : function(cookieName) {
    document.cookie = cookieName + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
  },

  getClientAreaSize : function() {
    // IE以外
    if ( !P6TBL.Lib.isIE() ) {
      return { width : window.innerWidth, height : window.innerHeight };
    }
    // IE6・標準準拠モード
    if ( P6TBL.Lib.isIE() && P6TBL.Lib.isCompat() ) {
      return { width  : document.documentElement.clientWidth,
               height : document.documentElement.clientHeight };
    }
    // その他のIE
    return { width  : document.body.clientWidth,
             height : document.body.clientHeight };
  },

  getBody : function() {
    return document[ P6TBL.Lib.isCompat() ? 'documentElement' : 'body' ];
  },

  scrollLeft : function(y, element) {
    var elm;
    if ( typeof element == 'undefined' ) {
      elm = P6TBL.Lib.getBody();
    } else {
      elm = P6TBL.Lib.getElement(element);
    }

    return elm;
  },

  scrollTop : function(element) {
    var elm;
    if ( typeof element == 'undefined' ) {
      elm = document[ P6TBL.Lib.isCompat() ? 'documentElement' : 'body' ];
    } else {
      elm = P6TBL.Lib.getElement(element);
    }

    return elm;
  },
  getText : function(element) {
    var elm = P6TBL.Lib.getElement(element);
    return ( P6TBL.Lib.isFx() ) ? element.textContent : element.innerText;
  },

  setText : function(element, text) {
    var elm = P6TBL.Lib.getElement(element);
    ( P6TBL.Lib.isFx() ) ? element.textContent = text : element.innerText = text;
  },

  getElement : function(element) {
    return ( typeof element == 'string' ) ? $(element) : element;
  },

  convertHash : function(Objs) {
    var hash_ = new Array();
    $H(Objs).each(function(arr) {
      hash_.push({ key   : arr[0],
                   value : arr[1] }
      );
    });

    return hash_;
  },

  Nodes2Array : function(element, tag) {
    var nodes = P6TBL.Lib.getElement(element).getElementsByTagName(tag);
    return $A(nodes);
  }
};
