
(function(){

var YUE = YAHOO.util.Event;
var YUD = YAHOO.util.Dom;
var YUC = YAHOO.util.CustomEvent ;
var isDrag = false;

if(window.opera)
{
    YUD.addClass(document.documentElement , 'borwser-opera');
};


var stopAction = function(e)
{
    YUE.stopEvent(e);
    return false;
};

var setValue =  function(nFrom,nTo,dFromBtn , bNotChangePosition )
{    
    var nOldFrom  = this.from ;
    var nOldTo    = this.to ;
    var ticks     = this.ticks || 100;

    var sElementId = this.id ;
    var  dEl = document.getElementById(sElementId);
    var  dBox =  document.getElementById(sElementId + '-box');
    var  dC0 = document.getElementById(sElementId + '-ctrl-0');
    var  dC1 = document.getElementById(sElementId + '-ctrl-1');
    var  dC2 = document.getElementById(sElementId + '-ctrl-2');
    var  dLn1 = document.getElementById(sElementId + '-line-1');
    var  nSliderWidth =dBox.offsetWidth;
    
    var nf  = Math.min( nTo , Math.max( 0 , nFrom ) );
    var nt = Math.min( ticks , Math.max( nFrom ,nTo ) );
    if(isNaN(nf)){ nf = 0 };
    if(isNaN(nt)){ nf = ticks };
    nf = Math.round(nf);
    nt = Math.round(nt);

    this.from = nf ;
    this.to = nt;

    if(!bNotChangePosition)
    {
        var nX1 = ( nSliderWidth * (this.from/ticks) ) ; 
        var nX2 = ( nSliderWidth * (this.to/ticks) ) - dC2.offsetWidth ; 
        
        if(dFromBtn == dC1 )
        {			
            dC1.style.left = nX1  + 'px';
        }
        else if(dFromBtn == dC2 )
        {
            dC2.style.left = nX2  + 'px';
        }
        else
        {
            dC1.style.left = nX1  + 'px';
            dC2.style.left = nX2  + 'px';			
        };			

        var nLeft =   parseInt( dC1.style.left ) - 1 ;
        var nRight =  parseInt( dC2.style.left ) + dC2.offsetWidth - 1;
        dC0.style.left = dC1.style.left;
        dC0.style.width = nRight - nLeft +  'px';
    };
    return [nX1, nX2] ;
};

var onMouseDown =  function(e,oSelf)
{        
        var dBtn = this; 
                    
        var  sElementId = oSelf.id ;
        var  dEl = document.getElementById(sElementId);
        var  dBox =  document.getElementById(sElementId + '-box');
        var  dC0 = document.getElementById(sElementId + '-ctrl-0');
        var  dC1 = document.getElementById(sElementId + '-ctrl-1');
        var  dC2 = document.getElementById(sElementId + '-ctrl-2');
        var  dLn1 = document.getElementById(sElementId + '-line-1');
        var  nSliderWidth = dBox.offsetWidth;	
        var nOffsetLeft = YUD.getX( dBox ) ||  YUD.getX( dEl  );

        var nLimitLeft   = 0;
        var nLimitRight  = nSliderWidth - dBtn.offsetWidth;
        var nSliderWidth = nSliderWidth	;        

        var prevFrom = oSelf.from;
        var prevTo   = oSelf.to;

        if(dBtn.className == 'ctrl-2')
        {
            nLimitLeft  = parseInt( dC1.style.left ) + dC1.offsetWidth ; 
        }
        else
        {
            nLimitRight = parseInt( dC2.style.left ) - dC1.offsetWidth ;
        };
        
        var onMouseUp = function(e)
        {
                YUE.stopEvent(e);			
                YUE.removeListener( document , 'mousemove' ,onMouseMove );
                YUE.removeListener( document , 'mouseup' , onMouseUp );			
                isDrag = false;	
	        if (oSelf.to != prevTo || oSelf.from != prevFrom)
                   oSelf.onChange.fire(oSelf);
                oSelf.onMouseUp.fire(e);
                return false;
        };

        var onMouseMove =  function(e)
        {		
                
                var nEx = e.clientX - nOffsetLeft ;		
                if(!isDrag )
                {	
                    onMouseUp.call( document ,e,aArg);
                    return;
                };					

                nEx = Math.max(  nLimitLeft  , nEx); 
                nEx = Math.min(  nEx ,nLimitRight ); 
                if( dBtn == dC1 )
                {
                    oSelf.from = Math.abs( ( nEx / (nSliderWidth - dC1.offsetWidth - dC2.offsetWidth) ) * oSelf.ticks );
                }
                else
                {
                    oSelf.to = Math.abs( ( nEx / (nSliderWidth - dC1.offsetWidth - dC2.offsetWidth) ) * oSelf.ticks );
                };

                if(Math.abs( oSelf.from - oSelf.to ) <= 3 )
                {
                     oSelf.from = oSelf.to 
                };

               
               setValue.call(oSelf, oSelf.from, oSelf.to, dBtn, true ); 
                if( dBtn == dC1 )
                {
                    dBtn.style.left =nEx + 'px';
		    var nLeft =   parseInt( dC1.style.left ) - 1 ;
	            var nRight =  parseInt( dC2.style.left ) + dC2.offsetWidth;
	            dC0.style.left = dC1.style.left;
	            dC0.style.width = nRight - nLeft +  'px';
					
                }
                else
                {                    
		    dBtn.style.left =nEx + 'px';
		    var nLeft =   parseInt( dC1.style.left ) - 1 ;
		    var nRight =  parseInt( dC2.style.left ) + dC2.offsetWidth - 1;
	            dC0.style.left = dC1.style.left;
	            dC0.style.width = nRight - nLeft +  'px';
					
                };
                YUE.stopEvent(e);
                oSelf.onSlide.fire(oSelf);
                return false;
        };


        YUE.on( document , 'mousemove' , onMouseMove  );
        YUE.on( document , 'mouseup' , onMouseUp );
        isDrag = true;	
        YUE.stopEvent(e);
        oSelf.onMouseDown.fire(e);
        return false;

};

var fineTuneFrom = function(e,oSelf) {
    oSelf.setValue(oSelf.from-1,oSelf.to);
}

var fineTuneTo = function(e,oSelf) {
    oSelf.setValue(oSelf.from,oSelf.to+1);
}

var selectAll = function(e,oSelf) {
  YUE.stopEvent(e);
  var left  = 0;
  var right = oSelf.ticks;

  if (oSelf.to == right && oSelf.from == left) {
    oSelf.setValue(oSelf.prevFrom,oSelf.prevTo);
  } else {
    oSelf.setValue(left,right);
  }
  oSelf.prevTo   = right;
  oSelf.prevFrom = left;

  oSelf.onChange.fire(oSelf);
}

var onMouseDownOnBackground = function(e,oSelf)
{
        var  sElementId = oSelf.id ;
    
        var  dBox =  document.getElementById(sElementId + '-box');
        var  dC0 = this;
        var  dC1 = document.getElementById(sElementId + '-ctrl-1');
        var  dC2 = document.getElementById(sElementId + '-ctrl-2');
        var  dLn1 = document.getElementById(sElementId + '-line-1');
        var  nSliderWidth = dBox.parentNode.offsetWidth || dBox.offsetWidth;	
        var  nAvailRangeWidth = nSliderWidth +  dC1.offsetWidth -  dC2.offsetWidth;
        var  nOffsetLeft = YUD.getX( dBox );
    
        var  ndC0Width   = dC0.offsetWidth;
        var  ndC2Width   = dC2.offsetWidth;
        var  nLimitRight = nSliderWidth -  ndC0Width;
        var  nMouseDownX = e.clientX - YUD.getX( dC0 );
        var  nRange =  oSelf.to - oSelf.from - 1;

        var prevFrom = oSelf.from;
        var prevTo   = oSelf.to;

        var onMouseMove = function(e)
        {
	    if(!isDrag )
            {	
                onMouseUp.call( document ,e);
                return;
            };	
            var nEx =  Math.max( 0, e.clientX - nOffsetLeft - nMouseDownX  ) ;
            if(nEx < nLimitRight)
            {				
                var nTo = Math.min(oSelf.ticks, Math.round(((nEx + ndC0Width )/nAvailRangeWidth) * oSelf.ticks ));
                var nFrom = nTo - nRange ;

                setValue.call( oSelf , nFrom , nTo, null, true );
                dC0.style.left = nEx + 'px';					
                dC0.style.width = ndC0Width + 'px';
                dC2.style.left = nEx + ndC0Width - ndC2Width + 1 + 'px';
            }
            else { // slide to right
	        oSelf.to = oSelf.ticks;
                dC0.style.left  = nLimitRight + 'px';					
                dC0.style.width = ndC0Width + 'px';	
                dC2.style.left  = nLimitRight + ndC0Width - ndC2Width + 'px';
            };
            dC1.style.left = dC0.style.left;
            oSelf.onSlide.fire(oSelf);            
        };

        var onMouseUp = function(e)
        {
            YUE.removeListener( document , 'mousemove' , onMouseMove );
            YUE.removeListener( document , 'mouseup' , onMouseUp );
            if (oSelf.to != prevTo || oSelf.from != prevFrom)
               oSelf.onChange.fire(oSelf);
            isDrag = false;	
        };

        YUE.on( document , 'mousemove' ,onMouseMove );
        YUE.on( document , 'mouseup' , onMouseUp );
        isDrag = true;	
        YUE.stopEvent(e);
        oSelf.onMouseDown.fire(e);
        return false;
};



YAHOO.widget.RangeSlider = function(sElementId , oAttr)
{
    
    var oSelf = this;	
    
    oSelf.onMouseDown = new YUC('mousedown');
    oSelf.onMouseUp = new YUC('mouseup');
    oSelf.onSlide = new YUC('slide');
    oSelf.onLoad = new YUC('load');
    oSelf.onChange = new YUC('change');

    if( typeof( oAttr ) != 'object' )
    {
        oAttr = {};
    };

    var init = function()
    {
        var dEl = this;
        
	this.ua = navigator.userAgent.toLowerCase();

        dEl.className = 'yui-range-slider';
        if(dEl.tagName!='SPAN')
        {
            throw Error('YAHOO.widget.RangeSlider Error: \n Element['+sElementId+']\'s tagName is not SPAN'); 
            return;
        };

        oSelf.type = ( YUD.hasClass(dEl ,  'yui-range-slider-v' ) )?2:1;
      
        YUD.addClass( dEl , 'yui-range-slider yui-range-slider-' + oSelf.type );
        
        var sTagName = window.opera?'span':'button';
        dEl.innerHTML = 
        [
            '<span class="iesucks">',
            '<span class="s-hd" id="',sElementId,'-span">',
            '<span class="s-line-1" id="',sElementId,'-line-1"></span>',
            '<',sTagName,' class="ctrl-0" id="',sElementId,'-ctrl-0" ></',sTagName,'>',
            '<button id="',sElementId,'-ctrl-1" class="ctrl-1">&nbsp;</button>',
            '<button id="',sElementId,'-ctrl-2" class="ctrl-2">&nbsp;</button>',
            '<span class="s-line-2" id="',sElementId,'-line-2"></span>',
            '</span></span>',
            '<',sTagName,' hidefocus="hidefocus" class="s-bd" id="',sElementId,'-box" >&nbsp;</',sTagName,'>',

        ].join('');

       
	document.getElementById(sElementId).style.position = 'relative';
        var  dBox =  document.getElementById(sElementId + '-box');
        dBox.style.width = (  (oAttr.width || 200)) + 'px';
        oAttr.height = (oAttr.height || 23);
        dBox.style.height = oAttr.height + 'px';
	document.getElementById(sElementId + '-span').style.height = oAttr.height + 3 + 'px';

        var marginTopOffset = 8;
	if (this.ua.match(/msie/)) {
           marginTopOffset = 12;
        } else {
	   if (this.ua.match(/safari/)) {
             marginTopOffset = 6;
           }
        }
        document.getElementById(sElementId + '-span').style.marginTop = -(parseInt(oAttr.height/2) + marginTopOffset) + 'px';
	document.getElementById(sElementId + '-span').style.background = 'transparent';
        oSelf.from = oAttr.from || oSelf.from;
        oSelf.ticks = oAttr.ticks || 100;
        oSelf.to = oAttr.to || oSelf.to || oSelf.ticks;

        var  dC0  = document.getElementById(sElementId + '-ctrl-0');
        var  dC1  = document.getElementById(sElementId + '-ctrl-1');
        var  dC2  = document.getElementById(sElementId + '-ctrl-2');
        var  dLn1 = document.getElementById(sElementId + '-line-1');
        var  nSliderWidth =dBox.offsetWidth;	
    
        dLn1.style.width =  parseInt(dBox.style.width)-2 + 'px';
        // dC0.style.height = ( parseInt( dBox.style.height)) + 'px';
        dC0.style.height = dC1.style.height = dC2.style.height = oAttr.height + 'px';
        
        // dLn1.style.top = (  oAttr.height || 23 ) /2 + 'px';
        
        YUE.on(  dC1 , 'mousedown' , onMouseDown , oSelf );
        YUE.on(  dC2 , 'mousedown' , onMouseDown , oSelf );
        YUE.on(  dC0 , 'mousedown' , onMouseDownOnBackground , oSelf );
        YUE.on(  dEl , 'selectstart' , stopAction  );			
        YUE.on(  dBox , 'mousedown' , stopAction  );			
	// YUE.on(  dC0 , 'dblclick' , selectAll, oSelf);
        // YUE.on(  dC1 , 'dblclick' , fineTuneFrom, oSelf);
        // YUE.on(  dC2 , 'dblclick' , fineTuneTo, oSelf);

        setValue.call(oSelf, oSelf.from, oSelf.to);
        oSelf.readyState = 'complete';
		
        oSelf.onLoad.fire(oSelf);
        //Debug('slidebar element:  ' + sElementId + ' complete' , 2);
    
    };

    this.id = sElementId;
    YUE.onAvailable( sElementId , init );
};

YAHOO.widget.RangeSlider.prototype = 
{
    id:'',
    ticks: 100,
    readyState:'uninitialized',
    type:1,
    from:60,
    to:100,
    setValue : function(nFrom , nTo)
    {
        var oSelf =this;
        nFrom =  isNaN(nFrom)?0: (nFrom || 0 );
        nTo = isNaN(nTo)?oSelf.ticks: (nTo || oSelf.ticks);		
        
        nFrom = Math.max( 0 , nFrom );  
        nTo = Math.min( oSelf.ticks , nTo);  
        nFrom = Math.min( nFrom , nTo );
        
        var fAction = function()
        {
            setValue.call(oSelf, nFrom, nTo );
        };

        if(oSelf.readyState!='complete')
        {
            oSelf.onLoad.subscribe(fAction);
        }
        else
        {
            fAction(); 
        };
        
    },
    
    getValue:function()
    {
        return [this.from,this.to];
    },

    getElement:function()
    {
        return document.getElementById(this.id);
    }
};
})();
