﻿
window.onresize = function() {
    slider.minX = parseInt((getWindowWidth() / 2) + 303);
}

function slider(theArray)
{
	this.minX = 0;
    this.minX = parseInt((getWindowWidth() / 2) + 303);
	this.sliderWidth = 100;
	this.offsetX = 13;

    this.steps = theArray;

    this.minIndex = 0;
	this.maxIndex = this.steps.length-1;
	this.stepPercent = 100 / (this.steps.length-1);
	this.stepWidth = (this.sliderWidth - this.minX) / this.steps.length;
			
	this.elm = document.getElementById("slider");
	this.elmMinInput = this.elm.getElementsByTagName("input")[0];
	this.elmMaxInput = this.elm.getElementsByTagName("input")[1];
	this.elmMinHandle = this.elm.getElementsByTagName("div")[3];
	this.elmMaxHandle = this.elm.getElementsByTagName("div")[4];
	this.coverMin = this.elm.getElementsByTagName("div")[1];
	this.coverMax = this.elm.getElementsByTagName("div")[2];

	this.startPos = 0;
	this.startMousePos = 0;
				
	function init()
	{
        this.elmMinInput.value = this.steps[0];
		this.elmMaxInput.value = this.steps[this.steps.length-2];
	}
				
	function startDrag(minmax)
	{
        drag_active=true;
        if (!dragTimer)
	    {
	        this.startMousePos = mousePosX;
		    globalObject = this;
		    if (minmax == "min")
		    {
		        dragTimer = setInterval("globalObject.draggingMin()",50);
			    this.startPos = this.elmMinInput.value * (this.sliderWidth / this.max);						
		    }
		    else
		    {
		        dragTimer = setInterval("globalObject.draggingMax()",50);			
                this.startPos = this.startMousePos;
		    }
		}
	}
			
    function draggingMin()
	{
        percent = ((mousePosX - this.minX) / this.sliderWidth) * 100;
		newIndex = Math.round( percent / this.stepPercent );

        /*
        if (newIndex <= 0)
		{
		    newIndex = 0;
		}
		if (newIndex >= this.maxIndex)
		{
		    newIndex = (this.maxIndex - 1);
		}
		*/
		if (newIndex <= 0)
		{
		    newIndex = 0;
		}
		if (newIndex >= this.maxIndex-1)
		{
		    newIndex = (this.maxIndex - 2);
		}
		this.minIndex = newIndex;
		//document.getElementById("minIndex").value = newIndex;
		
		newSliderPos = Math.round( ( newIndex / (this.steps.length - 1) ) * (this.sliderWidth) ) - 3;
				
        this.elmMinInput.value = this.steps[newIndex];
					
		//this.elmMinHandle.style.left = newSliderPos + this.offsetX - 4  + "px";
		this.elmMinHandle.style.left = newSliderPos + this.offsetX - 4 + "px";
	}

    function draggingMax()
    {
        percent = ((mousePosX - this.offsetX - this.minX) / this.sliderWidth) * 100;
		newIndex = Math.round( percent / this.stepPercent );

        /*
		if (newIndex <= this.minIndex)
		{
		    newIndex = this.minIndex + 1;
		}
		if (newIndex >= this.steps.length )
		{
		    newIndex = (this.steps.length - 1);
		}
		*/
		if (newIndex <= this.minIndex+1)
		{
		    newIndex = this.minIndex + 2;
        }
		if (newIndex >= this.steps.length-1)
		{
		    newIndex = (this.steps.length - 1);
		}
		this.maxIndex = newIndex;
		//document.getElementById("maxIndex").value = newIndex;

		newSliderPos = Math.round( ( newIndex / (this.steps.length - 1) ) * (this.sliderWidth) ) + 3;
				
        this.elmMaxInput.value = this.steps[newIndex-1];
					
		//this.elmMaxHandle.style.left = newSliderPos + this.offsetX  + 4 + "px";
		this.elmMaxHandle.style.left = newSliderPos + this.offsetX + 4 + "px";
	}
				
    function updateMin()
	{
        var newIndex = 0;
		if(this.elmMinInput.value)
		{
		    for (var i=0; i<this.steps.length-1; i++)
			{
			    if(parseInt(this.elmMinInput.value.replace(/ /g,'').replace(/>/,"")) > parseInt(("" + this.steps[i]).replace(/ /g,'').replace(/>/,"")) )
				{
				    if((""+this.elmMinInput.value).indexOf(">")!=-1)
					    newIndex = this.steps.length-2;
					else
					    newIndex = i + 1;
                }
			}
		}
				
		newSliderPos = Math.round( ( newIndex / (this.steps.length - 1) ) * (this.sliderWidth) ) - 3;
		this.elmMinHandle.style.left = newSliderPos + this.offsetX - 4  + "px";
		this.coverMin.style.width = Math.abs(newSliderPos + this.offsetX + 2) + "px";
	}
				
	function updateMax()
	{
		var newIndex = this.steps.length-1;
		if(this.elmMaxInput.value)
		{
		    for (var i=this.steps.length-1; i>=0; i--)
			{
			    if(parseInt(("" + this.steps[i]).replace(/ /g,'')) >= parseInt(this.elmMaxInput.value.replace(/ /g,'')))
				{
				    newIndex = i;
				}
			}
		}
		if(newIndex < (this.steps.length-1))
		newIndex++
		var newSliderPos = Math.round( ( newIndex / (this.steps.length - 1) ) * (this.sliderWidth) ) + 3;
		this.elmMaxHandle.style.left = newSliderPos + this.offsetX  + 4 + "px";
		this.coverMax.style.width = Math.abs(this.sliderWidth - newSliderPos + this.offsetX + 5) + "px";		
    }
				
	function resetSliders()
	{
	    this.elm.getElementsByTagName("input")[0].value = this.steps[0];
		this.elm.getElementsByTagName("input")[1].value = this.steps[this.steps.length-2];
    }
    
			
	this.init = init;
	this.stopDrag = stopDrag;
	this.startDrag = startDrag;
	this.draggingMin = draggingMin;
	this.draggingMax = draggingMax;
	this.updateMin = updateMin;
	this.updateMax = updateMax;
	this.resetSliders = resetSliders;
	
	this.init(); // do not reset the min/max values if the page has been resized
}
						
