var isIE = false;

var swfPair = false;

var firstClick = true;


$(document).ready(function(){

	isIE = $.browser.msie;
	$(document).pngFix(); 

	// cargamos el primer proyecto a mostrar
	loadFirstProject();
	
	// asignamos los eventos a los botones
	
	// Contact Action
	$('.yb_btn').click(function() {
		var ybLeft = 0;
		$('#yb_contact').stop().css("left", "-500px").show();
		
		$(this).oneTime(100, function() {
			$('#yb_contact').animate({left:ybLeft+"px"}, 1000);
		});
	
		$('#contact_over').show();
		$('#contact_over').stop().css("opacity", 0).fadeTo('slow', 1);
		$('#contact_over').css({cursor:"pointer"});
		
	}); 
	
	$('#contact_over').click(function() {
		hideContact();
	}); 
	
	function hideContact() {
		$('#contact_over').stop().css("opacity", 1).fadeTo('slow', 0, function(){
			$('#contact_over').html('').hide();
			$('#menu_contact .contact_level li:eq(0)').removeClass('contact_active');
			$('#menu_contact .contact_level li:eq(0)').addClass('contact_normal');
		});

		var ybLeft = -500;
		$('#yb_contact').stop().css("left", "0px").show();
		
		$(this).oneTime(100, function() {
			$('#yb_contact').animate({left:ybLeft+"px"}, 1000);
		});
}		
	
	$('.project_previous').click(function(e) {
		// galería anterior
		showPreviousImage();
		e.preventDefault();
	});
	
	$('.project_next').click(function(e) {
		// galería siguiente
		showNextImage();
		e.preventDefault();
	});
	
	$('a.media').media();

});



$(window).resize(function(){
	// gestión del resize de la ventana
	resizeImageYB();
});
		
// resize y galería de imágenes
var imgCenter = "#image_center";
var imgLeft = "#image_left";
var imgRight = "#image_right";
var imgUp = "#image_up";
var imgDown = "#image_down";

function resizeImageYB() {
	// resize de las imágenes de fondo para encajar en pantalla
	
	// ratio proporcional de la ventana del navegador
	var navWidth = $(window).width();
	var navHeight = $(window).height();
	var navRatio = navWidth / navHeight;

	// ratio proporcional de la imagen
	if ($('#main_image').width() > 1) picWidth = $('#main_image').width();
	if ($('#main_image').height() > 1) picHeight = $('#main_image').height();
	picRatio = picWidth / picHeight;
	
	// escala resultante
	if (navRatio > picRatio) {
		// escalamos la imagen para ajustarse al ancho del navegador
		var newHeight = (navWidth / picWidth) * picHeight;
		var newWidth = navWidth;
	} else {
		// escalamos la imagen para ajustarse a la altura del navegador
		var newHeight = navHeight;
		var newWidth = (navHeight / picHeight) * picWidth;
	}
	
	// centramos la imagen
	newTop = 0 - ((newHeight - navHeight) / 2);
	newLeft =  0 - ((newWidth - navWidth) / 2);

	// colocamos todas
	$('#image_holder').css({height: navHeight, width: navWidth});
	$('#image_holder').css({visibility:"visible", display:"block"});

	$('.main_image').css({height: newHeight, width: newWidth});
	
	// centro y aledañas...
	$(imgCenter).css({top: newTop, left: newLeft});
	$(imgLeft).css({top: newTop, left: newLeft-newWidth});
	$(imgRight).css({top: newTop, left: newLeft+newWidth});
	$(imgUp).css({top: newTop-newHeight, left: newLeft});
	$(imgDown).css({top: newTop+newHeight, left: newLeft});
	
	resizeElementsYB();
}

