var diapoTimer = null;
var hideTitleTimer = null;
var diapoEnabled = false;

$(document).ready(function() {

	$("a[rel=fancy_album] img").lazyload({
            placeholder : public_static_url+'/Images/load-'+albumColor+'.gif',
            threshold : 200
        });


	$("a[rel=fancy_album]").fancybox({
		'onComplete'		: function(data){

			clearTimeout(diapoTimer);
			
			if(diapoEnabled){
				diapoTimer = setTimeout('suiv()',5000);
			}
			
			$('#play').click(function(){
		
				play();
				return false;
			});
			
			$('#stop').click(function(){
				
				stop();
				return false;
			});
			
//			$("#fancybox-wrap").hover(function() {
//				showTitle();
//			}, function() {
//				hideTitle();
//			});
			
			//hideTitleTimer = setTimeout('hideTitle()',3000);
		},
		'onClosed'			: function(){ startDiapo = true;},
		'cyclic'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'inside',
                'overlayOpacity':0.8,
                'overlayColor':'black',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			tmp = title.split('|');
			title = tmp[0];			
			if(typeof tmp[1] != 'undefined'){
				desc = tmp[1]+'<br />';
			}
			else {
				desc = '';
			}
			
			if(diapoEnabled){
				dPlay = 'style="display:none" ';
				dStop = '';
			}
			else {
				dStop = 'style="display:none" ';
				dPlay = '';
			}
			
		    return '<span id="fancybox-title-inside">'+
					'<div id="playpause">'+
					'<a href="" '+dPlay+'id="play"><img src="'+public_static_url+'/Images/play1.png" alt="play"/></a>'+
					'<a href="" '+dStop+'id="stop"><img src="'+public_static_url+'/Images/pause1.png" alt="pause"/></a>'+
					'</div>'+
					'<b>'+title+'</b><br />'+nl2br(desc)+
					'Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});
	

	
});


function play(){
	$('#play').hide();
	$('#stop').show();
	diapoTimer = setTimeout('suiv()',5000);
	diapoEnabled = true;
}
function stop(){
	$('#stop').hide();
	$('#play').show();
	clearTimeout(diapoTimer);
	clearTimeout(hideTitleTimer);
	diapoTimer = null;
	diapoEnabled = false;
}
function suiv(){
	$.fancybox.next();
}

//function hideTitle(){
//	$("#fancybox-title").slideUp(200);
//}
//function showTitle(){
//	clearTimeout(hideTitleTimer);
//	$("#fancybox-title").slideDown(200);
//}
function nl2br (str) {
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ '<br />' +'$2');
}
