// Title: tigra slider control
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_slider_control/
// Version: 1.0 (commented source)
// Date: 02/15/2006
// Tech. Support: http://www.softcomplex.com/forum/
// Notes: This script is free. Visit official site for further details.

function slider (a_init, a_tpl) {

	this.f_setValue  = f_sliderSetValue;
	this.f_getPos    = f_sliderGetPos;
	this.f_getSlider = f_sliderGetSlider;
	
	// register in the global collection	
	if (!window.A_SLIDERS)
		window.A_SLIDERS = [];
	this.n_id = window.A_SLIDERS.length;
	window.A_SLIDERS[this.n_id] = this;

	// save config parameters in the slider object
	var s_key;
	if (a_tpl)
		for (s_key in a_tpl)
			this[s_key] = a_tpl[s_key];
	for (s_key in a_init)
		this[s_key] = a_init[s_key];

	this.n_pix2value = this.n_pathLength / (this.n_maxValue - this.n_minValue);
	if (this.n_valueLeft == null)
		this.n_valueLeft = this.n_minValue;
	if (this.n_valueRight == null)
		this.n_valueRight = this.n_maxValue;

	// generate the control's HTML
	selectedDiv = get_element(this.s_div);
	if(selectedDiv == undefined) {
		alert("Error: Cannot find price selector div: "+this.s_div);
		return;
	}
		
	selectedDiv.innerHTML = (
		'<div style="width:' + this.n_controlWidth + 'px;height:' + this.n_controlHeight + 'px;border:0; background-image:url(' + this.s_imgControl + ');position:relative;" id="sl' + this.n_id + 'base">' +
		'<img src="' + this.s_imgSliderLeft + '" width="' + this.n_sliderWidth + '" height="' + this.n_sliderHeight + '" border="0" style="position:absolute;left:' + this.n_pathLeft + 'px;top:' + this.n_pathTop + 'px;z-index:' + this.n_zIndex + ';cursor:hand;visibility:hidden;" name="sl' + this.n_id + 'sliderLeft" id="sl' + this.n_id + 'sliderLeft" onmousedown="return f_sliderMouseDown('+ this.n_id + ",'L'" + ')"/>' + 
		'<img src="' + this.s_imgSliderRight + '" width="' + this.n_sliderWidth + '" height="' + this.n_sliderHeight + '" border="0" style="position:absolute;left:' + this.n_pathLeft  + 'px;top:' + this.n_pathTop + 'px;z-index:' + this.n_zIndex + ';cursor:hand;visibility:hidden;" name="sl' + this.n_id + 'sliderRight" id="sl' + this.n_id + 'sliderRight" onmousedown="return f_sliderMouseDown(' + this.n_id + ",'R'" + ')"/></div>'
	);
	this.e_base   = get_element('sl' + this.n_id + 'base');
	this.e_sliderLeft = get_element('sl' + this.n_id + 'sliderLeft');
	this.e_sliderRight = get_element('sl' + this.n_id + 'sliderRight');

	// safely hook document/window events
	if (document.onmousemove != f_sliderMouseMove) {
		window.f_savedMouseMove = document.onmousemove;
		document.onmousemove = f_sliderMouseMove;
	}
	if (document.onmouseup != f_sliderMouseUp) {
		window.f_savedMouseUp = document.onmouseup;
		document.onmouseup = f_sliderMouseUp;
	}
	
	// preset to the value in the input box if available
	var e_inputLeft = get_element(this.s_nameLeft);
	var e_inputRight = get_element(this.s_nameRight);
	
	// SET VALUES : for Left and for Right slider
	this.f_setValue(e_inputLeft && e_inputLeft.value != '' ? e_inputLeft.value : null, 1 , 'L');
	this.f_setValue(e_inputRight && e_inputRight.value != '' ? e_inputRight.value : null, 1 , 'R');
	this.e_sliderLeft.style.visibility = 'visible';
	this.e_sliderRight.style.visibility = 'visible';
}

function f_sliderSetValue (n_value, b_noInputCheck , sliderType) {
	if (n_value == null)
		n_value = this.n_value == null ? this.n_minValue : this.n_value;
	if (isNaN(n_value))
		return false;
	// round to closest multiple if step is specified
	if (this.n_step)
		n_value = Math.round((n_value - this.n_minValue) / this.n_step) * this.n_step + this.n_minValue;
	// smooth out the result
	if (n_value % 1)
		n_value = Math.round(n_value * 1e5) / 1e5;

	if (n_value < this.n_minValue)
		n_value = this.n_minValue;
	if (n_value > this.n_maxValue)
		n_value = this.n_maxValue;
	
	// SET TYPE
	if(sliderType == undefined)
		sliderType = 'L';
		
	// SET VALUE
	if(sliderType == 'L')	
		this.n_valueLeft = n_value;
	else 
		this.n_valueRight = n_value;
	
	// SET ACTIVE SLIDER
	var activeSlider = this.f_getSlider(sliderType);
	
	// move the slider
	if (this.b_vertical) 
		activeSlider.style.top = (this.n_pathTop + this.n_pathLength - Math.round((n_value - this.n_minValue) * this.n_pix2value)) + "px";
	else 
		activeSlider.style.left = (this.n_pathLeft + Math.round((n_value - this.n_minValue) * this.n_pix2value)) + "px";

	// save new value
	
	var e_input;
	if(sliderType == 'L')
		e_input = get_element(this.s_nameLeft);
	else 
		e_input = get_element(this.s_nameRight);
		
	e_input.value = n_value;
}

