// Title: tigra slider control pro
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_slider_control_pro/
// Version: 1.2
// Date: 11/06/2007
// Notes: This script is commercial software. License must be purchased to use it legally.

function slider (a_init, a_tpl) {

	this.f_setValue = f_sliderSetValue;
	this.f_getPos   = f_sliderGetPos;
	this.f_getClip  = f_sliderGetClip;
	this.f_onButton = f_sliderOnButton;
	
	// 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];
	for (s_key in this)
		if (typeof(this[s_key]) == 'string' && s_key.indexOf('s_img') == 0) {
		var e_img = new Image();
		e_img.src = this[s_key];
		this['cache_' + s_key] = e_img;
	}

	this.n_pix2value = this.n_pathLength / (this.n_maxValue - this.n_minValue);
	this.n_increments = this.n_step ? this.n_step : (this.n_maxValue - this.n_minValue) / 10;
	if (this.n_value == null)
		this.n_value = this.n_minValue;

	// generate the control's HTML
	var s_butMinus = this.s_imgMinus ? '<td' + (this.s_imgMinusHover ? ' onmouseover="A_SLIDERS[' + this.n_id + '].f_onButton(\'Minus\', \'over\')"' : '') + ' onmouseout="A_SLIDERS[' + this.n_id + '].f_onButton(\'Minus\', \'out\')" onmousedown="A_SLIDERS[' + this.n_id + '].f_onButton(\'Minus\', \'down\')" onmouseup="A_SLIDERS[' + this.n_id + '].f_onButton(\'Minus\', \'up\')"><img src="' + this.s_imgMinus + '" ondrag="return false" name="sl' + this.n_id + 'minus" id="sl' + this.n_id + 'minus" border="0" style="cursor:pointer;"/></td>' : 0,
		s_slider = 
			'<td background="' + this.s_imgControl + '"><div id="sl' + this.n_id + 'base" onmousedown="return f_sliderMouseDown(' + this.n_id + ')" valign="top" style="position:relative;width:' + this.n_controlWidth + 'px;height:' + this.n_controlHeight + 'px;">' +
			(this.s_imgBar ? '<img src="' + this.s_imgBar + '" border="0" style="position:absolute;left:0px;top:0px;visibility:hidden;z-index:' + this.n_zIndex + '" name="sl' + this.n_id + 'bar" id="sl' + this.n_id + 'bar" onmousedown="return f_sliderMouseDown(' + this.n_id + ')">' : '') +
			'<img src="' + this.s_imgSlider + '" width="' + this.n_sliderWidth + '" height="' + this.n_sliderHeight + '" border="0" style="position:relative;left:' + this.n_pathLeft + 'px;top:' + this.n_pathTop + 'px;z-index:' + (this.n_zIndex + 1) + ';cursor:pointer;visibility:hidden;" name="sl' + this.n_id + 'slider" id="sl' + this.n_id + 'slider"/></div></td>',
		s_butPlus  = this.s_imgPlus  ? '<td' + (this.s_imgPlusHover ? ' onmouseover="A_SLIDERS[' + this.n_id + '].f_onButton(\'Plus\', \'over\')"' : '')  + ' onmouseout="A_SLIDERS['  + this.n_id + '].f_onButton(\'Plus\', \'out\')" onmousedown="A_SLIDERS[' + this.n_id + '].f_onButton(\'Plus\', \'down\')" onmouseup="A_SLIDERS['  + this.n_id + '].f_onButton(\'Plus\', \'up\')"><img src="'  + this.s_imgPlus  + '" ondrag="return false" name="sl' + this.n_id + 'plus" id="sl'  + this.n_id + 'plus" border="0" style="cursor:pointer;"/></td>' : 0;
	if (this.b_reverse) {
		var s_temp = s_butMinus;
		s_butMinus = s_butPlus;
		s_butPlus = s_temp;
	}

	document.write(
		'<table cellpadding="0" cellspacing="' + this.n_spacing + '" border="0"><tr>' +
		(this.b_vertical ? (s_butPlus ? s_butPlus + '</tr><tr>' : '') : (s_butMinus ? s_butMinus : '')) +
		s_slider +
		(this.b_vertical ? (s_butMinus ? '</tr><tr>' + s_butMinus  : '') : (s_butPlus ? s_butPlus : '')) +
		'</tr></table>'
	);
	
	this.e_imgBase   = get_element('sl' + this.n_id + 'base');
	this.e_imgSlider = get_element('sl' + this.n_id + 'slider');
	if (s_butMinus) this.e_imgMinus = get_element('sl' + this.n_id + 'minus');
	if (s_butPlus)  this.e_imgPlus  = get_element('sl' + this.n_id + 'plus');
	
	// preset to the value in the input box if available
	var e_input = this.s_form == null
		? get_element(this.s_name)
		: document.forms[this.s_form]
			? document.forms[this.s_form].elements[this.s_name]
			: null;
	this.f_setValue(e_input && e_input.value != '' ? e_input.value : null, 1);
	this.e_imgSlider.style.visibility = 'visible';
	
	if (this.s_imgBar) {
		this.e_imgBar = get_element('sl' + this.n_id + 'bar');
		this.e_imgBar.style.clip = this.f_getClip();
		this.e_imgBar.style.visibility = 'visible'
	}

	// safely hook document/window events
	if (document.addEventListener) {
		document.addEventListener('mousemove', f_sliderMouseMove, false);
		document.addEventListener('mouseup',   f_sliderMouseUp, false);
	}
	if (window.attachEvent) {
		document.attachEvent('onmousemove', f_sliderMouseMove);
		document.attachEvent('onmouseup',   f_sliderMouseUp);
	}
	else {
		document.onmousemove = f_sliderMouseMove;
		document.onmouseup = f_sliderMouseUp;
	}
}

