/*
 *  __  __       _     _ _ _ _         _   
 * |  \/  |     | |   (_) (_) |       | |  
 * | \  / | ___ | |__  _| |_| |_  __ _| |_ 
 * | |\/| |/ _ \| '_ \| | | | __|/ _` | __|
 * | |  | | (_) | |_) | | | | |_| (_| | |_ 
 * |_|  |_|\___/|_.__/|_|_|_|\__|\__,_|\__|
 *
 * [google.map.js] iquadrat 2009
 * 
 * 	Script general del Mapa de Mobilitat.ad
 * 
 * TODO:
 *  - OK: Deshabilitar el mapa entero, mostrar texto y no dibujar los tramos
 *  - OK: Poner la fecha de ultima actualizacion del fichero de tram 
 *  - OK: Acceder a una incidencia desde la home al mapa
 * 
 */


/*
 * Variables globales
 */
var map = null;
var timePanel = null;
var gMode = 'n';
var gOnline = false;
var gTimeCam = null;

var iconsGroupTemp = [];
var iconsGroupCamara = [];
var iconsGroupObra = [];
var iconsGroupSemaforo = [];
var iconsGroupAccidente = [];
var iconsGroupCadenas = [];
var iconsGroupPanel1 = [];
var iconsGroupPanel2 = [];
var iconsGroupCross = [];

/*
 * Control de errores
 */
window.onerror = function(msg, url, linenumber) {
	//alert('Error de JavaScript: '+msg+'\nURL: '+url+'\nNúmero de línea: '+linenumber);
	return true;
};

/*
 * initializeMap()
 * funcion principal de carga del mapa 
 * 
 * layer: la capa donde de incrusta el mapa
 * lat: latitud inicial
 * long: longitud inicial
 * zoom: zoom inicial
 * coords: se utiliza para la administracion
 */
function initializeMap(layer, lat, long, zoom, coords, mode, online)
{
	if(!mode) {
		mode = 'n';
	}
	
	gMode = mode;
	gOnline = online;
	
	if(!coords) {
		if(mode=='h') {
			$("#loading").hide();
		} else {
			if(mode=='i' || mode=='f') {
				$("#loading").css("top", "280px");
			}
			$("#loading").css("left", (($(window).width() / 2) - ($("#loading").css("width").replace("px", "")) / 2) + "px");
			
		}
	}
	
	if(GBrowserIsCompatible()) 
	{ 
		map = new GMap2(document.getElementById(layer));
		if(mode=='n' || mode=='i' || mode=='f') {
			GEvent.addListener(map, "load", function(){
				setTimeout("closeLoading()", 2000);
			});
		}
		
		if(mode=='n' || mode=='i' || mode=='f') {
			map.addControl(new GLargeMapControl3D());
			var keyb = new GKeyboardHandler(map);
			map.enableContinuousZoom();
			map.enableScrollWheelZoom();
			map.enableDragging();
			map.enableDoubleClickZoom();
		} else if(mode=='h') {
			map.addControl(new GSmallMapControl());
			map.enableContinuousZoom();
			map.enableScrollWheelZoom();
			map.enableDragging();
			map.enableDoubleClickZoom();
		}
	    map.clearOverlays();
	  	
	    if(mode=='n' || mode=='i' || mode=='f') {
	    	var ovMap = new GOverviewMapControl();
	    	map.addControl(ovMap);
	    	var arrMapTypes = this.map.getMapTypes() || [];
			for (var i=0; i<arrMapTypes.length; i++) {
				arrMapTypes[i].getMinimumResolution = function () { return 11; };
				arrMapTypes[i].getMaximumResolution = function () { return 18; };
			}
	    } else {
	    	var arrMapTypes = this.map.getMapTypes() || [];
			for (var i=0; i<arrMapTypes.length; i++) {
				arrMapTypes[i].getMinimumResolution = function () { return 10; };
				arrMapTypes[i].getMaximumResolution = function () { return 18; };
			}
	    }
	  	
		if(coords) {
			initCoords();
		} else {
			if(online) loadTramos();
			loadObras(function(){
				if(mode=='h') {
					markersPaint(iconsGroupObra, 0);
				}
			});
			loadPanels();
			loadTemperaturas();
			loadCamaras(null);
			if(online) {
				GEvent.addListener(map, "moveend", function(){
					if(map.getZoom() >= 16) {
						for(n=0; n<TRAM_ALL.length;n++) {
							eval(TRAM_ALL[n]).weight = 8;
						}
					} else if(map.getZoom() >= 14) {
						for(n=0; n<TRAM_ALL.length;n++) {
							eval(TRAM_ALL[n]).weight = 6;
						}
					} else {
						for(n=0; n<TRAM_ALL.length;n++) {
							eval(TRAM_ALL[n]).weight = 4;
						}
					}
				});
			}
		}
		map.setCenter(new GLatLng(lat, long), zoom);
		
	}
	else
	{
		$("#loading").html("<br/><br/>Tu navegador no es compatible<br/>con Google Maps");
	}
}

