/*
 * Ajax presence check.
 */
function ajax_check() {
	var xmlhttp = false;
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		return false;
	}
	return true;
}

/* 
 * Preload onmouseover images
 */
function preload_image(_image) {
	var image = new Image;
	image.src = _image;
}

/*
 * Onload queue helper, first one uses setTimeout to allow browser rendering
 */
function OnloadExecute() {
	if (!window.onload_queue || window.onload_queue.length == 0)
		return;

	for (var i = 0; i < window.onload_queue.length; i++) {
		window.onload_queue[i]();
	}
}

function Onload(func, highprio) {
	if (typeof(func) != 'function')
		return;

	if (!window.onload_queue_set) {
		window.onload_queue_set = true;
		var oldonload = window.onload;

		window.onload = function () {
			if (oldonload)
				oldonload();

			setTimeout(OnloadExecute, 10);
		};
	}

	if (!window.onload_queue)
		window.onload_queue = [];

	if (highprio)
		window.onload_queue.unshift(func);
	else
		window.onload_queue[window.onload_queue.length] = func;
}

/*
 * Auto resize iframe
 */
function resize_eas_frame(self, max_width) {
        if (!document.org_domain)
                document.org_domain = document.domain;

        /* Only domain, no subdomain */
        var domain = location.host;

	if (domain.match(/^([0-9].){4}/)) {
		if (domain.indexOf(':'))
			domain = domain.substr(0, domain.indexOf(':'));
	} else {
		var domain_arr = domain.split(".");
		if (domain_arr.length >= 2)
			domain = domain_arr[domain_arr.length - 2] + '.' + domain_arr[domain_arr.length - 1];
	}
	document.domain = domain;

        var content = self.contentWindow ?
                self.contentWindow.document :
                self.contentDocument.document;

        self.height = content.body.scrollHeight;
	if (self.height == 0)
		self.parentNode.removeChild(self);
	else {
		if (content.body.scrollWidth < max_width)
			self.width = content.body.scrollWidth;
		else if (max_width) 
			self.width = max_width;
	}
}

/*
 * Pass request through redir.
 */
function clickcounter(name) {
	if (ajax_check()) {
		var httpreq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		httpreq.open('GET', '/redir?nc=1&s=' + name, true);
		httpreq.send(null);
	}
}

/*
 * JS script include, with callback functionality
 */
function include_script(src, callback) {
	var script = document.createElement('script');
	script.src = src;
	script.type = 'text/javascript';
	if (callback) {
		script.onload = callback;
		script.onreadystatechange = function () {
			if (this.readyState == "complete" || this.readyState == "loaded") {
				this.onload();
				this.onload = this.onreadystatechange = null;
			}
		};
	}
	document.getElementsByTagName('head')[0].appendChild(script);
}

