$(document).ready(function(e) {
	
	ajustarFooter();
	cargarScripts();
	cargarMapa();
	
	$(window).resize(function(e) {
		ajustarLigthBox();
	});
	
	$('a[href="#"]').click(function(e) {
		e.preventDefault();
	});
	
	$('#ligth-box').click(function(e) {
		esconderLigthBox();
	});
	
	$('form .input').change(function(e) {
		if($(this).hasClass('error') && $(this).val() != '')
			$(this).removeClass('error');
	});
	
	$('a[id^="item-"]').click(function(e) {
		var a = $(this);
		
		if(!a.hasClass('active')) {
		
			if(a.is('a[class*="section-"]') ) {
				$('a[id!="item-submenu"].active').removeClass('active');
				
				if($('a#item-submenu').hasClass('active') && a.hasClass('level-0')) {
					mostraresconderSubmenu();
					$('a#item-submenu.active').removeClass('active');
				}
			}
			
			a.addClass('active');
		}
		
		if(a.attr('id') == 'item-submenu')
			mostraresconderSubmenu();
		
		if(a.hasClass('item-contact-open')) {
			$('a.item-contact-open').addClass('active');
			mostrarFormularioContacto();
		}
		
		if(a.attr('id') == 'item-contact')
			enviarMensaje();
		
		if(a.hasClass('box-close'))
			esconderLigthBox();
		
		if(a.attr('id') == 'item-map-open') {
			mostrarMapa();
		}
		
		if(a.attr('id') == 'item-page-top') {
			irInicio();
			a.removeClass('active');
		}
	});
	
	$('a.external').click(function(e) {
		e.preventDefault();
		window.open($(this).attr('href'));
	});
});

function cargarScripts() {
	$.getScript('lib/js/pngfix.js', function() {
		$(document).pngFix();
	});
	
	// Google Maps Key para beta.tigabytes.com
	//$.getScript('http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAAMlFek0JUK71rVXiEZZXG3BRFeVYkWWvnSSScFvHB4wNwzWuDuxR29DWs4ihD7YPgiVH2JEh3Lr0zIA');
	
	// Google Maps Key para tigabytes.com
	//$.getScript('http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAA-WClqReOyKLLq9ZKrNY3zRTgSDpzoaQqQ5jU73Gpn_miCi1ovxSPb6DutdwBOp5HtFG41m82HxPsgw');
}

function ajustarLigthBox() {
	$('#ligth-box').css({
		'width' : $(window).width(),
		'height' : $(window).height(),
		'opacity' : .5
	});
	
	$('.box').each(function() {
		$(this).css({
			'top' : ($(window).height()-$(this).height())/2,
			'left' : ($(window).width()-$(this).width())/2
		});
	});
}

function mostarLigthBox(box_id) {
	ajustarLigthBox();
	$('#ligth-box, '+box_id).fadeIn('fast').addClass('active');
}

function esconderLigthBox() {
	$('#ligth-box.active, .box.active').fadeOut('fast').removeClass('active');
	$('.ligth-box.active').removeClass('active');
}

function mostraresconderSubmenu() {
	if(!$('ul.submenu li a').hasClass('active'))
		$('ul.submenu').slideToggle('fast').toggleClass('active');
}

function mostrarFormularioContacto() {
	$('#contact .input').each(function() {
		if($(this).hasClass('error'))
			$(this).removeClass('error');
		if($(this).val() != '')
			$(this).val('');
	});
	
	mostarLigthBox('#contact');
}

function mostrarMapa() {
	mostarLigthBox('#map');
}

function enviarMensaje() {
	var enviar = true;
	
	$('#contact .input').each(function() {
		if($(this).val() == '') {
			$(this).addClass('error');
			enviar = false;
		} else if($(this).hasClass('error'))
			$(this).removeClass('error');
	});
	
	if(enviar) {
		$.post('send.php', $('#contact-form').serialize(), function(data) {estadoMensaje(data);});
	}
}

function estadoMensaje(valor) {
	if(valor == 'true')
		esconderLigthBox();
}

function cargarMapa() {
	if(GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('map-top'));
		var tigabytes = new GLatLng(-33.4259, -70.61332);
		map.setCenter(new GLatLng(-33.4247, -70.61332), 17);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addOverlay(new GMarker(tigabytes));		
		map.openInfoWindowHtml(tigabytes, '<a href="http://www.tigabytes.com" title="Tigabytes" class="alignleft logo"><img src="lib/images/tigabytes_logo_a.png" alt="Tigabytes logo"></a><div><strong><a href="http://www.tigabytes.com" title="Tigabytes">Tigabytes S.A.</a></strong><br/>Fidel Oteiza 1916, 13<sup>th</sup> Floor<br/>Providencia<br/>Santiago de Chile</div>');
	}
	
	$('#map').hide();
}

function irInicio() {
	$('html, body').animate({
		'scrollTop' : 0
	}, 'slow');
}

function ajustarFooter() {
	if($('body, html').height() < $(window).height()) {
		$('#main-wrapper').height($(window).height()-$('#footer-wrapper').height()-20);
	}
}
