/* variables iniciales */

var menusOn = false; //controla el display de los menues
var sectionOn = false; //control el display de las secciones
var sectionExit = function(ref){ref();} //guarda la funcion que se ejecutara de salida de una seccion

var divMediaMenu, divMainNav, divHomeMenu, divHmEnglishText //variables para los menus principales
var ulMediaMenu, ulMainNav, ulHomeMenu, hmPDF, hmReel //variables para los elementos internos de los menus principales

var divNoticias //variables para la seccion Noticias
var divDonde, divDondeContent, divDDdiv, divMapa //variables para la seccion Donde
var divContacto, divContactoContent, divCCdiv, divFormContacto //variables para la seccion Contacto
var divPortafolio, divPortafolioContent, divPPdiv, divPaisesMenu, divPaisesMenuLi, divPGalery //variabnes para la seccin de Portafolio
var divProyecto //variables para la seccion Proyecto

var menuDisplayVel = 280 //velocidad de movimiento de los menues
var menuFadeVel = 180 //velocidad de fade de los menues
var sectionFadeVel = 600 //velocidad de aparicin de las secciones

var formContacto = '<form><div id="fcLeft"><label>Tu nombre</label>';
formContacto += '<input type="text" id="nombre" /><label>Un correo donde te<br />';
formContacto += 'podamos contactar</label><input type="text" id="email" />';
formContacto += '<label>&iquest;De qué país nos escribes?</label><input type="text" id="pais" />';
formContacto += '</div><label>&iquest;Qué nos quiere decir?</label>';
formContacto += '<textarea wrap="virtual" rows="5" id="comments"></textarea><div id="fcEnviar">';
formContacto += '<a href="#" onclick="sendComments(); return false;">Enviar</a></div></form>';


//==============================================================================================

/* funcion para mostrar el menu de media */
function showMedia(){
	divMediaMenu.animate({left: 0}, menuDisplayVel, function(){
		ulMediaMenu.fadeIn(menuFadeVel);
	})
}

/* funcion para ocultar el menu de media */
function hideMedia(){
	ulMediaMenu.fadeOut(menuFadeVel, function(){
		divMediaMenu.animate({left: -31}, menuDisplayVel);
	})
}

/* funcion para mostrar los menues */
function showMenus(){
	divHomeMenu.animate({top: 0}, menuDisplayVel, function(){
		hmReel.fadeIn(menuFadeVel);
		hmPDF.fadeIn(menuFadeVel);
		ulHomeMenu.fadeIn(menuFadeVel);
	});
	divMainNav.animate({top: 415}, menuDisplayVel, function(){
		divHmEnglishText.fadeIn(menuFadeVel);
		ulMainNav.fadeIn(menuFadeVel);											 
	});
}

/* funcion para ocultar los menues */
function hideMenus(){
	hmReel.fadeOut(menuFadeVel, function(){
		divHomeMenu.animate({top: -30}, menuDisplayVel, function(){
			if(divHmEnglishText.css('display') == 'block'){
				divHmEnglishText.fadeOut(menuFadeVel);
			}
		});
	});
	hmPDF.fadeOut(menuFadeVel);
	ulHomeMenu.fadeOut(menuFadeVel);
	divHmEnglishText.fadeOut(menuFadeVel);
	ulMainNav.fadeOut(menuFadeVel, function(){
		divMainNav.animate({top: 535}, menuDisplayVel);
	});
}

/* funcion para ocultar seccion Home */
function hideHome(ref){
	if(!sectionOn){
		sectionOn = true;
		divNoticias.fadeOut(sectionFadeVel, function(){
			$('#logolink').bind('click', function(){
				sectionExit(showHome);
				return false;
			});
		});
		ref();
	}
}

/* funcion para cargar seccin de home */
function showHome(){
	$('#logolink').unbind('click').bind('click', function(){
		return false;
	});
	divNoticias.fadeIn(sectionFadeVel, function(){
		sectionExit = hideHome;
		sectionOn = false;
	});
}

/* funcion para ocultar seccion Donde Estamos */
function hideDonde(ref){
	if(!sectionOn){
		sectionOn = true;
		divDDdiv.fadeOut(menuFadeVel, function(){
			divDondeContent.animate({top: 535}, menuDisplayVel, function(){
				divDonde.fadeOut();
				$('#mnDonde').bind('click', function(){
					sectionExit(showDonde);
					return false;
				});
				divMapa.attr('src','blank.html');
				ref();
			});
		});
	}
}

