


	//Sider JS

	var GLOBALOBJECT; //This represents the currently active slider;

	function Slider (tg_id, tg_class, tg_label_id, tg_parent, tg_sibling, tg_min, tg_max, tg_inc, tg_callback)
	{
		var self = this;

		if (!(this.parent = document.getElementById( tg_parent))) return false;

		this.init = false;
	this.newChangeValue = null;

		this.sibling = ( tg_sibling) ? document.getElementById( tg_sibling) : false;
      
		// Slider handle (knob)
		this.handle	= document.createElement("div");
		this.handle.className	= "tg_sliderHandle";

		// Slider Body
		this.body	= document.createElement("div");
		this.body.self = this;
		this.body.id		= (tg_id) ? tg_id : "";
		this.body.className	= (tg_class) ? "tg_sliderControl " + tg_class : "tg_sliderControl";
		this.body.appendChild( this.handle);

		// Slider Label - displays values
		this.label	= document.createElement("label");
		this.label.id = (tg_label_id) ? tg_label_id : ""; 
		this.label.className = "sliderControl_label"; 
		this.label.appendChild( document.createTextNode("SliderControl"));


		//slider caption - displays titles
		this.caption = document.createElement("label");
		this.caption.className="sliderControl_caption";
		this.caption.appendChild( document.createTextNode("default caption"));


		//slider focus - hidden control user for getting focus
		this.focusControl= document.createElement("input");
		this.focusControl.className	= "tg_sliderFocus"; 


		this.parent.appendChild( this.focusControl);
		this.parent.appendChild( this.caption);
		this.parent.appendChild( this.label);
		
		this.parent.appendChild( this.body);

		//events

		if ( typeof( tg_callback) == "function")
		{
			this.userFunction = tg_callback;
		}
		//
		// Properties
		//
		this.min	= (tg_min) ? tg_min : 0;
		this.max	= (tg_max) ? tg_max : 100;
		this.incrementByValue	= (tg_inc) ? tg_inc : 1;

		this.incMax	= (this.max - this.min) / this.incrementByValue;


		this.x		= 0;
		this.delta	= 0;
		this.deltaX	= 0;
	
		this.minX	= 0;
		this.maxX	= 0;
		this.offsetLeft = 11;
		this.offsetRight = 10;

		this.clickRate	= 4;
		this.keyRate	= 1;
	

	

        this.setCaption = function( captionText)
        {
            if (this.caption.innerHTML) this.caption.innerHTML = "";
            this.caption.appendChild( document.createTextNode( captionText));
        }
	    this.setValue = function(v)
	    {

		    this.value = v;
            this.manualx = true;
		    setSliderOutput(0, this);
	        this.manualx=false;

	
		    GLOBALOBJECT = this;


		    GLOBALOBJECT.cml = 
        	{
        		body: $TGFUNC_getPos( GLOBALOBJECT.body).left,
			    handle:(GLOBALOBJECT.x -  $TGFUNC_getPos( GLOBALOBJECT.body).left)
		    }
		    
		
            GLOBALOBJECT.handle.style.marginLeft=GLOBALOBJECT.cml.handle+"px";
          
            GLOBALOBJECT.handle.style.position = "";
	    }
	

		//methods

		this.hideSibling = function()
		{
			if (this.sibling)
			{
			
				this.sibling.style.display="none";
				
			}
			if (this.parent)
			{
				this.parent.style.display="";
			}
	
		}
		this.showSibling = function()
		{
			
			if (this.sibling)
			{
				this.sibling.style.display="";
			}
			if (this.parent)
			{
				this.parent.style.display="none";
			}
				
		}
		this.init = true;
	
	}
	
	function getKeyDelta( e)
	{
		return ($TGFUNC_e(e).keyCode == 39) ? true : ( $TGFUNC_e(e).keyCode == 37) ? false : null;
	}