function resizeElementsYB() {
	var navWidth = $(window).width();
	var navHeight = $(window).height();
	
	
	var navProjectHeight = navHeight/2-60;
	$('#yb_contact').css("marginTop", navProjectHeight+"px");
	
	
	// también centramos la precarga
	var preloadTop = navHeight/2-20;
	var preloadLeft = navWidth/2-20;
	$('#project_preload').css({top: preloadTop, left: preloadLeft});
	
	// y porque no.. centremos el vídeo
	var videoTop = (navHeight-496)/2;
	var videoLeft = (navWidth-640)/2;
	$('#video').css({top: videoTop, left: videoLeft});
}

function preloadImage(source, nextEvent) {
   	// tapamos y precargamos una imagen
  // 	if (isIE) $(this).oneTime(50, "preload", nextEvent);
  // 	else {
		$('#project_tapa').show(); 
		if (imageActual!=1 || !projects[projectActual][7]){
			if (isSWF){
				
				$('#flashPreload').load(source,function(){
					// ocultamos la precarga y mostramos
					if (imageActual==1) projects[projectActual][7] = true;
					//$('#project_tapa').stop().fadeOut('slow');
					$(this).oneTime(500, "preload", nextEvent);
				});
			} else {
		   		$('<img />').attr('src', source).load(function(){
					// ocultamos la precarga y mostramos
					if (imageActual==1) projects[projectActual][7] = true;
					//$('#project_tapa').stop().fadeOut('slow');
					$(this).oneTime(500, "preload", nextEvent);
				});
			}
		} else {
			//$('#project_tapa').stop().fadeOut('slow');
			$(this).oneTime(250, "preload", nextEvent);
		}
	//}
}

function preloadAllNextImages(source, nextEvent) {
   	// tapamos y precargamos una imagen
  // 	if (isIE) $(this).oneTime(50, "preload", nextEvent);
  // 	else { 
		if (PreloadImageActual!=1 || !projects[projectActual][7]){
			if (isSWF){
				$('#flashPreload').load(source,function(){
					// ocultamos la precarga y mostramos
					if (imageActual==1) projects[projectActual][7] = true;
					//$('#project_tapa').stop().fadeOut('slow');
					$(this).oneTime(500, "preload", nextEvent);
				});
			} else {
		   		$('<img />').attr('src', source).load(function(){
					// ocultamos la precarga y mostramos
					if (PreloadImageActual==1) projects[projectActual][7] = true;
					//$('#project_tapa').stop().fadeOut('slow');
					$(this).oneTime(500, "preload", nextEvent);
				});
			}
		} else {
			//$('#project_tapa').stop().fadeOut('slow');
			$(this).oneTime(250, "preload", nextEvent);
		}
	//}
}

function preloadNext() {
	// precargamos una imagen
	preloadActual++;
	if (preloadActual==projects.length) preloadActual = 1;
	preloadActual = getNextToPreload();
	if (preloadActual != -1){
		var source = getPreloadImageSourceActual();
		if (preloadSWF){
			$('#flashPreload').flash({
				swf: source,
				width: '100%',
				height: '100%',
				hasVersion: 8,
				id: 'flashMovie',
				bgcolor: '#1e1e1e',
				flashvars: {},
				params: { wmode: 'opaque', allowScriptAccess:'always'}
			});
			projects[preloadActual][7] = true;
			//alert("precargado el..."+preloadActual)
			preloadNext();
		}
		$('<img />').attr('src', source).load(function(){
			projects[preloadActual][7] = true;
			//alert("precargado el..."+preloadActual)
			preloadNext();
		});
	}
}

function getNextToPreload() {
	if (!projects[preloadActual][7]) return preloadActual;
	else {
		var someToPreload = -1;
		for (var i=preloadActual; i<projects.length; i++){
			if (!projects[i][7]){
				someToPreload = i;
				break;
			}
		}
		if (someToPreload == -1){
			for (var i=1; i<preloadActual; i++){
				if (!projects[i][7]){
					someToPreload = i;
					break;
				}
			}
		}
		return someToPreload;
	}
}

