/*Maps Vars*/
var map, mapProv, iconoAPP, iconSize;

/*Google maps lib*/
function createMarker(point, title, content, icono) {
	var marker = new GMarker(point, icono);
	GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(title +"&nbsp;"+ content); });
	return marker;
}

function loadMap(maptype) {

	mapLoad = (maptype == 'store') ? map : mapProv;

	if (typeof(mapLoad) == "undefined")
	{
		mapLoad = new GMap2(document.getElementById("gmapprov"));

				iconoAPP = new GIcon(G_DEFAULT_ICON);
		iconoAPP.image = "http://www.appinformatica.com/app-iconito.png";
		iconoAPP.printImage = "http://www.appinformatica.com/app-iconito.gif";
		iconSize = new GSize(32,32);
		iconoAPP.iconSize = iconSize;
		iconoAPP.infoWindowAnchor = new GPoint(8, 35);

	}



	//ESPAÑA
	GDownloadUrl("/maps/country.xml", function(data) {

		//removemos los iconos anteriores
		mapLoad.clearOverlays();

		//parseamos el xml
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");

		var titulo, prov, local, vinculo, texto = '';

		for (var i = 0; i < markers.length; i++) {
			var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
									parseFloat(markers[i].getAttribute("long")));

			titulo	= '<div class="titulo">Tienda de Inform&aacute;tica APP<br /> en '+ markers[i].getAttribute("nombre").toUpperCase() +'</div>';
			prov 	= markers[i].getAttribute("prov").toLowerCase().replace(/ /g,"-");
			local	= markers[i].getAttribute("nombre").toLowerCase().replace(/ /g,"-");
			vinculo	= 'http://www.appinformatica.com/tienda-de-informatica-'+replaceNonAcute(prov+'-'+local)+'.php';
			texto	= '<div class="subtitulo">&iquest;C&oacute;mo llegar a la tienda?&nbsp;<a href="'+vinculo+'">Click aqu&iacute;</a></div>';

			mapLoad.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));
		}

		var latlng = new GLatLng(parseFloat(xml.documentElement.getAttribute("lat")),
								 parseFloat(xml.documentElement.getAttribute("long")));
		mapLoad.setCenter(latlng, parseInt(xml.documentElement.getAttribute("mapzoom")));


		mapLoad.addControl( new GSmallZoomControl3D() );
		mapLoad.enableDragging();
		mapLoad.enableDoubleClickZoom();
	});

}

