var videos = new Array('http://www.youtube.com/embed/HsQc_d41taw?rel=0','http://www.youtube.com/embed/gez7F44BV74?rel=0','http://www.youtube.com/embed/oi-bpmWMDJ4?rel=0','http://www.youtube.com/embed/hxtpkMLA-1I?rel=0','http://www.youtube.com/embed/N9oI5WzHhVg?rel=0','http://www.youtube.com/embed/KDrd_Am30bU?rel=0','http://www.youtube.com/embed/h1R03TFrrQo?rel=0','http://www.youtube.com/embed/tRjv2yFUjvc?rel=0');

var titulos = new Array('Retrato', 'Desnudo', 'Movimiento', 'Movimiento Fotografico', 'Contra Luz', 'Noche', 'Exposicion', 'Paisaje');

$(window).bind('resize', function(){ clearTimeout(resizeTimeout); resizeWindow(false);});

function slideShowStart(){
	$('#slideshows li').each(function(i, item){		
		if(i == 0){
			$(item).css({zIndex: 1});
		}else{
			$(item).css({zIndex: 0}).hide();						
		}
		
		adjustCss($('img', item));	
		carrousel();
	});
	
	$('#carrousel li a').click(function(){
		//selectSlide($(this).parent().index());
		location.href = '#exp:'+($(this).parent().index()+1);
		//$.historyLoad('exp:'+$(this).parent().index());
		return false;
	});
	
	$('.prev').click(animatePrev);
	$('.next').click(animateNext);
	
	$('.prevc').click(animatePrevSlider); 
	$('.nextc').click(animateNextSlider);
	
	$('a.close').click(closeModal);
	//$('.como a').click(function(){ showModal(); return false;});
	$('#carrousel').animate({bottom: 61, easing: 'easeOutCubic'}, 1500);
	$('#carrousel li').hover(function(){
		if(actualLi.index() != $(this).index()){
			$(this).animate({opacity: .9}, 500);
		}
	}, function(){	
		if(actualLi.index() != $(this).index()){
			$(this).animate({opacity: .41}, 500);
		}
	});
	
	configVideo();
	
	//swfobject.embedSWF("mp3.swf", "playermp3", "41", "15", "10.0.0", "expressInstall.swf", {musicUrl: 'mp3/song.mp3'}, {wmode: 'transparent'});
	
}

function resizeWindow(fromtimeout, noadjust){
	w = ($(window).width() < 1000)?1000:$(window).width();
	h = ($(window).height() < 560 )?560:$(window).height();	
	
	if(w == 1000 || h == 560){
		$('body').css({overflow: 'auto'});
	}else{
		$('body').css({overflow: 'hidden'});
	}
		
	$('#container').css({width: w, height: h }); 			
	
	if(fromtimeout && !(noadjust)){		
		$('#slideshows li').each(function(i, item){
			adjustCss($('img', item));	
		});
	}else{		
		if(!noadjust){
			adjustCss($('img',actualLi));
			resizeTimeout = setTimeout('resizeWindow(true, false)', 300);
		}
	}	
}

function adjustCss(imageLi){		
	try{
	    var bgCSS = {left: 0, top: 0}
	    var bgWidth = w;
	    var bgHeight = bgWidth / imgRatio;
	
	    if(bgHeight >= h) {
	        bgOffset = (bgHeight - h) /2;
	        bgCSS = {top: "-" + parseInt(bgOffset) + "px", left: 0};
	    } else {
	        bgHeight = h;
	        bgWidth = bgHeight * imgRatio;
	        bgOffset = (bgWidth - w) / 2;
	        bgCSS = {left: "-" + parseInt(bgOffset) + "px", top: 0};
	    }
	    	
	    imageLi.width( parseInt(bgWidth) ).height( parseInt(bgHeight) ).css(bgCSS);	    
	}catch(err) {
		console.log(err);
	}
}

function carrousel(){
	var selected = actualLi.index();
	$('#carrousel li').css({opacity: .41}, 800);
	$('#carrousel li:eq('+selected+')').css({opacity: 1}, 800);
}

var timerSlide = null;
var animatiOn = false;

function animateNext(){	
	var pNext = actualLi.next();
	
	if(!pNext.hasClass('slide')){
		pNext = $('#slideshows li:first');
	}
	
	pNext = parseInt($(pNext).index()) + 1; 
	$.historyLoad('exp:'+pNext);

	return false;	
}