/*
 * closeLoading()
 * cierra la ventanita de carga
 */
function closeLoading()
{
	$("#loading").slideUp("slow", function(){
		if(!gOnline) {
			if(gMode=='i') {
				$("#offline").css("top", "80px");
			}
			$("#offline").css("left", (($(window).width() / 2) - ($("#loading").css("width").replace("px", ""))) + "px");
			$("#offline").slideDown("slow");
		}
		if(typeof loadInciStart == "function") {
			loadInciStart.apply();
		}
	});
}

/*
 * loadPanels()
 * crea el array con todos los paneles
 */
function loadPanels()
{
	iconsGroupPanel1 = [];
	iconsGroupPanel2 = [];
	
	// Grupo 1
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.436626,1.474979), { icon:panel1Icon, title:"Sentit Espanya", zIndexProcess:orderOfCreation }), "panel1", "panel22", "Sentit Espanya"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.457844,1.487521), { icon:panel1Icon, title:"Sentit Espanya", zIndexProcess:orderOfCreation }), "panel1", "panel2", "Sentit Espanya"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.486687,1.492939), { icon:panel1Icon, title:"Margineda - Sentit Espanya", zIndexProcess:orderOfCreation }), "panel1", "panel3", "Margineda - Sentit Espanya"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.506733,1.534942), { icon:panel1Icon, title:"Sentit baixada", zIndexProcess:orderOfCreation }), "panel1", "panel18", "Sentit baixada"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.521603,1.558857), { icon:panel1Icon, title:"Sentit baixada", zIndexProcess:orderOfCreation }), "panel1", "panel6", "Sentit baixada"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.550835,1.738157), { icon:panel1Icon, title:"Sentit Pas", zIndexProcess:orderOfCreation }), "panel1", "panel15", "Sentit Pas"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.551388,1.509633), { icon:panel1Icon, title:"Sentit Massana", zIndexProcess:orderOfCreation }), "panel1", "panel13", "Sentit Massana"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.514786,1.531692), { icon:panel1Icon, title:"Sentit Andorra", zIndexProcess:orderOfCreation }), "panel1", "panel7", "Sentit Andorra"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.494186,1.500020), { icon:panel1Icon, title:"Avda. Salou-Tobira - Sentit Espanya", zIndexProcess:orderOfCreation }), "panel1", "panel17", "Avda. Salou-Tobira - Sentit Espanya"));
	iconsGroupPanel1.push(Array(new GMarker(new GLatLng(42.566103748324245,1.594240665435791), { icon:panel1Icon, title:"Sentit Andorra", zIndexProcess:orderOfCreation }), "panel1", "panel2", "Sentit Andorra"));
	
	// Grupo 2
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.512871,1.551797), { icon:panel1Icon, title:"La Bartra - Sentit França", zIndexProcess:orderOfCreation }), "panel2", "panel5", "La Bartra - Sentit França"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.436436,1.475172), { icon:panel2Icon, title:"Sentit Andorra", zIndexProcess:orderOfCreation }), "panel2", "panel", "Sentit Andorra"));
	// Quitado
	//iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.470989,1.493282), { icon:panel1Icon, title:"Sant Julia - Sentit Andorra", zIndexProcess:orderOfCreation }), "panel1", "panel19", "Sant Julia - Sentit Andorra"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.483532,1.490836), { icon:panel1Icon, title:"Rotonda Margineda - Sentit Andorra", zIndexProcess:orderOfCreation }), "panel2", "panel21", "Rotonda Margineda - Sentit Andorra"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.489574,1.494741), { icon:panel1Icon, title:"Sentit Andorra", zIndexProcess:orderOfCreation }), "panel2", "panel401", "Sentit Andorra"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.491627,1.496201), { icon:panel1Icon, title:"Sentit Andorra", zIndexProcess:orderOfCreation }), "panel2", "panel4", "Sentit Andorra"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.506218,1.532818), { icon:panel1Icon, title:"Sentit pujada", zIndexProcess:orderOfCreation }), "panel2", "panel20", "Sentit pujada"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.537239,1.589413), { icon:panel1Icon, title:"Sentit França", zIndexProcess:orderOfCreation }), "panel2", "panel9", "Sentit França"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.568474,1.604283), { icon:panel1Icon, title:"Sentit França", zIndexProcess:orderOfCreation }), "panel2", "panel10", "Sentit França"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.559101,1.686037), { icon:panel1Icon, title:"Sentit França", zIndexProcess:orderOfCreation }), "panel2", "panel11", "Sentit França"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.546028,1.731398), { icon:panel1Icon, title:"Pas - Sentit França", zIndexProcess:orderOfCreation }), "panel2", "panel12", "Pas - Sentit França"));
	iconsGroupPanel2.push(Array(new GMarker(new GLatLng(42.549934,1.525533), { icon:panel1Icon, title:"Sentit Ordino", zIndexProcess:orderOfCreation }), "panel2", "panel14", "Sentit Ordino"));
	
	
}

