/*
 * <copyright>
 *  Copyright (c) 2009 by IICM, Graz University of Technology, Austria
 * </copyright>
 *
 *  This software is the confidential information of IICM,
 *  Graz University of Technology, Austria. You shall not disclose such
 *  confidential information and shall use it only in accordance with
 *  the IICM.
 *
 *  IICM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 *  SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 *  PURPOSE, OR NON-INFRINGEMENT. IICM SHALL NOT BE LIABLE FOR ANY DAMAGES
 *  SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 *  THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */
 
// ---------------------------------------------------------------------------------------------
// Initialisation and DWR-Method of Catalog GeoMap
// ---------------------------------------------------------------------------------------------


// Globals
var resultListBookmarks;
var timeout = null;

var geoMapMessages= new Array();
var geoMapParameters= new Array();

/* Initialises Arrays with GeoMap Messages */
function initGeoMapMessages(geoMapMessages) {
	this.geoMapMessages=geoMapMessages;
}

/* Initialises Arrays with GeoMap Messages */
function initGeoMapParameters(geoMapParameters) {
	this.geoMapParameters = geoMapParameters;
}

function initGeoMap( mapBaseQueries, languageKey, mapDbInfo ) {
	var sortCriteria1 = "";
	var sortCriteria2 = "";
	var hitsPerPage = "10";
	var action = "next";
	var selectedDbs = "";
	var totalHits = 0;
	var searchTerm="";
	
	// get SearchTerm
	if (document.getElementById('idHistoryText') != null) {
		searchTerm = trim(document.getElementById('idHistoryText').value);
	}
	
	// Create database-list depending on 'hits>0' and 'database checked'
	for (db in mapDbInfo) {
		if ( mapDbInfo[db] == 'yes') { 
			// Check if hits of selected database greater 0, otherwise do not append to checked list for remote search 
			var hitsCurrentDb= dwr.util.getValue('idSpanDBConnected'+db);
			if ( isNaN(Number(hitsCurrentDb)) ) { hitsCurrentDb = 0; }
				selectedDbs = selectedDbs+","+trim(db)+"|||"+Number(hitsCurrentDb);
				totalHits = Number(totalHits) + Number(hitsCurrentDb);
		}
	}
	if ( selectedDbs.length > 0 ) { selectedDbs = selectedDbs.substring(1,selectedDbs.length); } else { return; }
	if ( trim(languageKey) == "") { languageKey="en"; }
	
	var limitationHits = 5000;
	if ( !isNaN(Number(geoMapParameters['limitationHits'])) ) { 
		if ( Number(geoMapParameters['limitationHits'])>0 ) {
			limitationHits = Number(geoMapParameters['limitationHits']);
		} 
	}
	
	if (totalHits <= limitationHits ){
		//if (totalHits > 2000 ){alert(this.geoMapMessages['warningGeoMap']);}		
		getGeoMap( mapBaseQueries, languageKey, selectedDbs, searchTerm );
	}
	else{
		alert(this.geoMapMessages['errorGeoMap']+' '+limitationHits);
	}
}



function getGeoMap( mapBaseQueries, languageKey, selectedDbs, searchTerm ){
	useLoadingImage("resources/appl_images/loadingImage24x24.gif","idCatalogGeoMapPosition"); // CHECK THIS
	
	dwr.engine.setOrdered(true);	
				 	
	CatalogGeoMap.getGeoMap( mapBaseQueries, languageKey, selectedDbs, searchTerm,
		{ 	callback:function(GeoMapTO){ fillGeoMap(GeoMapTO, mapBaseQueries, languageKey, selectedDbs);}, 
			errorHandler:function(message) { errorHandler('geomap',message); },
			timeout:0
		}		
	 );
	 return;
}

// ---------------------------------------------------------------------------------------------
// GeoMap
// ---------------------------------------------------------------------------------------------