var mousePosX = 0;
function getMouseXY(e)
{
    if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
        mousePosX = e.pageX;
    }
	else if (e.clientX || e.clientY)
	{
	    mousePosX = e.clientX + document.body.scrollLeft;
    }	
}
			
function stopDrag()
{
    if (dragTimer)
	{
	    clearTimeout(dragTimer);
		dragTimer = false;
	}
	
	if(drag_active)
	    __doPostBack(InputSliderMin, 'a');
	    
	drag_active=false;
}
	
var dragTimer = false;

var globalObject;
var slider;
var drag_active=false;

/*function initSliders()*/
function initSlider()
{
    slider = new slider(new Array("0 kr",">500 kr","1"));
	document.onmousemove = getMouseXY;
	document.onmouseup = stopDrag;			
}


			
function getWindowWidth()
{
    var x;
    if (self.innerHeight) // all except Explorer
    {
	    x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
	    // Explorer 6 Strict Mode
    {
	    x = document.documentElement.clientWidth + 35;
    }
    else if (document.body) // other Explorers
    {
	    x = document.body.clientWidth;
    }
    return x;
}



/* Vid postback */

function updateValues(min, max) { 
        var newIndex = 0;
		if(min)
		{
		    for (var i=0; i<slider.steps.length-1; i++)
			{
			    if(parseInt(min.replace(/ /g,'').replace(/>/,"")) > parseInt(("" + slider.steps[i]).replace(/ /g,'').replace(/>/,"")) )
				{
				    if((""+min.value).indexOf(">")!=-1)
					    newIndex = slider.steps.length-2;
					else
					    newIndex = i + 1;
                }
			}
		}
				
		newSliderPos = Math.round( ( newIndex / (slider.steps.length - 1) ) * (slider.sliderWidth) ) - 3;
		slider.elmMinHandle.style.left = newSliderPos + slider.offsetX - 4  + "px";
		//slider.coverMin.style.width = Math.abs(newSliderPos + slider.offsetX + 2) + "px";
		slider.minIndex = newIndex;
		
		slider.elmMinInput.value = slider.steps[newIndex];
		
		newIndex = slider.steps.length-1;
		if(max)
		{
		    for (var i=slider.steps.length-1; i>=0; i--)
			{
			    if(parseInt(("" + slider.steps[i]).replace(/ /g,'')) >= parseInt(max.replace(/ /g,'')))
				{
				    newIndex = i;
				}
			}
		}
		if(newIndex < (slider.steps.length-1))
		    newIndex++;
		var newSliderPos = Math.round( ( newIndex / (slider.steps.length - 1) ) * (slider.sliderWidth) ) + 3;
		slider.elmMaxHandle.style.left = newSliderPos + slider.offsetX  + 4 + "px";
		//slider.coverMax.style.width = Math.abs(slider.sliderWidth - newSliderPos + slider.offsetX + 5) + "px";
		slider.elmMaxInput.value = slider.steps[newIndex-1];
		slider.maxIndex = newIndex;
}