// Title: tigra slider control
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_slider_control/
// Version: 1.0.2 (commented source)
// Date: 08/21/2008
// Tech. Support: http://www.softcomplex.com/forum/
// Notes: This script is free. Visit official site for further details.

function slider (a_init, a_tpl , idElemToInsertSlider) {
	
	 
	this.f_setValue  = f_sliderSetValue;
	this.f_getPos    = f_sliderGetPos;
	
	// 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_value == null)
		this.n_value = this.n_minValue;
	
	var arrowLeftPos = true ?  (this.n_pathLeft - 16 ) : ( this.n_pathLeft - this.n_controlWidth - 10 );
	var arrowRightPos = true ?  (this.n_pathLeft + this.n_controlWidth + 6 ) : ( this.n_pathLeft  + 16  );
	var sliderArrowPos = (this.n_pathLeft ) ;
	
	
	// generate the control's HTML
	document.getElementById(idElemToInsertSlider).innerHTML = 
		'<td ><img src="img/slider/leftArrow.gif" onmouseup="return onLeftArrowUp()" onmousedown="return onLeftArrowDown(' + this.n_id + ')" style="position:relative;left:' +  arrowLeftPos  + 'px;top:' + (this.n_pathTop + 15) + 'px;z-index:' + this.n_zIndex + ';cursor:pointer;"  />' +
		'<div style="width:' + this.n_controlWidth + 'px;height:' + this.n_controlHeight + 'px;border:0; background-image:url(' + this.s_imgControl + ')" id="sl' + this.n_id + 'base">' +
		'<img src="' + this.s_imgSlider + '" width="' + this.n_sliderWidth + '" height="' + this.n_sliderHeight + '" border="0" style="position:relative;left:' +  sliderArrowPos  + 'px;top:' + this.n_pathTop + 'px;z-index:' + this.n_zIndex + ';cursor:pointer;visibility:hidden;" name="sl' + this.n_id + 'slider" id="sl' + this.n_id + 'slider" onmousedown="return f_sliderMouseDown(' + this.n_id + ')"/>' + 
		'</div>'  +
		'<img src="img/slider/rightArrow.gif" onmouseup="return onRightArrowUp()"  onmousedown="return onRightArrowDown(' + this.n_id + ')" style="position:relative;left:' + (  arrowRightPos )+ 'px;top:' + (this.n_pathTop - 16) + 'px;z-index:' + this.n_zIndex + ';cursor:pointer;" /></td>'
		;
	
	this.e_base   = get_element('sl' + this.n_id + 'base');
	this.e_slider = get_element('sl' + this.n_id + 'slider');
	
	// safely hook document/window events
	if (!window.f_savedMouseMove && document.onmousemove != f_sliderMouseMove) {
		window.f_savedMouseMove = document.onmousemove;
		document.onmousemove = f_sliderMouseMove;
	}
	if (!window.f_savedMouseUp && 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_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_slider.style.visibility = 'visible';
}

var leftDown = false;
var rightDown = false;

function onLeftArrow(sliderId){

	if (leftDown) {
		var o_slider = window.A_SLIDERS[sliderId]; 
		e_input = get_element(o_slider.s_name);
		var value;
		if(isLTR()) {
			value = e_input.value * 1 - o_slider.n_step * 1;
		} else {
			value = e_input.value * 1 + o_slider.n_step * 1;
		}
		o_slider.f_setValue( parseInt( value ) , 1);
		setTimeout("onLeftArrow(" + sliderId + ")", 100);
	}
	
	
}


function onRightArrow(sliderId){
	if (rightDown) {
		var o_slider = window.A_SLIDERS[sliderId]; 
		e_input = get_element(o_slider.s_name);
		var value;
		if(isLTR()) {
			value = e_input.value * 1 + o_slider.n_step * 1;
		} else {
			value = e_input.value * 1 - o_slider.n_step * 1;
		}
		o_slider.f_setValue( parseInt(value) , 1);
		setTimeout("onRightArrow(" + sliderId + ")", 100);
	}
}


function onLeftArrowDown(sliderId) {
	leftDown = true;
	setTimeout("onLeftArrow(" + sliderId + ")", 10);
}
function onLeftArrowUp() {
	leftDown = false;
}

function onRightArrowDown(sliderId) {
	rightDown = true;
	setTimeout("onRightArrow(" + sliderId + ")", 10);
}
function onRightArrowUp() {
	rightDown = false;
}


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
	if (this.b_vertical) 
		this.e_slider.style.top  = (this.n_pathTop + this.n_pathLength - Math.round((n_value - this.n_minValue) * this.n_pix2value)) + 'px';
	else if( true ){
			this.e_slider.style.left = (this.n_pathLeft + Math.round((n_value - this.n_minValue) * this.n_pix2value)) + 'px';
		 } else {
		    //alert(this.n_pathLeft + Math.round((n_value - this.n_minValue) * this.n_pix2value))
		 	this.e_slider.style.left = ( this.n_pathLeft + Math.round((n_value - this.n_minValue) * this.n_pix2value)) + '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 + "'.");
	}
	//alert(n_value)
	e_input.value = n_value;
	if(isLTR()) {
		document.getElementById("sliderTextValue").innerHTML =  n_value + " " + pointsString +  " = " +  (  n_value / currPointValueInDolar   ).toFixed(2) + " " + usdString ; 
	} else {
		document.getElementById("sliderTextValue").innerHTML =  -n_value + " " + pointsString +  " = " +  ( (-n_value) /currPointValueInDolar    ).toFixed(2) + " " + usdString ; 
	}	

}

