var animationFlag = 0;

$(document).ready(function(){
	/****** HOVER EVENTS ******/
	
	/* LA EMPRESA */
	$('li#empresa a.main_menu_link').hover(
		function(){
			if(animationFlag == 0){
				$('li#empresa div.yellow_fill').show();
			}
		},
		
		function(){
			if(!$('li#empresa a.main_menu_link').hasClass('active')){
				$('li#empresa div.yellow_fill').hide();
			}
		}
	);
	
	/* EL PAIS */
	$('li#pais a.main_menu_link').hover(
		function(){
			if(animationFlag == 0){
				$('li#pais div.yellow_fill').show();
			}
		},
		
		function(){
			if(!$('li#pais a.main_menu_link').hasClass('active')){
				$('li#pais div.yellow_fill').hide();
			}
		}
	);
	
	/* REEL */
	$('li#reel a.main_menu_link').hover(
		function(){
			if(animationFlag == 0){
				$('li#reel div.yellow_fill').show();
			}
		},
		
		function(){
			if(!$('li#reel a.main_menu_link').hasClass('active')){
				$('li#reel div.yellow_fill').hide();
			}
		}
	);
	
	/* PRODUCCIONES */
	$('li#producciones a.main_menu_link').hover(
		function(){
			if(animationFlag == 0){
				$('li#producciones div.yellow_fill').show();
			}
		},
		
		function(){
			if(!$('li#producciones a.main_menu_link').hasClass('active')){
				$('li#producciones div.yellow_fill').hide();
			}
		}
	);
	
	/* LOCACIONES */
	$('li#locaciones a.main_menu_link').hover(
		function(){
			if(animationFlag == 0){
				$('li#locaciones div.yellow_fill').show();
			}
		},
		
		function(){
			if(!$('li#locaciones a.main_menu_link').hasClass('active')){
				$('li#locaciones div.yellow_fill').hide();
			}
		}
	);
	
	/* CONTACTO */
	$('li#contacto a.main_menu_link').hover(
		function(){
			if(animationFlag == 0){
				$('li#contacto div.yellow_fill').show();
			}
		},
		
		function(){
			if(!$('li#contacto a.main_menu_link').hasClass('active')){
				$('li#contacto div.yellow_fill').hide();
			}
		}
	);

	/****** CLICK EVENTS ******/
	
	/* LA EMPRESA */
	$('li#empresa a.main_menu_link').click(
		function(){
			if(animationFlag == 0){
				openContainer('li#empresa', 'div#empresa_content', 'li#footer_empresa');
			}
		}
	);
	
	/* EL PAIS */
	$('li#pais a.main_menu_link').click(
		function(){
			if(animationFlag == 0){
				openContainer('li#pais', 'div#el_pais_content', 'li#footer_pais');
			}
		}
	);
	
	/* REEL */
	$('li#reel a.main_menu_link').click(
		function(){
			if(animationFlag == 0){
				openContainer('li#reel', 'div#reel_content', 'li#footer_reel');
			}
		}
	);
	
	/* PRODUCCIONES */
	$('li#producciones a.main_menu_link').click(
		function(){
			if(animationFlag == 0){
				openContainer('li#producciones', 'div#producciones_content', 'li#footer_producciones');
			}
		}
	);
	
	/* LOCACIONES */
	$('li#locaciones a.main_menu_link').click(
		function(){
			if(animationFlag == 0){
				openContainer('li#locaciones', 'div#locaciones_content', 'li#footer_locaciones');
			}
		}
	);
	
	/* CONTACTO */
	$('li#contacto a.main_menu_link').click(
		function(){
			if(animationFlag == 0){
				openContainer('li#contacto', 'div#contacto_content', 'li#footer_contacto');
			}
		}
	);
	
	/* FOOTER CLICK EVENTS */
	$('div#sitemap_container ul li#footer_empresa a').click(function(){
		$('li#empresa div.yellow_fill').show();
		$('li#empresa a.main_menu_link').click();
	});
	
	$('div#sitemap_container ul li#footer_pais a').click(function(){
		$('li#pais div.yellow_fill').show();
		$('li#pais a.main_menu_link').click();
	});
	
	$('div#sitemap_container ul li#footer_reel a').click(function(){
		$('li#reel div.yellow_fill').show();
		$('li#reel a.main_menu_link').click();
	});
	
	$('div#sitemap_container ul li#footer_producciones a').click(function(){
		$('li#producciones div.yellow_fill').show();
		$('li#producciones a.main_menu_link').click();
	});
	
	$('div#sitemap_container ul li#footer_locaciones a').click(function(){
		$('li#locaciones div.yellow_fill').show();
		$('li#locaciones a.main_menu_link').click();
	});
	
	$('div#sitemap_container ul li#footer_contacto a').click(function(){
		$('li#contacto div.yellow_fill').show();
		$('li#contacto a.main_menu_link').click();
	});
});

function openContainer(liElement, divElement, footerLi){
	var li_a = liElement + ' a.main_menu_link';
	var yellowFill = liElement + ' div.yellow_fill';
	var yellowBar = liElement + ' div.full_yellow_bar';
	var footerLink = 'div#sitemap_container ul ' + footerLi + ' a';
	
	if(animationFlag == 0){
		animationFlag = 1;
		
		if(!$(li_a).hasClass('active')){		
			$('div#sitemap_container ul li a').removeClass('active');
			$(footerLink).addClass('active');
			
			if($('li a.active').parent('li').children('div').hasClass('open_container')){
				$('div.open_container').slideUp('slow', function(){
					$('li a.active').parent('li').children('div.full_yellow_bar').animate({
						'left': '-4000px'
					}, 1000, function(){
						$('div.open_container').removeClass('open_container');
						$('li a.active').parent('li').children('div.yellow_fill').hide();
						$('li a.active').removeClass('active');
						$(yellowFill).show();
						$(li_a).addClass('pre');
						$(yellowBar).animate({
							'left': '0px'
						}, 1000, function(){
							$(li_a).addClass('active');
							$(divElement).addClass('open_container');
							$(divElement).slideDown('slow', function(){
								animationFlag = 0;
								$(li_a).removeClass('pre');
							});
						});
					});
				});
			} else {
				$(li_a).addClass('active');
				
				$(yellowBar).animate({
					'left': '0px'
				}, 1000, function(){
					$(divElement).addClass('open_container');
					$(divElement).slideDown('slow', function(){
						animationFlag = 0;
						
						$('div#footer').animate({
							'marginTop': '75px'						
						});
					});
				});
			}
		} else {
			$(yellowBar).animate({
				'left': '-4000px'
			}, 1000, function(){
				$(divElement).removeClass('open_container');
				$(divElement).slideUp('slow', function(){
					animationFlag = 0;
					
					$('div#footer').animate({
						'marginTop': '150px'
					});
					
					$(yellowFill).hide();
					$(li_a).removeClass('active');
				});
			});
		}
	}
}