// get absolute position of the element in the document
function f_sliderGetPos (b_vertical, b_base , sliderType) {
	var n_pos = 0,
		s_coord = (b_vertical ? 'Top' : 'Left');
		
	var activeSlider = this.f_getSlider(sliderType);
	var o_elem = o_elem2 = ( b_base ) ? this.e_base : (activeSlider);
	
	while (o_elem) {
		n_pos += o_elem["offset" + s_coord];
		o_elem = o_elem.offsetParent;
	}
	o_elem = o_elem2;

	var n_offset;
	while (o_elem.tagName != "BODY") {
		n_offset = o_elem["scroll" + s_coord];
		if (n_offset)
			n_pos -= o_elem["scroll" + s_coord];
		o_elem = o_elem.parentNode;
	}
	return n_pos;
}

function f_sliderMouseDown (n_id , type) {
	window.n_activeSliderId = n_id;
	window.n_activeSliderType = type;
	return false;
}

function f_sliderMouseUp (e_event, b_watching) {
	if (window.n_activeSliderId != null) {		
		var o_slider = window.A_SLIDERS[window.n_activeSliderId];		
		var sliderType = (window.n_activeSliderType == undefined) ? 'L' : window.n_activeSliderType;
		var activeSlider = (sliderType == 'L') ? o_slider.e_sliderLeft : o_slider.e_sliderRight ;
		
		o_slider.f_setValue(o_slider.n_minValue + (o_slider.b_vertical
			? (o_slider.n_pathLength - parseInt(activeSlider.style.top) + o_slider.n_pathTop)
			: (parseInt(activeSlider.style.left) - o_slider.n_pathLeft)) / o_slider.n_pix2value , 1 , window.n_activeSliderType);
		if (b_watching)	return;
		window.n_activeSliderId = null;
		
		if(o_slider.s_actionOnMouseUp != undefined)
			eval(o_slider.s_actionOnMouseUp);
	}
	
	if (window.f_savedMouseUp)
		return window.f_savedMouseUp(e_event);
}

function f_sliderMouseMove (e_event) {

	if (!e_event && window.event) e_event = window.event;

	// save mouse coordinates
	if (e_event) {
		window.n_mouseX = e_event.clientX + f_scrollLeft();
		window.n_mouseY = e_event.clientY + f_scrollTop();
	}

	// check if in drag mode
	if (window.n_activeSliderId != null) {
		var o_slider = window.A_SLIDERS[window.n_activeSliderId];

		var n_pxOffset;
		var sliderType = (window.n_activeSliderType == undefined) ? 'L' : window.n_activeSliderType;
		var activeSlider = (sliderType == 'L') ? o_slider.e_sliderLeft : o_slider.e_sliderRight;		
		var otherSlider = (sliderType == 'L') ? o_slider.e_sliderRight : o_slider.e_sliderLeft;		
		
		if (o_slider.b_vertical) {
			var n_sliderTop = window.n_mouseY - o_slider.n_sliderHeight / 2 - o_slider.f_getPos(1, 1, sliderType) - 3;
			// limit the slider movement
			if (n_sliderTop < o_slider.n_pathTop)
				n_sliderTop = o_slider.n_pathTop;
			var n_pxMax = o_slider.n_pathTop + o_slider.n_pathLength;
			if (n_sliderTop > n_pxMax)
				n_sliderTop = n_pxMax;
				
			activeSlider.style.top = n_sliderTop + 'px';
			n_pxOffset = o_slider.n_pathLength - n_sliderTop + o_slider.n_pathTop;
		}
		else {
			var n_sliderLeft = window.n_mouseX - o_slider.n_sliderWidth / 2 - o_slider.f_getPos(0, 1, sliderType) - 3;
			// limit the slider movement
			if (n_sliderLeft < o_slider.n_pathLeft)
				n_sliderLeft = o_slider.n_pathLeft;
			var n_pxMax = o_slider.n_pathLeft + o_slider.n_pathLength;
			if (n_sliderLeft > n_pxMax)
				n_sliderLeft = n_pxMax;
			
			if(sliderType == 'L') {
				var other_left = parseInt(otherSlider.style.left);
				if(n_sliderLeft > other_left - o_slider.n_sliderWidth)
					n_sliderLeft = other_left - o_slider.n_sliderWidth;
			} else {
				var other_left = parseInt(otherSlider.style.left);
				if(n_sliderLeft  < other_left + o_slider.n_sliderWidth)
					n_sliderLeft = other_left + o_slider.n_sliderWidth;
			}
				
			activeSlider.style.left = n_sliderLeft + 'px';
			n_pxOffset = n_sliderLeft - o_slider.n_pathLeft;
		}
		if (o_slider.b_watch)
			 f_sliderMouseUp(e_event, 1);
		
		// perform aditional action on mouse move
		// egz. effects, set inputs...
		if(o_slider.s_actionOnMouseMove != undefined)
			eval(o_slider.s_actionOnMouseMove);
			
		return false;
	}
	
	if (window.f_savedMouseMove)
		return window.f_savedMouseMove(e_event);
}

// get the scroller positions of the page
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_sliderError (n_id, s_message) {
	alert("Slider #" + n_id + " Error:\n" + s_message);
	window.n_activeSliderId = null;
}

function f_sliderGetSlider(type) {
	var sliderType = (type == undefined) ? 'L' : type;
	var activeSlider = (sliderType == 'L') ? this.e_sliderLeft : this.e_sliderRight;

	return activeSlider;
}

get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

