/*                                                                  
* Copyright © 1999-2009 TeaLeaf Technology, Inc.  
* All rights reserved.
*
* THIS SOFTWARE IS PROVIDED BY TEALEAF ``AS IS'' 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE DISCLAIMED.  
* IN NO EVENT SHALL TEALEAF BE LIABLE FOR ANY DIRECT, INDIRECT, 
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
* @fileoverview 
* This is the main UI Client Event Capture JavaScript file that is  
* used by other JavaScript to register their onload routines.
*
* @requires 
* TeaLeafCfg.js
*
* @version 2009.01.15.1
*                                                                   
*/

if(TeaLeaf && TeaLeaf.Configuration && TeaLeaf.Configuration.tlinit == false){
    TeaLeaf.Configuration.tlinit = true;
    if( ! Array.prototype.push ) {
	    Array.prototype.stackEnd = 0;
        /**
        * Add push if the browser does not supply
        * them with the Array object (IE6 and earlier)
        * @addon
        */
	    Array.prototype.push = function(obj) {
		    this[this.stackEnd] = obj;
		    this.stackEnd++;
	    }
    }
    if( ! Array.prototype.pop ) {
        /**
        * Add pop if the browser does not supply
        * them with the Array object (IE6 and earlier)
        * @addon
        */
	    Array.prototype.pop = function(obj) {
		    this.stackEnd--;
		    return this[this.stackEnd];
	    }
    }
     
    /**
    * Set TeaLeaf UI Client Event Capture as an SDK. 
    * The default behavior is not set as an SDK 
    * @requires 
    * TeaLeafCfg.js
    * @addon
    */
    TeaLeaf.settlSDK = function(){
        TeaLeaf.Configuration.tlSDK = true;
    }

    /**
    * Reset TeaLeaf UI Client Event Capture not to act as an SDK.
    * The default behavior is not set as an SDK 
    * @requires 
    * TeaLeafCfg.js
    * @addon
    */    
    TeaLeaf.resettlSDK = function(){
        TeaLeaf.Configuration.tlSDK = false;
    }

    /**
    * Array to store all the object that need to be loaded after the page is rendered.
    * NOTE: This will not be used if the UI Client Event Capture is used as an SDK.
    */    
    TeaLeaf.tLoadObjs = [];
    /**
    * This function is used for the other javascript files to register their
    * onload functions to be called
    * @param obj object that is registered from other JavaScript to be loaded
    * @param functionName object function that is registered from other JavaScript to be loaded 
    * @requires 
    * TeaLeafCfg.js
    * @addon
    */    
    TeaLeaf.addOnLoad = function(obj, functionName){    
	    if(arguments.length == 1) {	    
		    TeaLeaf.tLoadObjs.push(obj);
	    } else if(arguments.length > 1) {	
			TeaLeaf.tLoadObjs.push(obj[functionName]);
	    }
    }
    /**
    * This function is used to load UI Client Event Caputre or the
    * UI Client Event Capture SDK.
    * @requires 
    * TeaLeafCfg.js
    * @addon
    */    
	TeaLeaf.PageSetup = function() {
	    if(TeaLeaf.Configuration.tlSDK == false ){ 		
	        for(var i=0; i<TeaLeaf.tLoadObjs.length; i++){
                TeaLeaf.tLoadObjs[i]();	    
	        }
	    }
		TeaLeaf.EndLoad = new Date();
	}	

    if (document.addEventListener) {
	    document.addEventListener("DOMContentLoaded", TeaLeaf.PageSetup, null); // mozilla
    }   
    else {
	    if (typeof(document.readyState) != "undefined") {
		    if( typeof document.onreadystatechange == "function" ) {
			    TeaLeaf.ReadyStateChange = document.onreadystatechange;
		    }
		    else {
			    TeaLeaf.ReadyStateChange = null;
		    }
		    document.onreadystatechange = function() {
			    if(document.readyState == "complete") {
				    TeaLeaf.PageSetup();
			    }
			    if( TeaLeaf.ReadyStateChange ) {
				    TeaLeaf.ReadyStateChange();
			    }
		    }; // ie
        }
	    else {
		    if( typeof window.onload == "function" ) {
			    TeaLeaf.OnLoad = window.onload;
		    }
		    else {
			    TeaLeaf.OnLoad = null;
		    }
		    window.onload = function() {
			    TeaLeaf.PageSetup();
			    if( TeaLeaf.OnLoad ) {
				    TeaLeaf.OnLoad();
			    }
		    }; // other
	    }
    }
}

