/**
 * cadestations.js: several utilities to show stations info, etc. for
 *                the CadeMET project, using the YUI library.
 *
 * by Eduardo Hernando Izcara. Copyright 2009 agroGEX.
 */
// ------------------------------------------------------------------------

// Make sure we haven't already been loaded
var CadeStations;
if (CadeStations && (typeof CadeStations != "object" || CadeStations.NAME))
{
	throw new Error("Namespace 'CadeStations' already exists");
}

// Create our namespace, and specify some meta-information
CadeStations = {};
CadeStations.NAME = "CadeStations";    // The name of this namespace
CadeStations.VERSION = 1.0;            // The version of this namespace


// Algunas variables globales del namespace:
CadeStations.SelectedStat = null;
CadeStations.SelectedWithCombo = false;

// ------------------------------------------------------------------------

CadeStations.render_station = function(estacion)
{
	YAHOO.util.Dom.setStyle(document.body, 'cursor', 'progress');

	if (estacion)
		CadeStations.SelectedStat = estacion;
	else
		CadeStations.SelectedStat = CadeLayers.stations.features[0].attributes;

	CadeStations.SelectedWithCombo = false;
	var url = Rutinas.baseUrl() + "/stations/showinfo/format/html/cod_station/" + CadeStations.SelectedStat.cod;

	var callback =
	{ 
		success: CadeStations.render_station_exito,
		failure: CadeStations.render_station_fracaso
	}; 

	YAHOO.util.Connect.initHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
    YAHOO.util.Connect.asyncRequest('GET', url, callback);
}
// ------------------------------------------------------------------------

CadeStations.render_station_exito = function(o)
{
	document.getElementById('contenido').innerHTML = o.responseText;

	CadeAuth.getLoggedStatus();
	CadeAuth.protect('home-link-mon');
	CadeAuth.protect('home-link-mot');
	CadeAuth.protect('home-link-rag');
	CadeAuth.protect('home-link-car');

	// se establece aquí el 'listener' para el link del riego, ya que hay que
	// hacerlo cada vez que se muestre una estación; no sirve hacerlo en rutinas.js
	YAHOO.util.Event.addListener("input_riego-link", 'click', function(e){
				YAHOO.util.Event.preventDefault(e);
				CadeRiego.inputLaunch();
			});

	// Un poco rebuscado, pero encontramos así el selector de estaciones sin ambigüedad:
	var selector = YAHOO.util.Dom.getElementBy(function(el){return el.name=='sel-estacion';}, 'select', 'stats-selection');
	YAHOO.util.Event.addListener(selector, 'change', function(e){
				var targ;
				if (!e) var e = window.event;
				if (e.target) targ = e.target;
				else if (e.srcElement) targ = e.srcElement;
				if (targ.nodeType == 3) // defeat Safari bug
					targ = targ.parentNode;
				//alert("evento: " + e.type + "; " + targ.value + "->" + targ.options[targ.selectedIndex].text);
				var stat = CadeLayers.stations.features[targ.selectedIndex];
				CadeStations.render_station(stat.attributes);
				CadeLayers.centerStation(stat.geometry.x, stat.geometry.y);
				CadeStations.SelectedWithCombo = true;
			});
	/*
	if (CadeStations.SelectedStat.cod==39) selector.selectedIndex = 2;			// Madrid
	else if (CadeStations.SelectedStat.cod==40) selector.selectedIndex = 1;		// Rucandio
	else if (CadeStations.SelectedStat.cod==41) selector.selectedIndex = 3;		// Bentretea
	else if (CadeStations.SelectedStat.cod==42) selector.selectedIndex = 0;		// Salas
	*/
	if (CadeStations.SelectedWithCombo)
		selector.focus();

	YAHOO.util.Dom.setStyle(document.body, 'cursor', 'auto');
}
// ------------------------------------------------------------------------

CadeStations.render_station_fracaso = function(o)
{
	document.getElementById('contenido').innerHTML = "";
	YAHOO.util.Dom.setStyle(document.body, 'cursor', 'auto');
}
// ------------------------------------------------------------------------