function hideVideo() {
	var waitTime = 300;
	if (isVideo){
		waitTime = 1000;
		$('#video').stop().animate({top:"-500px"}, 1000, function(){
			$('#video').html('').hide();
		});
	}
	
	
	return waitTime;
}

function showVideo() {
	if (isVideo){
		// cargamos el vídeo actual
		var idVideo = projects[projectActual][6].split('|')[imageActual-1];
			idVideo = idVideo.split("VIDEO")[0];
		var videoSource = imagePath+idVideo+".mov";
		
		$('#video').html('<object width="640" height="496" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" align="top"><param name="bgcolor" value="#000000"><param name="cache" value="true"><param name="src" value="'+videoSource+'"><param name="autoplay" value="true"><param name="controller" value="true"><embed width="640" height="496" src="'+videoSource+'" align="top" pluginspage="http://www.apple.com/quicktime/download/" type="video/quicktime" controller="true" autoplay="true" cache="true" bgcolor="#000000"></object>').show();
		
		// lo mostramos
		var navHeight = $(window).height();
		var videoTop = (navHeight-496)/2;
		$('#video').stop().css("top", "-500px").show();
		
		$(this).oneTime(100, function() {
			$('#video').animate({top:videoTop+"px"}, 1000);
		});
	}
}

function hideSWF() {
	var waitTime = 300;
	if (isSWF){
		waitTime = 1000;
		$('#video').stop().animate({top:"-1500px"}, 1000, function(){
			$('#video').html('').hide();
		});
	}
	
	return waitTime;
}

function showSWF() {
	if (isSWF){
		// cargamos el vídeo actual
		var idSWF = projects[projectActual][6].split('|')[imageActual-1];
			idSWF = idSWF.split("SWF")[0];
		var swfSource = imagePath+idSWF+".swf";
	
		if (swfPair) {
			swfPair=false;
			$('#flashPair').flash({
				swf: swfSource,
				width: '100%',
				height: '100%',
				hasVersion: 8,
				id: 'flashMovie',
				bgcolor: '#1e1e1e',
				flashvars: {},
				params: { wmode: 'opaque', allowScriptAccess:'always'}
			}).show();
	
		} else {
			swfPair=true;
			$('#flashImpair').flash({
				swf: swfSource,
				width: '100%',
				height: '100%',
				hasVersion: 8,
				id: 'flashMovie',
				bgcolor: '#1e1e1e',
				flashvars: {},
				params: { wmode: 'opaque', allowScriptAccess:'always'}
			}).show();
		}	
	}
}

function hideProjectData() {
	if (isIE){
		$('#image_data').hide();
		$('#project_navigation').hide();
	} else {
		$('#image_data').stop().fadeTo('fast', 0);
		$('#project_navigation').stop().fadeTo('fast', 0);
	}
}

