// Variables Globales
var imgLeft = 0;
var imgTop = 0;
var imgWidth = 1200;
var imgHeight = 365;

var timer;
var id_Gen;

function startAnim(id){
	clearTimeout(timer);
	$(".obj_" + id).stop().fadeTo(300, 1.0);
	id_Gen = id+1;
	timer = window.setTimeout('startAnim(id_Gen)', 50);
}

function cerrarMenu(menu){
	menu.toggleClass('abierto');
	menu.stop().animate({height: "30px"}, 400, "easeInOutQuad");
	$('#estudio').stop().animate({top: "-60px"}, 400, "easeInOutQuad");
	$('#head-handle').stop().animate({top: "-27px"}, 400, "easeInOutQuad");
	$('#aux, #aux-news').stop().animate({top: "-10px"}, 400, "easeInOutQuad");
	$('#aula, #contact').stop().animate({top: "0px"}, 400, "easeInOutQuad");
	$('#pie .wrapper, #pie .fade, #pie .sellos').stop().animate({'padding-top': "0px"}, 400, "easeInOutQuad");
	$('.fade').stop().fadeTo(700, 0.0, function() { $(this).hide(); });
}

function abrirMenu(menu){
	menu.toggleClass('abierto');
	menu.stop().animate({height: "125px"}, 400, "easeInOutQuad");
	$('#estudio').stop().animate({top: "0px"}, 400, "easeInOutQuad");
	$('#head-handle').stop().animate({top: "0px"}, 400, "easeInOutQuad");
	$('#aux, #aux-news').stop().animate({top: "50px"}, 400, "easeInOutQuad");
	$('#aula, #contact').stop().animate({top: "60px"}, 400, "easeInOutQuad");
	$('#pie .wrapper, #pie .fade, #pie .sellos').stop().animate({'padding-top': "60px"}, 400, "easeInOutQuad");
	$('.fade').stop().show().fadeTo(700, 0.6);
}

