// SCRIPT MODULES LIBRARY   ECMAScript v3.0   (c) 2006 Kleeton PL - all rights reserved

// a Global object CSS07 is assumed declared  CSS07  may be changed to make it  unique for deployment 
try
{

// ===========================================================================
// NAMESPACE MANAGEMENT - ONLY ONE GLOBAL IDENTIFIER RESERVED ('CSS07')
if (!CSS07)      { window.status = 'KPL-LIB: CSS07 not loaded]'; throw new Error(window.status); }

if (!CSS07.kpl)  { CSS07.kpl = {USAGE:'kleeton.com.au ecmascript & event lib'}; } 

CSS07.kpl.defLoadTime = 20.0;      // 20 secs to load before we decide its a dial-up

CSS07.kpl.lastLoadHandler = null; // 

// ===========================================================================
// SCRIPT INITIALISATION
// Browser independent binding fns and helpers
// This script formally binds the page events, hence does not use deprecated 'window.onload' event, etc.

CSS07.kpl.enableNotifyOnPageLoad = function (loadHandler)
{
    try
    {
        if (!(loadHandler instanceof Function))  { return; }
        loadHandler.pageloadStartTime = (new Date()).getTime();
        var w3cCapture = false;
        CSS07.kpl.lastLoadHandler = loadHandler;
        bindWindowEvents();
    }
    catch(e){ alert('Exception binding load');}

    function bindWindowEvents()
    {
        if (window.addEventListener)                                                  // w3c-DOM method?
        {
            window.addEventListener('load',   loadHandler, w3cCapture);               // w3c standard binding
            window.addEventListener('unload', unbindWindowEvents, w3cCapture);
        }
        else if (window.attachEvent)                                                  // IE method?
        {            
            window.attachEvent('onload',   loadHandler);                              // non-standard binding for IE
            window.attachEvent('onunload', unbindWindowEvents);
        }
    }

    function unbindWindowEvents()
    {
      try
      {
          if (window.removeEventListener)
          {
              window.removeEventListener('load',   loadHandler, w3cCapture);
              window.removeEventListener('unload', unbindWindowEvents, w3cCapture);
          }
          else
          {
              window.detachEvent('onload',   loadHandler);
              window.detachEvent('onunload', unbindWindowEvents);
          }
      }
      catch(e){}
    }
};

// BEHAVIOUR: PAGE INITIALIZATION (on PageLoaded)

CSS07.kpl.slowLoad = function (tooLong)
{
    var PAGELOAD_SLOW_SECS = tooLong || CSS07.kpl.defLoadTime;
    var startTime = CSS07.kpl.lastLoadHandler.pageloadStartTime;
    
    var pageLoad_secs = ((new Date()).getTime() - startTime) / 1000;
    return (pageLoad_secs >= PAGELOAD_SLOW_SECS) ? true : false;
};


// ===========================================================================
// CORE ECMASCRIPT - EXTENSION LIB

CSS07.kpl.Encapsulator = function ()                                                 // CLASS CONSTRUCTOR // - typically inherited by call/apply methods
        {
            this.bind        = CSS07.kpl.bind;
            this.encaseArgs  = CSS07.kpl.encaseArgs;
            this.bindEvent   = CSS07.kpl.bindEvent;
            this.unbindEvent = CSS07.kpl.unbindEvent;
        };                                                                          // END: Encapsulators class //

CSS07.kpl.bind = function (obj, funct)                                               // bind-as-method - method bindery (callback-object factory, etc)
        {
            var method = (funct instanceof Function) ? funct : obj[funct];          // function ('method') may be passed by name or reference
            if (method === undefined) {                                             // PJM Debugging
              return null;
            }
            return  function method_proxy ()                                        // note: by returning function-ref, closure persists
            
                    {
                        try
                        {
                            return method.apply(obj, arguments);                    // invoke with args as method of obj, returning result if any
                        }
                        catch(e){return null;}                                      // (return is to satisfy JavaScript-Lint)
                    };
        };
CSS07.kpl.encaseArgs = function (argsObject, iFirstArg, optionalFolder)              // parse function-arguments & return array
        {
            var fldr = (optionalFolder) ? (optionalFolder) : '';
            var last = (fldr.length > 0) ? fldr.charAt(fldr.length -1) : '/';
            if (!(last == '/' || last == '\\')) { fldr += '/'; }
            var len  = argsObject.length - iFirstArg;                               // iFirstArg must be idx 0-based
            var args = new Array();                                                 // indexed 0-based
            for (var i=0; i<len; i++)
            {
                args[i] = fldr + argsObject[i + iFirstArg];
            }
            return args;
        };
//  Note that this fn returns the event handler supplied as an argument if it succeeds
//  a reminder that the client must store this to unbind on completion, for memory leak prevention  --  PJM
CSS07.kpl.bindEvent = function (obj, evtHandler, evt_w3c, onevt_ie)                 // DOM-event bindery (deprecated bindings not supported)
        {
          try
          {
            //window.status = "bindEvent: " + (obj === window ? "window" : obj.tagName) + '  ' + evt_w3c;
            if (window.addEventListener)                                             // w3c-DOM method?
            {
                var capture = false;
                obj.addEventListener(evt_w3c, evtHandler, capture);                 // w3c standards-compliant binding
            }
            else
            { 
                if (window.attachEvent)                                             // IE method
                {
                   var evtIE = onevt_ie || ('on' + evt_w3c);                        // optional arg; default if not supplied
                   obj.attachEvent(evtIE, evtHandler);                              // non-standard binding for IE
                } else {
                    alert('No bindEvent Fn');
                }
            }
            return evtHandler;                                                      // return function ref for unbinding
          }
          catch(e){ alert('bindEvent Exception ' + evt_w3c); return null;}
        };
        
CSS07.kpl.unbindEvent = function (obj, evtHandler, evt_w3c, onevt_ie)               // DOM-event unbindery
        {
          try
          {
            if (window.removeEventListener)
            {
                var capture = false;
                obj.removeEventListener(evt_w3c, evtHandler, capture);
            }
            else
            {
                if (window.detachEvent)
                {
                    var evtIE = onevt_ie || ('on' + evt_w3c);
                    obj.detachEvent(evtIE, evtHandler);
                }
            }
          }
          catch(e){ alert('unbindEvent Exception ' + evt_w3c); }
        };

CSS07.kpl.viewportHeight =  function ()
        {
          try
          {
            var doc = window.document;
            if (window.innerHeight)                                                  // all except ie
            {
              return window.innerHeight;
            }
            else if (doc.documentElement && doc.documentElement.clientHeight)        // ie6 Strict Mode
            {
              return doc.documentElement.clientHeight;
            }
            else if (doc.body)                                                       // other ie
            {
              return doc.body.clientHeight;
            }
          }
          catch(e){return null; }
        };

CSS07.kpl.viewportWidth =  function ()
        {
          try
          {
            var doc = window.document;
            if (window.innerWidth)                                                  // all except ie
            {
              return window.innerWidth;
            }
            else if (doc.documentElement && doc.documentElement.clientWidth)        // ie6 Strict Mode
            {
              return doc.documentElement.clientWidth;
            }
            else if (doc.body)                                                       // other ie
            {
              return doc.body.clientWidth;
            }
          }
          catch(e){return null; }
        };
        
CSS07.kpl.isNmbr =  function(aValue)
        { return ( !isNaN(Number(aValue)) ); };
        
CSS07.kpl.isNmbrPos = function(aValue)
        { return ( (CSS07.kpl.isNmbr(aValue)) ? (aValue >= 0) : false ); };

CSS07.kpl.viewportToBodyTop = function ()
        {
          try
          {
            var sIE6, sIE5, sNS6, scrl;   
            sIE6 = sIE5 = sNS6 = scrl = 0;
            if (window.document.documentElement)
                  { sIE6 = parseInt(window.document.documentElement.scrollTop); }
            sIE5 = parseInt(window.document.body.scrollTop);
            sNS6 = parseInt(window.scrollY);
            if (!(CSS07.kpl.isNmbrPos(sIE6)))  { sIE6 = 0; }
            if (!(CSS07.kpl.isNmbrPos(sIE5)))  { sIE5 = 0; }
            if (!(CSS07.kpl.isNmbrPos(sNS6)))  { sNS6 = 0; }
            if      (sIE6 > 0) { scrl = sIE6; }
            else if (sIE5 > 0) { scrl = sIE5; }
            else if (sNS6 > 0) { scrl = sNS6; }
            else               { scrl = 0;    }
            return Math.round(scrl);
          }
          catch(e){return null; }
        };

CSS07.kpl.getStyle = function (elem, IEStyleProp, CSSStyleProp) 
        {
            if (elem.currentStyle) 
            { return elem.currentStyle[IEStyleProp]; } 
            if (window.getComputedStyle) 
            { return window.getComputedStyle(elem, '').getPropertyValue(CSSStyleProp); }
            return '';
        };

CSS07.kpl.eltIsOfClass = function (elementRef, className)
        {
            var CLASS_NAME_SEPARATOR = ' ';
            var classes  = elementRef.className.split(CLASS_NAME_SEPARATOR);
            for(var i=0; i<classes.length; i++)
            { if (classes[i] == className) { return true; } }
            return false; 
        };

CSS07.kpl.domLeftToBody = function(el)
        {
            return ( (el.offsetParent) ? (el.offsetLeft + CSS07.kpl.domLeftToBody(el.offsetParent)) : el.offsetLeft ); 
        };

CSS07.kpl.domTopToBody = function(el) 
        {
            return ( (el.offsetParent) ? (el.offsetTop  + CSS07.kpl.domTopToBody(el.offsetParent))  : el.offsetTop  ); 
        };



// (c) 2006 Ian Brown  

}
catch(e) {}

// end script