function showProjectData() {
	// cargamos los datos buenos
	//var imageDataSource = imagePath+"proyecto_titulo_"+projectActual+".png";
	var idImage = projects[projectActual][0];
	var imageDataSource = imagePath+"proyecto_titulo_"+idImage+".jpg";
	
	var title = projects[projectActual][1];
	var desc = projects[projectActual][2];
	var link = (projects[projectActual][3]=="")? "" : '<a href="'+projects[projectActual][5]+'" onMouseOver="SwapBack(); return false;" onMouseOut="SwapOut(); return false;" target="_blank">'+projects[projectActual][3]+'</a>';
	var numImages = parseInt(projects[projectActual][4]);
	
	$('#image_data h2').html(title);
	$('#image_data p:eq(0)').html(desc);
	$('#image_data p:eq(1)').html(link);
	
	$('#image_data .image_number').html("1/"+numImages);
	
	if (title=="") $('#image_data h2').hide();
	else $('#image_data h2').show();
	if (desc=="") $('#image_data p:eq(0)').hide();
	else $('#image_data p:eq(0)').show();
	if (link=="") $('#image_data p:eq(1)').hide();
	else $('#image_data p:eq(1)').show();
	if (isIE){
		$('#image_data').show();
		$('#project_navigation').show();
	} else {
		$('#image_data').show();
		$('#image_data').stop().css("opacity", 0).fadeTo('slow', 1);

		$('#project_navigation').show();
		$('#project_navigation').stop().css("opacity", 0).fadeTo('slow', 1);	}
	
	if (numImages > 1){
		$('#image_data .image_navigation').show();
		$('#image_data .image_navigation').stop().css("opacity", 0).fadeTo('fast', 1);
		$('.project_previous').show();
		$('.project_previous').stop().css("opacity", 0).fadeTo('fast', 0.4).addClass('inactive');
		$('.project_next').show();
		$('.project_next').stop().css("opacity", 0).fadeTo('fast', 1);
	}
	else {
		$('#image_data .image_navigation').css({display:"none"});
		$('.project_previous').css({display:"none"});
		$('.project_next').css({display:"none"});
	}
	
	// aprovechamos aquí y mostramos/ocultamos los +/-
	// toggleProjectPrevious(projectActual==1);
	// toggleProjectNext((projectActual==(projects.length-1)));
	
	toggleImagePrevious(true);
	toggleImageNext((numImages==1));
	
	if (isIE) resizeImageYB();
}

function changeProjectImage() {
	var numImages = parseInt(projects[projectActual][4]);
	$('#image_data .image_number').html(imageActual+"/"+numImages);
	
	// aprovechamos aquí y mostramos/ocultamos los +/- del proyecto en particular
	toggleImagePrevious((numImages==1 || imageActual==1));
	toggleImageNext((imageActual==numImages));
}

function toggleImagePrevious(hideOrShow) {
	if (hideOrShow){
		$('.project_previous').stop().css({cursor:"default"}).fadeTo('slow', 0.4).addClass('inactive');
		$('.project_previous').unbind("click").click(function() { return false; });
	} else {
		$('.project_previous').stop().css({cursor:"pointer"}).fadeTo('slow', 1).removeClass('inactive');
		$('.project_previous').unbind("click").click(function(e) {
			showPreviousImage();
			e.preventDefault();
		});
	}
}

function toggleImageNext(hideOrShow) {
	if (hideOrShow){
		$('.project_next').stop().css({cursor:"default"}).fadeTo('slow', 0.4).addClass('inactive');
		$('.project_next').unbind("click").click(function() { return false; });
	} else {
		$('.project_next').stop().css({cursor:"pointer"}).fadeTo('slow', 1).removeClass('inactive');
		$('.project_next').unbind("click").click(function(e) {
			showNextImage();
			e.preventDefault();
		});
	}
}
function getImageSourceActual(){

	var idImage = projects[projectActual][6].split('|')[imageActual-1];
	var imageSource = imagePath+idImage+".jpg";
	
			
	// comprobamos si es un gif
	if (idImage.indexOf("GIF")!=-1){
		imageSource = imageSource.split("GIF")[0]+".gif";
	}
	
	// comprobamos si es un swf
	if (idImage.indexOf("SWF")!=-1){
		
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || !(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])) {
			imageSource = imageSource.split("SWF")[0]+".jpg";
			
			isSWF = false;
		} else {
			//---
			imageSource = imageSource.split("SWF")[0]+".swf";
			
			isSWF = true;
		}
		
	}	else {
		isSWF = false;
	}
	
	// comprobamos si es un vídeo o no
	if (idImage.indexOf("VIDEO")!=-1){
		//imageSource = imagePath+"video_background.jpg";
		imageSource = imageSource.split("VIDEO")[0]+".jpg";
		isVideo = true;
	} else {
		isVideo = false;
	}
	return imageSource;
}

