// SpryDOMReady.js - version 0.2 - Spry Pre-Release 1.6.1
//
// Copyright (c) 2007. Adobe Systems Incorporated & 3rd-Eden.com.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of Adobe Systems Incorporated nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 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.

var Spry; if (!Spry) Spry = {}; if (!Spry.Utils) Spry.Utils = {};

Spry.Utils.DOMreadyCallback = [];
Spry.Utils.DOMTimer;
Spry.Utils.DOMready; 

Spry.Utils.onDOMReady = function(handler)
{	
	if(Spry.Utils.DOMreadyCallback.length == 0){
		
		var ua = navigator.userAgent.toString();
		var re_msie = /MSIE.([0-9\.]*)/i;
		var re_webkit = /WebKit/i;
		
		var domReady = function(){if (!Spry.Utils.DOMready)Spry.Utils.DOMCallback();};
		
		if(document.readyState && ua.match(re_msie)){ //IE 
			document.write("<scr" + "ipt id='DOMReady' defer=true " + "src=//:><\/scr" + "ipt>");
			Spry.$("DOMReady").onreadystatechange=function(){if (this.readyState=="complete"&&(!Spry.Utils.DOMready))Spry.Utils.DOMCallback();}
		} else if(document.readyState && ua.match(re_webkit)){ //safari
		 	Spry.Utils.DOMTimer = setInterval(function(){if(/loaded|complete/.test(document.readyState))Spry.Utils.DOMCallback();},0);
		} else if(typeof document.addEventListener != 'undefined'){ //mozilla + opera (using else statements else this would fire on safari again to
			document.addEventListener("DOMContentLoaded", domReady, false);
		} else {
			window.setTimeout(Spry.Utils.DOMCallback,0); 
		}
		Spry.Utils.addLoadListener(domReady); //back up, so we know that our data gets fired.
	}
	Spry.Utils.DOMreadyCallback.push(handler); //push it in a arrray
};
//This function will be used by cookie to check if the value is allready in the cookie, if so it returns it position
Spry.Utils.DOMCallback = function()
{
	if(Spry.Utils.DOMready == true) return;
    Spry.Utils.DOMready = true;
	
    if(Spry.Utils.DOMTimer)
		clearInterval(Spry.Utils.DOMTimer);
    
    for(i=0; i<Spry.Utils.DOMreadyCallback.length; i++)
		Spry.Utils.DOMreadyCallback[i]();

    Spry.Utils.DOMreadyCallback = null;
};
