if (navigator.product == "Gecko")
{
	var BwCapturedEvents = ["click","mousedown","mouseup","mousemove","mouseover","mouseout" ];
	
	HTMLElement.prototype.setCapture = function()
	{
		if (this._capture != null) this.releaseCapture();
		
		var _this = this;
		this._capture = function (e)
		{
			e.preventDefault();
			e.stopPropagation();
			var f = _this['on'+e.type];
			if (f) f.call (_this, e);
		};
		
		var c = BwCapturedEvents;
		var l = c.length;
		for (var i = 0; i < l; i++)
		{
			window.addEventListener (c[i], this._capture, true);
			window.captureEvents (Event[c[i]]);
		}
	};

	HTMLElement.prototype.releaseCapture = function()
	{
		var c = BwCapturedEvents;
		var l = c.length;
		for (var i = 0; i < l; i++)
		{
			window.releaseEvents (Event[c[i]]);
			window.removeEventListener (c[i], this._capture, true);
		}
		this._capture = null;
	};
}


Bw = {};

Bw.Core =
{
	bind: function (el, proto)
	{
		var p = null;
		var n = null;
			
		try	{
			p = (typeof proto == 'string') ? eval(proto) : proto;
		}
		catch (e){}
		
		if (p && p.selfclassName)
		{
			p.selfclass = eval (p.selfclassName);
			if (p.superclassName)
			{
				p.superclass = eval (p.superclassName);
				Bw.Core.bind (el, p.superclassName);
			}
			
			for (e in p) el[e] = p[e];
			
			el.className = p.selfclassName.replace (/\./g, "");
			
			return p;
		}		
	},
	
	bootstrap: function (el, c)
	{
		var n = c || el.className;
		
		if (n && Bw.Core.bind(el, n) && !el.initialize()) return;
		
		var e = el.firstChild;
		while (e != null)
		{
			try
			{
				Bw.Core.bootstrap(e);
			}
			catch (ex){
			//	log (ex);
			}
			
			e = e.nextSibling;
		}
	},
	
	load: function (parent, url, force)
	{
		var q = Bw.IO.Query.create();
		if (force) q.setNoCache();
		q.get (url);
		parent.innerHTML = q.getText();
		
		Bw.Core.bootstrap (parent);
		Bw.Core.evalScript(parent.innerHTML, force);
	},
	
	evalScript : function (html, force)
	{
		var idx = html.toLowerCase().indexOf('<script');

		while(idx > -1)
		{
			var idxEnd = html.toLowerCase().indexOf('>', idx);
			
			var balise = html.substring(idx + 8, idxEnd);
			
			var idxSrc = balise.toLowerCase().indexOf("src=");
			
			if( idxSrc == -1 )
			{
				var idxSlashScript = html.toLowerCase().indexOf('</script>', idx + 8);
				
				var script = html.substring(idxEnd + 1, idxSlashScript);
				eval(script);
			}
			else
			{
				if( balise.charAt(idxSrc + 4) == '"' || balise.charAt(idxSrc + 4) == "'" )
				{
					idxSrc++;	
				}
				
				var idxSrcEnd = -1;
				
				for(var i = idxSrc + 4; i < balise.length ; i ++ )
				{
					if( balise.charAt(i) == ' ' || balise.charAt(i) == '"' || balise.charAt(i) == "'" )
					{
						idxSrcEnd = i;	
					}
				}
				
				var source = balise.substring(idxSrc + 4, idxSrcEnd);
				
				var q = Bw.IO.Query.create();
				if (force) q.setNoCache();
				q.get (source);
				if(q.getStatus() == 200)
				{
					eval(q.getText());
				}
			}
			
			idx = html.toLowerCase().indexOf('<script', idx + 8);
		}
	},
	
	check: function ()
	{
		var ss = document.styleSheets;
		var l = ss.length;
		for (var i = 0; i < l; i++)
		{
			var s = ss.item (i);
			if( s.href == null )
			{
				continue;
			}
			
			var p = s.href.indexOf ("Bw.css");
			if (p != -1)
			{
				var prefix = s.href.substring (0, p);
				Bw.themePath = prefix;
			}
		}
				
		if (Bw.themePath == null) {
			alert ("Bw.css MUST be linked to the page"); 
		}
	},
	
	start: function ()
	{
		if (self.dialogArguments) self.opener = self.dialogArguments;
		
		Bw.Core.check();
		Bw.Core.bootstrap(document.body);
	}
};

Bw.instanceOf = function (el, type)
{
	return (el && ((el.selfclass && el.selfclass == type) || (el.className && el.className == type.selfclassName)));
};

