// Variables globales.
var finalizarfotos = false;
var finalizarisla = false;
var tienda;
var isla;
var map;
var marcas = new Array();
var numimg;
var cerrojo;
var total;

// Cuando se cargen todas las imagenes. !!!
// $(window).load(function(){ $("div#right").css("display", "none");});

$(document).ready(function(){
	
	// Google Maps
	initialize();
	$(document).unload( function(){ GUnload(); });

	$.extend({direction: "left"}, {
		easing: "easeOutQuint"
	});
		
	// Livequery !!!!
	$(".enlace").livequery("click", cargarenlaces);
	$(".punto").livequery(
		function(){
	    	// use the helper function hover to bind a mouseover and mouseout event 
	        $(this).hover(entraMouse, saleMouse);
		},
		function(){
    		// unbind the mouseover and mouseout events 
    		$(this).unbind('mouseover').unbind('mouseout');
		}
	);
	$(".punto").livequery("click", cargarpuntos);
	$('div.fotostiendas').livequery(function(){
		cerrojo = true;
		var tiendas = $(this).find("img");
		total = tiendas.length;
		numimg = 0;
		
		tiendas.each(function(){
			if (!this.complete) {
				$(this).load(function(){
					numimg++;
					if (numimg == total && cerrojo) {
						cerrojo = false;
						$("div.fotostiendas").cycle({
							fx: 'fade',
							before: onBefore,
							after: onBefore
						});
						$("div#tienda").show("slide", {
							direction: "left"
						}, 1000);
					}
				});
			} else {
				numimg++;
				if (numimg == tiendas.length && cerrojo) {
					cerrojo = false;
					$("div.fotostiendas").cycle({
						fx: 'fade',
						before: onBefore,
						after: onBefore
					});
					$("div#tienda").show("slide", {
						direction: "left"
					}, 1000);
				}
			}
		});
	});
	$('a.nyroModalMan').livequery(function(){
		$(this).nyroModal({
			minWidth: 0, // Minimum width
			minHeight: 0
		});
	});
	
/***************************************************************************************************/		
	
    $(".islamap").bind("click", function(event){
		event.preventDefault();		
		isla = $(this);
        if ("none" != $("div#tienda").css("display")) {
            finalizarfotos = true;
            finalizarisla = true;
        }
        else {
            if ("none" != $("div#isla div#puntos div.enlaces").css("display")) {
                $("div#isla div#puntos div.enlaces").hide("slide", {direction: "up"}, 1000);
            }
            if ("none" != $("div#isla").css("display")) {
                $("#isla").hide("slide", {direction: "left"}, 1000, mostrarisla);
            }else{
                mostrarisla();
			}
        }
    });
	
	if(acceso_tienda && acceso_isla){
		$("h1#selec1").hide("drop", { direction: "up" }, 1000);
		$("div#isla").css("background", "url(" + ABS_IMG_PERF + "perfumeria-m1-isla" + acceso_isla + ".jpg) no-repeat");
		$("div#isla").load(BaseUrl + "/index/ajaxisla", {isla: acceso_isla}, function(){
			$("div#isla").show("slide", {direction: "left"}, 1000, function(){
				$("div#tienda").load(BaseUrl + "/index/ajaxtienda", {tienda: acceso_tienda});
				tienda = $("<a href='#' alt='"+ acceso_tienda +"'>tienda</a>");
				isla = new Array();
				isla[0] = new Object();
				isla[0].alt = acceso_isla;
				mostrargaleria();
				mostrarmapa(acceso_tienda);
			});
		});
	}
});