function f_sliderSetValue (n_value, b_noInputCheck) {
	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;

	this.n_value = n_value;

	// move the slider
	var n_pxOffset = Math.round((n_value - this.n_minValue) * this.n_pix2value);
	if (this.b_vertical)
		this.e_imgSlider.style.top  = (this.b_reverse
			? (this.n_pathTop + n_pxOffset)
			: (this.n_pathTop + this.n_pathLength - n_pxOffset)) + 'px';
	else
		this.e_imgSlider.style.left = (this.b_reverse
			? (this.n_pathLength - n_pxOffset + this.n_pathLeft)
			: (this.n_pathLeft + n_pxOffset)) + 'px';

	// save new value
	var e_input;
	if (this.s_form == null) {
		e_input = get_element(this.s_name);
		if (!e_input)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with ID='" + this.s_name + "'.");
	}
	else {
		var e_form = document.forms[this.s_form];
		if (!e_form)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the form with NAME='" + this.s_form + "'.");
		e_input = e_form.elements[this.s_name];
		if (!e_input)
			return b_noInputCheck ? null : f_sliderError(this.n_id, "Can not find the input with NAME='" + this.s_name + "'.");
	}
	// handle min and max values
	if (this.e_imgMinus) {
		if (this.n_value == this.n_minValue) {
			this.e_imgMinusDisabled = true;
			this.e_imgMinus.src = this.s_imgMinusDisabled ? this.s_imgMinusDisabled : this.s_imgMinus;
		}
		else if (this.e_imgMinusDisabled) {
			this.e_imgMinusDisabled = false;
			this.e_imgMinus.src = this.s_imgMinus;
		}
	}
	if (this.e_imgPlus) {
		if (this.n_value == this.n_maxValue) {
			this.e_imgPlusDisabled = true;
			this.e_imgPlus.src = this.s_imgPlusDisabled ? this.s_imgPlusDisabled : this.s_imgPlus;
		}
		else if (this.e_imgPlusDisabled) {
			this.e_imgPlusDisabled = false;
			this.e_imgPlus.src = this.s_imgPlus;
		}
	}
	if (this.e_imgBar)
		this.e_imgBar.style.clip = this.f_getClip();

	e_input.value = n_value;
	if (this.h_onChange)
		this.h_onChange(n_value);
}

function f_sliderOnButton (s_button, s_event) {
	// customer event hanldler
	if (this.h_onButton && !this.h_onButton(s_button, s_event))
		return;

	// switch back background even if disabled
	if (this.b_bgActive && s_event == 'up') {
		this.e_imgBase.style.backgroundImage = 'url(' + this.s_imgControl + ')';
		this.b_bgActive = false;
	}

	// ignore if button is disabled
	if (this['e_img' + s_button + 'Disabled'])
		return;

	var e_img = this['e_img' + s_button];

	if (s_event == 'over') { // mouseover
		e_img.src = this['s_img' + s_button + 'Hover'];
		return;
	}
	if (s_event == 'out') { // mouseout
		if (this.e_tmRepeat)
			clearTimeout(this.e_tmRepeat);
		e_img.src = this['s_img' + s_button];
		return;
	}
	if (s_event == 'down') { // mousedown
		if (this['s_img' + s_button + 'Down'])
			e_img.src = this['s_img' + s_button + 'Down'];
		if (this.s_imgControlActive && !this.b_bgActive) {
			this.e_imgBase.style.backgroundImage = 'url(' + this.s_imgControlActive + ')';
			this.b_bgActive = true;
		}

		this.f_setValue(this.n_value + this.n_increments * (s_button == 'Minus' ? -1 : 1));
		this.e_tmRepeat = setTimeout("A_SLIDERS[" + this.n_id + "].f_onButton('" + s_button + "', 'down')", this.n_repeatPeriod);
		return;
	}
	if (s_event == 'up') { // mouseup
		if (this.e_tmRepeat)
			clearTimeout(this.e_tmRepeat);
		if (this['s_img' + s_button + 'Down'])
			e_img.src = this['s_img' + s_button + 'Hover'] ? this['s_img' + s_button + 'Hover'] : this['s_img' + s_button];
		return;		
	}
}

