var __results_view__ = null;

function __changePage__(sSearch, iPage) {
	var d = new Date();
	var strUrl = '&dummy=' + d.getTime();
	strUrl = 'get_search_results.php?cerca=' + sSearch + '&pag=' + iPage + strUrl;
	__results_view__.mapObject.downloadUrl(strUrl, __results_view__.showSearchResults, __results_view__);
}

function ResultsView(containerId, container, mapObject) {
	__results_view__ = this;
	this.mapObject = mapObject;
	this.page = 1; 
	this.totpages = 0; 
	this.numresults = 0;
	this.sql = '';
	this.HilightColor = '#FFFF00';
	this.BackgroundColor = 'transparent';
	this.CellBackgroundColor = '#F0E8E8';
	this.titleFont = 'Verdana, Arial, Helvetica, sans-serif';
	this.titleFontColor = '#FFFFFF';
	this.titleFontBold = 'normal';
	this.titleFontSize = '8pt';
	this.font = 'Verdana, Arial, Helvetica, sans-serif';
	this.fontColor = '#FFFFFF';
	this.fontBold = 'normal';
	this.fontSize = '8pt';
	this.SelectedIndex = -1;
	this.onItemClick = null;
	this.Box = document.createElement('DIV');
	this.Box.style.position = 'relative';
	this.Box.style.top = 0;
	this.Box.style.left = 0;
	this.Box.style.width = '99%';
	this.Box.style.height = '100%';
	this.Box.style.border = '1px inset silver';
	this.Box.style.overflow = 'auto';
	this.Box.style.fontSize = '9pt';
	this.Box.style.backgroundColor = this.BackgroundColor;
	this.Box.style.textAlign = 'left';
	if (container == null) {
		if (containerId != null) {
			container = document.getElementById(containerId);
		}
	}
	if (container == null) {
		container = document.body;
	}
	container.appendChild(this.Box);
	this.Table = document.createElement('TABLE');
	this.TableBody = document.createElement("TBODY");
	this.Table.appendChild(this.TableBody);
	this.Box.appendChild(this.Table);
	this.Table.border = 0;
	this.Table.bgColor = 'transparent';
	this.Table.style.position = 'relative';
	this.Table.style.margin = 0;
	this.Table.style.padding = 0;
	this.Table.style.backgroundColor = 'transparent';
	
	var oRow = document.createElement('TR');
	this.cellRicerca = document.createElement('TD');
	this.cellRicerca.colSpan = 2;
	this.cellRicerca.vAlign = 'middle';
	with (this.cellRicerca.style) {
		backgroundColor = 'transparent';
		margin = '0px';
		padding = '0px';
		border = '0px solid black';
		textAlign = 'left';
		verticalAlign = 'middle';
	}
	oRow.appendChild(this.cellRicerca);
	this.TableBody.appendChild(oRow);
	
	this.txtCerca = document.createElement('INPUT');
	this.txtCerca.type = 'text';
	this.txtCerca.style.border = '1px solid black';
	this.txtCerca.style.fontSize = '8pt';
	this.txtCerca.style.width = '180px';
	this.txtCerca.style.height = '18px';
	this.txtCerca.style.backgroundColor = '#D0D0D0';
	this.cellRicerca.appendChild(this.txtCerca);
	this.btnCerca = document.createElement('INPUT');
	this.btnCerca.type = 'submit';
	this.btnCerca.value = ' trova ';
	this.btnCerca.style.fontSize = '8pt';
	this.btnCerca.style.width = '71px';
	this.btnCerca.style.height = '18px';
	this.btnCerca.style.border = '1px solid black';
	this.cellRicerca.appendChild(this.btnCerca);
	this.btnCerca.owner = this;
	GEvent.bindDom(this.btnCerca, "click", this, this.ricerca);
	
	var oRow = document.createElement('TR');
	this.cellNResults = document.createElement('TD');
	this.cellNResults.style.border = '0px solid black';
	this.cellNResults.style.backgroundColor = this.BackgroundColor;
	this.cellNResults.colSpan = 2;
	oRow.appendChild(this.cellNResults);
	this.TableBody.appendChild(oRow);
	var oRow2 = document.createElement('TR');
	this.cellPager = document.createElement('TD');
	this.cellPager.className = 'pager';
	this.cellPager.style.border = '0px solid black';
	this.cellPager.style.backgroundColor = this.BackgroundColor;
	this.cellPager.colSpan = 2;
	oRow2.appendChild(this.cellPager);
	this.TableBody.appendChild(oRow2);
	this.allItems = new Array();
}

ResultsView.prototype.showSearchResults = function(httpObj, owner) {
	if (httpObj.readyState == 4) {
		if (httpObj.status == 200) {
			owner.processXML(GXml.parse(httpObj.responseText));
		}
	}
}

