var Timer
var baloonTooltipShowAfterSecs = 1;
var baloonTooltipHideAfterSecs = 11;
var baloonEstimatedTime = 0;
var baloonTooltipVisible = false;
var baloonTooltipShow = true;

var mouseDownPosX = 0;
var mouseDownButtonMarginLeft = 0;
var buttonMove = false;
var buttonMouseDown = false;

var maxWidth = 166;
var breakPoint1 = parseInt(maxWidth / 2);
var breakPoint2 = parseInt((maxWidth / 2)  + (maxWidth / 4)) + 1;

var minValue = 100;
var maxValue1 = 500;
var maxValue2 = 1000;
var maxValue3 = 9999;


//	Initialize
function Slider_Initialize()
{
	//	Set default margin left
	document.getElementById("div_Slider").style.marginLeft = "0px";
}


//	Slider button mouse over
function Slider_ButtonMouseOver()
{
	//	Continue if button mouse down false
	if (!buttonMouseDown)
	{
		//	Change cursor style
		document.body.style.cursor = "pointer";

		//	Change image background to mouse over
		document.getElementById("div_Slider").style.backgroundImage = "url(graphics/content/slider/button_mouseover.png)";
		
		//	Continue if baloon can be shown
		if (baloonTooltipShow)
		{
			//	Reset time
			baloonEstimatedTime = 0;
			
			//	Initialize and start timer
			Timer = window.setInterval("BaloonInterval()", 1000);
		}
	}
}


//	Slider button mouse out
function Slider_ButtonMouseOut()
{
	//	Continue if baloon is hidden, but can be shown
	if (!baloonTooltipVisible && baloonTooltipShow)
	{
		//	Hide tooltip
		HideTooltip();
		
		//	Show tooltip at mouse over
		baloonTooltipShow = true;
	}
	
	//	Continue if button mouse down false
	if (!buttonMouseDown)
	{
		//	Change cursor style
		document.body.style.cursor = "default";
		
		//	Change image background to default
		document.getElementById("div_Slider").style.backgroundImage = "url(graphics/content/slider/button_disabled.png)";
	}
}


//	Slider button mouse down
function Slider_ButtonMouseDown()
{
	//	Hide tooltip and show never again
	HideTooltip();
	
	//	Button mouse down true
	buttonMouseDown = true;
	
	//	Mouse position at mouse down
	mouseDownPosX = mouseX;
	
	//	Margin left at mouse down
	mouseDownButtonMarginLeft = parseInt(document.getElementById("div_Slider").style.marginLeft.replace(/px/, ''));

	//	Change background image
	document.getElementById("div_Slider").style.backgroundImage = "url(graphics/content/slider/button_enabled.png)";
	
	//	Change cursor style
	document.body.style.cursor = "e-resize";
		
	//	Set button clicked
	buttonMouseDown = true;
	
	//	Cancel out any text selections
	document.body.focus();
	
	// prevent text selection in IE
	document.onselectstart = function () { return false; };
	
	// prevent IE from trying to drag an image
	//target.ondragstart = function() { return false; };
	
	// prevent text selection (except IE)
	return false;
}


//	Mouse up called by site_mousecapture.js
function MouseUp()
{
	//	Continue if baloon is visible
	if (baloonTooltipVisible)
	{
		//	Hide tooltip and show never again
		HideTooltip();
	}
	
	//	Continue if mouse down is true
	if (buttonMouseDown)
	{
		//	Create object
		var buttonImage = document.getElementById("div_Slider");

		//	Change image
		buttonImage.style.backgroundImage = "url(graphics/content/slider/button_disabled.png)";
		
		//	Change cursor style
		document.body.style.cursor = "default";
		
		//	Button mouse down false
		buttonMouseDown = false;
		
		
		//	########### REFRESH DATA ###########
		
		//	Get value of drop down menu
		var valueChanged = document.site_form.PanelReservation_Kilometers.value;
		
		//	Create location href strting
		var loactionHref = 'modules/reservation.php?aid=19&res_mileage=' + valueChanged;
		
		//	Continue if panel car picker is visible
		if (PanelCarpicker.style.display == 'block' || PanelCarpickerLoading.style.display == 'block')
		{
			//	Hide panel
			HideDiv(PanelCarpicker);
			
			//	Show panel
			ShowDiv(PanelCarpickerLoading);
			
			//	Add det value to location href string
			loactionHref += '&ApplyCarpickerRefresh=true';
		}
	
		//	Parse reservation
		parent.iframe_reservation.location.href = loactionHref;
		
		//	########### REFRESH DATA END ###########
	}
}


