// JavaScript Document



////////////////////////////////////////////////////////
//                                            		  //
//               For the search panel			      //
//                                             		  //
////////////////////////////////////////////////////////

function	DrawSearchPanel(td) {
	if (!td)
		return ;
	var div = td.firstChild? td.insertBefore(dc('div'), td.firstChild): td.appendChild(dc('div'));
	var absdiv = div.appendChild(dc('div'));
	absdiv.style.right = '0px';
	var search_box = absdiv.appendChild(dc('div'));
	
	with (search_box.style) {textAlign='right';verticalAlign='bottom';padding='0px';}
	var table = search_box.appendChild(dc('table'));
	with(table.style){height='32px';}
	table.style.background='url('+GLOBALS.ROOT+GLOBALS.PICT_FOLDER+'search_left.gif) left bottom repeat-y';
	var tbody = table.appendChild(dc('tbody'));
	var tr = tbody.appendChild(dc('tr'));

	var td = tr.appendChild(dc('td'));
	with(td.style){height='32px';width='183px';verticalAlign='middle';paddingLeft='0px';textAlign='left'}
//	td.style.background='url('+GLOBALS.ROOT+GLOBALS.PICT_FOLDER+'ensavoirplus.gif) no-repeat';
	td.style.backgroundColor = "#990000"; 
	
	var kmtable = td.appendChild(dc('table'));
	
	kmtable.className = 'knowmore'
	kmtable.style.width = '100%';
	var kmtbody = kmtable.appendChild(dc('tbody'));
	var kmtr = kmtbody.appendChild(dc('tr'));
	var kmtd = kmtr.appendChild(dc('td'));
	kmtd.className = 'clickable';
	kmtd.onclick = function(e) {
		window.location = GLOBALS.ROOT+'?r';
	}
	
	
	/*
	*/
	
	BodySearchResult = tbody;
	var prev = GetCookie('q');
	var kmtd = kmtr.appendChild(dc('td'));
	var search_input = kmtd.appendChild(MakeInput({name: 'search', tbody: tbody, value: prev? prev: ''}));
	
	MakeAutoText(search_input, GLOBALS.lang=='EN'? 'search ...': 'rechercher ...');
	SearchInput = search_input;
	search_input.onkeypress = function(e) {
		if(GetKeyCode(e)==13) LaunchSearch(this.value);
	}
	search_input.ondblclick = function(e) {
		LaunchSearch(this.value);
	}
	var kmtd = kmtr.appendChild(dc('td'));
	kmtd.style.paddingRight = '10px';
	var search_button = kmtd.appendChild(dc('img'));
	search_button.src = GLOBALS.ROOT+GLOBALS.PICT_FOLDER+'SearchButton_Left.gif';
	search_button.className = 'clickable';
	search_button.onclick = function(e) {
		LaunchSearch(this.parentNode.parentNode.getElementsByTagName('input')[0].value);
	}
	
	var advsearch = search_box.appendChild(dc('div'));
	advsearch.style.position = 'absolute';
	advsearch.style.left = '200px';
	advsearch.style.top = '0px';
/*	var kmtd = rowtr.appendChild(dc('td'));
	kmtd.style.width = '10px';
	
	var kmtd = rowtr.appendChild(dc('td'));
	kmtd.style.width = '108px';
*/		
//	var img = advsearch.appendChild(dc('img'));
//	img.src = GLOBALS.ROOT+GLOBALS.PICT_FOLDER+'Advanced_Search.gif';
	//var kmtd = kmtr.appendChild(dc('td'));
	//kmtd.innerHTML += 'Recherche<br /><b>avanc&eacute;e</b>';
	advsearch.className = 'clickable advsearchbt';
	advsearch.onclick = function(e) {
		window.location = GLOBALS.ROOT+'?r';
	}


}