ResultsView.prototype.ricerca = function(ev, obj) {
	if (obj != null) {
		var sSearch = this.txtCerca.value;
		var d = new Date();
		var strUrl = '&dummy=' + d.getTime();
		strUrl = 'get_search_results.php?cerca=' + sSearch + strUrl;
		this.mapObject.downloadUrl(strUrl, this.showSearchResults, this);
	}
}

ResultsView.prototype.resultitemClick = function(ev, obj) {
	if (obj != null) {
		var sId = obj.id;
		var sElement = sId.substr(0, 4);
		var iNodeId = parseInt(sId.substr(4));
		var iblogId = obj.blogId;
		this.selectItem(iblogId);
	}
}

ResultsView.prototype.SetVisible = function(bVisible) {
	if (bVisible) {
		this.Box.style.display = 'block';
	} else {
		this.Box.style.display = 'none';
	}
}

ResultsView.prototype.SetHeight = function(iHeight) {
	this.Box.style.height = iHeight; // + 'px';
}

ResultsView.prototype.SetWidth = function(iWidth) {
	this.Box.style.width = iWidth; // + 'px';
}

ResultsView.prototype.GetDOMContainer = function() {
	return this.Box;
}

ResultsView.prototype.SetBackgroundColor = function(bgcol) {
	this.BackgroundColor = bgcol;
	this.Box.style.backgroundColor = this.BackgroundColor;
	this.Table.bgColor = this.BackgroundColor;
	this.cellNResults.style.backgroundColor = this.BackgroundColor;
	this.cellPager.style.backgroundColor = this.BackgroundColor;
}
ResultsView.prototype.SetCellBackgroundColor = function(bgcol) {
	this.CellBackgroundColor = bgcol;
}

ResultsView.prototype.SetHilightColor = function(hlcol) {
	this.HilightColor = hlcol;
}

ResultsView.prototype.SetFont = function(sFont) {
	this.font = sFont;
	this.cellNResults.style.fontFamily = sFont;
	this.cellPager.style.fontFamily = sFont;
}
ResultsView.prototype.SetFontColor = function(sFontCol) {
	this.fontColor = sFontCol;
	this.cellNResults.style.color = this.fontColor;
	this.cellPager.style.color = this.fontColor;
}
ResultsView.prototype.SetFontBold = function(bBold) {
	if (bBold) {
		this.fontBold = 'bold';
	} else {
		this.fontBold = 'normal'; 
	}
	this.cellNResults.style.fontWeight = this.fontBold;
	this.cellPager.style.fontWeight = this.fontBold;
}
ResultsView.prototype.SetFontSize = function(iFontSize) {
	this.fontSize = iFontSize + 'pt';
	this.cellNResults.style.fontSize = this.fontSize;
	this.cellPager.style.fontSize = this.fontSize;
}
ResultsView.prototype.SetTitleFont = function(sFont) {
	this.titleFont = sFont;
}
ResultsView.prototype.SetTitleFontColor = function(sFontCol) {
	this.titleFontColor = sFontCol;
	this.cellNResults.style.color = this.titleFontColor;
	this.cellPager.style.color = this.titleFontColor;
}
ResultsView.prototype.SetTitleFontBold = function(bBold) {
	if (bBold) {
		this.titleFontBold = 'bold';
	} else {
		this.titleFontBold = 'normal';
	}
}
ResultsView.prototype.SetTitleFontSize = function(iFontSize) {
	this.titleFontSize = iFontSize + 'pt';
}
ResultsView.prototype.setBorder = function(sBorderStyle) {
	this.Box.style.border = sBorderStyle;
}

ResultsView.prototype.addItem = function(vId, blogId, sNazione, sLabel, sSummary, oMapObject) {
	var iId = parseInt(vId);
	var newItem = new ResultItem(this, iId, blogId, sNazione, sLabel, sSummary, oMapObject);
	if (newItem != null) {
		this.allItems[blogId] = newItem;
		this.TableBody.appendChild(newItem.oRow);
	}
	return newItem;
}

ResultsView.prototype.selectItem = function(sId) {
	var iId = parseInt(sId);
	if (this.SelectedIndex != iId) {
		if (this.SelectedIndex > 0) {
			this.allItems[this.SelectedIndex].Select(false);
		}
		this.SelectedIndex = iId;
		this.allItems[iId].Select(true);
		this.itemClick(sId);
	}
}

ResultsView.prototype.itemClick = function(sId) {
	if (this.onItemClick != null) {
		var iId = parseInt(sId);
		this.onItemClick(this.allItems[iId]);
	}
}

ResultsView.prototype.removeItem = function(vId) {
	var iId = parseInt(vId);
	var oItem = this.allItems[iId];
	this.TableBody.removeChild(oItem.oRow);
	this.allItems[iId] = null;
	if (this.SelectedIndex != -1) {
		this.SelectedIndex = -1;
	}
}

ResultsView.prototype.removeAll = function() {
	for (var i = 0; i < this.allItems.length; i++) {
		if (this.allItems[i]) {
			this.removeItem(i);
		}
	}
}

