$(document).ready(function(){

//Centrado vertical
	nuevo_padding=Math.floor(($(window).height()-610)/2);
	if (nuevo_padding>0) $('div.contenidor').addClass('csscentrado');
						   
	//iehover para IE6
	if ($.browser.msie && parseInt($.browser.version)<7) {
		$('li')
			.mouseover(function () {
				$(this).addClass('iehover');
			})
			.mouseout(function () {
				$(this).removeClass('iehover');
			});
	}

	$('div.thumbs a')
		.mouseover(function() {
			$('div.imagen img')[0].src=$(this).attr("imgMedia");
			$('div.imagen a')[0].href=$(this)[0].href;
			$('div.imagen a')[0].title=$(this)[0].title;
		});
		
	//Preload de las imagenes en media resolucion
	$('div.thumbs img').load(function() {
		$("body").append('<div class="preload"><img src="' + busca_media($(this)[0].src) + '" width="100" height="100" /></div>');
	});

	//Aņadimos fancybox a los links
	$("div.thumbs a").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'inside',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-inside">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; <strong style="color:#666666;">' + title : '') + '</strong></span>';
		}
		});

	$("div.imagen a").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'inside',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-inside">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; <strong style="color:#666666;">' + title : '') + '</strong></span>';
		}
		});
		
	$(document).ready(function(){   
		$("#popup").fancybox({   
		'frameWidth': 700,   
		'frameHeight': 600,   
		'hideOnContentClick': false,    
		'overlayOpacity': 0.85   
		});   
	});  	


	// Scroll horizontal
	
	$(function(){
	  //Get our elements for faster access and set overlay width
	  var div = $('div.cont_scroll'),
				   ul = $('div.cont_scroll ul'),
				   // unordered list's left margin
				   ulPadding = 15;
	
	  //Get menu width
	  var divWidth = div.width();
	
	  //Remove scrollbars
	  div.css({overflow: 'hidden'});
	
	  //Find last image container
	  var firstLi = ul.find('li:first-child');
	  var lastLi = ul.find('li:last-child');
	
	  //When user move mouse over menu
	  div.mousemove(function(e){
	
		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft - firstLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	  });
	});
});

function busca_media(baja) {
	return baja.replace('thumb_','media_');
}


