/*
Inicialitzacio js inicial
Requereix: 
	Javascript: mootools.js (1.1)
				mootools-more.js (1.1)
			
*/
var slider = {};
var stepIntervalId;

//Construeix una barra de scroll amb javascript
// @param content id del element a scrollable
// @param scrollbar id del element que contindra la barra de scroll
// @param handle id del element que contindra el element controlador de scroll
// @param horizontal true o false Scroll horizontal o vertical
// @param ignoreMouse
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
				
	//Eliminem els possibles overflows del element scrollable
	content.style.overflow='hidden';
	//Mostrem la barra de scroll
	scrollbar.getParent().setStyle("display", "block");
				
	var steps = (horizontal?(content.getSize().scrollSize.x - content.getSize().size.x):(content.getSize().scrollSize.y - content.getSize().size.y))
	
	slider = new Slider(scrollbar, handle, {	
			steps: steps,
			mode: (horizontal?'horizontal':'vertical'),
			onChange: function(step){
				var x = (horizontal?step:0);
				var y = (horizontal?0:step);
				content.scrollTo(x,y);
				changeSlide(this);				
			}
		}).set(0);
			
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 50;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});

	
	$(document.body).addEvent('mouseup',function(e) {
											if (stepIntervalId) {
												clearInterval(stepIntervalId);
												stepIntervalId = false;
											}
										});
	
	//Afegim els controladors dreta i esquerra	
	$('handleLeft2').addEvent('mousedown', function(e){
											e = new Event(e).stop();
											stepLeft();
											stepIntervalId = setInterval(stepLeft, 400);											
										});
											
	$('handleLeft2').addEvent('mouseup', function(e){
											e = new Event(e).stop();
											clearInterval(stepIntervalId);
											stepIntervalId = false;
										});
		
	$('handleRight2').addEvent('mousedown', function(e){
											e = new Event(e).stop();
											stepRight();
											stepIntervalId = setInterval(stepRight, 400);
										});
		
	$('handleRight2').addEvent('mouseup', function(e){
											e = new Event(e).stop();
											clearInterval(stepIntervalId);
											stepIntervalId = false;
										});
	
}


function changeSlide(slider) {
	//Canviem la icona depenent si arribem al tope dreta o esquerra
	//Si no posem aquesta validacio pot canviar al fletxa per un NaN
	

	if (!isNaN(slider.step)) {
		
		if (slider.step <= 0) {
			//Si estem al tope esquerra
			currentImg = $('handleLeft2').getAttribute('src');
			if (currentImg.indexOf('FW_fletxaLeftTope.') < 0) {
				currentImg = currentImg.replace(/FW_fletxaLeft./, 'FW_fletxaLeftTope.'); 
				$('handleLeft2').setAttribute('src', currentImg);
				$('handleLeft2').setAttribute('style','cursor:default');
				
			}
			currentImg = $('handleRight2').getAttribute('src');		
			if (!(currentImg.indexOf('FW_fletxaRight.') >= 0)) {
				currentImg = currentImg.replace(/FW_fletxaRightTope./, 'FW_fletxaRight.');
				$('handleRight2').setAttribute('src', currentImg);
				$('handleRight2').setAttribute('style','cursor:hand');
			}
		}else if (slider.step >= slider.options.steps) {
			//Si estem al tope dreta
			currentImg = $('handleRight2').getAttribute('src');
			if (currentImg.indexOf('FW_fletxaRightTope.') < 0) {		
				currentImg = currentImg.replace(/FW_fletxaRight./, 'FW_fletxaRightTope.'); 
				$('handleRight2').setAttribute('src', currentImg);
				$('handleRight2').setAttribute('style','cursor:default');
			}
		}else {
			//Si estem pel mig comprovem que les icones son les correctes
			currentImg = $('handleRight2').getAttribute('src');		
			if (!(currentImg.indexOf('FW_fletxaRight.') >= 0)) {
				currentImg = currentImg.replace(/FW_fletxaRightTope./, 'FW_fletxaRight.');
				$('handleRight2').setAttribute('src', currentImg);
				$('handleRight2').setAttribute('style','cursor:hand');
			}
			currentImg = $('handleLeft2').getAttribute('src');		
			if (!(currentImg.indexOf('FW_fletxaLeft.') >= 0)) {
				currentImg = currentImg.replace(/FW_fletxaLeftTope./, 'FW_fletxaLeft.');
				$('handleLeft2').setAttribute('src', currentImg);
				$('handleLeft2').setAttribute('style','cursor:hand');
			}
		}
	}
}


function stepLeft() {
	var step = 0;
	if (slider.step) {
		step = slider.step;
	}
	step = step - 100;
	slider.set(step);	
}

function stepRight() {
	var step = 0;
	if (slider.step) {
		step = slider.step;
	}
	step = step + 100;
	slider.set(step);	
}
//var cercaAvancada = {};
/**
 * 

function makeCercaAvancada(openCloseLink_, targetMenuDiv_, menuContentHref_) {
	cercaAvancada = new Desplegable({ openCloseLink: openCloseLink_,  
							   					   targetMenuDiv: targetMenuDiv_, 
							   					   menuContentHref: menuContentHref_, 
							   					   openFromLeft: false,  
							   					   onOpen: initializeCercaAvancadaForm
							   					   });
}
 */
//Fem desapareixer el link de totes de les galeries
function initTotesLesGaleries() {	
	$('LinkTotesLesGaleries').remove();
}

//Inicialitzacio de la pagina
window.addEvent('domready', function(){				
			makeScrollbar( $('FW_scrollH2'), $('scrollbar2'), $('handle2'), true, false );
			});