/*
 * loadTemperaturas()
 * crea el array de las temperaturas
 */
function loadTemperaturas()
{
	iconsGroupTemp = [];
	//iconsGroupTemp.push(Array(new GMarker(new GLatLng(42.510102569254684, 1.5323781967163086), { icon:temperaturaIcon, title:"Sensor Pont del Plà Sud", zIndexProcess:orderOfCreation }), "temp", "0101"));
	iconsGroupTemp.push(Array(new GMarker(new GLatLng(42.5153854574561, 1.5287303924560546), { icon:temperaturaIcon, title:"Sensor Pont del Plà Nord", zIndexProcess:orderOfCreation }), "temp", "0102"));
}

/*
 * loadCamaras()
 * carga dinamicamente con ajax las camaras del XML
 */
function loadCamaras(cb)
{
	/*
	<cam id="47" lat="42.440180912414554" lng="1.4783477783203125" file="../fotos/adm_h263_Riu Runner.jpg" estado="1">
		CG1-Frontera Hispano-Andorrana
	</cam>
	*/ 
	
	iconsGroupCamara = [];
	
	$.ajax({
		type: "GET",
		url: "../mapacontinu_v2/cams.asp",
		dataType: "xml",
		async: true,
		cache: false,
		success: function(xml){
			$(xml).find('cam').each(function(){
				id = $(this).attr("id");
				file = $(this).attr("file");
				lat = $(this).attr("lat");
				lng = $(this).attr("lng");
				estado = $(this).attr("estado");
				name = $(this).text();
				icono = camaraIcon;
				type = "cam";
				if(estado != "1") {
					icono = camaraIconOff;
					type = "camoff";
				}
				marker = new GMarker(new GLatLng(lat,lng), { icon:icono, title:name, zIndexProcess:orderOfCreation });
				iconsGroupCamara.push(Array(marker, type, Array(id, file, estado, name)));
			});
			if(cb) {
				cb.apply();
			}
		},
		error: function(xmlobj, message) {
			//alert("loadCamaras::Error(" + message + ")");
		}
	});
	
}

