// JavaScript Document

function seguirMouse (miDiv) {

var divName = miDiv; // div that is to follow the mouse
                       // (must be position:absolute)
var offX = 15;          // X offset from mouse position
var offY = 15;          // Y offset from mouse position

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style; obj.visibility = 'visible';
obj.left = (parseInt(mouseX(evt))+offX) + 'px';
obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}
document.onmousemove = follow;
                    
}


window.onload = function () {
	
	var zonas1 = $$("#Map area"); // Argentina
	var zonas2 = $$("#Map2 area"); // Capital Federal
	var zonas3 = $$("#Map3 area"); // Gran Buenos Aires

	mapear(zonas1);
	
	function mapear (opciones) {
		
		document.oncontextmenu = function() { return false; }
		document.onselectstart = function () { return false; } // ie
		document.onmousedown = function () { return false; } // mozilla
		
		selecccionDirecta(opciones);
		
		function selecccionDirecta(zona) {
			
			if ($("volver2"))
				$("volver2").style.display = "none";
				

			var zonaOrdenada = new Array();
			
			for (var h=0; h<zona.length; h++) {
				zonaOrdenada[h] = $(zona[h]).alt;
			}
			zonaOrdenada.sort();
			
			$("informacion").innerHTML = "";
			
			var temporal = document.createElement("div");
			temporal.id = "seleccionDirecta";
			//temporal.setAttribute("style","border:1px solid black; float:left");
			//temporal.alt = zonaOrdenada[h];
			$("informacion").appendChild (temporal);

			var col = 1;
			temporal2 = document.createElement("div");
			temporal2.id = "col_"+col;
			temporal2.className = "cols";
			$("seleccionDirecta").appendChild (temporal2);
			
			
			for (var h=0; h<zonaOrdenada.length; h++) {
				var temporal = document.createElement("div");
				temporal.innerHTML = zonaOrdenada[h];
				temporal.alt = zonaOrdenada[h];
				temporal.id = "sd_"+zonaOrdenada[h];
				//temporal.setAttribute("alt",zonaOrdenada[h]);
				temporal.onmouseover = function () { this.className = "sdOver"; }
				temporal.onmouseout = function () { this.className = "sd"; }
				temporal.onclick = obtenerInfo;
				
				//$("seleccionDirecta").appendChild (temporal);
				
				if ( h % 25 == 0 && h!=0) {
					col++;
					temporal2 = document.createElement("div");
					temporal2.id = "col_"+col;
					temporal2.className = "cols";
					$("seleccionDirecta").appendChild (temporal2);
				}
				
				$("col_"+col).appendChild (temporal);
			
			}
			
		}
		
		opciones.each(function(opcion) {
			
			opcion.onmouseover = function () {
				$('info').style.display = "block";
				$("info").innerHTML = this.alt;
				seguirMouse($("info").id);
				this.style.cursor = "pointer";
				if ($("sd_" + this.alt))
					$("sd_" + this.alt).className = "sdOver";
			}
			
			opcion.onmouseout = function () {
				var tiempo = setTimeout("peperina('"+this.alt+"')", 100);
				if ($("sd_" + this.alt))
					$("sd_" + this.alt).className = "sd";
			}
			/*
			var temporal = createElement;
			temporal.innerHTML = opcion.alt;
			temporal.onclick = obtenerInfo;
			$("informacion").appendChild (temporal);
			*/
			
			opcion.onclick = obtenerInfo;
		
		});
		
		function obtenerInfo() {
				
				peperina(this.alt);
				$("cuadroDer").style.display = "block";
				
				if (this.alt == "Capital Federal") {
					$("caba").style.display = "block";
					
					if ($("conurbano") != undefined)
						$("conurbano").style.display = "none";

					new Effect.Morph('mapaCaba', {
					  style: {
						width: '350px',
						height: '375px'
					  }, // CSS Properties
					  duration: 0.8 // Core Effect properties
					});
					$("informacion").innerHTML = "";
					mapear(zonas2);
					
					opciones = zonas2;
				}
				
				else if (this.alt == "Gran Buenos Aires") {
					
					$("conurbano").style.display = "block";
					
					if ($("caba") != undefined)
						$("caba").style.display = "none";
					
					new Effect.Morph('Conurbano', {
					  style: {
						width: '350px',
						height: '375px'
					  }, // CSS Properties
					  duration: 0.8 // Core Effect properties
					});
					$("informacion").innerHTML = "";
					mapear(zonas3);
					
					opciones = zonas3;
				}
				
				else {
					
					
					
					if (opciones == zonas1) {
						if ($("caba") != undefined)
							$("caba").style.display = "none";
						if ($("conurbano") != undefined)
							$("conurbano").style.display = "none";

					}
					else {
						
						$("informacion").style.width = "300px";
						new Effect.Morph('mapaArgentina', { style: { width: '92px', height: '200px' }, // CSS Properties
						  duration: 0.8 // Core Effect properties
						});
						
						$("mapaArgentina").useMap = "#MapVacio";
						$("volver1").style.display = "block";
						$("cuadroIzq").style.cursor = "pointer";
						
						
						// Volver al inicio
						
						$("cuadroIzq").onclick = function () {
							$("informacion").innerHTML = "";
							$("mapaArgentina").useMap = "#Map";
							$("volver1").style.display = "none";
							//$("informacion").style.width = "70%";
							new Effect.Morph('mapaArgentina', { style: { width: '184px', height: '400px' }, // CSS Properties
							  duration: 0.8 // Core Effect properties
							});
							new Effect.Morph('mapaCaba', { style: { width: '25px', height: '27px' }, // CSS Properties
							  duration: 0.8 // Core Effect properties
							});
							new Effect.Morph('Conurbano', { style: { width: '25px', height: '27px' }, // CSS Properties
							  duration: 0.8 // Core Effect properties
							});
							setTimeout("$('caba').style.display = 'none';$('conurbano').style.display = 'none';", 800);
							$("cuadroIzq").onclick = function () {};
							
							selecccionDirecta(zonas1);
							
							opciones = zonas1;
						}
						
					}
					
					var archivoAjax = "estudios_pilates_ajax.php";
					
					if (opciones == zonas1) {
						if (this.alt != "Gran Buenos Aires")
							urlAjax = "?subzona=&zona=&provincia="+ this.alt +"&pais=Argentina";
						else
							urlAjax = "?subzona=&zona="+ this.alt +"&provincia=Buenos Aires&pais=Argentina";
					}
					else if (opciones == zonas2) {
						urlAjax = "?subzona="+ this.alt +"&zona=Capital Federal&provincia=Buenos Aires&pais=Argentina";
					}
					else if (opciones == zonas3) {
						urlAjax = "?subzona="+ this.alt +"&zona=Gran Buenos Aires&provincia=Buenos Aires&pais=Argentina";
					}
					
					
					function obtenerInfo2(paginaACargar) {
						
						$Ajax(paginaACargar, {
							avisoCargando: "divCargando",
							cache: true,
							onfinish: function (html) {
								
								$("informacion").innerHTML = html;
								
								$("volver2").style.display = "block";
								
								$("volver2").onclick = function () {
									selecccionDirecta(opciones);
								}
								
								
								var paginas = $$("#paginas .paginaNoSeleccionada");
								for (var k=1; k<=paginas.length; k++) {
									paginas[k - 1].onclick = function () {
										obtenerInfo2(paginaACargar + "&nPagina=" + this.innerHTML);
									}
								};
														
												
								var datosLugar = $$("#datosLugar_0 span");
								//alert(datosLugar);
								var paraGET = "";
								for(var j=0; j < datosLugar.length; j++) {
									var dato = datosLugar[j].id.replace("div", "");
									var valor = datosLugar[j].innerHTML;
									paraGET += dato + "=" + valor + "&";
								}
								var posUltimocosito = paraGET.lastIndexOf("&");
								paraGET.substr(0, posUltimocosito);


								
								var verMapas = $$(".verMapas");
								verMapas.each(function(verMapa) {
									
									verMapa.onclick = function () {
										idMapa = this.id.replace("vm_", "");
										initialize($("dir_"+idMapa).innerHTML);
									}
								});
								





    function initialize(direccion) {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
	var geocoder = new GClientGeocoder();

	varTop = f_scrollTop() + 75;
	$("map_canvas").style.display = "block";
	$("map_canvas").style.top =  varTop + "px";

	varTopCerrar = f_scrollTop() + 55;
	$("cerrarMap_canvas").style.display = "block";
	$("cerrarMap_canvas").style.top = varTopCerrar + "px";

   	function showAddress(address) {
  	geocoder.getLatLng(address,
    	function(point) {
      	if (!point) {
        	alert(address + " no se encontró");
      	} else {
       	 map.setCenter(point, 15);
        	var marker = new GMarker(point);
        	map.addOverlay(marker);
        	marker.openInfoWindowHtml("<div id='titulos4'></div>"+address);
      	}
    	}
 	);
	}

	$("cerrarMap_canvas").onclick = function () {
		this.style.display = "none";
		$("map_canvas").style.display = "none";
		GUnload();
	}

	showAddress(direccion);

 

      }
    }

								
								
								archivoAjax = "estudios_pilates_agregar.php";
								$("agregar").onclick = function () {
									this.style.display = "none";
									$Ajax(archivoAjax + urlAjax, {
										avisoCargando: "divCargando",
										cache: true,
										onfinish: function (html) {
											
											$("informacion").innerHTML += html;	
											document.onmousedown = function () { return true; } // mozilla
											
											$("enviarDatos").onclick = chequear;
											
											function chequear () {
												var elementos = $$("#formulario .camposForm");
												var mensajeTemp = "";
												
												var tituloError = "Los siguientes campos están incompletos o tienen errores.\n_______________________________________";
												
												for (var i=0; i<elementos.length; i++) {
													var elemento = elementos[i];
													
													if (elemento.name == "email") {
														if (!(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(elemento.value))) {
															mensajeTemp += "\n"+elemento.name; 
														}
													}
													
													else if (elemento.value == "" || (/^\s+$/.test(elemento.value))) {
														mensajeTemp += "\n"+elemento.name; 
													}
												
												}
												
												if (mensajeTemp != "") {
													mensajeError = tituloError + mensajeTemp;
													mensajeError += "\n_______________________________________\nCompletelos y vuelva a intentar."; 
													alert(mensajeError);
													elementos[0].focus();
												}
												else
													paso2();
											}
												
												
											
											function paso2() {
												archivoAjax = "estudios_pilates_agregar_2.php";
												var valores = $("formulario").serialize();
												$Ajax(archivoAjax, {
													  metodo: $metodo.POST,
													  parametros: valores,
													  onfinish: function (html) {
														  $("informacion").innerHTML = html;
													  }
												});
											}
											
											
											
											
										}
									});
												
								}
								
							}
							
						});
						
					}
				
					obtenerInfo2(archivoAjax + urlAjax);
				}
				
			
			}
	}
	
	
}
function peperina(actual) {
	//alert($("info").innerHTML+" - "+actual);
	if ($("info").innerHTML == actual) {
		$('info').style.display = "none";
		$('info').innerHTML = '';
	}
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}