// get absolute position of the element in the document
function f_sliderGetPos (b_vertical, b_base) {
	var n_pos = 0,
		s_coord = (b_vertical ? 'Top' : 'Left');
	var o_elem = o_elem2 = b_base ? this.e_base : this.e_slider;
	
	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) {
	window.n_activeSliderId = n_id;
	return false;
}

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.n_pathLength - parseInt(o_slider.e_slider.style.top) + o_slider.n_pathTop)
			: (parseInt(o_slider.e_slider.style.left) - o_slider.n_pathLeft)) / o_slider.n_pix2value);
		if (b_watching)	return;
		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) {
		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;
		if (o_slider.b_vertical) {
			var n_sliderTop = window.n_mouseY - o_slider.n_sliderHeight / 2 - o_slider.f_getPos(1, 1) - 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;
			o_slider.e_slider.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) - 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;
			o_slider.e_slider.style.left = n_sliderLeft + 'px';
			n_pxOffset = n_sliderLeft - o_slider.n_pathLeft;
		}
		if (o_slider.b_watch)
			 f_sliderMouseUp(e_event, 1);

		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) };



function getSliderProps(){
     if (document.getElementById("sliderComponent") != null) {
	   	hideSlider();
		if (isGetSlider() == false){
			return false;	 	
		}
	    var params = addToQuery ("origin" , "origin" , true);
	    params = params +  addToQuery ("destination" , "destination" , true);
	    params = params +  addToQuery ("cabin" , "cabin" , true);
	    params = params +  addToQuery ("adults" , "adults" , true);
	    params = params +  addToQuery ("children" , "children" , true);
	    params = params +  addToQuery ("infants" , "infants" , true);
	    params = params +  addToQuery ("goldenAges" , "goldenAges" , true);
	    params = params +  addToQuery ("students" , "students" , true);
	    params = params +  addToQuery ("youths" , "youths" , true);	        
	    params = params +  "&oneWayRadio=" + document.getElementById("oneWayRadio").checked;
	    params = params +  "&openJawRadio=" + document.getElementById("openJawRadio").checked;
	    params = params +  addToQuery ("retDepartures" , "retDepartures" , true); 
	   	params = params +  addToQuery ("retDestinations" , "retDestinations" , true); 
		makeSyncronizedCall("getLegProperties.do?" + params , parseLegPropsResults );
	}
}

var NOT_SELECTED_LIST_VALUE = '000';

// Check if all paramters exist to get slider data
function isGetSlider(){
	if(document.getElementById("origin").value == NOT_SELECTED_LIST_VALUE){
		return false;
	}
	if(document.getElementById("destination").value == NOT_SELECTED_LIST_VALUE){
		return false;
	}
	// For RoundTrip and openJaw
	if(document.getElementById("openJawRadio") != null && document.getElementById("openJawRadio").checked ) {
		if(document.getElementById("retDepartures").value == NOT_SELECTED_LIST_VALUE){
			return false;
		}
		if(document.getElementById("retDestinations").value == NOT_SELECTED_LIST_VALUE){
			return false;
		}
	}
	if(document.getElementById("cabin") != null && document.getElementById("cabin").value == NOT_SELECTED_LIST_VALUE){
		return false;
	} 
	if(document.getElementById("bonusPointsCheckbox").checked == false){
		return false;
	}
	
	
	return true;
}