Bw.inherits = function (el, type)
{
	if (!el) return false;
	var t = el.selfclass;
	while (t)
	{
		if (t == type) return true;
		t = t.superclass;
	}
	return false;
};

Bw.getById = function (id)
{
	return document.getElementById (id);
};

Bw.getEvent = function (e)
{
	if (!e) {
		e = window.event;
		e.target = e.srcElement;
	}
		
	return e; 
};

Bw.getGlobalMousePosition = function (e)
{
	var p = { x: e.clientX, y: e.clientY };
	var d = document.documentElement;
	var b = document.body;
	var w = window;

	p.x += (w.scrollX) ? w.scrollX : (d.scrollLeft + b.scrollLeft);
	p.y += (w.scrollY) ? w.scrollY : (d.scrollTop + b.scrollTop);

	return p;
};


Array.prototype.lookup = function (o)
{
	var l = this.length;
	for (var i = 0; i < l; i++) {
		if (this[i] == o) return i;
	}
	return -1;
};

function log (str)
{
	var l = Bw.getById ("log");
	if (!l)
	{
		l = document.createElement("DIV");
		l.style.font="menu";
		l.style.clear="both";
		l.id = "log";
		document.body.appendChild (l);
	}
	l.innerHTML += (str + "<br>");
}

window.onload = function () { Bw.Core.start(); };

Bw.Xml = {};

Bw.Xml.Path = 
{
	cache: {},
	
	compile: function (path)
	{
		var code = "var n=r;";
		var start = 0;
		var end = false;
		
		while (!end)
		{
			var stop = path.indexOf('/', start);
			if (stop == -1) {
				stop = path.length;
				end = true;
			}
			
			var e = path.substring (start, stop);

			var a = null;
			var p = e.indexOf ('@');
			if (p != -1) {
				a = e.substring (p+1, e.length);
				e = e.substring (0, p);
			}

			var i = 0;
			var bo = e.indexOf ('[');
			if (bo != -1) 
			{
				var bc = e.indexOf (']', bo);
				if (bc != -1)
				{
					i = e.substring (bo+1,bc);
					e = e.substring (0,bo);
				}
			}
			
			if (e != "" || i != 0) {
				code=code+("n=Bw.Xml.Path._el(n,'"+e+"',"+i+",c);");
			}
			
			if (a != null && end) {
				code=code+("n=Bw.Xml.Path._at(n,'"+a+"',c);");
			}

			start=stop+1;
		}
		
		return new Function("r","c",code+"return n;");
	},
	
	navigate: function (path, root, create)
	{
		var c = Bw.Xml.Path.cache[path];
		if (!c || c == null) {
			c = Bw.Xml.Path.compile (path);
			Bw.Xml.Path.cache[path] = c;
		}
	
		return c(root,create);
	},

	_el: function (node, name, index, create)
	{	
		if (node == null) return null;
		if (name == '.' || name == '') return node;
		
		function arr (parent, name)
		{
			var n = [];
			var i = 0;
			var c = parent.firstChild;
			while (c != null)
			{
				if (c.nodeType == 1 && c.nodeName == name) n[i++] = c;
				c = c.nextSibling;
			}
			return n;
		};
	
		var nodes = arr (node, name);
		var l = nodes.length;

		if (index < l) return nodes[index];
		if (!create) return null;
		
		var gap = index - l + 1;
		var doc = node.ownerDocument;
		var n = null;
		for (var j = 0; j < gap; j++)
		{
			n = doc.createElement (name);
			node.appendChild (n);
		}
		
		return n;
	},

	_at: function (node, name, create)
	{	
		if (node == null) return null;
		var a = node.getAttributeNode (name);
		if (a != null || (a == null && !create)) {
			return a;
		}
		node.setAttribute (name, "");
		return node.getAttributeNode (name);
	}
};


