/* $Id: wc_osd.js 55499 2008-08-04 17:45:59Z sboulais $ */
if(typeof Prototype != "undefined")
{
var OSD = {
	_viewport: null,	
	_front: null,
	_box: null,
	_top: null,
	_left: null
};

/* OSD.resize */

OSD.resize = function(evt) {
	try {
		OSD._viewport = document.viewport.getDimensions(); 			
		OSD._front = $("WC_front").getDimensions();
		OSD._box = $("WC_osd").getDimensions();
		OSD._top = Math.round((OSD._viewport.height - OSD._box.height) / 2);
		OSD._left = Math.round((OSD._front.width - OSD._box.width) / 2);
		if(OSD._top < 140) OSD._top = 140;
		if(OSD._left < 1) OSD._left = 0;
		$("WC_osd").setStyle({"top": OSD._top + "px", "left": OSD._left + "px"});
		$("WC_darken").setStyle({"height": Math.max(OSD._front.height, OSD._viewport.height)});
	}
	catch(e) {}
}

Event.observe(window, "resize", OSD.resize.bindAsEventListener(OSD));

/* OSD.make */

OSD.make = function(evt, o) { 
	var divNode, output = ""; 

	try { 
		if(undefined != $("WC_osd"))
			throw(new Error("OSD already created"));
		$("WC_front").appendChild(divNode = $(document.createElement("div")));
		divNode.setAttribute("id", "WC_osd");
		divNode.addClassName(o.classname);
	}
	catch(e) {
		window.alert(e.message);
		return null; // exit OSD.make()
	}
	
	if(!document.implementation.hasFeature("Core", "2.0")) { // IE6
		$$("select").each(function(elt) { elt.toggle(); });				
	}
	
	$("WC_front").appendChild(darkenNode = $(document.createElement("div")));
	darkenNode.setAttribute("id", "WC_darken");
	darkenNode.setStyle({"background": "#000 none no-repeat top left"});
	darkenNode.setOpacity(0);
	if(true == o.darken) {
		darkenNode.setOpacity(0.5);
		$$("body").first().addClassName("halfbright");
	}
	
	OSD.resize();

	try { o.customHtmlContent = o.callfrom(evt); }
	catch(e) {}
	
	if(undefined != o.customHtmlContent) {
		divNode.appendChild(customContentNode = $(document.createElement("div")));
		customContentNode.setAttribute("id", "custom");
		customContentNode.innerHTML = o.customHtmlContent;
	}
	else try {
		if(undefined != o.title) output += "<h1>" + o.title + "</h1>";
		output += "<p>" + o.msg + "</p>";
		if(undefined != o.buttons) {
			output += "<span class=\"buttons\">";
			o.buttons.each(function(button) {
				var target = button.target || "javascript:OSD.close();";
				output += "<a class=\"button\" href=\"" + target + "\"><img src=\"" + button.image + "\" /></a> "
			});
			output += "</span>";
		}
		divNode.innerHTML = output;
	}
	catch(e) {}

	if(true == o.close) {
		var aNode;
		divNode.appendChild(aNode = $(document.createElement("a")));
		aNode.addClassName("close");
		aNode.setAttribute("href", "#");
		aNode.innerHTML = "Fermer";
		Event.observe(aNode, "click", function(evt) { OSD.close(); });
	}
	if(undefined != o.closeDelay) {
		window.setTimeout(function() { OSD.close(); }, 1000 * o.closeDelay);
	}
}
	
/* OSD.close */

OSD.close = function() {
	if(null != $("WC_osdShadow")) $("WC_osdShadow").remove();
	new Effect.Fade($("WC_osd"), {duration: 0.2, afterFinish: function() { 
			$("WC_osd").remove();
			if(null != $("WC_darken")) $("WC_darken").remove();
			$$("body").first().removeClassName("halfbright");
			if(!document.implementation.hasFeature("Core", "2.0")) { // IE6
				$$("select").each(function(elt) { elt.toggle(); });				
			}
		}
	});
	Event.stopObserving(window, "resize", OSD.resize.bindAsEventListener(OSD));
}

/* OSD.create */

OSD.create = function(o) {
	Event.observe(window, "load", function(evt) {
		if(undefined != o.elements) { 
			var iterator;
			if(o.elements instanceof Array) iterator = o.elements;
			else iterator = $$(o.elements);
			iterator.each(function(elt) {
				Event.observe(elt, "click", OSD.make.bindAsEventListener(OSD, o));
			});
		}
		else OSD.make(evt, o);
	});
}
};