/* funcion para cargar seccin de Donde Estamos */
function showDonde(){
	var start = new Date();
	
	$('#mnDonde').unbind('click').bind('click', function(){
		return false;
	});
	
	divDonde.fadeIn(sectionFadeVel, function(){
		divDondeContent.animate({top: 203}, menuDisplayVel, function(){
			divDDdiv.fadeIn(menuFadeVel, function(){
				divMapa.attr('src','mapa.html?'+start.getTime());
				sectionExit = hideDonde;
				sectionOn = false;
			});
		});
	});
}

/* funcion para ocultar seccion contacto */
function hideContacto(ref){
	if(!sectionOn){
		sectionOn = true;
		divCCdiv.fadeOut(menuFadeVel, function(){
			divContactoContent.animate({top: 535}, menuDisplayVel, function(){
				divContacto.fadeOut();
				$('#mnContacto').bind('click', function(){
					sectionExit(showContacto);
					return false;
				});
				ref();
			});
		});
	}
}

/* funcion para cargar seccin de contacto */
function showContacto(){
	$('#mnContacto').unbind('click').bind('click', function(){
		return false;
	});
	divFormContacto.html(formContacto);
	divContacto.fadeIn(sectionFadeVel, function(){
		divContactoContent.animate({top: 282}, menuDisplayVel, function(){
			divCCdiv.fadeIn(menuFadeVel, function(){
				sectionExit = hideContacto;
				sectionOn = false;
			});
		});
	});
}

/* funcion para ocultar seccin de portafolio */
function hidePortafolio(ref){
	
	id = '00';
	if(arguments.length > 1){
		id = arguments[1];	
	}
	if(!sectionOn){
		sectionOn = true;
		divPaisesMenu.fadeOut(menuFadeVel);
		divPPdiv.fadeOut(menuFadeVel, function(){
			divPortafolioContent.animate({top: 535}, menuDisplayVel, function(){
				divPortafolio.fadeOut();
				$('#mnPortafolio').bind('click', function(){
					sectionExit(showPortafolio);
					return false;
				});
				if(id != '00'){
					ref(id);
				}
				else{
					ref();
				}
			});
		});
	}
}

/* funcion para cargar seccin de portafolio */
function showPortafolio(){
	
	getPGalery('1','all','all','all');
	
	$('#mnPortafolio').unbind('click').bind('click', function(){
		return false;
	});
	divPortafolio.fadeIn(sectionFadeVel, function(){
		divPortafolioContent.animate({top: 268}, menuDisplayVel, function(){
		    divPPdiv.fadeIn(menuFadeVel, function(){
				sectionExit = hidePortafolio;
				sectionOn = false;
			});
			divPaisesMenu.fadeIn(menuFadeVel);
		});
	});
}

/* funcion para ocultar Proyecto */
function hideProyecto(ref){
	if(!sectionOn){
		sectionOn = true;
		$('#screenshots').animate({top: 565}, menuDisplayVel);
		$('#cuadroTextoAbajo').animate({top: 535}, menuDisplayVel);
		$('#cuadroTextoLateral').animate({left: -225}, menuDisplayVel, function(){
			divProyecto.fadeOut();
			ref();
		});
	}
}

/* funcion para mostrar Proyecto */
function showProyecto(id){
	var start = new Date();
	
	divProyecto.html('<div id="proyectoLoading"><img src="images/loader.gif" width="128" height="15" /></div>')
	divProyecto.fadeIn(sectionFadeVel, function(){
		$.get(
			'proyecto.php',
			{id: id, ctr: start.getTime()},
			function(data){
				divProyecto.html(data);
				
				setScreenshotsPages();
				
				$('#cuadroTextoAbajo').animate({top: 412}, menuDisplayVel);
				$('#screenshots').animate({top: 73}, menuDisplayVel);
				$('#cuadroTextoLateral').animate({left: 0}, menuDisplayVel, function(){
					showCuadro('1')
					sectionExit = hideProyecto;
					sectionOn = false;
				});
			}
		);
	});

}

/* funciones para el comportamiento del menu de paises en el portafolio */
function displayCountryMenu(){
	$('li.current', divPaisesMenu).unbind('click').removeClass('current');
	$('li:eq(0)', divPaisesMenu).animate({top: -36}, menuDisplayVel);
	$('li:eq(1)', divPaisesMenu).animate({top: -24}, menuDisplayVel);
	$('li:eq(2)', divPaisesMenu).animate({top: -12}, menuDisplayVel);
	divPaisesMenuLi.css('z-index','100');
	divPaisesMenuLi.bind('click',function(){
		$(this).css('z-index','101').addClass('current');
		divPaisesMenuLi.unbind('click').animate({top: 0}, menuDisplayVel);
		$(this).bind('click', function(){
			displayCountryMenu();
			return false;
		});
		return false;
	});
}