function getImageSourceNextPreloads(){

	var idImagePreload = projects[projectActual][6].split('|')[PreloadImageActual-1];
	var imageSourcePreload = imagePath+idImagePreload+".jpg";
	
			
	// comprobamos si es un gif
	if (idImagePreload.indexOf("GIF")!=-1){
		imageSourcePreload = imageSourcePreload.split("GIF")[0]+".gif";
	}
	
	// comprobamos si es un swf
	if (idImagePreload.indexOf("SWF")!=-1){
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || !(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])) {
			imageSourcePreload = imageSourcePreload.split("SWF")[0]+".jpg";
		
			isSWF = false;
		} else {
			//---
			imageSourcePreload = imageSourcePreload.split("SWF")[0]+".swf";
		
			isSWF = true;
		}
	}	else {
		isSWF = false;
	}
	
	// comprobamos si es un vídeo o no
	if (idImagePreload.indexOf("VIDEO")!=-1){
		//imageSourcePreload = imagePath+"video_background.jpg";
		imageSourcePreload = imageSourcePreload.split("VIDEO")[0]+".jpg";
		isVideo = true;
	} else {
		isVideo = false;
	}
	return imageSourcePreload;
}

function getPreloadImageSourceActual(){

	var idImage = projects[preloadActual][6].split('|')[0];
	var imageSource = imagePath+idImage+".jpg";
	if (idImage.indexOf("GIF")!=-1) imageSource = imageSource.split("GIF")[0]+".gif";
	if (idImage.indexOf("SWF")!=-1) {
		
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || !(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])) {
			imageSource = imageSource.split("SWF")[0]+".jpg";
			
			preloadSWF = false;
		} else {
			//---
			imageSource = imageSource.split("SWF")[0]+".swf";
			
			preloadSWF = true;
		}
	} else {
		preloadSWF = false;
	}
	//if (idImage.indexOf("VIDEO")!=-1) imageSource = imagePath+"video_background.jpg";
	if (idImage.indexOf("VIDEO")!=-1) imageSource = imageSource.split("VIDEO")[0]+".jpg";
	
	return imageSource;
}

function loadFirstProject() {
	// cargamos la imagen del proyecto actual y sus datos
	hideVideo();
	resizeElementsYB();
	
	// conseguimos el imageSource del array:
	var imageSource = getImageSourceActual();
	
	preloadImage(imageSource, loadFirstProjectEnd);
}

function loadFirstProjectEnd() {
	
	if (isIE){
		$('#menu_home').show();
	} else {
		$('#menu_home').show();
		$('#menu_home').stop().css("opacity", 0).fadeTo('slow', 1);
	}
	
	
	var imageSource = getImageSourceActual();
	
	resizeImageYB();
	showProjectData();
	showVideo();
	if (isSWF){
		showSWF();
		$('#image_holder').css({opacity:0});
		$('#flashImpair').show();
		$('#flashImpair').stop().css({opacity:0}).fadeTo(1000, 1, function(){
			$('#image_holder').css({opacity:0}).fadeTo(1000, 1);
		});
	} else {
		$(imgCenter)[0].src = imageSource;
		$('#image_holder').css({opacity:0}).fadeTo(1000, 1);
	}
	
	if (!isIE) {
		projects[projectActual][7] = true;
		preloadNext();
	}

	$('#project_tapa').stop().fadeOut('slow');
}

function showDirectProject(n) {
	firstClick = true;
	imageActual = 1;
	// ir directo al proyecto n
	// se busca en el array el ID con quién coincide en el array
	var nP = 0;
	for (var i=1; i<projects.length; i++){
		if (projects[i][0]==n) nP = i;
	}
	
	if (nP>projectActual){
		projectActual = nP-1;
		showNextProject();
	} else {
		projectActual = nP+1;
		showPreviousProject();
	}
}
   
function showNextProject() {
	// el siguiente proyecto es el de la derecha
	hideProjectData();
	var waitTime = hideVideo();
	
	$(this).oneTime(waitTime, function() {
		projectActual += 1;
		if (projectActual == projects.length) projectActual = 0;
		imageActual = 1;
	
		var imageSource = getImageSourceActual();
		
		preloadImage(imageSource, showNextProjectEnd);	
  	});
  	
  	//if (isIE) $(imgCenter)[0].src = "css/css_img/sp.gif";	
}