function	AdjustSearchSize() {
	var beg = DivSearchResult.src_height;
	var end = DivSearchResult.dst_height;
	if (DivSearchResult.cur_i == 100) {
		DivSearchResult.src_height = end;
		DivSearchResult.style.height = end+'px';
		return ;	
	}
	DivSearchResult.cur_i += 2;
	var newh = 1-((Math.cos(DivSearchResult.cur_i*Math.PI/100)/2)+0.5);

	newh = parseInt(beg+(newh*(end-beg)));
	DivSearchResult.style.height = newh+'px';

	try {
		DivSearchResult.firstChild.style.height = (newh<7? 0: newh-7)+'px';
	}catch(e){/*alert(e.message);*/}
	setTimeout('AdjustSearchSize()', 10);
}
var DivSearchResult = false;
var BodySearchResult = false;
var SearchText = '';
var SearchXmlRequest = new XmlRequest();
var SearchInput = false;
String.prototype.replaceAll = function(from, to) {
	var s = this;
	if (to.toLowerCase().indexOf(from) != -1) {
		var bef = to.substr(0, to.toLowerCase().indexOf(from));
//		var repl = to.substr(to.toLowerCase().indexOf(from), to.length);
		var aft = to.substr(to.toLowerCase().indexOf(from)+from.length);
		var dst = '';
		while (s) {
			var pos = s.toLowerCase().indexOf(from);
			if (pos == -1) {
				dst += s;
				break;
			}
			dst += s.substr(0, pos)+bef+s.substr(pos,from.length)+aft;
			s = s.substr(pos+from.length);
		}
		return (dst);
	}
	else {
		while (s.indexOf(from) != -1)
			s = s.replace(from, to);
	}
	return (s);
}
function	ParseXmlObjectList(xml) {
	var list=[];
	var total = 0;
	if (!xml)
		return (list);
	for (node=xml.firstChild;node;node=node.nextSibling){
		if(node.tagName=='found')total=getXmlAttribute(node,'total');
		if(node.tagName!='node')continue;
		var name = getXmlAttribute(node,'name');
		var obj={
			ClassType:getXmlAttribute(node,'class'),
			id:getXmlAttribute(node,'id'),
			link:getXmlAttribute(node,'link'),
			name:name? name.replaceAll('&quot;', '"').replaceAll('&amp;', '&'): ''
		};
		for (prop=node.firstChild;prop;prop=prop.nextSibling) {
			if(prop.tagName!='property')continue;
			var name=getXmlAttribute(prop,'name');
			if(name)obj[name]=getTextContent(prop);
		}
		list.push(obj);
	}
	return ({list: list, total: total});
}
SearchXmlRequest.onReceive = function(status, xml, text) {
	var obj = ParseXmlObjectList(xml);
	DrawSearchResponse(obj.list, obj.total);
}

function	DrawSearchResult(width, list) {
//	alert('list: '+list.length);
	var div = dc('div');
	with(div.style){paddingLeft='8px';width='170px';overflow='hidden';scrollY='visible';textAlign='left';}
	var table = div.appendChild(dc('table'));
	table.style.width = '170px';
	var tbody = table.appendChild(dc('tbody'));
//try{
	for (i in list) {
		var tr = tbody.appendChild(dc('tr'));
		var td = tr.appendChild(dc('td'));
		var a = td.appendChild(dc('a'));
		a.href = list[i].fulllink;
		var sdiv = a.appendChild(dc('div'));
		with(sdiv){overflow='hidden';textAlign='left';width='240px';}
		sdiv.className = a.className = 'highlight';
		var puce = sdiv.appendChild(dc('img'));
		puce.src = GLOBALS.ROOT+GLOBALS.PICT_FOLDER+'redsquare.gif';
		if (list[i].desc)
			AddHelp(sdiv, list[i].desc.replace(SearchText, '<b>'+SearchText+'</b>'));
		else
			AddHelp(sdiv, list[i].name.replace(SearchText,'<b>'+SearchText+'</b>')+(list[i].ClassType!='Folder'&&list[i].ClassType!='DBPage'&&list[i].ClassType!='Evenement'? ' ('+list[i].ClassType+')': (list[i].ClassType=='Evenement')? ' (Activite)': ''));
		var t = list[i].name.replace(SearchText, '<b>'+SearchText+'</b>');
		sdiv.innerHTML += t;
//		alert(sdiv.innerHTML);
	}
//	alert(div.innerHTML);
//}catch(e){alert(e.message);}
	return (div);
}

