/************************************************************************************************************
(C) www.dhtmlgoodies.com, September 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var form_widget_amount_slider_handle = 'images/slider_handle.gif';
var slider_handle_image_obj = false;
var sliderObjectArray = new Array();
var slider_counter = 0;
var slideInProgress = false;
var handle_start_x;
var event_start_x;
var currentSliderIndex;
var sliderHandleWidth = 9;
var finishAction = true;

function form_widget_cancel_event()
{
	return false;		
}

function getImageSliderHeight(){
	if(!slider_handle_image_obj){
		slider_handle_image_obj = new Image();
		slider_handle_image_obj.src = form_widget_amount_slider_handle;
	}
	if(slider_handle_image_obj.width>0){
		return;
	}else{
		setTimeout('getImageSliderHeight()',50);
	}
}


function positionSliderImage(e,theIndex)
{
	
	if(!theIndex)theIndex = this.getAttribute('sliderIndex');
	var theValue = sliderObjectArray[theIndex]['formTarget'].value;
	if(!theValue.match(/^[0-9]*$/g))theValue=sliderObjectArray[theIndex]['min'] +'';
	if(theValue/1>sliderObjectArray[theIndex]['max'])theValue = sliderObjectArray[theIndex]['max'];
	if(theValue/1<sliderObjectArray[theIndex]['min'])theValue = sliderObjectArray[theIndex]['min'];
	sliderObjectArray[theIndex]['formTarget'].value = theValue;
	
	var ratio = sliderObjectArray[theIndex]['width'] / (sliderObjectArray[theIndex]['max']-sliderObjectArray[theIndex]['min']);
	var currentValue = sliderObjectArray[theIndex]['formTarget'].value-sliderObjectArray[theIndex]['min'];
	
	var handleImg = document.getElementById('slider_handle' + theIndex);
	handleImg.style.left = Math.round(currentValue * ratio) + 'px';
}



function adjustFormValue(theIndex)
{
	var handleImg = document.getElementById('slider_handle' + theIndex);	
	var ratio = sliderObjectArray[theIndex]['width'] / (sliderObjectArray[theIndex]['max']-sliderObjectArray[theIndex]['min']);
	var currentPos = handleImg.style.left.replace('px','');
	sliderObjectArray[theIndex]['formTarget'].value = Math.round(currentPos / ratio) + sliderObjectArray[theIndex]['min'];
}
	
function initMoveSlider(e)
{
	if(document.all)e = event;	
	slideInProgress = true;
	MouseDown = true;
	event_start_x = e.clientX;
	handle_start_x = this.style.left.replace('px','');
	currentSliderIndex = this.id.replace(/[^\d]/g,'');
	return false;
}

function startMoveSlider(e)
{
	if(document.all)e = event;	
	if(!slideInProgress)return;	
	finishAction = false;
	var leftPos = handle_start_x/1 + e.clientX/1 - event_start_x;
	if(leftPos<0)leftPos = 0;
	if(leftPos/1>sliderObjectArray[currentSliderIndex]['width'])leftPos = sliderObjectArray[currentSliderIndex]['width'];
	
	if(currentSliderIndex==1){
		var handleImg = document.getElementById('slider_handle' + 2);
		var currentPos = parseInt(handleImg.style.left.replace('px',''));
		
		if(leftPos+sliderHandleWidth>currentPos) leftPos = currentPos-sliderHandleWidth;
	}else if(currentSliderIndex==2){
		var handleImg = document.getElementById('slider_handle' + 1);
		var currentPos = parseInt(handleImg.style.left.replace('px',''));
		
		if(leftPos-sliderHandleWidth<currentPos) leftPos = currentPos+sliderHandleWidth;
	}else if(currentSliderIndex==3){
		var handleImg = document.getElementById('slider_handle' + 4);
		var currentPos = parseInt(handleImg.style.left.replace('px',''));
		
		if(leftPos+sliderHandleWidth>currentPos) leftPos = currentPos-sliderHandleWidth;
	}else if(currentSliderIndex==4){
		var handleImg = document.getElementById('slider_handle' + 3);
		var currentPos = parseInt(handleImg.style.left.replace('px',''));
		
		if(leftPos-sliderHandleWidth<currentPos) leftPos = currentPos+sliderHandleWidth;
	}
	
	document.getElementById('slider_handle' + currentSliderIndex).style.left = leftPos + 'px';
	
	adjustFormValue(currentSliderIndex);
	
	if(sliderObjectArray[currentSliderIndex]['onchangeAction']){
		eval(sliderObjectArray[currentSliderIndex]['onchangeAction']);
	}
}

function stopMoveSlider()
{
	slideInProgress = false;
	if(!finishAction){
		finishAction = true;
		
		if(sliderObjectArray[currentSliderIndex] && sliderObjectArray[currentSliderIndex]['onFinishAction']){
			eval(sliderObjectArray[currentSliderIndex]['onFinishAction']);
		}
	}
}

function finishMoveSlider(){
	slideInProgress = false;
	if(sliderObjectArray[currentSliderIndex] && sliderObjectArray[currentSliderIndex]['onFinishAction']){
		eval(sliderObjectArray[currentSliderIndex]['onFinishAction']);
	}
}

function form_widget_amount_slider(targetElId,formTarget,width,min,max,onchangeAction,onFinishAction)
{
	if(!slider_handle_image_obj){
		getImageSliderHeight();		
	}
			
	slider_counter = slider_counter + 1;
	sliderObjectArray[slider_counter] = new Array();
	sliderObjectArray[slider_counter] = {"width":width - sliderHandleWidth,"min":min,"max":max,"formTarget":formTarget,"onchangeAction":onchangeAction,"onFinishAction":onFinishAction};
	
	formTarget.setAttribute('sliderIndex',slider_counter);
	formTarget.onchange = positionSliderImage;
	
	if(slider_counter==1 || slider_counter==3 || slider_counter==5){
		var parentObj = document.createElement('DIV');
		
		parentObj.style.height = '12px';	// The height of the image
		parentObj.style.position = 'relative';
		parentObj.id = 'slider_container' + slider_counter;
		document.getElementById(targetElId).appendChild(parentObj);
	}else{
		var parentObj = document.getElementById('slider_container' + (slider_counter - 1));
		document.getElementById(targetElId).appendChild(parentObj);
	}
	
	var obj = document.createElement('DIV');
	obj.className = 'fs_slider';
	obj.innerHTML = '<span></span>';
	obj.style.width = width + 'px';
	obj.id = 'slider_slider' + slider_counter;
	obj.style.position = 'absolute';
	obj.style.bottom = '0px';
	parentObj.appendChild(obj);
	
	var handleImg = document.createElement('IMG');
	handleImg.style.position = 'absolute';
	handleImg.style.left = '0px';
	handleImg.style.zIndex = 5;
	handleImg.style.cursor = "w-resize";
	handleImg.src = slider_handle_image_obj.src;
	handleImg.id = 'slider_handle' + slider_counter;
	handleImg.onmousedown = initMoveSlider;
	//handleImg.onmouseup = finishMoveSlider;
	
	parentObj.style.width = obj.offsetWidth + 'px';
	
	if(document.body.onmouseup){
		if(document.body.onmouseup.toString().indexOf('stopMoveSlider')==-1){
			//alert('You allready have an onmouseup event assigned to the body tag');
		}
	}else{
		document.body.onmouseup = stopMoveSlider;	
		document.body.onmousemove = startMoveSlider;	
	}
	handleImg.ondragstart = form_widget_cancel_event;
	parentObj.appendChild(handleImg);
	
	positionSliderImage(false,slider_counter);
}