/*
 * orderOfCreation()
 * funcion para el posicionamiento de los marcadores
 */
function orderOfCreation(marker, b)
{
	return 1;
}

/*
 * loadObras()
 * carga dinamicamente del XML las incidencias del mapa
 */
function loadObras(cb)
{
	/*
	<obra id="30062" tipo="3" lat="42.53588010092859" lng="1.5875244140625" icon="1" stat="0">
		<mensaje titol="">Test Meteo 1</mensaje>
	</obra>
	*/
	
	var str = "";
	iconsGroupObra =[];
	
	$.ajax({
		type: "GET",
		url: "../mapacontinu_v2/obras.asp",
		dataType: "xml",
		async: true,
		cache: false,
		success: function(xml){
			$(xml).find('obra').each(function(){
				id = $(this).attr("id");
				icon = iconObra[$(this).attr("icon")];
				stat = $(this).attr("stat");
				lat = $(this).attr("lat");
				lng = $(this).attr("lng");
				tipo = $(this).attr("tipo");
				bigicon = $(this).attr("bigicon");
				
				newTit = $(this).attr("newTit");
				newSub = $(this).attr("newSub");
				newMsg = $(this).attr("newMsg");
				newPie = $(this).attr("newPie");
				
				$(this).find('mensaje').each(function(){
					titulo = $(this).attr("titol");
					message = $(this).text();
				});
				marker = new GMarker(new GLatLng(lat,lng), { icon:icon, title:titulo, zIndexProcess:orderOfCreation });
				
				arr = Array(marker, "inci", Array(id, $(this).attr("icon"), stat, titulo, message, tipo, bigicon, newTit, newSub, newMsg, newPie));
				iconsGroupObra.push(arr);
				
				if(stat == 1 || stat == 2 || stat == 3) {
					markerPaint(arr, true);
					if(stat == 3) {
						closeInci();
						loadInci(arr[2]);
					}
				}
				
			});
			if(cb) {
				cb.apply();
			}
		},
		error: function(xmlobj, message) {
			//alert("loadObras::Error(" + message + ")");
		}
	});
}

/*
 * loadTramos()
 * carga los colores de los tramos del XML
 */
function loadTramos()
{
	/*
	<trams no="113">
		<tram nom="T1" col="1"/>
		<tram nom="T2" col="1"/>
		<tram nom="T3" col="1"/>
		...
	</trams>
	*/
	
	$.ajax({
		type: "GET",
		url: "../mapacontinu_v2/data/trams.xml",
		dataType: "xml",
		async: true,
		cache: false,
		success: function(xml){
			$(xml).find('tram').each(function(){
				name = $(this).attr("nom");
				color = $(this).attr("col");
				newName = "TRAM_" + name;
				if($.inArray(newName, TRAM_ALL) >= 0) {
					eval(newName).color = "#" + TRAM_colors[parseInt(color)];
					map.addOverlay(eval(newName));
				}
			});
		}
	});
}

/*
 * markersPaint()
 * dibuja todos los marcadores de un array
 * 
 * arr: array que queremos pintar
 */
function markersPaint(arr, tipo)
{
	if(tipo > 0) {
		for(j=0; j<arr.length; j++) {
			if(arr[j][2][5] == tipo) {
				markerPaint(arr[j], false);
			}
		}
	}
	else
	{
		for(j=0; j<arr.length; j++) {
			markerPaint(arr[j], false);
		}
	}
	
}

