/*
 * Goweb Sitemanager 4 Framework
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2008 (c) Goweb (http://www.goweb.pt)
 */

function buttonUp(div) {
	window[div + 'slider'].setValue(window[div + 'slider'].value-0.1);
}

function buttonDown(div){
	window[div + 'slider'].setValue(window[div + 'slider'].value+0.1);
}

function buttonLeft(div) {
	window[div + 'slider'].setValue(window[div + 'slider'].value-0.1);
}

function buttonRight(div){
	window[div + 'slider'].setValue(window[div + 'slider'].value+0.1);
}


function createSliderVertical(div, reverse , width, height){
			is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
			if (reverse != 1) reverse = 0;
			if (!$(div + '_scrollable')){
				alert('É necessário criar a div "' + div + '_scrollable" para que as coisas funcionem correctamente!');
				return false;
			}
			if (!$(div + '_handle')){
				alert('É necessário criar a div "' + div + '_handle" para que as coisas funcionem correctamente!');
				return false;
			}
			if (!$(div + '_track')){
				alert('É necessário criar a div "' + div + '_track" para que as coisas funcionem correctamente!');
				return false;
			}

			if (reverse == 1){
				window[div + 'slider'] = new Control.Slider(div + '_handle', div + '_track', {
					sliderValue: 1,
					axis: 'vertical',
					onSlide: function(v) { scrollVertical(v, $(div + '_scrollable'), window[div + 'slider'], reverse);  },
					onChange: function(v) { scrollVertical(v, $(div + '_scrollable'), window[div + 'slider'], reverse); }
				});
			} else {
				window[div + 'slider'] = new Control.Slider(div + '_handle', div + '_track', {
					axis: 'vertical',
					onSlide: function(v) { scrollVertical(v, $(div + '_scrollable'), window[div + 'slider'], reverse);  },
					onChange: function(v) { scrollVertical(v, $(div + '_scrollable'), window[div + 'slider'], reverse); }
				});
			}

			function scrollVertical(value, element, slider, rev) {
				element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
			}

			function handle(delta) {
				window[div + 'slider'].setValueBy(-delta);
			}
			if (!height) height = "20px";
			if (!width) width = "100px";

			if ($(div + '_scrollable').style.overflow != "hidden"){
				$(div + '_scrollable').style.overflow = "hidden";
				//if (is_ie) $(div + '_scrollable').style.position = "relative";
			}

			if (!$(div + '_scrollable').style.width){
				$(div + '_scrollable').style.width = width;
			}

			if (!$(div + '_scrollable').style.height){
				$(div + '_scrollable').style.height = height;
			}

			if ($(div + '_buttonDown')){
					$(div + '_buttonDown').onmouseover = new Function('Timer = setInterval(\'buttonDown("' + div + '")\', 100);');
					$(div + '_buttonDown').onmouseout = new Function('clearInterval(Timer)');
			}

			if ($(div + '_buttonUp')){
				$(div + '_buttonUp').onmouseover = new Function('Timer = setInterval(\'buttonUp("' + div + '")\', 100);');
				$(div + '_buttonUp').onmouseout = new Function('clearInterval(Timer)');
			}

			if ($(div + '_scrollable').scrollHeight <= $(div + '_scrollable').offsetHeight) {
				window[div + 'slider'].setDisabled();
				$(div + '_track').hide();
			}
			//if (reverse == 1) $(div + '_scrollable').scrollTop = $(div + '_scrollable').scrollHeigh-$(div + '_scrollable').offsetHeight;
			//if (reverse == 1) window[div + 'slider'].setValue(1);


			function wheel(event){
				var delta = 0;
				if (!event) /* For IE. */
					event = window.event;
				if (event.wheelDelta) { /* IE/Opera. */
					delta = event.wheelDelta/120;
				} else if (event.detail) { /** Mozilla case. */
					delta = -event.detail/3;
				}
				if (delta)
					handle(delta);
				if (event.preventDefault)
					event.preventDefault();
				event.returnValue = false;
			}
			Event.observe(div + '_scrollable', 'DOMMouseScroll', wheel);
			Event.observe(div + '_scrollable', 'mousewheel', wheel);
}

function createSliderHorizontal(div,reverse , width, height){
			this.currentValue = 1;
			is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
			if (reverse != 1) reverse = 0;
			if (!$(div + '_scrollable')){
				alert('É necessário criar a div "' + div + '_scrollable" para que as coisas funcionem correctamente!');
				return false;
			}
			if (!$(div + '_handle')){
				alert('É necessário criar a div "' + div + '_handle" para que as coisas funcionem correctamente!');
				return false;
			}
			if (!$(div + '_track')){
				alert('É necessário criar a div "' + div + '_track" para que as coisas funcionem correctamente!');
				return false;
			}

			window[div + 'slider'] = new Control.Slider(div + '_handle', div + '_track', {
				onSlide: function(v) { scrollHorizontal(v, $(div + '_scrollable'), window[div + 'slider']);  },
				onChange: function(v) { scrollHorizontal(v, $(div + '_scrollable'), window[div + 'slider']); }
			});

			function scrollHorizontal(value, element, slider) {
				this.currentValue = value;
				element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
			}
			if (!height) height = "20px";

			if (!width) width = "100px";

			if ($(div + '_scrollable').style.overflow != "hidden"){
				//$(div + '_scrollable').style.overflow = "hidden";
				//if (is_ie) $(div + '_scrollable').style.position = "relative";
			}

			if (!$(div + '_scrollable').style.width){
				//$(div + '_scrollable').style.width = width;
			}

			if (!$(div + '_scrollable').style.height){
				//$(div + '_scrollable').style.height = height;
			}

			if ($(div + '_buttonRight')){
					$(div + '_buttonRight').onmousedown = new Function('Timer = setInterval(\'buttonRight("' + div + '")\', 50);');
					$(div + '_buttonRight').onmouseup = new Function('clearInterval(Timer)');
			}

			if ($(div + '_buttonLeft')){
				$(div + '_buttonLeft').onmousedown = new Function('Timer = setInterval(\'buttonLeft("' + div + '")\', 50);');
				$(div + '_buttonLeft').onmouseup = new Function('clearInterval(Timer)');
			}

			if ($(div + '_scrollable').style.whiteSpace != 'nowrap'){
				//$(div + '_scrollable').style.whiteSpace = 'nowrap';
			}

			if (reverse == 1) window[div + 'slider'].setValue(window[div + 'slider'].maximum);

}

