var ajax_widget = function ()
	{
	this.name=null;
	this.container=null;
	this.content=null;
	this.config=null;
	this.data=null;
	this.current=null;
	this.update_browser=false;

	/* -------- INITIALIZATION ----- */

	this.init=function()
		{
		this.initWidget();
		if(this.content)
			{
			if((this.content.hasAttribute&&this.content.hasAttribute('title'))||((!this.content.hasAttribute)&&this.content.getAttribute('title')))
				{
				this.config = new Array();
				this.initConfig(this.content.getAttribute('title').split('|'));
				this.content.setAttribute('title','')
				this.requestData();
				}
			else
				{
				this.error('No config given by the title attribute of the content div');
				}
			}
		};

	/* -------- WIDGET ----- */

	this.display=function()
		{
		};

	this.notice=function(mess)
		{
		this.content.innerHTML='<p id="ajax_notice">'+mess+'</p>';
		};

	this.error=function(err)
		{
		this.content.innerHTML='<p id="ajax_error">'+err+'</p>';
		};

	this.wait=function()
		{
		this.content.innerHTML='<p style="text-align:center;"><img src="http://cdn.planete-chr.com/images/widgets/widgets-loader.gif" alt="chargement..." /></p>';
		};

	/* -------- DATA ----- */
	
	this.requestJSonData=function(src)
		{
		var script = document.createElement('script');
		script.setAttribute('type','text/javascript');
		script.setAttribute('src',src);
		this.content.parentNode.appendChild(script);
		this.wait();
		};

	this.requestXmlData=function(src)
		{
		xhr = new XMLHttpRequest();
		xhr.open('GET',src);
		xhr.send(null);
		var handler = function (obj,method)
			{
			var fx = method;
			return function () { return fx.apply(obj, arguments); }
			}
		xhr.onreadystatechange = handler(this, this.handleResponse);
		this.wait();
		}

	/* -------- NAVIGATION ----- */

	this.setFirstItem=function()
		{
		this.current = 0;
		if (location.hash!=''&&((!this.name)||location.hash.indexOf(this.name)>0))
			{
			this.current=parseInt(location.hash.substr(1 + (this.name?this.name.length:0)));
			}
		this.loadCurrentItem();
		};

	this.goNextItem=function()
		{
		this.current++;
		if (this.current == this.data.length) this.current = 0;
		this.loadCurrentItem();
		this.updateBrowserLocation();
		};

	this.goPreviousItem=function()
		{
		this.current--;
		if (this.current < 0) this.current = this.data.length - 1;
		this.loadCurrentItem();
		this.updateBrowserLocation();
		};

	this.getCurrentCount=function()
		{
		return (this.current + 1) + "/" + this.data.length;
		};

	/* ---- BROWSER ---- */
	
	this.updateBrowserLocation=function()
		{
		if(this.config['update_browser'])
			{
			location.hash = '#' + (this.name?this.name:'') + this.current;
			}
		};
	};

function utf8_decode(utftext)
	{
	if(utftext)
		{
		var string = "";
		var i = 0; var c = 0; var c2 = 0; var c3 = 0;
		while ( i < utftext.length )
			{
			c = utftext.charCodeAt(i);
			if (c < 128)
				{
				string += String.fromCharCode(c);
				i++;
				}
			else if((c > 191) && (c < 224))
				{
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
				}
			else
				{
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
				}
			}
			return string;
		}
	else
		return "";
	}