// JavaScript Document

var cubeColors = [ '#C2A18A', '#E8E033', '#C7D10A', '#8CB317', '#FACC80', '#F2912E', '#E3294D', '#E31F82', '#872B85', '#9C7847', '#C2A18A', '#E8E033', '#C7D10A', '#8CB317', '#FACC80', '#F2912E', '#E3294D', '#E31F82', '#872B85' ];

function initMenus() {
	$('.-highlightOnHover').each(function() {	$(this).mouseenter(function() {	executeEffect($(this), 'highlight', 'slow'); }); });
	$('.-pulsateOnHover').each(function() {	$(this).mouseenter(function() {	executeEffect($(this), 'pulsate', 100); }); });
	$('.-loadsSection').each(function() { $(this).click(function() { loadSection($(this).attr('name')); }); });

	initDropDownMenu($('#theMallMenuOption'), $('.dropDownMenuTheMall'));
	initDropDownMenu($('#whoWeAreMenuOption'), $('.dropDownMenuWhoWeAre'));	
}

function initDropDownMenu(menuOption, dropDownMenu) {
	$(menuOption).click(function() { showHideDropDownMenu($(dropDownMenu), 'toggle'); });
	$(menuOption).mouseenter(function() { showHideDropDownMenu($(dropDownMenu), 'show'); });
	$(menuOption).mouseleave(function(e) {
		// Comprueba que el puntero no sale por la parte inferior de #theMallMenuOption
		if(e.pageY < ($(menuOption).offset().top + $(menuOption).height())) {
			showHideDropDownMenu($(dropDownMenu), 'hide');
		}
	});
	$(dropDownMenu).mouseleave(function(e) {
		var o = $(menuOption);
		var ool = o.offset().left;
		var oor = o.offset().left + o.width();
		var oot = o.offset().top;
		var oob = o.offset().top + o.height();
		if(e.pageX < ool || e.pageX > oor || e.pageY < oot || e.pageY > oob) showHideDropDownMenu($(dropDownMenu), 'hide');
	});
}

function moveCubesToInitialPosition() {
	var c = 0;
	$('.menuWrapper').show();
	$.when($('.menuCube').each(function () { $(this).delay(c*TIME_BETWEEN_MENU_CUBE_FADE_IN).animate({backgroundColor:cubeColors[c++]}) })).done( function() {
		expandMenuOptions();
	});
}

function expandMenuOptions() {
	$('.menuWrapper').animate({left:99});
	$.when($('.menuOption').each(function() { $(this).animate({width:115}); })).done(function() {
		setTimeout('hideTitleAndShowGallery();', 5000);
	});
	if(SHOW_LANGUAGE_SELECTION_BAR) $('.languageSelectionBarWrapper').fadeIn(FADE_SPEED);
	loadSection('as-cancelas');
}

function executeEffect(sender, effect, speed) {
	if($(sender).queue().length > 0) return;
	$(sender).effect(effect, speed);
}

function showHideDropDownMenu(dropDownMenu, showHide) {
	if($(dropDownMenu).queue().length > 0) return;
	switch(showHide) {
		case 'toggle':
			$(dropDownMenu).fadeToggle();
			break;
		case 'show':
			$(dropDownMenu).fadeIn();
			break;
		default:
			$(dropDownMenu).fadeOut();
	}
}