function initialize(){
    if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("mapa"));
		map.addControl(new GLargeMapControl);
		map.addControl(new GMapTypeControl);
		map.setCenter(new GLatLng(28.125283, -15.435791), 7);
		map.addOverlay(createMarker(new GLatLng(28.922937, -13.672153), "<p>C/ Roque Nublo, 10</p><p>Puerto del Carmen, Tías</p><p>Tlf: 928 51 65 24</p>"));
		map.addOverlay(createMarker(new GLatLng(28.919819, -13.654912), "<p>Avda. de las Playas, 57 (C.Cial. La Peñita)</p><p>Puerto del Carmen, Tías</p><p>Tlf: 928 51 62 21</p>"));
		map.addOverlay(createMarker(new GLatLng(28.861795, -13.83168), "<p>C/ Los Limones, 7</p><p>Playa Blanca</p><p>Tlf: 928 51 98 61</p>"));
		map.addOverlay(createMarker(new GLatLng(28.859004, -13.815844), "<p>C/ Prto. Marina Rubicón, L48B</p><p>Playa Blanca</p><p>Tlf: 928 51 89 86</p>"));
		map.addOverlay(createMarker(new GLatLng(28.45925, -16.255978), "<p>C/ C.Cial. 3 de Mayo</p><p>Sta. Cruz de Tenerife</p><p>Tlf: 922 53 53 63</p>"));
		map.addOverlay(createMarker(new GLatLng(28.090183, -16.735611), "<p>C/ C.Cial. Plaza, Local 5-9-10</p><p>Costa Fañabe, Adeje</p><p>Tlf: 922 71 71 04</p>"));
		map.addOverlay(createMarker(new GLatLng(28.089928, -16.737392), "<p>C/ C.Cial. Playa Fañabe, Local 45-47</p><p>Costa Fañabe, Adeje</p><p>Tlf: 922 71 56 49</p>"));
		map.addOverlay(createMarker(new GLatLng(28.089615, -16.736866), "<p>C/ C.Cial. Playa Fañabe, Local 5</p><p>Costa Fañabe, Adeje</p><p>Tlf: 922 71 30 21</p>"));
		map.addOverlay(createMarker(new GLatLng(28.051625, -16.718992), "<p>C/ Juan XXIII, 8, 38640</p><p>Los Cristianos, Arona</p><p>Tlf: 928 79 04 87</p>"));
		map.addOverlay(createMarker(new GLatLng(28.052013, -16.717951), "<p>C/ Juan XXIII, 18, 38640</p><p>Los Cristianos, Arona</p><p>Tlf: 922 79 43 23</p>"));
		map.addOverlay(createMarker(new GLatLng(28.135237, -15.433441), "<p>C/ Galicia, 29, 35010</p><p>Las Palmas de G.C.</p><p>Tlf: 928 22 85 18</p>"));
		map.addOverlay(createMarker(new GLatLng(28.105298, -15.415846), "<p>C/ Mayor de Triana, 52, 35002</p><p>Las Palmas de G.C.</p><p>Tlf: 928 38 49 55</p>"));
		map.addOverlay(createMarker(new GLatLng(28.10315, -15.414902), "<p>C/ San Pedro, 4, 35002</p><p>Las Palmas de G.C.</p><p>Tlf: 928 36 21 10</p>"));
		map.addOverlay(createMarker(new GLatLng(28.103112, -15.415074), "<p>C/ San Pedro, 7, 35002</p><p>Las Palmas de G.C.</p><p>Tlf: 928 38 52 34</p>"));
		map.addOverlay(createMarker(new GLatLng(28.138605, -15.42738), "<p>Avda. de Italia, 9, 35100</p><p>San Bartolomé de Tirajana</p><p>Tlf: 928 76 65 18</p>"));
		map.addOverlay(createMarker(new GLatLng(27.738879, -15.601675), "<p>C/ Mar Mediterraneo, 35100</p><p>San Bartolomé de Tirajana</p><p>Tlf: 928 73 00 76</p>"));
		map.addOverlay(createMarker(new GLatLng(27.767254, -15.608826), "<p>Avda. Touroperador Holland Int., 35100</p><p>San Bartolomé de Tirajana</p><p>Tlf: 902 200 425</p>"));
		map.addOverlay(createMarker(new GLatLng(27.751347, -15.575636), "<p>Avda. de Francia, 2, 35100</p><p>San Bartolomé de Tirajana</p><p>Tlf: 928 38 52 34</p>"));
		map.addOverlay(createMarker(new GLatLng(27.751328, -15.575711), "<p>C/ El Pez, 6, 35100</p><p>San Bartolomé de Tirajana</p><p>Tlf: 928 38 52 34</p>"));
		map.addOverlay(createMarker(new GLatLng(28.120439, -15.524025), "<p>C/ Poeta Pedro Lezcano, Local 12, 35400</p><p>Arucas</p><p>Tlf: 928 62 35 12</p>"));
	}
}

function createMarker(point, message){
    var marker = new GMarker(point);
	marcas.push(marker);
    GEvent.addListener(marker, "click", function(){
        var myHtml = message;
		map.setZoom(15);
        map.openInfoWindowHtml(point, myHtml);
    });
    return marker;
}

function mostrarisla(){
	if ("none" != $("h1#selec1").css("display")) {
		$("h1#selec1").hide("drop", {
			direction: "up"
		}, 1000, function(){
			$("h1#selec2").show("drop", {
				direction: "up"
			}, 1000, function(){
				$(this).css("filter", "");
			});
		});
	} else {
		if ("none" != $("h1#selec3").css("display")) {
			$("h1#selec3").hide("drop", {
				direction: "up"
			}, 1000, function(){
				$("h1#selec2").show("drop", {
					direction: "up"
				}, 1000, function(){
					$(this).css("filter", "");
				});
			});
		} else {
			if ("none" == $("h1#selec2").css("display")) {
				$("h1#selec2").show("drop", {
					direction: "up"
				}, 1000, function(){
					$(this).css("filter", "");
				});
			}
		}
	}
	$("div#isla").css("background", "url(" + ABS_IMG_PERF + "perfumeria-m1-isla" + isla.attr("alt") + ".jpg) no-repeat");
	$("div#isla").load(BaseUrl + "/index/ajaxisla", {isla: isla.attr("alt")}, function(){
		$("div#isla").show("slide", {direction: "left"}, 1000);
	});
}

