// $Id: getFeatureInfo.js,v 1.12 2009/09/22 14:24:31 goncalves Exp $

// Prerequisites


/**
 * Run the GetFeatureInfo on the specified layer and display the result (with extra services if any).
 */
function getFeature(divName, uuid, x, y, imageWidth, imageHeight) {
	if(document.getElementById(divName).innerHTML == "&nbsp;"){
		showFeature(divName+'Loading');
		
		// Build the request
		var request = 
			{
				uuid : uuid,
				X: x,
				Y: y,
				imageWidth: imageWidth,
				imageHeight: imageHeight
			};
		
		// Send it to the server side
		dojo.xhrGet({
			url: "query",
			content: request,
			load: function(response, ioArgs){displayFeatureResult(divName, response)}
		});		
	}else{
		showFeature(divName);
	}
}

function displayFeatureResult (divName, data) {
	document.getElementById(divName).innerHTML = data;
	showFeature(divName);
	dojo.parser.parse(document.getElementById(divName));
}

function showFeature(divName) {
	maskAllFeature();
	var div = document.getElementById("GetFeatureInfoContent").childNodes;
	for(var i = 0; i < div.length ; i++){
		if(div[i].id == divName){
			if(document.getElementById(div[i].id) != null)
				if(document.getElementById(div[i].id).innerHTML != "")
					document.getElementById(div[i].id).style.display = '';
		}
	}
}

function maskAllFeature(){
	var div = document.getElementById("GetFeatureInfoContent").childNodes;
	for(var i = 0; i < div.length ; i++){
		if(document.getElementById(div[i].id) != null)
			if(document.getElementById(div[i].id).innerHTML != "")
				document.getElementById(div[i].id).style.display = 'none';
		
	}
}

function selectFeatureThumb(divName) {
	maskAllFeature();
	var div = document.getElementById("GetFeatureInfoThumbs").childNodes;
	for(var i = 0; i < div.length ; i++){
		if(div[i].id != divName){
			if((div[i].id) != null)
				document.getElementById(div[i].id).className = "unselectedThumb";
		}
	}
	if(document.getElementById(divName) != null)
		document.getElementById(divName).className = "selectedThumb";
}

function selectFeature(thumbDivName, divName, uuid, x, y, imageWidth, imageHeight) {
	selectFeatureThumb(thumbDivName);
	getFeature(divName, uuid, x, y, imageWidth, imageHeight);
}

/**************
 * Functions for the services of Features
 **************/
 
/*
 * SWE Main Function
 * @param id Id of the System
 * @param sosUrl Url of the SOS Server
 */

function getCapteursList(id, sosURL) {
	//TODO : Tiana � toi de jouer
	//ID du ContentPane qui doit contenir le graph : SWEPanel
	//Pour modifier son aspect par css, utilises le fichier /style/default/main.css (ligne 518) (et si il y a besoin : IE.css et FF.css pour les specifiques navigateurs
	
	
	loadDojoSWELibs();
	
	//Load scripts 
//	var url =  "proxy?url="+escape(sosURL);
	var url =  sosURL;
	var sensor=id;
	window.sd=SensorDialog;
	window.sd.initializeSensorDialog(sensor,url);
	swe_showSwepanel();
}
function swe_showSwepanel(){
	document.getElementById("SWEPanel").style.display="block";
	
}

/* 
 * Script Loader
 * @param file : Script FileName
 */
 function swe_loadScript(file){
	var scriptMB = document.createElement('script');
	scriptMB.src = ""+getContext()+file;
	scriptMB.defer = false;   //not sure of effect of this?
	scriptMB.type = "text/javascript";
	scriptMB.onreadystatechange=function(){checkMBIELoad(this)};
	document.getElementsByTagName('head')[0].appendChild(scriptMB);
 }
 
 /* 
 * Get Server Context 
 * @return context : Context path
 */
 function swe_getContext(){
 	var url = window.location.pathname.split('/');
	url=url[1];
	return ("/"+url+"/");
 }
 
 /**
  * Set the Begin Date for the chart
  * called by DateTextBox Change Event
  **/
  
 function swe_updateGrapheBegin(arg){
	window.sd.updateGrapheBegin(arg);
}

 /**
  * Set the End Date for the chart
  * called by DateTextBox Change Event
  **/
  
 function swe_updateGrapheEnd(arg){
	window.sd.updateGrapheEnd(arg);
}

 /**
  * Update the chart 
  * called by Update Chart Button Click Event
  **/ 
 function swe_updateChart(){
	window.sd.updateChart();
}


 /**
  * Show Series
  * Called by Raw Data CheckBox Click Event
  **/ 
 function swe_showHideSeries(name,obj){
	if (obj.value.indexOf("on") !=-1) obj.value="off";
	else	obj.value="on";
	window.sd.showHideSeries(name,obj.value);
}

 /**
  * swe_clean
  * Destroy all dijit objects within the SWEPanel
  **/ 
 function swe_clean(){
 	dijits_tab= [
			"swe_date_begin_input",
			"swe_date_end_input",
			"swe_chart_update_button"
	];
 	for(var i=0;i<dijits_tab.length;i++){
			if (dijit.byId(dijits_tab[i])){
				dijit.byId(dijits_tab[i]).destroy();
			}
		}
}