﻿var ssActual;
var directionDisplay;
var directionsService;
var map;
var wayra;
var usuario;

$(document).ready(init);
function init() {
	pongoSubseccion(true);
	ajustarPagina();
	ajustarBotonera();
	activoSubBotones();
	$(window).resize(ajustarPagina);
}

function localizar() {
	directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer();
	wayra = new google.maps.LatLng(-12.062278, -77.089884);
	var myOptions = {
		zoom: 15,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		center: wayra
	};
	map = new google.maps.Map(document.getElementById('ss3'),myOptions);
	directionsDisplay.setMap(map);

	// Try HTML5 geolocation
	if(navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function(position) {
			usuario = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

			var infowindowUsu = new google.maps.InfoWindow({
				map: map,
				position: usuario,
				content: 'Ubicación actual.'
			});
			
			var infowindowWayra = new google.maps.InfoWindow({
				map: map,
				position: wayra,
				content: 'Wayra Escuela Deportiva.'
			});
			
			/*var markerUsu = new google.maps.Marker({
				position: usuario,
				map: map,
				title: 'Ubicación actual.'
			});
			
			var markerWayra = new google.maps.Marker({
				position: wayra,
				map: map,
				title: 'Wayra Escuela Deportiva.'
			});*/
			
			map.setCenter(usuario);
			calcRoute();
		}, function() {
			handleNoGeolocation(true);
		});
	} else {
		// Browser doesn't support Geolocation
		handleNoGeolocation(false);
	}
}

function calcRoute() {
	var request = {
		origin: usuario, 
		destination: wayra,
		// Note that Javascript allows us to access the constant
		// using square brackets and a string value as its
		// "property."
		travelMode: google.maps.DirectionsTravelMode['DRIVING']
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		}
	});
}

function handleNoGeolocation(errorFlag) {
	if (errorFlag) {
		$('.alerta').text('Google no pudo encontrar tu ubicación.');
		var content = 'Wayra - Escuela Deportiva';
	} else {
		$('.alerta').text('La version de tu navegador no es compatible.');
		var content = 'Wayra - Escuela Deportiva';
	}

	var options = {
		map: map,
		position: new google.maps.LatLng(-12.062278, -77.089884),
		content: content
	};
	
	var marker = new google.maps.Marker({
		position: options.position,
		map: map,
		title: content
	});

	var infowindow = new google.maps.InfoWindow(options);
	map.setCenter(options.position);
}

function ajustarPagina() {
	var banner_h = $('#banner_top').height();
	var cabecera_h = $('#cabecera').height();
	var lateral_h = $('#lateral').height();
	var contenido_h = $('#contenido').height();
	var contenido_texto_h = maxTextoHeight();
	var pie_h = $('#pie').height();
	
	var document_h = $(window).height();
	var rest_document_h = document_h - pie_h - cabecera_h;
	
	if(contenido_texto_h < 426) {
		contenido_texto_h = 426;
	}
	var section_h = (contenido_texto_h + banner_h / 2 + 7);
	var rest_section_h = section_h;
	
	var rest_contenido_h;
	if(section_h < document_h - pie_h - cabecera_h) {
		rest_contenido_h = rest_document_h;
	} else {
		rest_contenido_h = rest_section_h;
	}
	
	$('#contenido').height(rest_contenido_h);
	$('#seccion').height(rest_contenido_h);
	
	$('#fondo_gris').height(rest_contenido_h);
	$('#fondo_lineas').height(rest_contenido_h);
	$('#lateral').height(rest_contenido_h);
	$('#contenido').height(rest_contenido_h);
	$('#index').height(rest_contenido_h + pie_h);
	$('#main').height(rest_contenido_h + pie_h);
	//alert(section_h + 'px');
	$('#pie').css({'top':rest_contenido_h + 'px'});
	
	$('#fondo_gris').fadeTo(0,0.5);
	$('#fondo_lineas').fadeTo(0,0.1);

	jQuery.each(jQuery.browser, function(i, val) {
		if(i == 'msie') {
			if(val){
				$('#sombra_lteIE8').height(rest_contenido_h + pie_h);
			}
		}
	});
}

function maxTextoHeight() {
	var h = 0;
	for(var i = 1; i < 10; i++) {
		if($('#b' + i)[0] != 'undefined') {
			if($('#ss' + i).height() > h) {
				h = $('#ss' + i).height()
			}
		}
	}
	return h + $('#banner_top').height() * 2;
}

function ajustarBotonera() {
	var botones = ['inicio','quienes_somos','donde_entrenamos','staff','inscripciones','galerias','contactanos']
	var menu_w = 0;
	
	for(i = 0; i < botones.length; i++){
		menu_w += $('#' + botones[i]).width();
	}
	
	var rest_w = $('#botones').width();
	var div = Math.round((rest_w - menu_w) / (botones.length * 2));
	div = div - (div / (botones.length * 2))
	
	var rest_h = $('#botones').height() - $('#' + botones[0]).height();
	var top = Math.round(rest_h / 2);
	var bottom = rest_h - top;
	
	for(ii = 0; ii < botones.length; ii++){
		$('#' + botones[ii] + ' a').css({'padding':top + 'px ' + div + 'px ' + bottom + 'px ' + div + 'px '});
	}
	
	$('#botones').css({'margin-right':'-' + (div/2) + 'px'});
}

function activoSubBotones() {
	for(var i = 1; i < 10; i++) {
		if($('#b' + i)[0] != undefined) {
			var boton = $('#b' + i);
			boton.bind("mouseover",overSubseccion);
			boton.bind("mouseout",outSubseccion);
			boton.bind("click",pongoSubseccion);
		}
	}
}

function overSubseccion() {
	var valor = $(this)[0].id.split('b').join('');
	$('#b' + valor).css({'border-right-style': 'solid','color':'#3f5f00','font-weight':'bold'});
}

function outSubseccion() {
	var valor = $(this)[0].id.split('b').join('');
	if(valor != ssActual){
		$('#b' + valor).css({'border-right-style': 'none','color':'black','font-weight':'normal'});
	}
}

function pongoSubseccion(init) {
	if(init == true) {
		$('.contenido_subseccion').hide();
		$('#ss1').fadeIn('fast');
		ssActual = 1;
		$('#b' + ssActual).css({'border-right-style': 'solid','color':'#3f5f00','font-weight':'bold'});
		ajustarPagina();
	} else {
		var valor = $(this)[0].id.split('b').join('');
		if(valor != ssActual){
			$('#ss' + ssActual).hide();
			$('#ss' + valor).fadeIn('fast');
			$('#b' + valor).css({'border-right-style': 'solid','color':'#3f5f00','font-weight':'bold'});
			$('#b' + ssActual).css({'border-right-style': 'none','color':'black','font-weight':'normal'});
			ssActual = valor;
			ajustarPagina();
		}
		
		if(valor == 2) {
			//alert(valor);
			if($('.geolocalizar')[0] != undefined) {
				$('.geolocalizar').show();
				$('.alerta').show();
				localizar();
			} else {
				$('.geolocalizar').hide();
				$('.alerta').hide();
			}
		} else {
			$('.geolocalizar').hide();
			$('.alerta').hide();
		}
	}
}