function animatePrev(){
	var pPrev = actualLi.prev();
	
	if(!pPrev.hasClass('slide')){
		pPrev = $('#slideshows li:last');
	}
	
	pPrev = parseInt($(pPrev).index())+1; 
	$.historyLoad('exp:'+pPrev);

	return false;
}

function selectSlide(indexSlide, callback){
	if(animatiOn){ return; }
	animatiOn = true;
	
	var tempLi = $('#slideshows li:eq('+indexSlide+')');
	
	if(indexSlide != actualLi.index()){	
		var tmpActual = actualLi;
		actualLi = tempLi;		
		carrousel();
		
		$(actualLi).show();
		$(tmpActual).animate({opacity: 0}, 800, function(){
			$(this).css({zIndex: 0, opacity: 1}).hide();
			$(actualLi).css({zIndex: 1});
			animatiOn = false;
			changeTitle();
			
			if(callback){
				callbackModal();
			}
		});		
	}else{
		animatiOn = false;
		if(callback){
			callbackModal();
		}		
	}
}

function changeTitle(){
	$('.ptitle').attr('src', 'images/ptitles/'+actualLi.index()+'.png');
	//$('.como a').attr('href', '#mod:'+(actualLi.index()+1));	
	$('#link-aprende-como').attr('href', '#mod:'+(actualLi.index()+1));
	$('#tipo_foto').val(titulos[actualLi.index()]);
}


var modalOpen = false;
function showModal(){	
	selectSlide(modalLi.index(), true);
}

function callbackModal(){
	if(modalOpen){
		closeModal();
		return;
	}
	
	$('#modalbox').animate({opacity: 0}, 10, function(){
		$('#content').fadeIn(600);
		$(this).css({display: 'block'}).animate({opacity: .8}, 600);
		modalOpen = true;
	});
	
	$('.titular h2').css({backgroundImage: 'url(images/titles/'+actualLi.index()+'.png)'});
	$('.tips a').attr('href', 'pdf/'+actualLi.index()+'.pdf');
	
	configSlider(); // TESTING - DELETE THIS LINE
	configVideo();
	return false;
}


function configVideo(){
	$('.youtube-player').attr('src', videos[actualLi.index()]);
}

function closeModal(){
	$('#content').fadeOut(600);
	$('#modalbox').animate({opacity: 0}, 600, function(){
		$('.youtube-player').attr('src', 'about:blank');
		$(this).hide();
		
		$.historyLoad('exp:'+(actualLi.index()+1));
		modalOpen = false;
	});
	
	return false;
}

function keyPressEvent(){
	$(document).keydown(function(e){
    	if (e.keyCode == 37 && !modalOpen) { 
			animatePrev();
       		return false;
    	}else if(e.keyCode == 39 && !modalOpen){
    		animateNext();
    		return false;
    	}
	});
}

var slider_max = 1;
var slider_items = 0;
var slider_actual = 1;
var slider_move_left = -720;

function configSlider(){
	slider_items = $('div.mask ul.slider li').length;
	slider_max = Math.ceil((slider_items != 0)?(slider_items / 5):0);
	slider_actual = 1;
	
	$('.prevc').hide();
	$('.nextc').hide();
	
	if(slider_items > 5){
		$('.nextc').show();		
	}
	
	$('div.mask ul.slider').css({marginLeft: 0});
}

function animatePrevSlider(){
	slider_actual--;
	$('div.mask ul.slider').stop().animate({marginLeft: ((slider_actual - 1) * slider_move_left), easing: 'easeOutCubic' }, 800);	
	$('.prevc').hide();
	$('.nextc').hide();
	
	if(1 < slider_actual){$('.prevc').show();}
	if(slider_max > slider_actual){$('.nextc').show();}
	return false;
}

function animateNextSlider(){
	slider_actual++;
	$('div.mask ul.slider').stop().animate({marginLeft: ((slider_actual - 1) * slider_move_left), easing: 'easeOutCubic' }, 800);	
	$('.prevc').hide();
	$('.nextc').hide();
	
	if(1 < slider_actual){$('.prevc').show();}
	if(slider_max > slider_actual){$('.nextc').show();}
	return false;
}
