
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function checkActiveLinks(base) {
	if(typeof(base) == 'undefined' && typeof(document.path) != 'undefined') base = document.path;

	linksArray = $$('a');

	var url = window.location;

	for(i = 0; i < linksArray.length; ++i ){
		var a = linksArray[i];

		url = new String(window.location);

		var link = a.href
		if(link.substring(link.length-4, link.length) == '.ait') link = link.substring(0, link.length-4)

		if(link == url || (link != base && link != (base + '/') && link == url.substring(0, link.length))) {
			if(link == url || (link + '/') == url || link == (url + '/')) {
				a.className += ' active_exact';
			}
			if(url.length <= link.length || (url.length > link.length && (url.substring(link.length, link.length+1) == '/' || url.substring(link.length, link.length+4) == '.ait'))) {
				a.className += ' active';
			}
		}
	}
}

function preLoad(images) {
	document.preLoad = new Array();

	if(document.images) {
		for(i = 0; i < images.length; ++i) {
			document.preLoad[i] = new Image();
			document.preLoad[i].src = images[i];
		}
	}
}

function mouseOver(element, image) {
	if(typeof(element.src_out) == 'undefined' || element.src_out == null) {
		element.src_out = element.src;
		if(typeof(image) != 'undefined') {
			element.src = image;
		} else {
			element.src = element.src.substring(0, element.src.length - 4) + '_up' + element.src.substring(element.src.length - 4, element.src.length);
		}
	}
}

function mouseOut(element) {
	if(typeof(element.src_out) != 'undefined' && element.src_out != null) {
		element.src = element.src_out;
		element.src_out = null;
	}
}


function emailProducts() {
	var request = new Request.JSON({
		url: document.path + '/shoppingcart_json.php?bogus=' + (new Date().getTime()),
		data: { 'action':'mail_list', 'email' : document.getElementById('shoppingcart_email_input').value },
		onSuccess: emailProductsReturn
		}).send();
}

function emailProductsReturn(responseJSON, responseText) {
	if(responseJSON == true) {
		hidePopup();
	} else {
		document.getElementById('shoppingcart_email_label').className = 'email_label error';
	}
}

function configuratorAddProducts(products) {
	var request = new Request.JSON({
		url: document.path + '/shoppingcart_json.php?bogus=' + (new Date().getTime()),
		data: { 'action':'add_product', 'products': JSON.encode(products) },
		onSuccess: configuratorAddProductReturn
		}).send();
}

function configuratorAddProductReturn(responseJSON, responseText) {
	if(responseJSON) {
		$('shoppingcart_total').innerHTML = responseJSON;
		showMessage(configurator_i18n['added_title'], configurator_i18n['added_text'], configurator_i18n['added_button'], 'hidePopup();');
	}
}

/* POPUP */

function showMessage(title, innerHTML, buttonText, popupFunction) {
	document.getElementById("popup_bg").style.display = "block";
	document.getElementById("popup_bg").style.height = getDocHeight() + "px";

	document.getElementById("popup").style.display = "block";
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("popup").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight)) + 'px';
	} else {
		document.getElementById("popup").style.top = (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight) + 'px';
	}

	document.getElementById("popup_title").innerHTML = title;
	document.getElementById("popup_text").innerHTML = innerHTML;

	document.getElementById("popup_b1").innerHTML = buttonText;
	document.popupFunction = popupFunction;
}

function hidePopup() {
	document.getElementById("popup").style.display = "none";
	document.getElementById("popup_bg").style.display = "none";
}

function positionPopup() {
	if(BrowserDetect.browser == "Explorer") {
		document.getElementById("popup").style.top = (document.body.scrollTop + (document.body.clientHeight/2 - document.getElementById("popup").offsetHeight)) + 'px';
	}
}

function popupButton(id) {
	eval(document.popupFunction);
}

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

/* BROWSER DETECT */

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
	{
		string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		prop: window.opera,
		identity: "Opera"
	},
	{
		string: navigator.vendor,
		subString: "iCab",
		identity: "iCab"
	},
	{
		string: navigator.vendor,
		subString: "KDE",
		identity: "Konqueror"
	},
	{
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	},
	{
		string: navigator.vendor,
		subString: "Camino",
		identity: "Camino"
	},
	{	// for newer Netscapes (6+)
		string: navigator.userAgent,
		subString: "Netscape",
		identity: "Netscape"
	},
	{
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "Explorer",
		versionSearch: "MSIE"
	},
	{
		string: navigator.userAgent,
		subString: "Gecko",
		identity: "Mozilla",
		versionSearch: "rv"
	},
	{ 	// for older Netscapes (4-)
		string: navigator.userAgent,
		subString: "Mozilla",
		identity: "Netscape",
		versionSearch: "Mozilla"
	}
	],
	dataOS : [
	{
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	},
	{
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	},
	{
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}
	]

};
BrowserDetect.init();