var fudge, cludge;
	function setGlobals()
	{
		var nl, g = GLOBALOBJECT;

		g.ol = g.offsetLeft;
		g.or = g.offsetRight;
		g.hw = $TGFUNC_getPos( g.handle).width;


		if ( parseInt( g.handle.style.marginLeft) > 0)
		{
			if (g.cml.body !=  $( g.body).left())
			{
		        nl =  $( g.body).left() + g.cml.handle;
			}
			else
			{ 
			    nl = $( g.body).left() +parseInt(GLOBALOBJECT.handle.style.marginLeft);
			}
			
            $(GLOBALOBJECT.handle).css({position:"absolute"}).left(nl);

		    g.handle.style.marginLeft=0;
		}
		
		g.x  = $( g.handle).left();
		
		g.cx = $( g.body).left() + g.ol;
		g.cw =$( g.body).width();
		

	
		if (!g.cw) g.cw = fudge;
		if (!g.hw) g.hw = cludge;
		
	 	g.minX = g.cx;
		g.maxX = ( g.cx + g.cw) - (g.hw + g.or + g.ol);




		g.incrementByPixel =  ( g.maxX - g.cx) / g.incMax;
	}
	
	function sliderControlMouseDownEvent( e)
	{
		GLOBALOBJECT = (getKeyDelta( e) == null) ? ($TGFUNC_p( $TGFUNC_t( e) ,"tg_sliderControl")).self : GLOBALOBJECT;


		if (GLOBALOBJECT)
		{
			var g = GLOBALOBJECT;
		

			setGlobals();

			g.handle.style.cursor = "move";
			
			//if ( $TGFUNC_p( $TGFUNC_t( e) ,"tg_sliderHandle"))
			//{
	        	g.drag = true;
				g.deltaX = $TGFUNC_m( e).mouseX;
		  
		
				document.onmousemove = moveHandle;
			//}
			//else
			//{
			  //  g.drag = false;
				//g.delta = g.incrementByPixel * (( getKeyDelta( e) != null) ?  g.keyRate : g.clickRate);
	 			//g.deltaX = getKeyDelta( e) || ($TGFUNC_m( e).mouseX > g.x); //<---this mite be rong
	 			
				//moveHandle( e);
			//}
			return false;
		}
	  	return true;
	  
	}



	function sliderControlMouseUpEvent( e)
	{
	
		if (!GLOBALOBJECT) return false;

		if ( GLOBALOBJECT.drag)
		{
			GLOBALOBJECT.drag = false;
			document.onmousemove=null;
		}
		if ( GLOBALOBJECT.handle)
		{
		 	GLOBALOBJECT.handle.style.cursor = "pointer";
		 	
        GLOBALOBJECT.cml = 
        {
            body: $TGFUNC_getPos( GLOBALOBJECT.body).left,
            handle: ($TGFUNC_getPos(GLOBALOBJECT.handle).left -  $TGFUNC_getPos( GLOBALOBJECT.body).left)
        }
 
      
        GLOBALOBJECT.handle.style.marginLeft=(  $TGFUNC_getPos(GLOBALOBJECT.handle).left - ($TGFUNC_getPos( GLOBALOBJECT.body).left))+"px";
		GLOBALOBJECT.handle.style.position = "";
		}
		return false;
	}	





	function moveHandle( e)
	{
	
		if ( GLOBALOBJECT)
		{	
		
			var delta	= GLOBALOBJECT.delta;
			var deltaX	= GLOBALOBJECT.deltaX;

		//	setSliderOutput
		//	( 
		//		(GLOBALOBJECT.drag) ? (GLOBALOBJECT.x + $TGFUNC_m(e).mouseX -deltaX) : (deltaX) ? (GLOBALOBJECT.x + delta) : (GLOBALOBJECT.x - delta),null
		//	);

			setSliderOutput
			( 
				(GLOBALOBJECT.x + $TGFUNC_m(e).mouseX -deltaX) ,null
			);


		}
		return false;
	}

	function setSliderOutput( xpos, obj)
	{
		if (obj)
		{
			GLOBALOBJECT = obj;
			setGlobals();
			xpos = getSliderHandlePositionsByValue( obj);
	
		}
		else
		{
			GLOBALOBJECT.value = GLOBALOBJECT.focusControl.value = getSliderHandlePositionsByCoord( xpos);
		}

		
		setSliderHandlePosition( xpos);
		
		fireCustomUserEvent();
	}

	function getSliderHandlePositionsByValue( obj)
	{
		if (!obj) return 0;


		var distanceOfSlide	= getValueInRange( GLOBALOBJECT.value, GLOBALOBJECT.min, GLOBALOBJECT.max) - GLOBALOBJECT.min;
		
		var incrementsPerSlide	= distanceOfSlide / GLOBALOBJECT.incrementByValue;
		return GLOBALOBJECT.minX + parseInt( incrementsPerSlide * GLOBALOBJECT.incrementByPixel);
	}

	function getSliderHandlePositionsByCoord( xDelta)
	{
		var distanceOfSlide	= xDelta - GLOBALOBJECT.minX;
		var incrementsPerSlide	= parseInt( distanceOfSlide/ GLOBALOBJECT.incrementByPixel);
		var valueEquation	= GLOBALOBJECT.min +  (incrementsPerSlide * GLOBALOBJECT.incrementByValue);

		return getValueInRange( valueEquation, GLOBALOBJECT.min, GLOBALOBJECT.max);
	}
			
	function setSliderHandlePosition( x)
	{
        x = parseInt(x);

        if (GLOBALOBJECT.manualx) {GLOBALOBJECT.x = x;}
	    $(GLOBALOBJECT.handle).css({position:"absolute"}).left( getValueInRange(x,GLOBALOBJECT.minX,GLOBALOBJECT.maxX));
		
	}

	function fireCustomUserEvent()
	{
		if ( typeof( GLOBALOBJECT.userFunction) == "function")
		{
		  //  $(document).ready
		   // (
		     //   function()
		     //   {
			        GLOBALOBJECT.userFunction();
            //    }
           // )
  		}
	}

	//document.onkeydown = document.onmousedown = sliderControlMouseDownEvent;
	document.onmousedown = sliderControlMouseDownEvent;
	document.onmouseup = sliderControlMouseUpEvent;