function initGmaps() {

	//Event.observe('com-msg', 'keydown', limitText);

	if (GBrowserIsCompatible()) {

		iconoAPP = new GIcon(G_DEFAULT_ICON);
		iconoAPP.image = "http://www.appinformatica.com/app-iconito.png";
		iconoAPP.printImage = "http://www.appinformatica.com/app-iconito.gif";
		iconSize = new GSize(32,32);
		iconoAPP.iconSize = iconSize;
		iconoAPP.infoWindowAnchor = new GPoint(8, 35);

		if (GMAP_FILE_G != '') {
			map = new GMap2(document.getElementById("gmap"));
			map.disableDragging();
			map.disableDoubleClickZoom();


			//LO CENTRAMOS AL COORD QUE CORRESPONDE SEGUN EL XML CARGADO
			GDownloadUrl(GMAP_FILE_G, function(data) {
				var xml_store = GXml.parse(data);

				var latlng = new GLatLng(parseFloat(xml_store.documentElement.getAttribute("lat")),
										 parseFloat(xml_store.documentElement.getAttribute("long")));

				titulo	= '<div class="titulo">Tienda de Inform&aacute;tica APP<br /> en '+ xml_store.documentElement.getAttribute("nombre").toUpperCase() +'</div>';
				prov 	= xml_store.documentElement.getAttribute("prov").toLowerCase().replace(/ /g,"-");
				local	= xml_store.documentElement.getAttribute("nombre").toLowerCase().replace(/ /g,"-");
				vinculo	= 'http://www.appinformatica.com/tienda-de-informatica-'+replaceNonAcute(prov+'-'+local)+'.php';
				texto	= '<div class="subtitulo">&iquest;C&oacute;mo llegar a la tienda?&nbsp;<a href="'+vinculo+'">Click aqu&iacute;</a></div>';


				map.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));
				map.setCenter(latlng, parseInt(xml_store.documentElement.getAttribute("mapzoom")));
			});
		}



		if (GMAP_FILE_P != '') {
			mapProv = new GMap2(document.getElementById("gmapprov"));
			mapProv.disableDragging();
			mapProv.disableDoubleClickZoom();
			GDownloadUrl(GMAP_FILE_P, function(data) {
						var xml_prov = GXml.parse(data);
						var markers = xml_prov.documentElement.getElementsByTagName("marker");

						var titulo, prov, local, vinculo, texto = '';

						for (var i = 0; i < markers.length; i++) {
							var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
													parseFloat(markers[i].getAttribute("long")));

							titulo	= '<div class="titulo">Tienda de Inform&aacute;tica APP<br /> en '+ markers[i].getAttribute("nombre").toUpperCase() +'</div>';
							prov 	= markers[i].getAttribute("prov").toLowerCase().replace(/ /g,"-");
							local	= markers[i].getAttribute("nombre").toLowerCase().replace(/ /g,"-");
							vinculo	= 'http://www.appinformatica.com/tienda-de-informatica-'+replaceNonAcute(prov+'-'+local)+'.php';
							texto	= '<div class="subtitulo">&iquest;C&oacute;mo llegar a la tienda?&nbsp;<a href="'+vinculo+'">Click aqu&iacute;</a></div>';


							//if (GMAP_FILE_P != '')
								mapProv.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));

								//map.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));
						}

						var latlng = new GLatLng(parseFloat(xml_prov.documentElement.getAttribute("lat")),
												 parseFloat(xml_prov.documentElement.getAttribute("long")));
						mapProv.setCenter(latlng, parseInt(xml_prov.documentElement.getAttribute("mapzoom")));
					});
		}


	}

}

function initGmaps_old() {

	Event.observe('com-msg', 'keydown', limitText);


	if (GBrowserIsCompatible()) {
		//ICONO APP
		var iconoAPP = new GIcon(G_DEFAULT_ICON);
		iconoAPP.image = "http://www.appinformatica.com/app-iconito.png";
		iconoAPP.printImage = "http://www.appinformatica.com/app-iconito.gif";
		var iconSize = new GSize(32,32);
		iconoAPP.iconSize = iconSize;
		iconoAPP.infoWindowAnchor = new GPoint(8, 35);

		var map = new GMap2(document.getElementById("gmap"));


		if (GMAP_FILE_P != '') {
			var mapProv = new GMap2(document.getElementById("gmapprov"));

			//LO CENTRAMOS AL COORD QUE CORRESPONDE SEGUN EL XML CARGADO PROVINCIA
			GDownloadUrl(GMAP_FILE_P, function(data) {
				var xml_prov = GXml.parse(data);

				var latlng = new GLatLng(parseFloat(xml_prov.documentElement.getAttribute("lat")),
										 parseFloat(xml_prov.documentElement.getAttribute("long")));
				mapProv.setCenter(latlng, parseInt(xml_prov.documentElement.getAttribute("mapzoom")));
			});
		}


		//UI
		map.addControl( new GSmallZoomControl3D() );
		mapProv.addControl( new GSmallZoomControl3D() );

		//ESPAÑA
		GDownloadUrl("maps/country.xml", function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");

			var titulo, prov, local, vinculo, texto = '';

			for (var i = 0; i < markers.length; i++) {
				var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										parseFloat(markers[i].getAttribute("long")));

				titulo	= '<div class="titulo">Tienda de Inform&aacute;tica APP<br /> en '+ markers[i].getAttribute("nombre").toUpperCase() +'</div>';
				prov 	= markers[i].getAttribute("prov").toLowerCase().replace(/ /g,"-");
				local	= markers[i].getAttribute("nombre").toLowerCase().replace(/ /g,"-");
				vinculo	= 'http://www.appinformatica.com/tienda-de-informatica-'+prov+'-'+local+'.php';
				texto	= '<div class="subtitulo">&iquest;C&oacute;mo llegar a la tienda?&nbsp;<a href="'+vinculo+'">Click aqu&iacute;</a></div>';


				if (GMAP_FILE_P != '')
					mapProv.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));

					map.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));
			}


			//si no hay archivo de "centrado" centramos a España
			if (GMAP_FILE_G == '') {
					var latlng = new GLatLng(parseFloat(xml.documentElement.getAttribute("lat")),
											 parseFloat(xml.documentElement.getAttribute("long")));
					map.setCenter(latlng,    parseInt(xml.documentElement.getAttribute("mapzoom")));
			}

		});

		if (GMAP_FILE_G != '') {
			//LO CENTRAMOS AL COORD QUE CORRESPONDE SEGUN EL XML CARGADO
			GDownloadUrl(GMAP_FILE_G, function(data) {
				var xml_store = GXml.parse(data);

				var latlng = new GLatLng(parseFloat(xml_store.documentElement.getAttribute("lat")),
										 parseFloat(xml_store.documentElement.getAttribute("long")));
				map.setCenter(latlng, parseInt(xml_store.documentElement.getAttribute("mapzoom")));
			});
		}

		//map.setUIToDefault();
	}

}