//	Mouse move called by site_mousecapture.js
function MouseMove()
{
	//	Continue if mouse down is true
	if (buttonMouseDown)
	{
		//	Difference to mouse down x position
		var mouseXDif = mouseX - mouseDownPosX;
		
		//	New left margin
		var buttonMarginLeft = mouseDownButtonMarginLeft + mouseXDif;
		
		//	Continue if margin left is less than zero and limit it
		if (buttonMarginLeft < 0)
			buttonMarginLeft = 0;
			
		//	Continue if margin left is higher than maximum width and limit it
		else if (buttonMarginLeft > maxWidth)
			buttonMarginLeft = maxWidth;

		//	Set margin left
		document.getElementById("div_Slider").style.marginLeft = buttonMarginLeft;
		
		
		//	########### REFRESH DATA ###########
		
		//	Kilometers
		var kilometers = "100";
		
		//	Under break point 1
		if (buttonMarginLeft <= breakPoint1)
			kilometers = (((maxValue1 - minValue) * buttonMarginLeft) / breakPoint1) + minValue;
		//	Exact break point 1
		else if (buttonMarginLeft == breakPoint1)
			kilometers = maxValue1;
		//	Under break point 2
		else if (buttonMarginLeft < breakPoint2)
			kilometers = (((maxValue2 - maxValue1) * (buttonMarginLeft - breakPoint1)) / (breakPoint2 - breakPoint1)) + maxValue1;
		//	Exact break point 2
		else if (buttonMarginLeft == breakPoint2)
			kilometers = maxValue2;
		//	Under break point 3
		else if (buttonMarginLeft < maxWidth)
			kilometers = (((maxValue3 - maxValue2) * (buttonMarginLeft - breakPoint2)) / (maxWidth - breakPoint2)) + maxValue2;
		//	Exact break point 2
		else if (buttonMarginLeft == maxWidth)
			kilometers = maxValue3;
		
		//	Refresh kilometers
		document.getElementById("PanelReservation_Kilometers").value = parseInt(kilometers);
		
		//	########### REFRESH DATA END ###########
		
		
		//	Continue if browser is firefox
		if (firefox)
		{
			//	Cancel out any text selections
			document.body.focus();
			
			// prevent text selection (except IE)
			return false;
		}
	}
}


//	Baloon interval
function BaloonInterval()
{
	//	Continue if baloon is hidden but can be shown
	if (!baloonTooltipVisible && baloonTooltipShow)
	{
		//	Increment time counter
		baloonEstimatedTime++;
		
		//	Continue if estimated is reached
		if (baloonEstimatedTime == baloonTooltipShowAfterSecs)
		{
			//	Create object
			var baloonTooltip = document.getElementById("div_SliderBaloonTooltip");
			
			//	Set baloon x position
			baloonTooltip.style.left = (mouseX - 10) + "px";
			
			//	Set baloon y position
			baloonTooltip.style.top = (mouseY - 95) + "px";
			
			//	Show baloon
			baloonTooltip.style.display = "block";
			
			//	Baloon visible
			baloonTooltipVisible = true;
		}
	}
	//	Continue if baloon is visible an can be shown
	else if (baloonTooltipVisible && baloonTooltipShow)
	{
		//	Increment time counter
		baloonEstimatedTime++;
		
		//	Continue if estimated is reached
		if (baloonEstimatedTime == baloonTooltipHideAfterSecs)
			//	Hide tooltip
			HideTooltip();
	}
}


//	Hide tooltip
function HideTooltip()
{
	clearInterval(Timer);
	document.getElementById("div_SliderBaloonTooltip").style.display = "none";
	baloonTooltipVisible = false;
	baloonTooltipShow = false;
	baloonEstimatedTime = 0;
}


//	Hide tooltip
function SetSliderByValue(ParamValue)
{
	marginLeft = 0;

	//	Under break point 1
	if (ParamValue <= maxValue1)
		marginLeft = ((ParamValue - minValue) * breakPoint1) / (maxValue1 - minValue);
	//	Under break point 2
	else if (ParamValue <= maxValue2)
		marginLeft = (((ParamValue - maxValue1) * (breakPoint2 - breakPoint1)) / (maxValue2 - maxValue1)) + breakPoint1;
	//	Under break point 3
	else if (ParamValue <= maxValue3)
		marginLeft = (((ParamValue - maxValue2) * (maxWidth - breakPoint2)) / (maxValue3 - maxValue2)) + breakPoint2;
		
	document.getElementById("div_Slider").style.marginLeft = marginLeft + "px";
}