Bw.Xml.Helpers = 
{
	getNodes: function (node, path)
	{
		var a = [];
		var n = Bw.Xml.Path.navigate (path, node, false);
		var c = n;
		while (c != null)
		{
			if (c.nodeName == n.nodeName) {
				a.push (c);
			}
			c = c.nextSibling;
		}
		
		return a;
	},

	getNode: function (node, path, create)
	{
		var tmp = Bw.Xml.Path.navigate (path, node, create);
		return tmp;
	},

	getNodeValue: function (node)
	{
		var c = node.childNodes;
		var l = c.length;
		var v = "";
		for (var i = 0; i < l; i++)
		{
			var n = c[i];
			var t = n.nodeType;
			if (t == 3 || t == 4) {
				v += n.nodeValue;
			}
		}
		
		return v;
	},

	setNodeValue: function (node, value)
	{
		if (!value) return;
		
		if (node.nodeType == 2)
		{
			node.value = value;
			return;
		}
		
		var c = node.childNodes;
		var l = c.length;
		for (var i = 0; i < l; i++)
		{
			var n = c[i];
			if (n.nodeType == 3)
			{
				if (value != null) n.data = value;
				else node.removeChild (n);
				return;
			}
		}
		
		if (value != null)
		{
			var f = node.firstChild;
			var t = node.ownerDocument.createTextNode (value);
			if (f == null) {
				node.appendChild (t);
			} else {
				node.insertBefore (t, f);
			}
		}
	},
	
	removeNode: function (node)
	{
		var n = node;
		if (n.nodeType == 2) {
			n.ownerElement.removeAttributeNode (n);
		} else {
			n.parentNode.removeChild (n);
		}
		return n;
	},

	importNode: function (dom, node, deep)
	{
		var n;

		if (node.nodeType == 1)
		{
			n = dom.createElement (node.nodeName);
			for (var i = 0; i < node.attributes.length; i++)
			{
				var attr = node.attributes[i];
				if (attr.nodeValue != null && attr.nodeValue != '')
				{
					n.setAttribute (attr.name, attr.nodeValue);
				}
			}
			
		}
		else if (node.nodeType == 3)
		{
			n = dom.createTextNode (node.nodeValue);
		}

		if (deep && node.hasChildNodes())
		{
			for (var i = 0; i < node.childNodes.length; i++) 
			{
				n.appendChild (Bw.Xml.Helpers.importNode (dom, node.childNodes[i], true));
			}
		}

		return n;
	},

	createDocument: function ()
	{
		var i = document.implementation;
		var d = (i.createDocument) ? i.createDocument ("","",null) : new ActiveXObject ("MSXML.DOMDocument");

		return d;
	},
	
	createHttpRequest: function ()
	{
		return (typeof ActiveXObject != "undefined") ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
};

Bw.IO = {};

Bw.IO.Query =
{
	superclassName: null,
	selfclassName: "Bw.IO.Query",
	
	create: function ()
	{
		var obj = new Object();
		
		Bw.Core.bootstrap (obj, this);
		
		return obj;
	},
	
	initialize: function ()
	{
		this.asyncCallback = null;
		this.headers = {};
		this.impl = Bw.Xml.Helpers.createHttpRequest();
	},
	
	get: function (url, callback)
	{
		this.transmit ("GET", url, null, callback);
	},
	
	post: function (url, obj, callback)
	{
		if (obj && obj.selfclass && obj.selfclass == Bw.IO.Message) obj = obj.dom;
		this.transmit ("POST", url, obj, callback);
	},

	transmit: function (method, url, obj, callback)
	{
		var async = (typeof callback != "undefined");
		
		if (async) {
			this.asyncCallback = callback;
		}
	
		this.impl.open (method, this.buildUrl (url), async);
	
		for (h in this.headers) {
			this.impl.setRequestHeader (h, this.headers[h]);
		}
	
		if (async) {
			var self = this;
			this.impl.onreadystatechange = function () { self.readyStateChanged(); };
		}
		
		this.impl.send (obj);
		
		window.status = "";
	},

	readyStateChanged: function ()
	{
		if (this.impl.readyState == 4)
		{
			window.status = "";
			this.asyncCallback ();
		}
	},
	
	setNoCache: function()
	{
		this.headers["If-Modified-Since"] = "Sat, 1 Jan 2005 00:00:00 GMT";
	},

	setHeader: function (name, value)
	{
		this.headers[name] = value;
	},

	getText: function ()
	{
		return this.impl.responseText;
	},

	getMessage: function ()
	{
		return Bw.IO.Message.createFromXml (this.impl.responseXML);
	},

	getXml: function ()
	{
		return this.impl.responseXML;
	},

	getStatus: function ()
	{
		return this.impl.status;
	},

	getStatusText: function ()
	{
		return this.impl.statusText;
	},

	buildUrl: function (url)
	{
		var l = document.location;
		if (url.indexOf ("/") == 0)
		{
			var p = l.port;
			var h = l.host;
			var u = l.protocol + "//" + h;
			if (p != null && p != "" && h.indexOf (p) < 0) {
				u += (":" + p);
			}
			return (u + url);
		} 
		else 
		{
			var l = l.toString();
			var p = l.lastIndexOf ("/");
			var u = l.substr (0, p);
			return (u + "/" + url);
		}
	},
	
	PostData: function ()
	{
		this.properties = {};
		this.toString = function ()
		{
			var s = '';
			for (name in this.properties)
			{
				if (s != '') s += '&';
				var value = this.properties[name];
				
				value = '' + value ;
				
				if( typeof value != 'string' )
				{
					continue;
				}	
				
				value = value.replace(/%/g, "%25");
				value = value.replace(/&/g, "%26");
				
				s += (name + '=' + value);
			}
			
			return s;
		};
	}
};


Bw.IO.Message = 
{
	superclassName: null,
	selfclassName: "Bw.IO.Message",
	
	create: function (name)
	{
		var obj = new Object();
		obj.name = (name) ? name : "message";
		
		Bw.Core.bootstrap (obj, this);
	
		return obj;
	},

	createFromXml: function (xml)
	{
		var obj = new Object();
		obj.dom = xml;
		
		Bw.Core.bootstrap (obj, this);
	
		return obj;
	},

	initialize: function ()
	{
		if (!this.dom)
		{
			var d = Bw.Xml.Helpers.createDocument();
			d.appendChild (d.createElement (this.name));
			this.dom = d;
		}
		
		this.root = this.dom;
		
		if (!this.name) {
			this.name = this.root.nodeName;
		}
	},

	changeRoot: function (path)
	{
		var n = Bw.Xml.Path.navigate (path, this.root, false);
		this.root = (node == null) ? this.dom : node;
	},
	
	put: function (path, value, from)
	{
		var f = (from) ? from : this.root;
		var n = Bw.Xml.Path.navigate (path, f, true);
		if (n) Bw.Xml.Helpers.setNodeValue (n, value);
		return n;
	},
	
	get: function (path, from)
	{
		var f = (from) ? from : this.root;
		var n = Bw.Xml.Path.navigate (path, f, false);
		return (!n) ? null : Bw.Xml.Helpers.getNodeValue (n);
	},
	
	getNodes: function (path, from)
	{
		var f = (from) ? from : this.root;
		return Bw.Xml.Helpers.getNodes (f, path);
	},

	getNode: function (path, from)
	{
		var f = (from) ? from : this.root;
		return Bw.Xml.Helpers.getNode (f, path, false);
	},
	
	count: function (path)
	{
		var n = Bw.Xml.Path.navigate (path, this.root, false);
		return (!n) ? -1 : Bw.Xml.Helpers.countHomonymNodes (n);
	},

	remove: function (path)
	{
		var n = Bw.Xml.Path.navigate (path, this.root, false);
		return (!n) ? null : Bw.Xml.Path.removeNode (n);
	}
};

Bw.Date =
{
	DEFAULT_DATE_FORMAT: "dd/mm/yyyy",
	WEEKS_STARTS_AT: 1,
	DAY_LABELS: [ "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche" ],
	SHORT_DAY_LABELS: [ "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim" ],
	MONTH_LABELS: [ "Janvier" ,"Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre" ]
};

Bw.Date.Format = 
{
	superclassName: null,
	selfclassName: "Bw.Date.Format",
	
	dfl_fmt: null,
	
	defaultFormat: function ()
	{
		if (this.dfl_fmt == null)
		{
			this.dfl_fmt = this.create();
		}
		return this.dfl_fmt;
	},
	
	create: function (fmt)
	{
		var obj = {};
		
		obj.fmt = (!fmt) ? Bw.Date.DEFAULT_DATE_FORMAT : fmt;
		
		Bw.Core.bootstrap (obj, "Bw.Date.Format");
		
		return obj;
	},
	
	initialize: function ()
	{
		this.act = [];
		
		var f = this.fmt;
		var a = this.act;
		var l = f.length;
		var prev = null;
		
		for (var i = 0; i < l; i++)
		{
			var c = f.charAt (i);
			
			if (c != prev)
			{
				var k = a.length;
				a[k] = new Array();
				a[k][0] = c;
				a[k][1] = i;
				a[k][2] = 1;
			}
			else
			{
				a[a.length-1][2]++;
			}
	
			prev = c;
		}
	},
	
	pad: function (v, len)
	{
		var l = v.length;
		if (l <= len) 
		{
			for (var i = l; i < len; i++)
			{
				v = '0' + v;
			}
			return v;
		}
	}
};

Bw.Date.Helpers = 
{	
	read: function (str, fmt)
	{
		if (!fmt) fmt = Bw.Date.Format.defaultFormat();

		var d = null;
		var m = null;
		var y = null;
		var c = fmt.act;
		var l = c.length;
	
		for (var i = 0; i < l; i++)
		{
			var a = c[i];
			var typ = a[0];
			var beg = a[1];
			var end = beg + a[2];
			
			var s = str.substring (beg, end);
			var n = new Number (s);
			n = n.valueOf();
			if (typ == 'd') d = n;
			else if (typ == 'm') m = n;
			else if (typ == 'y') y = n;
		}
	
		return new Date (y, m-1, d);
	},
	
	write: function (date, fmt)
	{
		if (!fmt) fmt = Bw.Date.Format.defaultFormat();

		var d = ''+(date.getDate());
		var m = ''+(date.getMonth() + 1);
		var y = ''+(date.getFullYear());
	
		var c = fmt.act;
		var l = c.length;
		
		var s = '';
		
		for (var i = 0; i < l; i++)
		{
			var a = c[i];
			var typ = a[0];
			var len = a[2];
			
			if (typ == 'd')
			{
				s += fmt.pad (d, len);
			} 
			else if (typ == 'm')
			{
				s += fmt.pad (m, len);
			} 
			else if (typ == 'y')
			{
				s += fmt.pad (y, len);
			}
			else
			{
				for (var j = 0; j < len; j++)
				{
					s += typ;
				}
			}
		}
		return s;
	},
	
	addMinutes: function (date, m)
	{
		date.setMinutes (date.getMinutes() + m);
		return date;
	},
	
	addHours: function (date, h)
	{
		date.setHours (date.getHours() + h);
		return date;
	},
	
	addDays: function (date, d)
	{
		date.setDate (date.getDate() + d);
		return date;
	},
	
	addMonths: function (date, m)
	{
		date.setMonth (date.getMonth() + m);
		return date;
	},
	
	addYears: function (date, y)
	{
		date.setFullYear (date.getFullYear() + y);
		return date;
	},
	
	prevWeek: function (date)
	{
		return this.addDays (date, -7);
	},
	
	nextWeek: function (date)
	{
		return this.addDays (date, 7);
	},
	
	weekStart: function (date)
	{
		var d = this.getDay (date);
		
		this.addDays (date, -d);
		
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		date.setMilliseconds(0);

		return date;
	},
	
	weekEnd: function (date)
	{
		var d = this.getDay (date);
		
		this.addDays (date, 6 - d);
		
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		date.setMilliseconds(999);

		return date;
	},
	
	monthStart: function (date)
	{
		date.setDate (1);
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		date.setMilliseconds(0);
		
		return date;
	},
	
	monthEnd: function (date)
	{
		this.monthStart (date);
		this.addMonths (date, 1);
		this.addDays (date, -1);
		
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		date.setMilliseconds(999);
		
		return date;
	},
	
	getDay: function (date)
	{
		var d = date.getDay() - Bw.Date.WEEKS_STARTS_AT;
		return (d < 0) ? 7 + d : d;
	},
	
	weeksInMonth: function (date)
	{
		var d = new Date (date);
		
		this.monthStart (d);
		var m = d.getMonth();
		
		var i = 0;
		while (d.getMonth() == m)
		{
			this.nextWeek (d);
			if (i == 0) this.weekStart (d);
			i++;
		}
			
		return i;
	}
};

Bw.Util =
{
	findParentElement: function (element, parentNodeName, parentClassName, parentId)
	{
		var e = element.parentNode;
		while (e != null)
		{
			var n = (e.nodeName == parentNodeName);
			var c = (e.className && e.className.indexOf (parentClassName) == 0);
			var i = (e.id == parentId);
			
			if (((parentNodeName != null && n) || (parentNodeName == null && !n)) &&
			((parentClassName != null && c) || (parentClassName == null && !c)) &&
			((parentId != null && i)  || (parentId == null && !i)))
			{
				return e;
			}
			
			e = e.parentNode;
		}
		
		return null;
	},
	
	findContainerWidget: function (element, widgetClass)
	{
		var e = element.parentNode;
		while (e != null)
		{
			if (e.selfclass)
			{
				if (!widgetClass)
				{
					return e;
				}
				else
				{
					if (e.selfclass == widgetClass)
					{
						return e;
					}
				}
			}
	
			e = e.parentNode;
		}
		
		return null;
	},
	
	getElementPosition: function (e)
	{
		var p = {};
		p.x = e.offsetLeft;
		p.y = e.offsetTop;
		
	    var c = e.offsetParent;
	    while (c)
	    {
	        p.x += c.offsetLeft;
	        p.y += c.offsetTop;
	        
	        c = c.offsetParent;
	    }
	    
	    return p;
	},
	
	getElementTopPosition: function (e)
	{
		var t = e.offsetTop;
	    var p = e.offsetParent;
	    while (p) {
	        t += p.offsetTop;
	        p = p.offsetParent;
	    }
	    return t;
	},
	
	getElementLeftPosition: function (e)
	{
		var l = e.offsetLeft;
	    var p = e.offsetParent;
	    while (p) {
	        l += p.offsetLeft;
	        p = p.offsetParent;
	    }
	    return l;
	},
	
	getElementLeftScrollPosition: function (e)
	{
		var l = e.scrollLeft;
	    var p = e.offsetParent;
	    while (p) {
	        l += p.scrollLeft;
	        p = p.offsetParent;
	    }
	    return l;
	},
	
	getElementTopScrollPosition: function (e)
	{
		var l = e.scrollTop;
	    var p = e.offsetParent;
	    while (p) {
	        l += p.scrollTop;
	        p = p.offsetParent;
	    }
	    return l;
	},
	
	forceRedraw: function(el)
	{
		if (!el) el = document.body;
		if (navigator.product == "Gecko")
		{
			el.style.display = "inline";
			setTimeout (function() { el.style.display = "block"; }, 1);
		}
	},
	
	call: function (obj, func)
	{
		if (!obj || !func) return;
		if (typeof func == 'function') return func.call (obj);
		else return eval(func);
	}
};
	
Bw.Widgets = {};

Bw.Widgets.Widget = 
{	
	superclassName: null,
	selfclassName: "Bw.Widgets.Widget",
	
	initialize: function ()
	{
		this.oncontextmenu = function () { return false; };
		return false;
	},
	
	getContainer: function (containerClass, terminal)
	{
		var e = this.parentNode;
		while (e != null)
		{
			if (e.selfclass)
			{
				if (!containerClass) return e;
				else if (terminal && Bw.instanceOf (e, containerClass)) return e;
				else if (!terminal && Bw.inherits (e, containerClass)) return e;
			}
	
			e = e.parentNode;
		}
		
		return null;
	},
	
	isContainedBy: function (el)
	{
		var p = this;
		while (p != null)
		{
			if (p == el)
			{
				return true;
			}
			p = el.parentNode;
		}
		return false;
	},
	
	setSize: function (w, h)
	{
		this.style.width = w + "px";
		this.style.height = h + "px";
	},
	
	getGlobalPosition: function ()
	{
		var t = this.offsetTop;
		var l = this.offsetLeft;
		
	    var p = this.offsetParent;
	    while (p) 
		{
	        t += p.offsetTop;
	       	l += p.offsetLeft;
	        p = p.offsetParent;
	    }
	    
	    return { x: l, y: t};
	},
	
	reparentChildren: function (to)
	{
		var f;
		while ((f = this.firstChild) != null)
		{
			this.removeChild (f);
			if (to) to.appendChild (f);
		}
	},
	
	enable: function ()
	{
		this.disabled = false;
	},

	disable: function ()
	{
		this.disabled = true;
	},
	
	show: function ()
	{
		this.style.display = "block";
	},
	
	hide: function ()
	{
		this.style.display = "none";
	},
	
	visible: function ()
	{
		return (this.style.display != "none");
	}
};

Bw.Widgets.Image =
{
	superclassName: "Bw.Widgets.Widget",
	selfclassName: "Bw.Widgets.Image",
		
	create: function (src)
	{
		var obj = document.createElement ("IMG");
	
		if (src) {
			obj.setAttribute ("source", src);
		}
	
		Bw.Core.bootstrap (obj, "Bw.Widgets.Image");
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.url = null;
		
		var s = this.style;
		s.MozUserSelect="none";
		s.MozUserInput="disabled";

		var u = this.getAttribute ("source");
		if (u) {
			this.setSource (u);
		}
		
		return false;
	},
	
	draw: function ()
	{
		var png = (this.url.toLowerCase().indexOf(".png") != -1);

		if (document.all && png)
		{
			this.src = Bw.themePath + Bw.Widgets.StockIcon.storagePath + "none.gif";
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.url + "')";
		}
		else
		{
			this.src = this.url;
		}
	},

	getSource: function ()
	{
		return this.url;
	},

	setSource: function (src)
	{
		this.url = src;
		this.draw();
	}
};


Bw.Widgets.StockIcon =
{
	superclassName: "Bw.Widgets.Image",
	selfclassName: "Bw.Widgets.StockIcon",
	
	storagePath: "icons/",
	
	create: function (src)
	{
		var obj = document.createElement ("IMG");
	
		if (src) obj.setAttribute ("source", src);
	
		Bw.Core.bootstrap (obj, "Bw.Widgets.StockIcon");
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Image.initialize.call (this);
		
		this.url = null;
		
		var s = this.style;
		s.MozUserSelect="none";
		s.MozUserInput="disabled";

		var u = this.getAttribute ("source");
		if (u) this.setSource (u);
		
		return false;
	},
	
	draw: function ()
	{
		var p = Bw.themePath + this.storagePath;
		
		if (this.url == "none")
		{
			this.src = p + "none.gif";
			if (document.all) this.style.filter = "";
		}
		else
		{
			if (document.all)
			{
				this.src = p + "none.gif";
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + p + this.url + ".png')";
			} 
			else
			{
				this.src = p + this.url + ".png";
			}
		}
	}
};


Bw.Widgets.Label =
{
	superclassName: "Bw.Widgets.Widget",
	selfclassName: "Bw.Widgets.Label",
	
	create: function (val)
	{
		var obj = document.createElement ("div");
		
		obj.setAttribute ("value", val);
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Label");
		
		return obj;
	},

	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call(this);
		
		with (this.style)
		{
			display = "inline";
			MozUserSelect = "none";
			cursor = "default";
		}
		
		this.onselectstart = function(){return false;};

		var a = this.getAttribute ("value");
		if (a != null) {
			this.setValue (a);
		}
		
		return false;
	},
	
	getValue: function ()
	{
		return this.innerHTML;
	},
	
	setValue: function (val)
	{
		this.innerHTML = val;
	}
};