function showNextProjectEnd() {
	// ya se ha precargado la imagen...
	preloadNextImage();
	var imageSource = getImageSourceActual();

	if (isIE) {
		$(imgCenter)[0].src = imageSource;
		showVideo();
	} else {
		
		if (!swfPair){
			if (isSWF) {
				
				showSWF();
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top - $(imgCenter).height();
				var downTopIni = $("#flashImpair").position().top;
				var downTopEnd = $(imgCenter).position().top;
		
				// lo mostramos
				var navHeight = $(window).height();
				$('#flashImpair').css("left", "0px");
				$('#flashImpair').css("top", navHeight+"px");
		
				$('#flashPair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
				
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$("#flashImpair").animate({top:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({top:centerTopIni});
			
				});
		
			} else {
		
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top - $(imgCenter).height();
				var downTopIni = $(imgDown).position().top;
				var downTopEnd = $(imgCenter).position().top;
		
				$(imgDown)[0].src = imageSource;
		
				$('#flashPair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$(imgDown).animate({top:downTopEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({top:centerTopIni});
					$(imgDown).css({top:downTopIni});
					showVideo();
				});
			}
		} else {
			if (isSWF) {
				showSWF();
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top - $(imgCenter).height();
				var downTopIni = $("#flashPair").position().top;
				var downTopEnd = $(imgCenter).position().top;
		
				// lo mostramos
				var navHeight = $(window).height();
				$('#flashPair').css("left", "0px");
				$('#flashPair').css("top", navHeight+"px");
		
				$('#flashImpair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
				
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$("#flashPair").animate({top:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({top:centerTopIni});
			
				});
		
			} else {
		
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top - $(imgCenter).height();
				var downTopIni = $(imgDown).position().top;
				var downTopEnd = $(imgCenter).position().top;
		
				$(imgDown)[0].src = imageSource;
		
				$('#flashImpair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$(imgDown).animate({top:downTopEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({top:centerTopIni});
					$(imgDown).css({top:downTopIni});
					showVideo();
				});
			}
		
		}
	}
	showProjectData();
	
	$('#project_tapa').stop().fadeOut('slow');
}

function showPreviousProject() {
	// el anterior proyecto es el de la izquierda
	hideProjectData();
	var waitTime = hideVideo();
	
	$(this).oneTime(waitTime, function() {
		projectActual -= 1;
		if (projectActual == 0) projectActual = (projects.length-1);
		imageActual = 1;
		
		var imageSource = getImageSourceActual();
		preloadImage(imageSource, showPreviousProjectEnd);
  	});
	
	//if (isIE) $(imgCenter)[0].src = "css/css_img/sp.gif";
}

