Zee = window.Zee || {};

/**
 * Modal
 *
 * Manage modal headaches
 *
 * @author Julien Cabanès <jc@aaz.fr>
 * @version 0.1
 * @class Zee.Modal
 */
Zee.Modal = {
	PageController: new Zee.PageController({
		get: {
			'#/modal/::modalPath': function(request, modalPath){
				request.keepLooping = true;
				//console.log('modalPath', modalPath);
				Zee.Modal.open(modalPath);
			}
		}
	}),
	open: function(href) {
		Zee.Modal.close();
		window.setTimeout(function() {
			//console.log(href);
			$.get(href, function(data) {
//				$('html, body').animate({scrollTop: 0}, 800);
				// Dangereux
				if (data.json==1) {
					if (data.isLightbox == 1) {
						Zee.Modal.create_lightbox('title', 'image', 'legend', 'next', 'prev');
					}
					else
					{
						Zee.Modal.create(data.title,data.content);
					}
					if (data.addClass) {
						$('div.modal').addClass(data.addClass);
					}
					var newTop = document.body.scrollTop;
					$('#modal').css({marginTop: ''+(newTop+70)+'px'});
				}
				else {
					$('body').append(data);
				}
				rebase_anchors();
				$('#overlay').show();
				var newTop = document.body.scrollTop;
				$('div.lightbox').animate({marginTop: ''+(newTop+70)+'px'}, 700);
//				$('body, html').css('overflow-y','hidden');
			});
		}, 250);
	},
	close: function(callback) {
		if($('#modal').length > 0) {
			$('#modal, #overlay').animate({opacity: 0}, 300, function() {
				$('#overlay').hide().css({opacity: 0.5});
				$('#modal').remove();
//				$('body, html').css('overflow-y','auto');
				callback && typeof callback == 'function' && callback();
			});
		}
	},
	create: function(title, content) {
		$('#overlay').show();
//		$('body, html').css('overflow-y','hidden');
		$('body').append(['<div class="modal" id="modal">',
			'<h2>'+title+'</h2>',
			'<div class="modal-scroll-content">'+content+'</div>',
			'</div>'].join(''));
//			$('html, body').animate({scrollTop: 0}, 800);
	},
	create_lightbox: function(title, image, legend, next, prev) {
		$('#modal').remove();
		$('#overlay').show();
		if (legend == undefined) {legend = '';}
//		$('body, html').css('overflow-y','hidden');
		$('body').append(['<div class="lightbox" id="modal">',
			'<h2>'+title+'</h2>',
			'<a class="close" href="#" title="Fermer">Fermer</a>',
			'<div class="lightbox-items">',
			'<img src="'+image+'" border="0" alt="'+legend+'"/>',
			'</div>',
			'<div class="legend">'+legend+'</div>',
			'<a class="arrow next" href="#/photo/'+next+'" title="Photo suivante">Photo suivante</a>',
			'<a class="arrow prev" href="#/photo/'+prev+'" title="Photo précédente">Photo précédente</a>',
			'</div>'].join(''));
			rebase_anchors();
//			$('html, body').animate({scrollTop: 0}, 800);
	},
	onOpen: function(e) {
		e.preventDefault();
		Zee.Modal.open($(this).attr('href'));
	},
	onClose: function(e) {
		e.preventDefault();
		Zee.Modal.close();
	}
};

Zee.Modal.PageController.run();

$('#modal a.close,#modal a.cancel, #overlay:visible').live('click', function(e) {
	e.preventDefault();
	Zee.Modal.close();
	Zee.Modal.PageController.setHash(Zee.Modal.PageController.lastHash || '/');
});