function mostrartienda(){
	if ("none" != $("h1#selec3").css("display")) {
		$("h1#selec3").hide("drop", {
			direction: "up"
		}, 1000);
	}
	var lugar = tienda.attr("alt");
	$("div#tienda").load(BaseUrl + "/index/ajaxtienda", {tienda: lugar});
	mostrargaleria();
	mostrarmapa(lugar);
}

function mostrarmapa(tienda){
	if ("none" == $("div#right").css("display")) {
		$("div#right").show("slide", {direction: "up"}, 1000, function(){
			GEvent.trigger(marcas[tienda], 'click');
		});
	}else{
		GEvent.trigger(marcas[tienda], 'click');
	}
}

function cargarenlaces(event){
	event.preventDefault();
	tienda = $(this);
	if ("none" != $("div#tienda").css("display")) {
		finalizarfotos = true;
	} else {
		var lugar = tienda.attr("alt");
		mostrartienda();
	}
}

// Asociamos eventos a puntos.
function cargarpuntos(){
	if ("none" != $("h1#selec2").css("display")) {
		$("h1#selec2").hide("drop", {
			direction: "up"
		}, 1000, function(){
			$("h1#selec3").show("drop", {
				direction: "up"
			}, 1000, function(){
				$(this).css("filter", "");
			});
		});
	} else {
		if ("none" == $("h1#selec3").css("display") && "none" == $("div#tienda").css("display")) {
			$("h1#selec3").show("drop", {
				direction: "up"
			}, 1000, function(){
				$(this).css("filter", "");
			});
		}
	}
	var zona = $(this).attr("alt");
	if("none" != $("div#isla div#puntos div.enlaces").css("display")){
		$("div#isla div#puntos div.enlaces").hide("slide", {direction: "up"}, 1000, function(){
			$("div#isla div#puntos div.enlaces").empty();
			$("div#isla div#puntos div.enlaces").append($("div#isla div#puntos div#"+zona+" > *").clone(true));
			$("div#isla div#puntos div.enlaces").show("slide", {direction: "up"}, 1000);
		});
	}else{
		$("div#isla div#puntos div.enlaces").empty();
		$("div#isla div#puntos div.enlaces").append($("div#isla div#puntos div#"+zona+" > *").clone(true));
		$("div#isla div#puntos div.enlaces").show("slide", {direction: "up"}, 1000);
	}
}

function onBefore(ant, act, options, d){
	if(finalizarfotos){
		$("div#tienda div#tiendafinal img").remove(":hidden");
		$("div#tienda div#tiendafinal img").attr("style", "");
		if(!finalizarisla){
			$("div#tienda").hide("slide", {direction: "left"}, 1000, mostrartienda);
		}else{
			$("div#tienda").hide("slide", {direction: "left"}, 1000, function(){
				if("none" != $("div#isla div#puntos div.enlaces").css("display")){
					$("div#isla div#puntos div.enlaces").hide("slide", {direction: "up"}, 1000);
				}
				if ("none" != $("div#isla").css("display")) {
					$("#isla").hide("slide", {direction: "left"}, 1000, mostrarisla);
				}
			});
			finalizarisla = false;
		}
		finalizarfotos = false;
	}
}

function mostrargaleria(){
	if ("none" != $("div#galeria").css("display")) {
		$("div#galeria").hide("slide", {
			direction: "up"
		}, 1000, function(){
			$("div#galeria").load(BaseUrl + "/index/ajaxgaleria", {
				tienda: tienda.attr("alt"),
				isla: isla[0].alt
			}, function(){
				$("div#galeria div").css({opacity: 0});
				$("div#galeria").css({opacity: 0}).show();
				$("div.jcarousel").jCarouselLite({
					btnNext: ".next",
					btnPrev: ".prev",
					auto: 5000,
					speed: 1000,
					width: 70,
					height: 60,
					inicio: function(){
						$("div#galeria div").css({opacity: 1}).css("filter", "");
						$("div#galeria").hide().css({opacity: 1}).css("filter", "");
						$("div#galeria").show("slide", {direction: "up"}, 1000);
					}
				});
			});
		});
	}
	else {
		$("div#galeria").load(BaseUrl + "/index/ajaxgaleria", {
			tienda: tienda.attr("alt"),
			isla: isla[0].alt
		}, function(){
			$("div#galeria div").css({opacity: 0});
			$("div#galeria").css({opacity: 0}).show();
			$("div.jcarousel").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				auto: 5000,
				speed: 1000,
				width: 70,
				height: 60,
				inicio: function(a){
					$("div#galeria div").css({opacity: 1}).css("filter", "");
					$("div#galeria").hide().css({opacity: 1}).css("filter", "");
					$("div#galeria").show("slide", {direction: "up"}, 1000);
				}
			});
		});
	}
}

function entraMouse(){
	var x;
	x = $(this);
	x.attr("src", ABS_IMG_PERF + "puntomas.png");
}

function saleMouse(){
	var x;
	x = $(this);
	x.attr("src", ABS_IMG_PERF + "punto.png");
}
