/*  webs_common.js - global utility functions for use on user sites
 *  
 *  @Author: Ryan McGrath
 *  @Requires: No external libraries
 */

if(typeof webs === "undefined") var webs = {};

webs.loadEvent = function(func) {
    // Try for for jQuery's document.ready availability
	if(typeof jQuery !== "undefined") jQuery(document).ready(func);
    else if(typeof FastInit !== "undefined" && typeof FastInit.addOnLoad === "function") FastInit.addOnLoad(func);
    else webs.fallbackLoadEvent(func);
};

webs.fallbackLoadEvent = (function() {
    var load_events = [], load_timer, script, done, exec, old_onload,
			
	init = function () {
        done = true;
		clearInterval(load_timer);
        for(i = 0; i < load_events.length; i++) {
		    exec = load_events.shift(); 
            exec();
		}
        if(script) script.onreadystatechange = '';
	};
		
	return function(func) {
	    if(done) return func();
		
        if(!load_events[0]) {
			if(document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);
		
			/*@cc_on @*/ /*@if (@_win32) 
				document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
				script = document.getElementById("__ie_onload");
				script.onreadystatechange = function() {
					if (this.readyState == "complete") init();
				};
			/*@end @*/
		
			if(/WebKit/i.test(navigator.userAgent)) {
				load_timer = setInterval(function() {
					if(/loaded|complete/.test(document.readyState)) init(); 
				}, 10);
			}
		
			old_onload = window.onload;
			window.onload = function() {
				init();
				if (old_onload) old_onload();
			};
		}
		load_events.push(func);
	}
})();

webs.siteLoginPopover = function(server, email, siteID) {
	return new Popover(server + '/s/login/siteLoginPopover?id=' + email + '&site=' + siteID, {
		width: 430, 
		height: (typeof acsLink !== "undefined" ? 300 : 175), 
		heading: 'Manage Website'
	}).show();
};