function markerPaint(arr, first)
{
	arr[0].type = arr[1];
	arr[0].id = arr[2];
	
	if(arr[1] != "inci" || first == true) {
		arr[0].visible = true;
		map.addOverlay(arr[0]);
	} else if(arr[1] == "inci") {
		if(arr[2][2] != 2 && arr[2][2] != 3) {
			if(arr[2][2] == 1) {
				arr[0].visible = false;
				map.removeOverlay(arr[0]);
			}
			arr[0].visible = true;
			map.addOverlay(arr[0]);
		}
	}
	
	if(gMode == 'n' || gMode == 'i' || gMode == 'f')
	{
		GEvent.addListener(arr[0], "click", function() {
			switch(this.type) {
				case "panel1":
					closeAll();
					this.setImage("../mapacontinu_v2/img/ico_panel1_on.png");
					loadPanel(this.id, 0);
					break;
				case "panel2":
					closeAll();
					this.setImage("../mapacontinu_v2/img/ico_panel2_on.png");
					loadPanel(this.id, 0);
					break;
				case "temp":
					closeAll();
					this.setImage("../mapacontinu_v2/img/ico_temperatura_on.png");	
					loadTemp(this.id);
					break;
				case "inci":
					closeAll();
					this.setImage(imageObraIconOn[this.id[1]]);
					loadInci(this.id);
					break;
				case "cam":
					closeAll();
					this.setImage("../mapacontinu_v2/img/ico_camara_on.png");
					loadCam(this.id);
					break;
			}
		});
	}
	else if(gMode == 'h')
	{
		GEvent.addListener(arr[0], "click", function() {
			switch(this.type) {
				case "inci":
					goMapAndLoad(this.id);
					break;
			}
		});
	}
	
}

function goMapAndLoad(id)
{
	parent.document.location = "mapacontinu_v2.asp?i=" + id[0];
}

/*
 * markersRemove()
 * quita todos los marcadores de un array
 */
function markersRemove(arr, tipo)
{
	for(j=0; j<arr.length; j++) {
		if(arr[j][1] != "inci") {
			arr[j][0].visible = false;
			map.removeOverlay(arr[j][0]);
		} else {
			if(tipo > 0) {
				if(arr[j][2][2] != 2 && arr[j][2][2] != 3 && arr[j][2][5] == tipo) {
					arr[j][0].visible = false;
					map.removeOverlay(arr[j][0]);
				}
			} else {
				if(arr[j][2][2] != 2 && arr[j][2][2] != 3) {
					arr[j][0].visible = false;
					map.removeOverlay(arr[j][0]);
				}
			}
		}
	}
}

/*
 * closePanel()
 * cierra la ventana del panel de informacion, tambien cambia los iconos
 */
function closePanel()
{
	if(timePanel != null) {
		clearTimeout(timePanel);
	}
	for(x=0; x<iconsGroupPanel1.length; x++) {
		if(iconsGroupPanel1[x][0].visible) {
			iconsGroupPanel1[x][0].setImage("../mapacontinu_v2/img/ico_panel1.png");
		}
	}
	for(x=0; x<iconsGroupPanel2.length; x++) {
		if(iconsGroupPanel2[x][0].visible) {
			iconsGroupPanel2[x][0].setImage("../mapacontinu_v2/img/ico_panel2.png");
		}
	}
	$("#panel_content1").html("");
	$("#panel_content2").html("");
	$("#panel").hide();
}

/*
 * loadPanel()
 * carga dinamicamente el contenido de un panel, lee el mensaje y la imagen del XML
 */