// get absolute position of the element in the document
function f_sliderGetPos (b_vertical, b_absPos) {
	var n_pos = 0,
		o_elem = o_elem2 = this.e_imgBase,
		s_coord = (b_vertical ? 'Top' : 'Left');
	
	while (o_elem) {
		if (b_absPos || !o_elem.style.position || o_elem.style.position == 'relative')
			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) {
	window.n_activeSliderId = n_id;
	var o_slider = A_SLIDERS[n_id];
	if (o_slider.s_imgControlActive) {
		o_slider.e_imgBase.style.backgroundImage = 'url(' + o_slider.s_imgControlActive + ')';
		o_slider.b_bgActive = true;
	}
	f_sliderMouseMove();
	return false;
}

function f_sliderGetClip () {
	return 'rect(' +
		(this.b_vertical  && !this.b_reverse ? (parseInt(this.e_imgSlider.style.top)  + Math.round(this.n_sliderHeight / 2)) + 'px' : 'auto') + ',' +
		(!this.b_vertical && !this.b_reverse ? (parseInt(this.e_imgSlider.style.left) + Math.round(this.n_sliderWidth  / 2)) + 'px' : 'auto') + ',' +
		(this.b_vertical  &&  this.b_reverse ? (parseInt(this.e_imgSlider.style.top)  + Math.round(this.n_sliderHeight / 2)) + 'px' : 'auto') + ',' +
		(!this.b_vertical &&  this.b_reverse ? (parseInt(this.e_imgSlider.style.left) + Math.round(this.n_sliderWidth  / 2)) + 'px' : 'auto') + ')';
}

function f_sliderMouseUp (e_event, b_watching) {
	if (window.n_activeSliderId != null) {
		var o_slider = window.A_SLIDERS[window.n_activeSliderId];

		o_slider.f_setValue(o_slider.n_minValue + (o_slider.b_vertical
			? o_slider.b_reverse
				? (parseInt(o_slider.e_imgSlider.style.top) - o_slider.n_pathTop)
				: (o_slider.n_pathLength - parseInt(o_slider.e_imgSlider.style.top) + o_slider.n_pathTop)
			: o_slider.b_reverse
				? (o_slider.n_pathLength - parseInt(o_slider.e_imgSlider.style.left) + o_slider.n_pathLeft)
				: (parseInt(o_slider.e_imgSlider.style.left) - o_slider.n_pathLeft)
		) / o_slider.n_pix2value);
		window.status = o_slider.n_value;

		if (b_watching)	return;
		if (o_slider.b_bgActive) {
			o_slider.e_imgBase.style.backgroundImage = 'url(' + o_slider.s_imgControl + ')';
			o_slider.b_bgActive = false;
		}
		window.n_activeSliderId = null;
	}
	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) {
		if (e_event.clientX != null) window.n_mouseX = e_event.clientX + f_scrollLeft();
		if (e_event.clientY != null) 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;
		if (o_slider.b_vertical) {
			var n_sliderTop = window.n_mouseY - o_slider.n_sliderHeight / 2 - o_slider.f_getPos(1, 1);
			// 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;
			o_slider.e_imgSlider.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);
			// 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;
			o_slider.e_imgSlider.style.left = n_sliderLeft + 'px';
			n_pxOffset = n_sliderLeft - o_slider.n_pathLeft;
		}

		if (o_slider.e_imgBar && !o_slider.b_watch)
			o_slider.e_imgBar.style.clip = o_slider.f_getClip();
		if (o_slider.b_watch)
			 f_sliderMouseUp(e_event, 1);

		if (o_slider.h_onMove)
			o_slider.h_onMove(o_slider.n_value);
		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;
}
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