function showPreviousProjectEnd() {
	// ya se ha precargado la imagen...
	var imageSource = getImageSourceActual();
	
	if (isIE) {
		$(imgCenter)[0].src = imageSource;
		showVideo();
	} else {
	
		
		if (!swfPair){
			if (isSWF){
				showSWF();
				
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top + $(imgCenter).height();
				var upTopIni = $("#flashImpair").position().top;
				var upTopEnd = $(imgCenter).position().top;
		
				// lo mostramos
				var navHeight = $(window).height();
				$('#flashImpair').css("left", "0px");
				$('#flashImpair').css("top", -navHeight+"px");
				
				$('#flashPair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$("#flashImpair").animate({top:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({top:centerTopIni});		
				});
		
			} else {
			
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top + $(imgCenter).height();
				var upTopIni = $(imgUp).position().top;
				var upTopEnd = $(imgCenter).position().top;
		
				$(imgUp)[0].src = imageSource;
		
				$('#flashPair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$(imgUp).animate({top:upTopEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({top:centerTopIni});
					$(imgUp).css({top:upTopIni});
					showVideo();
				});
			}
		} else {
			if (isSWF){
				showSWF();
				
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top + $(imgCenter).height();
				var upTopIni = $("#flashPair").position().top;
				var upTopEnd = $(imgCenter).position().top;
		
				// lo mostramos
				var navHeight = $(window).height();
				$('#flashPair').css("left", "0px");
				$('#flashPair').css("top", -navHeight+"px");
				
				$('#flashImpair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$("#flashPair").animate({top:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({top:centerTopIni});		
				});
		
			} else {
			
				var centerTopIni = $(imgCenter).position().top;
				var centerTopEnd = $(imgCenter).position().top + $(imgCenter).height();
				var upTopIni = $(imgUp).position().top;
				var upTopEnd = $(imgCenter).position().top;
		
				$(imgUp)[0].src = imageSource;
		
				$('#flashImpair').stop().animate({top:centerTopEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
				$(imgCenter).animate({top:centerTopEnd}, 1000);
				$(imgUp).animate({top:upTopEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({top:centerTopIni});
					$(imgUp).css({top:upTopIni});
					showVideo();
				});
			}
		
		}

	}
	
	showProjectData();
	
	$('#project_tapa').stop().fadeOut('slow');
}

function preloadNextImage() {
	// la siguiente imagen es la de abajo
	var waitTime = hideVideo();
	
	$(this).oneTime(waitTime, function() {
		if (firstClick){
			PreloadImageActual = 1;
			firstClick = false;
		}
		PreloadImageActual += 1;
		var imageSourcePreload = getImageSourceNextPreloads();	
		preloadAllNextImages(imageSourcePreload, preloadNextImage);
		
  	});
  	
  	//if (isIE) $(imgCenter)[0].src = "css/css_img/sp.gif";
}

function showNextImage() {
	// la siguiente imagen es la de abajo
	var waitTime = hideVideo();
	
	$(this).oneTime(waitTime, function() {
		if ($('.project_previous').hasClass('inactive')){
			imageActual = 1;
		}
		imageActual += 1;
		var imageSource = getImageSourceActual();	
		preloadImage(imageSource, showNextImageEnd);
  	});
  	
  	//if (isIE) $(imgCenter)[0].src = "css/css_img/sp.gif";
}