function loadPanel(id, screen)
{
	
	var count = 0;
	
	$("#panel_content1").html("");
	$("#panel_content2").html("");
	
	$.ajax({
		type: "GET",
		url: "../mapacontinu_v2/data/pannells.xml",
		dataType: "xml",
		cache: false,
		success: function(xml){
			$(xml).find('panel').each(function(){
				if(id == $(this).attr("id"))
				{
					var screens = $(this).find('pantalla').length;
					
					$(this).find('pantalla').each(function(){
						
						if(count == screen)
						{
							var message = "";
							var pantalla = ""
							var pantallita = "";
	
							var src = $(this).attr("src");
							$(this).find('mensaje').each(function(){
								message += $(this).text() + "<br />\n";
							});
							message = message.replace(/ /g, "&nbsp;");
							$.ajax({
								type: "GET",
								url: "../mapacontinu_v2/data/" + src,
								dataType: "html",
								async: false,
								success: function(html){
									pantalla = html.replace("dat=", "").split('');
								}
							});
							
							var cols = 0;
							var rows = 0;
							var colors = Array("#FFFFFF", "#FBD900", "#DA2810", "#46E416", "#16CEE4", "");
							for(p=0; p<pantalla.length; p++) {
								$("#panel_content1").append('<div style="background-color:' + colors[pantalla[p]] + ';width:2px;height:2px;float:left;margin-right:1px;margin-bottom:1px;overflow:hidden;">&nbsp;</div>');
								cols++;
								if(cols >= 32){
									$("#panel_content1").append('<div style="clear:both;"></div>');
									cols = 0;
								}
							}

							centerLayer("#panel");
							$("#panel_content2").html(message);
							$("#panel").show();
							$("#panel").easydrag();
							
							if(screens > 1) {
								newscreen = count + 1;
								if(newscreen >= screens) {
									newscreen = 0;
								}
								timePanel = setTimeout("loadPanel('" + id + "', " + (newscreen) + ")", 2000);
							}
						}
						count++;
					});
				}
			});
		},
		error: function(xmlobj, message) {
			//alert("loadPanel::Error(" + message + ")");
		}
	});
	
}

/*
 * centerLayer()
 * centra horizontalmente una capa
 */
function centerLayer(lyr)
{
	if($(lyr).length > 0) {
		if(gMode=='i' || gMode=='f') {
			if(lyr == "#cam") $(lyr).css("top", "180px");
			else  $(lyr).css("top", "280px");
		}
		var currentLeft = $(lyr).css("left").replace("px", "");
		if(currentLeft <= 0) {
			$(lyr).css("left", (($(window).width() / 2) - ($(lyr).css("width").replace("px", "")) / 2) + "px");
		}
	}
}

/*
 * closeTemp()
 * cierra la ventana de las temperaturas
 */
function closeTemp()
{
	for(x=0; x<iconsGroupTemp.length; x++) {
		if(iconsGroupTemp[x][0].visible) {
			iconsGroupTemp[x][0].setImage("../mapacontinu_v2/img/ico_temperatura.png");
		}
	}
	$("#temp").hide();
}

/*
 * loadTemp()
 * carga las temperaturas del XML
 */
function loadTemp(id)
{
	/*
	<meteo id="0101">
		<sensor src="TEMPERATURA">
			<medida>C</medida>
			<valor>Desconegut</valor>
		</sensor>
		<sensor src="HUMITAT">
			<medida>%</medida>
			<valor>Desconegut</valor>
		</sensor>
		<sensor src="...">
	</meteo>
	*/
	
	$.ajax({
		type: "GET",
		url: "../mapacontinu_v2/data/meteo_pp.xml",
		dataType: "xml",
		async: false,
		cache: false,
		success: function(xml){
			$(xml).find('meteo').each(function(){
				if(id == $(this).attr("id"))
				{
					var titulo = "";
					if(id == "0101") titulo = "Sensor Pont del Plà Sud";
					if(id == "0102") titulo = "Sensor Pont del Plà Nord";
					var html = '<table cellpadding=1 cellspacing=1 border=0><tr><td colspan="3"><b>' + titulo + '</b></td></tr>';
					$(this).find('sensor').each(function(){
						var sensor = $(this).attr('src');
						var valor = $(this).find('valor').text();
						var medida = $(this).find('medida').text();
						html += '<tr><td width=200>' + sensor + '</td><td>' + valor + '&nbsp;</td><td>' + medida + '</td></tr>';
					});
					html += '</table>';
					centerLayer("#temp");
					$("#temp_content").html(html);
					$("#temp").easydrag();
					$("#temp").show();
				}
			});
		}
	});
}