$(document).ready(function() {
	
	$('a').click(function() {
		if($(this).attr('rel') == 'external') {
			$(this).attr('target', '_blank');
		}
	});
	
	if ($('#aux-tweets').length) {
		$('#aux-tweets .ul').startTwitter();
	}

	if (!$('#head-handle').hasClass('abierto')) {
		$('#head-container .cover').css({height: "105px"});
		$('#cover .content').css({top: '-130px'});
	}
	
	$('#aux .li:not([class~=current]) div, #aux-news .li:not([class~=current]) div').css({height: "0px"});
	
	$('.fade').fadeTo(0, 0.0).hide();
	$('.fade-blue').fadeTo(0, 0.0).hide();
	
	$('.innovacion').mouseenter(function(){
		startAnim(1);
		$('.fade-blue').stop().show().fadeTo(400, 0.4);
	}).mouseleave(function(){
		clearTimeout(timer);
		$("#innova .obj").stop().fadeTo(100, 0.0);
		$('.fade-blue').stop().fadeTo(400, 0.0, function() { $(this).hide(); });
	});
	
	// Acordeón de Noticias
	$('#aux h3, #aux-news h3').click(function() {
		if($(this).parent().hasClass('current')) {
			$(this).parent().toggleClass('current').next().toggleClass('linea');
			$(this).siblings('div').stop().animate({height: "0px"}, 400, "jswing", function() {
			});
		} else {
			var current = $('#aux .li.current, #aux-news .li.current');
			current.toggleClass('current').next().toggleClass('linea');
			//$('div',current).stop().animate({height: "0px"}, 400, "jswing", function() {});
			$('div',current).syncAnimate($(this).siblings('div').not(this), {height: "0px"}, {duration:400, queue: false});
			$(this).parent().toggleClass('current').next().toggleClass('linea');
			var alto = 45;
			//if ($(this).siblings('.noticia-content img:first').length){
			if ($(this).parent().find('.noticia-content img:first').length){
				alto += $(this).siblings('.noticia-content img:first').height();
				//alert(alto);
			}
			if ($(this).parent().find('p:first').length){
				$(this).parent().find('p').each(function(index) {
					alto += $(this).height();
				});
				alto -= 35;
				if ($('#aux-news').length) { alto -= 20; };
				alto += ($(this).parent().find('p').length*5)-5;
				//alto += $(this).parent().find('p:first').height();
			}else {
				alto -= 5;
			}
			alto -= 5;
			
			//if ($('#aux-news').length) { alto -= 5; }
			
			//$(this).siblings('div').stop().animate({height: $(this).siblings('.noticia-content p:first').height()+alto+'px'}, 400, function() { });
			$(this).siblings('div').stop().animate({height: alto+'px'}, 400, function() { });
		}
		return false;
	});
	
	// Mostrar-Ocultar Cabecera
	$('#head-handle').click(function() {
		if ($(this).hasClass('abierto')) {
			$(this).toggleClass('abierto');
			$('#head-container .cover').stop().animate({height: "105px"}, 600, "easeInOutQuad");
			$('#cover .content').stop().animate({top: '-130px'}, 600, "easeInOutQuad");
			if (!$('#menu').hasClass('abierto')) {
				//$('#aux').stop().animate({top: "30px"}, 400);
			}
			if ($('#head-acc').hasClass('abierto')) {
				$('#head-acc').toggleClass('abierto');
				$('#fast-access').stop().animate({top: '-200px'}, 600, "easeInOutQuad");
				$('#form-login').fadeTo(600,0, function() { $(this).hide(); });
			}
			/*if ($('#head-reg').hasClass('abierto')) {
				$('#head-reg').toggleClass('abierto');
				$('#fast-access').stop().animate({top: '-200px'}, 600, "easeInOutQuad");
				$('#text-slider').stop().animate({top: '135px'}, 600, "easeInOutQuad");
				$('#form-registro').fadeTo(600,0, function() { $(this).hide(); });
			}*/
		} else {
			$(this).toggleClass('abierto');
			$('#head-container .cover').stop().animate({height: "365px"}, 600, "easeInOutQuad");
			$('#cover .content').stop().animate({top: '0px'}, 600, "easeInOutQuad");
			if (!$('#menu').hasClass('abierto')) {
				//$('#aux').stop().animate({top: "-10px"}, 400);
			}
		}
		return false;
	});
	
	// Mostrar-Ocultar Login
	$('#head-acc').click(function() {
		$('#head-reg').removeClass('abierto');
		if ($(this).hasClass('abierto')) {
			$(this).toggleClass('abierto');
			$('#fast-access').stop().animate({top: '-200px'}, 600, "easeInOutQuad");
			$('#form-login').fadeTo(600,0, function() { $(this).hide(); });
			if (!$('#head-handle').hasClass('abierto')) {
				$('#head-container .cover').stop().animate({height: "105px"}, 600, "easeInOutQuad");
				$('#cover .content').stop().animate({top: '-130px'}, 600, "easeInOutQuad");
			}
			
		} else {
			$(this).toggleClass('abierto');
			$('#fast-access').stop().animate({top: '-100px'}, 600, "easeInOutQuad");
			$('#form-registro').fadeTo(600,0, function() { $(this).hide(); });
			$('#form-login').fadeTo(600,1);
			if (!$('#head-handle').hasClass('abierto')) {
				$('#head-container .cover').stop().animate({height: "165px"}, 600, "easeInOutQuad");
				$('#cover .content').stop().animate({top: '-100px'}, 600, "easeInOutQuad");
			}
		}
		$('#text-slider').stop().animate({top: '135px'}, 600, "easeInOutQuad");
		return false;
	});
	
	// Mostrar-Ocultar Registro
	/*$('#head-reg').click(function() {
		$('#head-acc').removeClass('abierto');
		if ($(this).hasClass('abierto')) {
			$(this).toggleClass('abierto');
			$('#fast-access').stop().animate({top: '-200px'}, 600, "easeInOutQuad");
			$('#text-slider').stop().animate({top: '135px'}, 600, "easeInOutQuad");
			$('#form-registro').fadeTo(600,0, function() { $(this).hide(); });
			if (!$('#head-handle').hasClass('abierto')) {
				$('#head-container .cover').stop().animate({height: "105px"}, 600, "easeInOutQuad");
				$('#cover .content').stop().animate({top: '-130px'}, 600, "easeInOutQuad");
			}
			
		} else {
			$(this).toggleClass('abierto');
			$('#fast-access').stop().animate({top: '-42px'}, 600, "easeInOutQuad");
			$('#text-slider').stop().animate({top: '193px'}, 600, "easeInOutQuad");
			$('#form-login').fadeTo(600,0, function() { $(this).hide(); });
			$('#form-registro').fadeTo(600,1);
			if (!$('#head-handle').hasClass('abierto')) {
				$('#head-container .cover').stop().animate({height: "223px"}, 600, "easeInOutQuad");
				$('#cover .content').stop().animate({top: '-75px'}, 600, "easeInOutQuad");
			}
		}
		return false;
	});*/
	
	
	
	// Abrir-Cerrar Menu
	$('.sub').click(function(e) {
		e.preventDefault();
		var menu = $('#menu');
		if (menu.hasClass('abierto')) {
			if ($(this).siblings('.submenu').is(':visible')) {
				cerrarMenu(menu);
			} else {
				//$('.submenu').fadeTo(200, 0);
				//$(this).siblings('.submenu').fadeTo(200, 1);
				$('.submenu').hide();
				$(this).siblings('.submenu').show();
			}
		} else {
			$('.submenu').hide();
			$(this).siblings('.submenu').show();
			abrirMenu(menu);
		}
		return false;
	});
	
	// Cerrar Menu
	$('.fade').click(function(e) {
		e.preventDefault();
		//e.stopPropagation();
		var menu = $('#menu');
		if (menu.hasClass('abierto')) {
			cerrarMenu(menu);
		}
		return false;
	});
	
	// FullScreenr
	var FullscreenrOptions = { width: 1200, height: 365, maxHeight: 365};
	
	var i = 1;
	$('#cover .content img').each(function(index) {
		var bgID = '#bg'+i;
		jQuery.fn.fullscreenr(FullscreenrOptions,bgID);
		i++;
	});
	
	if ($('#videoWall').length != 0) {
		$('#videoWall').flash({
   		'src':'http://www.axia.es/default/documentos/videoWall.swf',
   		'width':'1200',
   		'height':'365',
   		'allowfullscreen':'false',
   		'allowscriptaccess':'always',
   		'wmode':'opaque',
   		'flashvars': {
   			'flvid':'http://www.axia.es/default/documentos/defocused_lights_03.flv'
			}
		});
		jQuery.fn.fullscreenr(FullscreenrOptions,'#videoWall');
	};
	
	// Sliders
	$('#menu-slider .content').cycle({ 
		fx:     'scrollHorz',
		speed:  'fast',
		timeout: 0,
		next:   '#menu-slider .right',
		prev:   '#menu-slider .left'
	});
	
	$('#text-slider .content').cycle({ 
		fx:     'scrollHorz',
		speed:  'fast',
		timeout: 0,
		//timeout: 10000,
		next:   '#text-slider .right',
		prev:   '#text-slider .left'
	});
	
	$('#cover .content').cycle({ 
		fx:     'fade',
		speed:  800,
		timeout: 0,
		next:   '#text-slider .right',
		prev:   '#text-slider .left',
		//timeout: 10000,
		before: onBefore
	});
	
	$('#fotos').cycle({ 
		fx:     'fade',
		speed:  800,
		timeout: 0,
		next:   '.navFotos .right',
		prev:   '.navFotos .left',
		before: onBefore
	});
 
	function onBefore() {
		//alert(imgLeft);
		//$(this).css('left', imgLeft);
		//$(this).css('top', imgTop);
		//alert(imgWidth);
		//$(this).css('width', imgWidth);
		//$(this).css('height', imgHeight);
		//alert($(this).attr('id'));
		jQuery.fn.fullscreenr(FullscreenrOptions,$(this).attr('id'));
	};
	
	
	// Casos de Estudio - Proyectos
/*
	$('#estudio').click(function() {
		window.location.assign($(this).find('a').attr('href'));
	});
*/
	
	$('.sector_home').click(function() {
		window.location.assign($(this).find('a').attr('href'));
	});
	
	// Aula Interactiva
	$('#contact').click(function() {
		window.location.assign($(this).find('a').attr('href'));
	});
	
	$('#aula').click(function() {
		$(this).stop().animate({'margin-bottom': "75px"}, 400, "easeInOutQuad");
		return false;
	});
	
	$('.submenu a:last').css('margin-left', '40px');
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('.submenu, .tic').remove();
	}
	
});