var currPointValueInDolar = 0;
/**
*	Parse respons XML
*/
function parseLegPropsResults(){ 
	if (req.readyState == 4) { // Complete
	      if (req.status == 200) { // OK response
	        var xmlDoc=req.responseXML.documentElement;
	        // Check if we got ok response
	      	if(xmlDoc.getElementsByTagName("error")[0] != null){
	      		showError(xmlDoc.getElementsByTagName("error")[0].childNodes[0].nodeValue);
	      		return;
	      	} else {
	      		var errMessage  = document.getElementById("errorMessageScript");
	      		if (errMessage != null) {
	      			errMessage.style.display = 'none';
	      		}
	      	}
			 var minVal = parseInt(xmlDoc.getElementsByTagName("min")[0].childNodes[0].nodeValue);
			var maxVal = parseInt(xmlDoc.getElementsByTagName("max")[0].childNodes[0].nodeValue);
			currPointValueInDolar =  xmlDoc.getElementsByTagName("pointValueInDolar")[0].childNodes[0].nodeValue
			document.getElementById("minValue").innerHTML = minVal + ' ' + pointsString;
			document.getElementById("maxValue").innerHTML = maxVal + ' ' + pointsString;
			document.getElementById("minPointValueInDolarValue").innerHTML = (minVal / currPointValueInDolar).toFixed(2) + ' ' + usdString;
			document.getElementById("maxPointValueInDolarValue").innerHTML = (maxVal / currPointValueInDolar).toFixed(2)	 + ' ' + usdString;
            var step = getTotalPassengers();
         	var A_TPL = {
							'b_vertical' : false,
							'b_watch': true,
							'n_controlWidth': 220,
							'n_controlHeight': 16,
							'n_sliderWidth': 16,
							'n_sliderHeight': 15,
							'n_pathLeft' : 1,
							'n_pathTop' : 1,
							'n_pathLength' : 202,
							's_imgControl': 'img/blueh_bg.gif',
							's_imgSlider': 'img/blueh_sl.gif',
							'n_zIndex': 1
						}
					
			var A_INIT1 = {
							's_form' : 0,
							's_name': 'sliderValue',
							'n_minValue' : isLTR() ? minVal : (0 - maxVal),
							'n_maxValue' : isLTR() ? maxVal : (0 - minVal),
							'n_value' : isLTR() ? minVal : (0 - minVal),
							'n_step' : isLTR() ? step: 0 - step//currPointValueInDolar : -currPointValueInDolar
						}
			new slider(A_INIT1, A_TPL , 'sliderId');
			document.getElementById("sliderComponent").style.display = '';	
		  }
		}
}

function getTotalPassengers(){
		
		var numOfAdults = document.getElementById("adults").value * 1;
		var numOfChilds = document.getElementById("children").value * 1;
		var numOfInfants = document.getElementById("infants").value * 1;
		var numOfGa = document.getElementById("goldenAges").value * 1;
		var numOfStudents = document.getElementById("students").value * 1;
		var numOfYouth = document.getElementById("youths").value * 1;
		
		return numOfAdults + numOfChilds + numOfInfants + numOfGa +  numOfStudents + numOfYouth; 
		
  		   
}

function hideSlider() {
	var slider= document.getElementById("sliderComponent");
	if (slider != null) { 
		slider.style.display = 'none';
	}
}

function showHideBonusPointsSlider() {
	if(document.getElementById("bonusPointsCheckbox").checked == false){
		hideSlider();
		return;
	}
	getSliderProps();

}

function showError(message){
	document.getElementById("errorMessageScript").style.display = '';
	if(document.getElementById("errorMessage") != null) {
		document.getElementById("errorMessage").style.display = 'none';
	}
	document.getElementById("errorMessageText").innerHTML = message;
}
/**
/ Check direction of page
/ Return true for ltr direction
**/
function isLTR(){
	return dir == 'ltr';
}