/*
 * closeInci()
 * cierra la venta de las incidencias y cambia los iconos
 */
function closeInci()
{
	for(x=0; x<iconsGroupObra.length; x++) {
		if(iconsGroupObra[x][0].visible) {
			iconsGroupObra[x][0].setImage(imageObraIcon[iconsGroupObra[x][2][1]]);
		}
	}
	$("#inci").hide();
}

/*
 * loadInci()
 * muestra la venta de una incidencia y escribe su contenido
 */
function loadInci(obj)
{
	
	//obj = [id, icon, stat, titulo, message]
	var id = obj[0];
	var icon = obj[1];
	var stat = obj[2];
	var bigicon = obj[6];
	var image = "../mapacontinu_v2/img/iconos/" + bigicon;
	var titulo = obj[7];
	var subtitulo = obj[8];
	var message = obj[9];
	var pie = obj[10];
	
	// DEMO
	//titulo = "Calçada tallada";
	//subtitulo = "Per Treballs de manteniment.<br/>Empresa responsable Circulació d’Andorra.";
	//message = "CS-101, Km. 101-102<br/>(101 - Cra. De la comella - Cra. de la Plana)<br/>Andorra la Vella, Andorra la Vella<br/>Data inici: 02/09/2010	Data Final: 03/09/2010";
	//pie = "Tall de circulació a la Crta. de la Comella, CS-101, al Km.3 el dia 03/09/2010 de 07h a 08h per tasques de manteniment.";
	
	centerLayer("#inci");
	$("#inci_content1").html('<img src="'+image+'" width="77" height="77" border="0" />');
	$("#inci_content2").html('<div class="inci_tit">' + titulo + '</div><div class="inci_sub">' + subtitulo + '</div><div class="inci_tex">' + message.replace(/\n/g, "<br>") + '</div>');
	$("#inci_content3").html(pie);
	$("#inci").easydrag();
	$("#inci").show();
	
}

/*
 * closeCam()
 * cierra la ventana de la camara y cambia los iconos
 */
function closeCam()
{
	for(x=0; x<iconsGroupCamara.length; x++) {
		if(iconsGroupCamara[x][0].visible) {
			if(iconsGroupCamara[x][1] == "cam") {
				iconsGroupCamara[x][0].setImage("../mapacontinu_v2/img/ico_camara.png");
			}
		}
	}
	if(gTimeCam) clearTimeout(gTimeCam);
	$("#cam").hide();
}

/*
 * loadCam()
 * muestra la venta de una camara y escribe su contenido
 */
function loadCam(obj)
{
	//obj = [id, file, estado, name]
	var id = obj[0];
	var file = obj[1];
	var estado = obj[2];
	var name = obj[3];
	
	centerLayer("#cam");
	redrawCam(file);
	$("#cam").easydrag();
	$("#cam").show();
	
}

function redrawCam(f)
{
	$("#cam_content").html('<img src="'+f+'?rnd='+Math.random()+'" width="384" height="288" border="0" />');
	gTimeCam = setTimeout("redrawCam('"+f+"');", 30000);
}

/*
 * closeAll()
 * cierra todas las ventanas flotantes
 */
function closeAll()
{
	closeTemp();
	closeInci();
	closePanel();
	closeCam();
}

/*
 * mapLog()
 * graba un log en la bbdd del uso
 */

function maplog(op, type, subtype)
{
	$.ajax({
		type: "GET",
		url: "../mapacontinu_v2/log.asp?op="+op+"&type="+type+"&subtype="+subtype,
		cache: false,
		success: function(data){
			// Nothing
		}
	});
}