function clearField(e) {
	switch (e.id) {
		case 'com-nombre': text = 'Nombre:'; break;
		case 'com-email': text = 'Email:'; break;
		default: return false;
	}

	if (e.value == '') {
		e.value = text;
		e.setStyle({'fontWeight': 'bold'});
	}
	else if (e.value == text) {
		e.value = '';
		e.setStyle({'fontWeight': 'normal'});
	}
}

function showTemsAndConditions(email) {
	$('comunicador').insert(new Element('div', {id: 'temsAndConds'}).setStyle({top: "16px"}).update('<p>De acuerdo con lo establecido por la Ley Org&aacute;nica 15/1999, de 13 de diciembre, de Protecci&oacute;n de Datos de Car&aacute;cter Personal, doy mi consentimiento para que estos datos sean incluidos en un fichero del que es titular APP Inform&aacute;tica, Asesor&iacute;a y Programaci&oacute;n Profesional S.L. y puedan ser utilizados para el env&iacute;o de informaci&oacute;n sobre las tiendas APP as&iacute; como ofertas de sus productos y servicios. Asimismo, se le informa tambi&eacute;n de que tiene el derecho a acceder a los datos, a rectificarlos y cancelarlos en su caso, as&iacute; como a oponerse a su tratamiento en los t&eacute;rminos contemplados en la referida Ley Org&aacute;nica 15/1999. Estos derechos podr&aacute; ejercerlos en la siguiente direcci&oacute;n: APP Inform&aacute;tica, Asesor&iacute;a y Programaci&oacute;n Profesional S.L.  C/Pintor Sorolla, 9 pta 2 46002 Valencia.</p><div class="bntholder clearfix"><a href="#" id="suscribirOk" onclick="suscribirEmail(\''+email+'\'); return false;">Continuar</a><a id="suscribirCancel" href="#" onclick="$(\'temsAndConds\').remove(); return false;">Cancelar</a></div>'));
}