Bw.Widgets.Calendar = 
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Calendar",
	
	dates :{},
	
	create: function (date)
	{
		var obj = document.createElement ("div");
	
		if (date) obj.setAttribute ("date", date);
		
		Bw.Core.bootstrap (obj, this.selfclassName);
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.calendar = null;
			
		var d = this.getAttribute ("date");
		this.currDate = (d) ? Bw.Date.Helpers.read (d) : new Date ();
		
		this.draw();
		
		this.onaction = this.getAttribute ("onaction");
		this.onselect = this.getAttribute ("onselect");
		
		return false;
	},
	
	draw: function ()
	{
		var r = this.drawHeader();
		this.appendChild (r);
		
		r = this.drawDays();
		this.appendChild (r);
		
		r = this.drawMonth();
		this.appendChild (r);
		
		this.month = r;
	},
	
	redraw: function ()
	{
		this.monthLabel.setValue (Bw.Date.MONTH_LABELS[this.currDate.getMonth()]);
		this.yearLabel.setValue (this.currDate.getFullYear());
		
		var m = this.month;
		if (m) this.removeChild (m);
		
		m = this.drawMonth();
		this.appendChild (m);
		
		this.month = m;
	},
	
	drawHeader: function ()
	{
		var self = this;
		
		var r = document.createElement ("div");
		r.className = "controls";
		
		var b = document.createElement ("div");
		b.className = "control";
		r.appendChild (b);
		
		var e = document.createElement ("div");
		b.appendChild (e);
		
		var yl = Bw.Widgets.StockIcon.create ("leftleft");		
		e.appendChild (yl);
		
		var ml = Bw.Widgets.StockIcon.create ("left");
		e.appendChild (ml);
		
		var b = document.createElement ("div");
		b.className = "display";
		r.appendChild (b);

		var mm = Bw.Widgets.Label.create (Bw.Date.MONTH_LABELS[this.currDate.getMonth()]);
		var sp = Bw.Widgets.Label.create ("&nbsp;");
		var yy = Bw.Widgets.Label.create (this.currDate.getFullYear());
		b.appendChild (mm);
		b.appendChild (sp);
		b.appendChild (yy);
		
		this.monthLabel = mm;
		this.yearLabel = yy;
		
		var b = document.createElement ("div");
		b.className = "control";
		r.appendChild (b);
	
		var e = document.createElement ("div");
		b.appendChild (e);
		
		var mr = Bw.Widgets.StockIcon.create ("right");
		e.appendChild (mr);
		
		var yr = Bw.Widgets.StockIcon.create ("rightright");
		e.appendChild (yr);
		
		yl.onclick = function(){ self.changeYear (-1); };
		ml.onclick = function(){ self.changeMonth(-1); };
		mr.onclick = function(){ self.changeMonth(1); };
		yr.onclick = function(){ self.changeYear (1); };
		
		return r;
	},
	
	drawRow: function ()
	{
		var r = document.createElement ("div");
		r.className = "row";
		
		for (var i = 0; i < 7; i++)
		{
			var c = document.createElement ("div");
			c.className = "cell";
			r.appendChild (c);
			
			var v = document.createElement ("div");
			v.className = "cellValue";
			c.appendChild (v); 
		}
		
		return r;
	},
	
	drawDays: function ()
	{
		var r = this.drawRow ();
		r.className += " weekDays";
		for (var i = 0; i < 7; i++)
		{
			var c = r.childNodes[i].firstChild;
			var l = Bw.Widgets.Label.create (Bw.Date.SHORT_DAY_LABELS[i]);
			c.appendChild (l);
		}
		
		return r;
	},
	
	drawMonth: function ()
	{
		var self = this;
		
		var cal = new Date();
		cal.setDate (1);
		cal.setMonth (this.currDate.getMonth());
		cal.setFullYear (this.currDate.getFullYear());
		
		var startDate = new Date (cal);
		var startOffset = Bw.Date.Helpers.getDay (startDate);
		
		var m = document.createElement ("div");
		var r;
		
		var q = Bw.IO.Query.create();
		
		q.get(Util.contextPath + "templates/agenda_mois.php?annee=" + cal.getFullYear() + "&mois=" + (cal.getMonth() + 1));
		eval(q.getText());
		
		this.dates = dates;
		
		while (cal.getMonth() == this.currDate.getMonth())
		{
			var d = cal.getDate();
			var j = Bw.Date.Helpers.getDay (cal);
			if (j == 0 || !r)
			{
				r = this.drawRow();
				m.appendChild (r);
			}
			
			var v = r.childNodes[j].firstChild;
			var l = Bw.Widgets.Label.create (d);
			v.onclick = function () { self.changeDay (this.firstChild.getValue()) };
			v.ondblclick = function () { if (self.onaction) return Bw.Util.call (self, self.onaction); };
			v.appendChild (l);
			if (d == this.currDate.getDate())
			{
				v.className += " selected";
			}
			else if( dates[Bw.Date.Helpers.write(cal)] == 1  )
			{
				v.className += " event";				
			}
			
			Bw.Date.Helpers.addDays (cal, 1);
		}
		
		var stopDate = new Date (cal);
		var stopOffset = Bw.Date.Helpers.getDay (stopDate);
		
		r = m.firstChild;
		for (var i = startOffset - 1; i >= 0; i--)
		{
			Bw.Date.Helpers.addDays (startDate, -1);

			var c = r.childNodes[i];
			c.style.backgroundColor = "#e5e6c7";
			//c.style.color = "#bbbbbb";
			
			var l = Bw.Widgets.Label.create (startDate.getDate());
			c.firstChild.appendChild (l);
		}
		
		r = m.lastChild;
		for (var i = stopOffset; i != 0 && i < 7; i++)
		{
			var c = r.childNodes[i];
			c.style.backgroundColor = "#e5e6c7";
			//c.style.color = "#bbbbbb";
			
			var l = Bw.Widgets.Label.create (stopDate.getDate());
			c.firstChild.appendChild (l);
			
			Bw.Date.Helpers.addDays (stopDate, 1);
		}
		
		return m;
	},
	
	changeDay: function (d)
	{
		var date = new Date (this.currDate);
		date.setDate (d);
		
		var c = this.dateToCell (this.currDate);
		c.className = "cellValue";
		
		if( this.dates[Bw.Date.Helpers.write(this.currDate)] == 1 )
		{
			c.className += " event";	
		}

		c = this.dateToCell (date);
		c.className = "cellValue";
		c.className += " selected";
		
		this.currDate = date;
		
		this.selectionChanged();
	},
	
	getValue: function ()
	{
		return this.currDate;
	},
	
	setValue: function (d)
	{
		var c = this.currDate;

		if (d.getMonth() != c.getMonth() || d.getFullYear() != c.getFullYear())
		{
			this.currDate = d;
			this.redraw ();
		}
		else
		{
			this.changeDay (d.getDate());
		}
	},
	
	dateToCell: function (t)
	{
		var d = new Date (t);
		var j = d.getDate() - 1;
		var s = Bw.Date.Helpers.monthStart (new Date (d));
		var o = Bw.Date.Helpers.getDay (s);
		
		var y = Math.floor (j / 7);
		var x = j - (y * 7) + o;
		if (x > 6)
		{
			x -= 7;
			y++;
		}
	
		return this.month.childNodes[y].childNodes[x].firstChild;
	},
	
	changeYear: function (o)
	{
		this.setValue (Bw.Date.Helpers.addYears (new Date(this.currDate), o));
 		this.selectionChanged();
	},
	
	changeMonth: function (o)
	{
		this.setValue (Bw.Date.Helpers.addMonths (new Date(this.currDate), o));
 		this.selectionChanged();
	},
	
	selectionChanged: function ()
	{
		if (this.onselect) return Bw.Util.call (this, this.onselect);
	}
};




