/*
Created By: Jitendra Upraity
Created On: 19 June, 2008
Description: Class in javascript for creating slider
*/

function Slider()
{
	//class local variabls
	var currObj;              // stores the current object being manipulated (leftSlider or rightSlider object)
	var currObjId;
	var leftPos = 0;          // the current left position after mouse is released
	var rightPos = 149;       // the current right position after mouse is released
	var sliderWidth = 12;      // the width of the sliders (includes borders)
	var sliderBarWidth = 149; // the width of the slider bar (does not include borders)
	var mouseStart = -99999   // an arbitrary number used to show that it is the first time we are hitting the moveSlider1 function.
	var pos;                  // position with offset used during the move 
	var min=0;            // minimum value of the data 
	var max=0;            // maximum value of the data
	var sliderLeftId;		 // id for left slider
	var sliderRightId;		// id for right slider
	var leftX;		 // id for left X
	var rightX;		// id for right X
	var showMaxMSGId;			// id for showing slider move min value
	var showMinMSGId;			// id for showing slider move max value
	var callFun;			// calling javascript function on mouse release event
	this.setMinVal = setMinVal;
	this.setMaxVal = setMaxVal;
	this.setLeftSlider = setLeftSlider;
	this.setRightSlider = setRightSlider;
	this.setLeftX = setLeftX;
	this.setRightX = setRightX;
	this.setMinMSGid = setMinMSGid;
	this.setMaxMSGid = setMaxMSGid;
	this.setCallFun = setCallFun;
	this.moveSlider = moveSlider;
	this.moveSlider1 = moveSlider1;
	this.moveDone = moveDone;
	
	//member function for setting minimum value of the data
	function setMinVal(minVal)
	{
		if (minVal==null||minVal==""||isNaN(minVal))
		{
		  ;//alert("Invalid minimum value assigned");return false;
		}
		else
		{
			min = minVal;
		}
	
	}

	//member function for setting maximum value of the data
	function setMaxVal(maxVal)
	{
		/*//Ravi Pandey oct-6-2008 if
		 isPlus = maxVal.substr(-1);
		if(isPlus=="+")
		{
			 max = 0;
		}
		else */if (maxVal==null||maxVal==""||isNaN(maxVal))
		{
		 //alert("Invalid maximum value assigned");return false;
		 
		}
		else
		{
			max = maxVal;
		}
	}

	//member function for setting slider left id
	function setLeftSlider(leftId)
	{
		if (leftId==null||leftId=="")
		{
		  alert("Invalid id assigned for left slider");return false;
		}
		sliderLeftId = leftId;
	}

	//member function for setting slider right id
	function setRightSlider(rightId)
	{
		if (rightId==null||rightId=="")
		{
		  alert("Invalid id assigned for right slider");return false;
		}
		sliderRightId = rightId;
	}
	
	//member function for setting slider left id
	function setLeftX(leftId)
	{
		if (leftId==null||leftId=="")
		{
		  alert("Invalid id assigned for left X");return false;
		}
		leftX = leftId;

	}

	//member function for setting slider right id
	function setRightX(rightId)
	{
		if (rightId==null||rightId=="")
		{
		  alert("Invalid id assigned for right X");return false;
		}
		rightX = rightId;
	}

	//member function for setting message min box id
	function setMinMSGid(msgId)
	{
		if (msgId==null||msgId=="")
		{
		  alert("Invalid id assigned for message that appear on mouse min move");return false;
		}
		showMinMSGId = msgId;
	}
	
	//member function for setting message box max id
	function setMaxMSGid(msgId)
	{
		if (msgId==null||msgId=="")
		{
		  alert("Invalid id assigned for message that appear on mouse max move");return false;
		}
		showMaxMSGId = msgId;
	}
	//member function for calling javascript function at last
	function setCallFun(funName)
	{
		if (funName==null||funName=="")
		{
		  alert("No functino is defined for mouse release event");return false;
		}
		callFun = funName;
	}
	/* funciton moveSlider(obj)
           parameter obj - object reference to the calling object
         This function sets the resets the starting mouse position and sets the document 
         methods used to watch for mouse events.
      */
	function moveSlider(obj) {
		mouseStart = -99999;   // set the beginning mouse position to an unlikely number.
		currObj = obj;         // set the currObj variable to the object that was clicked
		currObjId = obj.id;
		
		document.onmousemove = this.moveSlider1; // set the onmousemove method to the function that moves the slider
		document.onmouseup = this.moveDone;   // set the onmouseup method to the function that stops listening for the mousemove and reclaculates the data
	}


	/* function moveslider1
         This function is called by the mousemove event
         checks the current mouse position and compares it 
         to the mouse position when the move was started.
         uses the difference as an offset from the slider's
         staring position to move the slider with the mouse
      */
    function moveSlider1(e){
        if (!e) var e = window.event;  // if IE, then we need to assign e
        if (e.pageX) posX = e.pageX;   // FF, opera, netscape
        else if (e.clientX) {          // IE
          posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;  
        }
		
        if (mouseStart == -99999) mouseStart = posX;  // -99999 is an arbitrary number.  It is unlikely to be hit 
        var offset = posX - mouseStart; // determine the difference from where the mouse started to where it is now
        
		if (currObjId==sliderLeftId) {  // If we are moving the left slider
          pos = leftPos + offset;        // the new position should be the current left position + the offset
          if (pos <= 0) pos = 0;          // if this makes the left position less than 0 then use 0
          if (pos > rightPos - sliderWidth) pos = rightPos - sliderWidth; // if the position would put us past the right slider then move up to the right slider
          document.getElementById(leftX).value = pos/sliderBarWidth * (max-min) + min; // put the new left position in a text box to be viewed. (this is not required)
		  document.getElementById(showMinMSGId).innerHTML=Math.floor(document.getElementById(leftX).value);
        }
        else {
          pos = rightPos + offset   // the new position should be the current right position + the offset
          if (pos < leftPos + sliderWidth) pos = leftPos + sliderWidth;  // if the new position would take us past the left slider then move next to the left slider
          if (pos > sliderBarWidth ) pos = sliderBarWidth ;  // if the new position is past the end of the bar, position at the end of the bar.
          document.getElementById(rightX).value = pos/sliderBarWidth * (max-min) + min;  // put the new right position in a text box to be viewed. (this is not required)
		   document.getElementById(showMaxMSGId).innerHTML=Math.ceil(document.getElementById(rightX).value);
        }
	      
        currObj.style.left = pos + "px";  // move the slider to the new position
		
     }
     
      /* function moveDone()
         sets the current left/right position memory variable.
         clears the mouse monitoring methods.
         Calls the function to recaculate the data
      */
     function moveDone(e) {
        if (currObjId == sliderLeftId) leftPos = pos;    // set the current left position to the value of pos
        else rightPos = pos;                              // set the current right position to the value of pos
        document.onmousemove = function() {};             // stop listening for a mousemove event
        document.onmouseup = function() {};               // stop listening for a mouseup event
		eval(callFun);
	 }
}