/*FORM Post*/
function postQuestion() {

	if (($F('com-nombre') == '') && ($F('com-nombre') != 'Nombre:')) {
		alert('Ingrese su nombre');
		return false;
	}

	if (($F('com-susc') == 1) || ($F('com-respemail') == 1))
	{
		if (($F('com-email') == '') && ($F('com-email') != 'Email:')) {
			alert('Ingrese su E-Mail');
			return false;
		}
	}

	if (($F('com-msg') == '') && ($F('com-msg') != 'Escriba Aqui su mensaje...')) {
		alert('Ingrese un mensaje/pregunta');
		return false;
	}

	if ($('com-susc').checked)
		showTemsAndConditions($F('com-email'));



	new Ajax.Request('postq.php', {
		parameters: {
			nombre: 		$F('com-nombre'),
			email: 			$F('com-email'),
			suscboletin: 	0,
			replyNotify: 	$F('com-respemail'),
			mensaje: 		$F('com-msg'),
			storeId: ID_TIENDA_G
		},
		method: 'get',
		onCreate: function() {
			$('formComunicador').disable();
		},
		onComplete: function(transp) {

			var e = eval('('+transp.responseText+')') ;

			if (e.status == 'ok') {
				$('formComunicador').enable();
				new Effect.Opacity('formComunicador', {duration: 0.3, from: 1, to: 0, afterFinish: function(){
					new Effect.Appear('oksent', {duration: 0.3, afterFinish: function() {
						//Para cambiar el tiempo
						setTimeout('resetForm();', 5000);
						//5000 = 5 segundos
						}});
				}});
			} else {
				alert('error');
			}
		}
	});

}

function gmapPrint() {


	if (GBrowserIsCompatible()) {

		iconoAPP = new GIcon(G_DEFAULT_ICON);
		iconoAPP.image = "http://www.appinformatica.com/app-iconito.png";
		iconoAPP.printImage = "http://www.appinformatica.com/app-iconito.gif";
		iconSize = new GSize(32,32);
		iconoAPP.iconSize = iconSize;
		iconoAPP.infoWindowAnchor = new GPoint(8, 35);

		if (GMAP_FILE_G != '') {
			map = new GMap2(document.getElementById("gmap"));
			map.disableDragging();
			map.disableDoubleClickZoom();


			//LO CENTRAMOS AL COORD QUE CORRESPONDE SEGUN EL XML CARGADO
			GDownloadUrl(GMAP_FILE_G, function(data) {
				var xmlprint = GXml.parse(data);

				var latlng = new GLatLng(parseFloat(xmlprint.documentElement.getAttribute("lat")),
										 parseFloat(xmlprint.documentElement.getAttribute("long")));



				var __nom = xmlprint.documentElement.getAttribute("nombre").toUpperCase();
				var __pro = xmlprint.documentElement.getAttribute("prov").toLowerCase().replace(/ /g,"-");
				var __pob = xmlprint.documentElement.getAttribute("nombre").toLowerCase().replace(/ /g,"-");

				var titulo	= '<div class="titulo">Tienda de Inform&aacute;tica APP<br /> en ' + __nom + '</div>';
				var vinculo	= 'http://www.appinformatica.com/tienda-de-informatica-'+__pro+'-'+__pob+'.php';
				var texto	= '<div class="subtitulo">&iquest;C&oacute;mo llegar a la tienda?&nbsp;<a href="'+vinculo+'">Click aqu&iacute;</a></div>';


				map.addOverlay(createMarker(latlng, titulo, texto, iconoAPP));
				map.setCenter(latlng, parseInt(xmlprint.documentElement.getAttribute("mapzoom")));
			});
		}

	//window.print();
	}

}


function resetForm() {
	$('com-nombre').setStyle({'fontWeight': 'bold'}).value = 'Nombre:';
	$('com-email').setStyle({'fontWeight': 'bold'}).value = 'Email:';
	$('com-msg').value = 'Escriba Aqui su mensaje...';

	new Effect.Fade('oksent', {duration: 0.3,afterFinish: function(){
		new Effect.Opacity('formComunicador', {duration: 0.3, from: 0, to: 1});
	}});
}

function limitText(event) {

	if (
			($F(this).length+1 > 512) &&
			(
				(event.keyCode != 8) &&
				(event.keyCode != 46)
			)
		)
	event.stop();
}


