function SortableTable (tableEl) {
	var tempo = 0;
	function print_r(theObj){
	  if(theObj.constructor == Array ||
		 theObj.constructor == Object){
		document.write("<ul>")
		for(var p in theObj){
		  if(theObj[p].constructor == Array||
			 theObj[p].constructor == Object){
	document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
			document.write("<ul>")
			print_r(theObj[p]);
			document.write("</ul>")
		  } else {
	document.write("<li>["+p+"] => "+theObj[p].innerHTML+"</li>");
		  }
		}
		document.write("</ul>")
	  }
	}

	
 
	this.tbody = tableEl.getElementsByTagName('tbody');
	this.thead = tableEl.getElementsByTagName('thead');
	this.tfoot = tableEl.getElementsByTagName('tfoot');
 
	this.getInnerText = function (el) {
		if (typeof(el.textContent) != 'undefined') return el.textContent;
		if (typeof(el.innerText) != 'undefined') return el.innerText;
		if (typeof(el.innerHTML) == 'string') return el.innerHTML.replace(/<[^<>]+>/g,'');
	}
 
	this.getParent = function (el, pTagName) {
		if (el == null) return null;
		else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
			return el;
		else
			return this.getParent(el.parentNode, pTagName);
	}
 
	this.sort = function (cell) {
 
	    var column = cell.cellIndex;
		var newRows = new Array();
		var newRows2 = new Array();
		var itm = new Array();
	
		for (k=0;k<this.tbody.length;k++) 
		{
			if(column == 1)
			{
				tempo = tempo+1;
				if (tempo == 1)
				{
					cell.setAttribute('sortdir','down');
				}
				itm[k] = this.tbody[k].rows[0].getElementsByTagName("input")[0].value;
			}
			else
			{
				tempo = 0;
				itm[k] = this.getInnerText(this.tbody[k].rows[0].cells[column]);
			} 
			
			var sortfn = this.sortCaseInsensitive;
			
			if (itm[k].match(/(\d{2})\s(.{1,9})\s(\d{2})/))
			{
				sortfn = this.sortDate;
			}
			if (itm[k].replace(/^\s+|\s+$/g,"").match(/^[\d\.]+$/))
			{
				sortfn = this.sortNumeric;
			}		
		
			this.sortColumnIndex = column;
			
			newRows[k] = this.tbody[k].rows[0];
			newRows2[k] = this.tbody[k];
		}
		
		newRows.sort(sortfn);
		
		if (cell.getAttribute("sortdir") == 'down') 
		{
			newRows.reverse();
			cell.setAttribute('sortdir','up');
			for (j=0;j<tableEl.getElementsByTagName('th').length;j++) 
			{
				tableEl.getElementsByTagName("img")[j].style.display = "none";
			}
			tableEl.getElementsByTagName("img")[column].style.display= "";
			tableEl.getElementsByTagName("img")[column].src = "/templates/cinemas/images/arrow_down.png";
		} 
		else 
		{
			cell.setAttribute('sortdir','down');
			for (j=0;j<tableEl.getElementsByTagName('th').length;j++) 
			{
				tableEl.getElementsByTagName("img")[j].style.display = "none";
			}
			tableEl.getElementsByTagName("img")[column].style.display = "";
			tableEl.getElementsByTagName("img")[column].src = "/templates/cinemas/images/arrow_up.png";
		}
		
		
		var expDate = new Date();
		expDate.setTime(expDate.getTime() + (7 * 24 * 3600 * 1000));
		document.cookie = "sDirection="+escape(cell.getAttribute("sortdir")) + ";expires=" + expDate.toGMTString();
		if(column == 2)
		{
			document.cookie = "sOrdreCritique" + "=" + escape("date") + ";expires=" + expDate.toGMTString();
		}
		else if(column == 1)
		{
			document.cookie = "sOrdreCritique" + "=" + escape("critique") + ";expires=" + expDate.toGMTString();
		}
		else if(column == 0)
		{
			document.cookie = "sOrdreCritique" + "=" + escape("") + ";expires=" + expDate.toGMTString();
		}
		
		var sTemp;
		for (i=0;i<newRows.length;i++) 
		{
			for (j=0;j<newRows2.length;j++) 
			{
				if(newRows[i].innerHTML == newRows2[j].rows[0].innerHTML)
				{
					sTemp = newRows2[j];
					newRows2[j] = newRows2[i];
					newRows2[i] = sTemp;
				}
			}
			tableEl.insertBefore(newRows2[i],this.tbody[i]);
		}
		
		var iTempo = 0;
		var iCount = 0;
		for (k=0;k<this.tbody.length;k++) 
		{
			if(iTempo == 0)
			{
				iTempo = 1;
				this.tbody[k].rows[0].setAttribute("bgColor",'#BoxColorGeneral');
				this.tbody[k].rows[1].setAttribute("bgColor",'#BoxColorGeneral');
			}
			else
			{
				iTempo = 0;
				this.tbody[k].rows[0].setAttribute("bgColor",'#BoxColorGeneral');
				this.tbody[k].rows[1].setAttribute("bgColor",'#BoxColorGeneral');
			}
			
			if(iCount < 10){
				this.tbody[k].setAttribute("style","display:;");
			}else{
				this.tbody[k].setAttribute("style","display:none;");
			}
			iCount++;
		}
 
	}
 
	this.sortCaseInsensitive = function(a,b) {
		aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]).toLowerCase();
		bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]).toLowerCase();
		if (aa==bb) return 0;
		if (aa<bb) return -1;
		return 1;
	}
 
	this.sortDate = function(a,b) {
	
		
		var asMounth = new Array();
			asMounth["janvier"]="01";
			asMounth["février"]="02";
			asMounth["mars"]="03";
			asMounth["avril"]="04";
			asMounth["mai"]="05";
			asMounth["juin"]="06";
			asMounth["juillet"]="07";
			asMounth["août"]="08";
			asMounth["septembre"]="09";
			asMounth["octobre"]="10";
			asMounth["novembre"]="11";
			asMounth["décembre"]="12";
			
		sA = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]);
		sB = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]);
		
		aa = asMounth[sA.substr(sA.indexOf(" ",1), sA.indexOf(" ",4)-sA.indexOf(" ",1)).replace(' ', '')]+"-"+sA.substr("0", "3").replace(' ', '')+"-20"+sA.substr(sA.indexOf(" ",4), sA.length-sA.indexOf(" ",4)).replace(' ', '');
		bb = asMounth[sB.substr(sB.indexOf(" ",1), sB.indexOf(" ",4)-sB.indexOf(" ",1)).replace(' ', '')]+"-"+sB.substr("0", "3").replace(' ', '')+"-20"+sB.substr(sB.indexOf(" ",4), sB.length-sB.indexOf(" ",4)).replace(' ', '');
		
		date1 = aa.substr(6,4)+aa.substr(0,2)+aa.substr(3,2);
		date2 = bb.substr(6,4)+bb.substr(0,2)+bb.substr(3,2);
		
		if (date1==date2) return 0;
		if (date1<date2) return -1;
		return 1;
	}
 
	this.sortNumeric = function(a,b) {
		aa = parseFloat(a.getElementsByTagName("input")[0].value);
		if (isNaN(aa)) aa = 0;
		bb = parseFloat(b.getElementsByTagName("input")[0].value);
		if (isNaN(bb)) bb = 0;
		return aa-bb;
	}
 
	// define variables
	var thisObject = this;
	var sortSection = this.thead;
 
	// constructor actions
	if (!(this.tbody && this.tbody[0].rows && this.tbody[0].rows.length > 0)) return;
 
	if (sortSection && sortSection[0].rows && sortSection[0].rows.length > 0) {
		var sortRow = sortSection[0].rows[0];
	} else {
		return;
	}
 
	for (var i=0; i<sortRow.cells.length; i++) {
		sortRow.cells[i].sTable = this;
		sortRow.cells[i].onclick = function () {
			selectHeaderClick(this);
			this.sTable.sort(this);
			return false;
		}
	}
 
}