/*
 * Gestion de la galerie sur les fiches hôtel
 */

// init namespace (à utiliser pour éviter toute collision)
if(!SOF) {var SOF = {};}

// données pour les scripts de cette page
SOF.datasGallery = {
	'animDelay': 30,
	'animSlow': 6,
	'adjust': 6
};
if(!$$DG) {var $$DG = SOF.datasGallery;}

// galerie
SOF.gallery = {
	init: function() {
		$$DG.gallery = Box.nodes.get('#overture-gallery')[0];
		if(!$$DG.gallery) {return;}
		Box.nodes.addClass($$DG.gallery, 'js');
		$$DG.lastActive = Box.nodes.get('li.active', [$$DG.gallery])[0];
		var insert = '';
		$$DG.mask = Box.nodes.get('#overture-gallery-mask')[0];
		$$DG.img = Box.nodes.get('#visuel')[0];
		$$DG.maskH = $$DG.mask.offsetHeight;
		$$DG.items = Box.nodes.get('li', [$$DG.gallery]);
		$$DG.list = Box.nodes.get('ul', [$$DG.gallery])[0];
		$$DG.list.style.top = '0';
		$$DG.listH = $$DG.list.scrollHeight;
		$$DG.max = Math.ceil($$DG.listH - $$DG.maskH - $$DG.adjust);
		if($$DG.listH > $$DG.maskH + 10) {
			insert += '<a href="#" id="scroll-up" class="hide"><img alt="' + SOF[$$L].alt.scrollUp + '" src="/imagerie/fichehotel/sof/fleche_up.gif" /></a>';
			insert += '<a href="#" id="scroll-down"><img alt="' + SOF[$$L].alt.scrollDown + '" src="/imagerie/fichehotel/sof/fleche_down.gif" /></a>';
			Box.nodes.insert(insert, $$DG.gallery, 'first', {
				'tag': 'a',
				'click': function(e) {SOF.gallery.exe(e);}
			});
		}
		$$DG.up = Box.nodes.get('#scroll-up')[0];
		$$DG.down = Box.nodes.get('#scroll-down')[0];
		Box.events.add($$DG.list, 'click', function(e) {
			SOF.gallery.exe(e);
		});
	},
	
	exe: function(e) {
		var t = e.target || e.srcElement;
		while(t.nodeName.toLowerCase() != 'a' && t  != $$DG.gallery) {t = t.parentNode;}
		if(Box.nodes.attribute(t, 'id') == 'scroll-up') {
			if($$DG.inAnim) {return;}
			var to = parseInt($$DG.list.style.top) + $$DG.maskH + $$DG.adjust;
			to = to <= 0 ? - to : 0;
			$$DG.inAnim = true;
			SOF.gallery.slideUp(to);
		} else if(Box.nodes.attribute(t, 'id') == 'scroll-down') {
			if($$DG.inAnim) {return;}
			var to = Math.abs(parseInt($$DG.list.style.top)) + $$DG.maskH + $$DG.adjust;
			if(to > $$DG.max) {to = $$DG.max;}
			$$DG.inAnim = true;
			SOF.gallery.slideDown(to);
		} else {
			if($$DG.lastActive) {Box.nodes.removeClass($$DG.lastActive, 'active');}
			$$DG.lastActive = t.parentNode;
			Box.nodes.addClass($$DG.lastActive, 'active');
			$$DG.img.src = t.href;
		}
		e.preventDefault();
		t = null; // nettoyage
	},
	
	slideDown: function(to) {
		var top = Math.abs(parseInt($$DG.list.style.top));
		if(top >= to) {
			$$DG.list.style.top = - to + 'px';
			clearTimeout($$DG.timer);
			$$DG.inAnim = false;
			Box.nodes.removeClass($$DG.up, 'hide');
			if(to >= $$DG.max) {Box.nodes.addClass($$DG.down, 'hide');}
		} else {
			$$DG.list.style.top = - (top + Math.ceil((to - top) / $$DG.animSlow)) + 'px';
			$$DG.timer = setTimeout(function() {SOF.gallery.slideDown(to)}, $$DG.animDelay);
		}
	},
	
	slideUp: function(to) {
		var top = Math.abs(parseInt($$DG.list.style.top));
		if(top <= to) {
			$$DG.list.style.top = - to + 'px';
			clearTimeout($$DG.timer);
			$$DG.inAnim = false;
			Box.nodes.removeClass($$DG.down, 'hide');
			if(to <= 0) {Box.nodes.addClass($$DG.up, 'hide');}
		} else {
			$$DG.list.style.top = - (top + Math.floor((to - top) / $$DG.animSlow)) + 'px';
			$$DG.timer = setTimeout(function() {SOF.gallery.slideUp(to)}, $$DG.animDelay);
		}
	}
};

// chargement des fonctions
Box.events.load(SOF.gallery.init);

// déchargement (nettoyage variables)
Box.events.add(window, 'unload', function() {
	$$DG = null;
	SOF.datasGallery = null;
});