function showNextImageEnd() {
	// ya se ha precargado la imagen..
	var imageSource = getImageSourceActual();
	
	if (isIE) {
		$(imgCenter)[0].src = imageSource;
		showVideo();
	} else {
	
		if (!swfPair){
			if (isSWF){
				showSWF();
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left - $(imgCenter).width();
				var rightLeftIni = $("#flashImpair").position().left;
				var rightLeftEnd = $(imgCenter).position().left;
				
				// lo mostramos
				var navWidth = $(window).width();
				$('#flashImpair').css("top", "0px");
				$('#flashImpair').css("left", navWidth+"px");
		
				$('#flashPair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
		
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$("#flashImpair").animate({left:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({left:centerLeftIni});
					
				});
		
			} else {
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left - $(imgCenter).width();
				var rightLeftIni = $(imgRight).position().left;
				var rightLeftEnd = $(imgCenter).position().left;
		
				$(imgRight)[0].src = imageSource;
		
				$('#flashPair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
		
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$(imgRight).animate({left:rightLeftEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({left:centerLeftIni});
					$(imgRight).css({left:rightLeftIni});
					showVideo();
				});
			}
		} else {
		
			if (isSWF){
				showSWF();
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left - $(imgCenter).width();
				var rightLeftIni = $("#flashPair").position().left;
				var rightLeftEnd = $(imgCenter).position().left;
		
				// lo mostramos
				var navWidth = $(window).width();
				$('#flashPair').css("top", "0px");
				$('#flashPair').css("left", navWidth+"px");
		
				$('#flashImpair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
		
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$("#flashPair").animate({left:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({left:centerLeftIni});
					
				});
		
			} else {
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left - $(imgCenter).width();
				var rightLeftIni = $(imgRight).position().left;
				var rightLeftEnd = $(imgCenter).position().left;
		
				$(imgRight)[0].src = imageSource;
		
				$('#flashImpair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
		
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$(imgRight).animate({left:rightLeftEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({left:centerLeftIni});
					$(imgRight).css({left:rightLeftIni});
					showVideo();
				});
			}
		}
	}
	
	changeProjectImage();
	
	$('#project_tapa').stop().fadeOut('slow');
}

function showPreviousImage() {
	// la anterior imagen es la de arriba
	var waitTime = hideVideo();
	
	$(this).oneTime(waitTime, function() {
		imageActual -= 1;
	
		var imageSource = getImageSourceActual();
		preloadImage(imageSource, showPreviousImageEnd);
  	});
  	
  	//if (isIE) $(imgCenter)[0].src = "css/css_img/sp.gif";
}

function showPreviousImageEnd() {
	// ya se ha precargado la imagen...
	var imageSource = getImageSourceActual();

	if (isIE) {
		$(imgCenter)[0].src = imageSource;
		showVideo();
	} else {
	
		if (!swfPair){
			if (isSWF){
				showSWF();
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left + $(imgCenter).width();
				var leftLeftIni = $("#flashImpair").position().left;
				var leftLeftEnd = $(imgCenter).position().left;
		
				// lo mostramos
				var navWidth = $(window).width();
				$('#flashImpair').css("top", "0px");
				$('#flashImpair').css("left", -navWidth+"px");
				
				$('#flashPair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$("#flashImpair").animate({left:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({left:centerLeftIni});
			
				});
		
			} else {
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left + $(imgCenter).width();
				var leftLeftIni = $(imgLeft).position().left;
				var leftLeftEnd = $(imgCenter).position().left;
		
				$(imgLeft)[0].src = imageSource;
		
				$('#flashPair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashPair').html('').hide();
				});
		
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$(imgLeft).animate({left:leftLeftEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({left:centerLeftIni});
					$(imgLeft).css({left:leftLeftIni});
					showVideo();
				});
			}
		} else {
		if (isSWF){
				showSWF();
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left + $(imgCenter).width();
				var leftLeftIni = $("#flashPair").position().left;
				var leftLeftEnd = $(imgCenter).position().left;
		
				// lo mostramos
				var navWidth = $(window).width();
				$('#flashPair').css("top", "0px");
				$('#flashPair').css("left", -navWidth+"px");
				
				$('#flashImpair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$("#flashPair").animate({left:0}, 1000, function(){
					$(imgCenter)[0].src = "css/css_img/spacer.gif"
					$(imgCenter).css({left:centerLeftIni});
			
				});
		
			} else {
		
				var centerLeftIni = $(imgCenter).position().left;
				var centerLeftEnd = $(imgCenter).position().left + $(imgCenter).width();
				var leftLeftIni = $(imgLeft).position().left;
				var leftLeftEnd = $(imgCenter).position().left;
		
				$(imgLeft)[0].src = imageSource;
		
				$('#flashImpair').stop().animate({left:centerLeftEnd}, 1000, function(){
					$('#flashImpair').html('').hide();
				});
		
				$(imgCenter).animate({left:centerLeftEnd}, 1000);
				$(imgLeft).animate({left:leftLeftEnd}, 1000, function(){
					$(imgCenter)[0].src = imageSource;
					$(imgCenter).css({left:centerLeftIni});
					$(imgLeft).css({left:leftLeftIni});
					showVideo();
				});
			}
		}
	}
	
	changeProjectImage();
	
	$('#project_tapa').stop().fadeOut('slow');
}

// simulación de 'position: fixed;' para IE6
//function move_box() {
//	var main_image_element = document.getElementById('image_holder');
//    main_image_element.style.top = (document.documentElement.scrollTop + 0) + 'px';
//}