ResultsView.prototype.createPager = function() {
	this.cellNResults.innerHTML = 'Search results: <b>' + this.numresults + '</b>';
	var str = 'Page ' + this.page + ' of ' + this.totpages + '&nbsp;&nbsp;&nbsp;';
	if (this.totpages > 1) {
		for (var i = 1; i <= this.totpages; i++) {
			if (i == this.page) {
				str += '<b>' + i + '<b> ';
			} else {
				str += '<b><a href="javascript:__changePage__(\'' + this.sql + '\', ' + i + ');">' + i + '</a><b> ';
			}
		}
	}
	this.cellPager.innerHTML = str;
}

ResultsView.prototype.processXML = function(xmlobj) {
	this.removeAll();
	var xdoc = xmlobj.documentElement;
	this.numresults = xdoc.getAttribute("num");
	this.page = parseInt(xdoc.getAttribute("pag")); 
	this.totpages = parseInt(xdoc.getAttribute("totpages")); 
	this.sql = GXml.value(xdoc.getElementsByTagName("RICERCA")[0]);
	var polygons = xdoc.getElementsByTagName("POLYGON");
	if (polygons.length > 0) {
		var xpolygon = null;
		var titolo = '';
		var testo = '';
		var nazione = '';
		var polId = 0;
		var catId = 0;
		var punti = null;
		var path = null;
		var nvertici = 0;
		var vertici = null;
		for (var j = 0; j < polygons.length; j++) {
			xpolygon = polygons[j];
			nazione = GXml.value(xpolygon.getElementsByTagName("NAZIONE")[0]);
			titolo = GXml.value(xpolygon.getElementsByTagName("TITOLO")[0]);
			testo = GXml.value(xpolygon.getElementsByTagName("SOMMARIO")[0]);
			if (titolo == '') {
				titolo = 'Senza nome';
			}
			polId = parseInt(xpolygon.getAttribute("id"));
			blogId = parseInt(xpolygon.getAttribute("id_entry"));
			this.addItem(polId, blogId, nazione, titolo, testo, xpolygon);
		}
	}
	this.createPager();
	return this.numresults;
}


function ResultItem(oContainer, nId, blogId, sNazione, sLabel, sSummary, oMapObject) {
	this.Selected = false;
	this.cont = oContainer;
	this.id = nId;
	this.blogId = blogId;
	this.label = sLabel;
	this.oRow = document.createElement('TR');
	this.oRow.style.backgroundColor = this.cont.CellBackgroundColor;
	this.oRow.style.cursor = 'pointer';
	this.oCell0 = document.createElement('TD');
	this.oCell1 = document.createElement('TD');
	this.oLabel = document.createElement('SPAN');
	this.oText = document.createElement('SPAN');

	this.oCell0.style.verticalAlign = 'top';
	this.oCell0.style.color = this.cont.titleFontColor;
	this.oCell0.style.fontFamily = this.cont.titleFont;
	this.oCell0.style.fontSize = this.cont.titleFontSize;
	this.oCell0.style.fontWeight = this.cont.titleFontBold;
	this.oCell0.style.border = '1px solid #A0A0A0';
	this.oCell0.innerHTML = sNazione;
	
	this.oCell1.style.verticalAlign = 'top';
	this.oCell1.style.border = '1px solid #A0A0A0';
	
	this.oLabel.style.color = this.cont.titleFontColor;
	this.oLabel.style.fontFamily = this.cont.titleFont;
	this.oLabel.style.fontSize = this.cont.titleFontSize;
	this.oLabel.style.fontWeight = this.cont.titleFontBold;
	this.oLabel.innerHTML = sLabel;
	this.oLabel.listview = this.cont;
	this.oLabel.id = 'labl' + nId;
	this.oLabel.blogId = blogId;
	GEvent.bindDom(this.oLabel, "click", this.cont, this.cont.resultitemClick);
	
	this.oText.style.color = this.cont.fontColor;
	this.oText.style.fontFamily = this.cont.font;
	this.oText.style.fontSize = this.cont.fontSize;
	this.oText.style.fontWeight = this.cont.fontBold;
	this.oText.innerHTML = sSummary;
	this.oText.listview = this.cont;
	this.oText.id = 'text' + nId;
	this.oText.blogId = blogId;
	GEvent.bindDom(this.oText, "click", this.cont, this.cont.resultitemClick);
	
	this.oRow.appendChild(this.oCell0);
	this.oRow.appendChild(this.oCell1);
	this.oCell1.appendChild(this.oLabel);
	this.oCell1.appendChild(document.createElement('BR'));
	this.oCell1.appendChild(this.oText);
	this.objXML = oMapObject;
}

ResultItem.prototype.Select = function(isSelected) {
	if (isSelected) {
		this.oRow.style.backgroundColor = this.cont.HilightColor;
		this.Selected = true;
	} else {
		this.oRow.style.backgroundColor = this.cont.CellBackgroundColor;
		this.Selected = false;
	}
}