// Creates IFrame using JS-DOM containig GeoMap on position with id = 'idCatalogResultListPosition' 
function fillGeoMap(GeoMapTO, mapBaseQueries, languageKey, selectedDbs){
	var tableElement,tbodyElement,trElement,tdElement,iframeElement,buttonElement, imgElement;
	var geoMap = GeoMapTO;
	var urlToGeoMap = geoMap.urlToGeoMap;
	var nameGeoDataFile = geoMap.nameGeoDataFile;
	var extensionGeoDataFile = geoMap.extensionGeoDataFile;
	var pathGeoDataFile = geoMap.pathGeoDataFile;
	var idGeoDataFile = geoMap.idGeoDataFile;
	var language = geoMap.language;
	var messages = geoMap.lgKeys; 	
	var errorKey = geoMap.errorKey;	
	var errorMsg = geoMap.errorMsg;
	var debugMsg = geoMap.debugMsg;
		
	if ( !document.getElementById || !document.createElement ) { return; }
	// Clear IFrame including GeoMap if exists
	removeGeoMap();
	// Clear Message 'simpleSearchInfoBelow' if exist
	trElement = document.getElementById('idSimpleSearchInfoBelow_txt')
	if ( trElement != null ) {
		trElement.parentNode.removeChild(trElement);
	}
	trElement = document.getElementById('idSimpleSearchInfoBelow_hr')
	if ( trElement != null ) {
		trElement.parentNode.removeChild(trElement);
	}
	
	// Create complete URL to Map
	var urlToMap = urlToGeoMap+"?url="+pathGeoDataFile+nameGeoDataFile+"_"+idGeoDataFile+"."+extensionGeoDataFile+"&language="+language;
	// Create Table containing IFRAME
	tableElement = document.createElement("table");
	tableElement.setAttribute("id","idTableGeoMap");
	tableElement.className="geoMap";
	tbodyElement = document.createElement("tbody");
	// First Row(Headline)
	trElement = document.createElement("tr");
	tdElement = document.createElement("td");	
	tdElement.className = "geoMap_topLeft";	
	textElement = document.createTextNode(messages['headline']);
	tdElement.appendChild(textElement);
	trElement.appendChild(tdElement);
	tdElement = document.createElement("td");
	tdElement.className = "geoMap_topRight";
	buttonElement = document.createElement("input");	
	buttonElement.setAttribute("type","button");	
	buttonElement.setAttribute("value","X");
	buttonElement.className = "geoMap_topRight";
	buttonElement.onclick = new Function("removeGeoMap();");
	tdElement.appendChild(buttonElement);
	trElement.appendChild(tdElement);
	tbodyElement.appendChild(trElement);
	// Error Key and Message
	if (errorKey != "") {
		trElement = document.createElement("tr");
		tdElement = document.createElement("td");
		tdElement.colSpan="2";
		tdElement.className="geoMap_errMessage"	
		//textElement = document.createTextNode(messages['error']+" ( "+errorKey+" ): "+errorMsg);
		textElement = document.createTextNode(errorMsg);
		tdElement.appendChild(textElement);
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
	}
	// Debug Messages
	if (debugMsg != "") {
		trElement = document.createElement("tr");
		tdElement = document.createElement("td");
		tdElement.colSpan="2";
		tdElement.className="geoMap_message"	
		textElement = document.createTextNode("DEBUG: "+debugMsg);
		tdElement.appendChild(textElement);
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
	}	
	if ( errorKey != 'ExceptionNoValidData') {	
		// Second Row (Contacting Message and Image)
		trElement = document.createElement("tr");
		trElement.id="idContactingHost";	
		tdElement = document.createElement("td");
		tdElement.colSpan="2";
		tdElement.className="geoMap_message"	
		textElement = document.createTextNode(messages['contactingHost']+" ");
		tdElement.appendChild(textElement);
		imgElement = document.createElement("img");
		imgElement.setAttribute("src","resources/appl_images/loading_image43x11.gif");	
		tdElement.appendChild(imgElement);	
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
		// Third Row (IFrame)
		trElement = document.createElement("tr");
		tdElement = document.createElement("td");
		tdElement.colSpan="2";
		tdElement.className="geoMap_content"	
		iframeElement = document.createElement("iframe");
		iframeElement.name = "IframeGeoMap";
		iframeElement.id = "idIframeGeoMap";
		iframeElement.className="geoMap";
		iframeElement.src=urlToMap;
		tdElement.appendChild(iframeElement);
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
	}
	tableElement.appendChild(tbodyElement);
	document.getElementById('idCatalogGeoMapPosition').appendChild(tableElement);
	if (errorKey != 'ExceptionNoValidData') {
		// Clear Message 
		ifr = document.getElementById('idIframeGeoMap');
		if (ifr.attachEvent) { 
			ifr.attachEvent("onload", clearContactHostMsg); 
		}
		else {
			 ifr.onload = clearContactHostMsg; 
		}	
	    timeout = window.setTimeout("clearContactHostMsg()", 5000); // Ensure that message is deleted after 5 seconds		
	}    	
	return;
}	
	

// Clears IFrame including GeoMap if exists	
function removeGeoMap() {
	if ( document.getElementById('idCatalogGeoMapPosition') != null && document.getElementById('idTableGeoMap') != null) {
		document.getElementById('idCatalogGeoMapPosition').removeChild(document.getElementById('idTableGeoMap'));
	}
}	
	
	
// Clear Message displayed while loading IFrame
function clearContactHostMsg() {

	if ( document.getElementById('idContactingHost') != null ) {
		document.getElementById('idContactingHost').parentNode.removeChild(document.getElementById('idContactingHost'));
		if (timeout != null) { clearTimeout(timeout); }
	}
}	
	
	