/* funciones para el over de los items del portafolio */
function pItemOver(thisOverlay, thisP, thisA){
	thisOverlay.fadeTo(menuFadeVel, 0);
	thisP.animate({left: 0}, menuFadeVel);
	thisA.animate({top: 83}, menuFadeVel);
	
	
}
function pItemOut(thisOverlay, thisP, thisA){
	thisOverlay.fadeTo(menuFadeVel, 0.8);
	thisP.animate({left: -102}, menuFadeVel);
	thisA.animate({top: 95}, menuFadeVel);
	
}
function setPItems(){
	$('.pItem').hover(
		function(){
			var thisOverlay = $('.pItemOverlay', this);
			var thisP = $('.piMenuContainer p', this);
			var thisA = $('.piMenuContainer a.pimcLink', this);
			pItemOver(thisOverlay, thisP, thisA);
		},
		function(){
			var thisOverlay = $('.pItemOverlay', this);
			var thisP = $('.piMenuContainer p', this);
			var thisA = $('.piMenuContainer a.pimcLink', this);
			pItemOut(thisOverlay, thisP, thisA);
		}
	);
}

/* con esta funcion se carga la galeria del portafolio */
function getPGalery(type, cli, cty, seg){
	var start = new Date();
	
	divPGalery.html('<div id="galeriaLoading"><img src="images/loader.gif" width="128" height="15" /></div>');
	$.get(
		'portafolio.php',
		{
			type: type,
			cli: cli,
			cty: cty,
			seg: seg,
			ctr: start.getTime()
		},
		function(data){
			divPGalery.html(data);
			setPItems();
		}
	);
}

/* funciones para las paginas de screenshots */
function setScreenshotsPages(){
	$('#ssPage1').bind('click', function(){
		$('.screenshot').hide();
		$('#screenshot1').fadeIn();
		return false;
	});
	$('#ssPage2').bind('click', function(){
		$('.screenshot').hide();
		$('#screenshot2').fadeIn();
		return false;
	});
	$('#ssPage3').bind('click', function(){
		$('.screenshot').hide();
		$('#screenshot3').fadeIn();
		return false;
	});
}

/* funciones para los cuadros de texto de los proyectos */
function showCuadro(id){
	$('#cuadroTextoLateral ul li').removeClass('current');
	$('#ctlMenu'+id).addClass('current');
	$('.ctlCuadro').hide();
	$('#ctlCuadro'+id).fadeIn();
}

/* funciones para el envo de comentarios */
function sendComments(){
	var start = new Date();
	
	nombre = $('#nombre').val();
	email = $('#email').val();
	pais = $('#pais').val();
	comments = $('#comments').val();
	
	html_processing = '<div style="text-align:center;margin-top:40px;">';
	html_processing += '<img src="images/ajax-loader3.gif" width="128" height="15">';
	html_processing += '<p>Enviando comentarios</p></div>';
	divFormContacto.html(html_processing);
	
	html_error1 = '<div style="text-align:center;margin-top:40px;">';
	html_error1 += '<p>Debe completar todos los datos que se solicitan.<br />';
	html_error1 += 'Verifique tamb&eacute;in que el email ingresado sea un email v&aacute;lido.<br />';
	html_error1 += 'Haga <a href="#" onclick="resetComments(); return false;">clic aqu&iacute;</a> ';
	html_error1 += 'para volverlo a intentar</p></div>';
	
	html_error2 = '<div style="text-align:center;margin-top:40px;">';
	html_error2 += '<p>ha ocurrido un error inesperado.<br />';
	html_error2 += 'Haga <a href="#" onclick="resetComments(); return false;">clic aqu&iacute;</a> ';
	html_error2 += 'para volverlo a intentar</p></div>';
	
	html_ok = '<div style="text-align:center;margin-top:40px;">';
	html_ok += '<p>Sus comentarios fueron enviados con &eacute;xito.<br>';
	html_ok += 'Gracias por su participaci&oacute;n e inter&eacute;s</p>';
	html_ok += '<p>Si desea volver al formulario de contacto<br /> nuevamente, ';
	html_ok += 'haga <a href="#" onclick="resetComments(); return false;">clic aqu&iacute;</a></p></div>';
	
	$.get(
		'process_comments.php',
		{
			nombre: nombre,
			email: email,
			pais: pais,
			comments: comments,
			ctr: start.getTime()
		},
		function(data){
			if(data == '0'){
				divFormContacto.html(html_error1);
			}
			if(data == '1'){
				divFormContacto.html(html_ok);
			}
			if(data == '2'){
				divFormContacto.html(html_error2);
			}
		}
	);
}

function resetComments(){
	divFormContacto.html(formContacto);
}