function	DrawSearchResponse(list, total) {
//	alert('total: '+total);
	var tbody = BodySearchResult;
	var tr,td;
	var maxh = parseInt(getRef('divcontent').clientHeight);
	var curh = parseInt(tbody.firstChild.firstChild.clientHeight);
	if (!tbody.lastChild.is_result || !DivSearchResult) {
		tr = dc('tr');
		td = tr.appendChild(dc('td'));
//		td.style.paddingLeft = '5px';
	//	td.style.textAlign = 'center';
//		alert(')');
		var res = DrawSearchResult(tbody.parentNode.clientWidth, list);
		DivSearchResult = dc('div');
//	DivSearchResult.style.background = 'red';
		DivSearchResult.style.textAlign = 'left';
//		DivSearchResult.style.paddingLeft = '5px';
		td.style.width = DivSearchResult.style.width = tbody.parentNode.clientWidth+'px';
		DivSearchResult.src_height = 0;
		td.style.height = DivSearchResult.style.height = '0px';
//		DivSearchResult.style.width = td.clientWidth+'px';
		DivSearchResult.style.overflow = 'hidden';

		DivSearchResult.appendChild(res);
		tr.is_result = true;
		td.appendChild(DivSearchResult);

		tbody.appendChild(tr);
	}
	else {
		DivSearchResult.src_height = DivSearchResult.clientHeight;
//		DivSearchResult.style.paddingLeft = '5px';
		while (DivSearchResult.firstChild)
			DivSearchResult.removeChild(DivSearchResult.firstChild);
		var res = DrawSearchResult(parseInt(tbody.parentNode.clientWidth), list);
		DivSearchResult.appendChild(res);
	}
	if (total > 10) {
		tdhelp = dc('div');
		tdhelp.innerHTML += 'Affichage de '+list.length+' &eacute;l&eacute;ments sur '+total+' trouv&eacute;s, ';
		var a = tdhelp.appendChild(dc('a'));
		a.href = GLOBALS.ROOT+'?r='+escape(GetCookie('q'));
		a.style.fontSize = '12px';
		a.innerHTML += 'plus de r&eacute;sultats';
		with(tdhelp.style){paddingLeft='10px';paddingRight='10px';paddingBottom='10px';textAlign='left';color='black';}
		res.insertBefore(tdhelp, res.firstChild);
	}
	else if (total == 0) {
		tdhelp = dc('div');
		tdhelp.innerHTML += 'Aucun r&eacute;sultat pour votre recherche, essayez la ';
		var a = tdhelp.appendChild(dc('a'));
		a.href = GLOBALS.ROOT+'?r='+escape(GetCookie('q'));
		a.style.fontSize = '12px';
		a.innerHTML += 'recherche avanc&eacute;e';
		with(tdhelp.style){paddingLeft='10px';paddingRight='10px';paddingBottom='10px';textAlign='left';color='black';}
		tdhelp.innerHTML += '<br /><br />';
		res.insertBefore(tdhelp, res.firstChild);
	}
	var closeButton = dc('div');
	closeButton.style.color = 'black';
	closeButton.style.paddingLeft = '120px';
	closeButton.style.fontSize = '10px';
	closeButton.className = 'clickable';
	closeButton.style.width = '40px';
	closeButton.onclick = function() {
		CloseQuickSearch();
	}
	closeButton.innerHTML = 'Fermer';
	res.appendChild(closeButton);
//	alert(res.innerHTML);
//	alert('maxh: '+maxh);
	var newh = parseInt(res.clientHeight) + 8;
//	newh = newh > maxh? maxh: newh;
	DivSearchResult.dst_height = newh;
	DivSearchResult.cur_i = 0;
//	DivSearchResult.style.textAlign = 'center';
	AdjustSearchSize();
}
function	CloseQuickSearch() {
	while (DivSearchResult.firstChild)
		DivSearchResult.removeChild(DivSearchResult.firstChild);
	DivSearchResult.dst_height = 0;
	DivSearchResult.cur_i = 0;
	AdjustSearchSize();
}
function	LaunchSearch(searchtext) {
	SetCookie('q', searchtext);
	SearchText = searchtext;
	SearchXmlRequest.query('GET', '?xml&search='+escape(searchtext), true);
}

jQuery(function() {
	var req = ''+window.top.location.search;
	if (req.indexOf('?r')===0)
		return ;
	var menutd = getRef('search');
	if (!menutd)
		return ;
//	if (_GET('r'))
//		return ;
	var div = dc('div');
	div.style.position = 'absolute';
	div.style.width = '978px';
//	div.style.marginLeft = 'auto';
//	div.style.marginRight = 'auto';
//	div.style.textAlign = 'right';
//	div.style.width = '100%';
	var layer = div.appendChild(dc('div'));
	layer.style.backgroundColor = '#aa9845';
//	layer.style.textAlign = 'right';
//	layer.style.top = '0px';
//	layer.style.left = (menutd.clientWidth-(274+36))+'px';
//	var layer = layer.appendChild(dc('div'));
	layer.style.position = 'absolute';
//	layer.style.width = '978px';
//	layer.style.textAlign = 'right';
	layer.style.marginLeft = '600px';
//	alayer.style.textAlign = 'center';
	layer.style.color = 'white';
	DrawSearchPanel(layer);


//	var menutd = getRef('menu');
	menutd.appendChild(div);
//	menutd.parentNode.insertBefore(div, menutd.nextSibling);
//	
});