/* comportamientos que cargan al cargar la pgina */
$(function(){
		   
	/* guardamos en variables los menues principales */
	divMediaMenu = $('#mediaMenu'); //menu lateral con los links de media
	ulMediaMenu = $('#mediaMenu ul'); //ul interno del menu lateral con los links de media
	divMainNav = $('#mainNav'); //menu principal inferior
	ulMainNav = $('#mainNav ul'); //ul interno del menu principal inferior
	divHomeMenu = $('#homeMenu'); //menu superior del home
	ulHomeMenu = $('#homeMenu li.included a'); //elementos internos del menu superior del home
	hmPDF = $('#hmPDF a'); //boton PDF del menu superior del home
	hmReel = $('#hmReel a'); //boton Reel del menu superior del home
	divHmEnglishText = $('#hmEnglishText'); //texto 'English' del menu superior
	
	/* se le asigna el click al boton de reel */
	hmReel.bind('click',function(){
		$('#divReel').lightbox_me();
		return false;
	});
	
	/* se le asigna el mouse over a la ventana principal para mostrar los menues */
	$('#window').hover(showMenus, hideMenus);
	
	/* se le asigna el mouse over al menu de media para mostrarlo */
	divMediaMenu.hover(showMedia, hideMedia);
	
	
	/* guardamos en variables la seccion Noticias */
	divNoticias = $('#noticias');
	
	/* guardamos en variables la seccion Donde Estamos */
	divDonde = $('#dondeEstamos'); //seccion de Donde Estamos
	divDondeContent = $('#dondeContent'); //contenido seccion Donde Estamos;
	divDDdiv = $('#DDDiv'); //contenido seccion Donde Estamos
	divMapa = $('#mapa'); //contenido seccion Donde Estamos
	
	/* guardamos en variables la seccion Contacto */
	divContacto = $('#contacto'); //seccion de contacto
	divContactoContent = $('#contactoContent'); //contenido seccion contacto;
	divCCdiv = $('#CCDiv'); //contenido seccion contacto
	divFormContacto = $('#formContacto'); //contenido formulario contacto
	
	/* guardamos en variables la seccin de Portafolio */
	divPortafolio = $('#portafolio'); //seccion portafolio
	divPortafolioContent = $('#portafolioContent'); //contenido seccion portafolio
	divPPdiv = $('#PPdiv'); //contenido seccion portafolio
	divPaisesMenu = $('#paisesMenu'); // menues de la seccin portafolio
	divPaisesMenuLi = $('#paisesMenu li'); // menues de la seccin portafolio
	divPGalery = $('#pGaleria'); //galeria
	
	/* se reacomoda los menu de paises de portafolio */
	divPaisesMenuLi.css('z-index','100');
	$('li.current', divPaisesMenu).css('z-index','101');
	
	/* guardamos en variables la seccin de Proyecto */
	divProyecto = $('#proyecto'); //seccion proyecto
	
	/* se le asigna el comportamiento al menu de paises del portafolio */
	$('li.current', divPaisesMenu).bind('click',function(){
		displayCountryMenu();
		return false;
	});
	
	
	/* se le asigna los enlaces al men principal */
	$('#logolink').bind('click', function(){
		return false;
	});
	$('#mnDonde').bind('click', function(){
		sectionExit(showDonde);
		return false;
	});
	$('#mnContacto').bind('click', function(){
		sectionExit(showContacto);
		return false;
	});
	$('#mnPortafolio').bind('click', function(){
		sectionExit(showPortafolio);
		return false;
	});
	sectionExit = hideHome;
	
	
});

/* comportamiento al cargar todas la imagenes */
function siteOnLoad() {
	
	/* se obtienen las medidas del navegador y se posiciona ventana principal del site */
	var arrPageSizes = _getPageDimensions();
	pageWidth = arrPageSizes[0];
	pageHeight = arrPageSizes[1];
	if(pageWidth > 1030){
		$('#wrapper').width(pageWidth);	
	}
	
	if(pageHeight > 700){
		halfPage = pageHeight / 2;
		mPadding = halfPage - 290;
		$('#windowWrapper').css('padding-top',mPadding);	
		divHmEnglishText.css('top',mPadding-14);
	}
		
	/* muestra todo el site */
	$('#wrapper').show();
	
	/* reubica las noticias */
	t = 140;
	h = $('#noticia1').height();
	$('#noticia2').css({top: t + h});
	t = t + h;
	h = $('#noticia2').height();
	$('#noticia4').css({top: t + h});
	h = $('#noticia3').height();
	$('#noticia3').css({top: t - h});
	
	/* esconde los menus iniciales */
	hideMedia();
	hideMenus();
}

/* funcion para obtener dimensiones del la ventana de explorador */
function _getPageDimensions() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	var pageWidth, pageHeight
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}