// ==============================================================
// HANDLES SCROLLER/S
// Modified from Aaron Boodman http://webapp.youngpup.net/?request=/components/ypSimpleScroll.xml
// mixed ypSimpleScroll with dom-drag script and allowed multiple scrolelrs through array instances
// (c)2004 Sergi Meseguer (http://zigotica.com/), 04/2004:
// ==============================================================
var aSCTent = []; var theRoot = []; var arContainer = []; var arThumb = [];
var theThumbV = []; var thumbTravelV = []; var ratioV = [];
var theThumbH = []; var thumbTravelH = []; var ratioH = [];
var wndWidth, wndHeight;
var bWasFullContent_V = false; //shows if  the ciontent has been shown in full size of height without scrolling
var bWasFullContent_H = false; //shows if  the ciontent has been shown in full size of width without scrolling
var bIsNotFullWidth, bIsNotFullHeight;
var gThSmallDim=10, gThBigDim = 40;
var minThumbW = gThBigDim;
var cr = 'Container', ct='Content';   
var Event = YAHOO.util.Event,
        Dom   = YAHOO.util.Dom,
        lang  = YAHOO.lang

if (typeof iCustomThSmallDim !='undefined') gThSmallDim = iCustomThSmallDim;
if (typeof iCustomThBigDim !='undefined')   gThBigDim = iCustomThBigDim;


function instantiateScroller(count, id, left, top, width, height, ct_width, ct_height, sType, iThSmall, iThBig, sSBt){
	if (iThSmall) gThSmallDim = iThSmall;
	if (iThBig) gThBigDim = iThBig;
	if (document.getElementById) {
	    arContainer[count] = {id:id, top:top, left:left, CNerW: width, CNerH: height}
	    theRoot[count] = Dom.get('root'+count);
		if (Dom.get(id+ct)) aSCTent[count] = {obj:Dom.get(id+ct), W: ct_width, H:ct_height, left: left };
		                  
		if (!sType) sType='V';
		if (!sSBt) {sSBt=''; SBTxtWidth = 0;}
		else {
		    sSBt = (GE(sSBt))? GE(sSBt).innerHTML : '';
		    SBTxtWidth = GE('sbname').offsetWidth;
		}
		
		if (sType=='H'){
		    x = aSCTent[count].W;
            y = arContainer[count].CNerW;
            gThBigDim = CalcWidth(x,y);  
        }
		arThumb[count] = {type: sType, bigD: gThBigDim, smallD: gThSmallDim, sSBt: sSBt, sbtW:SBTxtWidth, slider: null, scaleF: 1};
	}
	
}

function CalcWidth(content, container){
    return Math.round(container - (content*container/(content+container)) );    //   ( container/(content + container) ) * container

}

// INITIALIZER:
function LoadIt(){
    for (a = 1; a< arContainer.length; a++){        //a=0
        if (arContainer[a]) LoadSB(a);
    }
 
}
function LoadSB(ind){

    if (!ind) ind = 0;
	    
    var sST, slider, bg, thumb;
    var beginConstraint, endConstraint, scaleFactor, bIsStart;
    var oC, oT;

    sST = arThumb[ind].type,
    bg = 'track'+sST+ind,
    thumb = 'thumb'+sST+ind;
    if (sST == 'H') thumb = 'thumb'+sST+ind+"_1";
    
    if (sST == 'H') {
        // The slider can move 0 pixels left   
        beginConstraint = 0;
        // The slider can move 200 pixels right
        endConstraint = arContainer[ind].CNerW - arThumb[ind].bigD; //-arThumb[ind].bigD;
        
        oT = aSCTent[ind].W - arContainer[ind].CNerW;
        oC = arContainer[ind].CNerW-arThumb[ind].bigD;
    }
    else { //'V'
        beginConstraint = 0;    //Slider Move from top;
        // The slider can move X pixels down
        endConstraint = arContainer[ind].CNerH - arThumb[ind].bigD;
        
        oT = aSCTent[ind].H - arContainer[ind].CNerH ;
        oC = arContainer[ind].CNerH-arThumb[ind].bigD;
        
    }

         // Custom scale factor for converting the pixel offset into a real value
        arThumb[ind].scaleF = oT / oC;      //scaleFactor = oT / oC  ;
        bIsStart = false;

    arThumb[ind].scaleFactor = scaleFactor;
    // The amount the slider moves when the value is changed with the arrow
    // keys
    var keyIncrement = 20;

    Event.onDOMReady(function() {

        AddDOMSlider(ind, sST);
        
        if (sST == 'H') if (typeof SetSBPos == 'function') SetSBPos();
        
           if (sST == 'H')
            slider = YAHOO.widget.Slider.getHorizSlider(bg, thumb, beginConstraint, endConstraint);
        else
            slider = YAHOO.widget.Slider.getVertSlider(bg, thumb, beginConstraint, endConstraint);
        
        //redefine methods 
        if (navigator.appName) 
            if (navigator.appName.indexOf('Internet Explorer')>-1)  {  
                 slider.thumb.onMouseDown = function(){
                    //**removed: return self.focus(); 
                }
                slider.onMouseDown = function(e) {
                    if (! this.isLocked() && this.backgroundEnabled) {
                        var x = YAHOO.util.Event.getPageX(e);
                        var y = YAHOO.util.Event.getPageY(e);
                   //**removed:     this.focus();
                        this.moveThumb(x, y);
                    }
                }
            }
    
                    
        slider.getRealValue = function() {
            return -Math.round(this.getValue() * arThumb[ind].scaleF);      //(scaleFactor);
        }    

        slider.subscribe("change", function(offsetFromStart) {

            var actualValue = slider.getRealValue();

            if (sST == 'H') {
                
                Dom.setX(aSCTent[ind].obj, actualValue);
                  
                //positon the text above the thumb  
               var thW, sbtxt, thLeft, E;
                thW = arThumb[ind].bigD;
                sbtxt = arThumb[ind].sbtW;
                pLeft = slider.getValue();
                a = thW;
                b = sbtxt;
                T = pLeft;
                E = slider.thumb.rightConstraint;        //endConstraint;
               
                if (thW < sbtxt){
                    if ( T >= Math.abs((a-b)/2) ){
                        
                            if ( pLeft >= E - Math.abs(a-b)/2 ){ // at the end - stay attached to right side
                             //  GE('thumbH0').style.width = (a + E-T) +'px';       
                             //  GE('sbname').style.left = (a-b + E-T) + 'px';    
                            }else {
                                //GE('thumbH0').style.width = (b- +(b-a)/2) +'px';
                                //GE('sbname').style.left = (a-b)/2+'px';
                                GE('thumbH0').style.left = T-Math.abs(a-b)/2+'px';
                              }
                    }else {// at the beginning - stay attached to left side
                        //GE('sbname').style.left = -T + 'px';
                       // GE('thumbH0').style.width = b + 'px';
                        GE('thumbH0').style.left = '0px';
                    }
                }else {
                   // GE('sbname').style.left = T+(a-b)/2+'px';
                    GE('thumbH0').style.left = T+(a-b)/2+'px';
                }
             }
            else  aSCTent[ind].obj.style.top = actualValue+'px';    //Dom.setY(aSCTent[ind].obj, 0);
           
                       
        });

        slider.subscribe("slideStart", function() {
                bIsStart = true;
            });

        slider.subscribe("slideEnd", function() {
                 bIsStart = false;
            });
       
     slider.RecalcSB = function(ind, ctner_width, contentW){
           
            //old data
            var cont1 = arContainer[ind].CNerW;
            var x1 = slider.getValue();   //end
            
               
            if (ctner_width) arContainer[ind].CNerW = ctner_width;
            if (contentW) aSCTent[ind].W = contentW;
           
           var bNoSlider = false;
           if (arContainer[ind].CNerW > aSCTent[ind].W) bNoSlider = true;
           
           if (!bNoSlider) {
                slider.RecalcSBThumb(ind);
               
                oT = aSCTent[ind].W - arContainer[ind].CNerW;
                oC = arContainer[ind].CNerW-arThumb[ind].bigD;
                arThumb[ind].scaleF = oT / oC;
                   
                slider.setValue( Math.round( x1 * arContainer[ind].CNerW /cont1),true, false, false)
                Dom.setX(aSCTent[ind].obj, slider.getRealValue()); 
               
                endConstraint = arContainer[ind].CNerW - arThumb[ind].bigD;       //ctner_width-
                slider.thumb.setXConstraint(0, endConstraint);
                
                a = arThumb[ind].bigD;
                b = arThumb[ind].sbtW;
                T = slider.getValue();
                Dom.get('thumbH0').style.width = arThumb[ind].sbtW + 'px';  
                if (arThumb[ind].sbtW > arThumb[ind].bigD) { //b>a
                    
                    if ( T < Math.abs((a-b)/2) ) {
                        //Dom.get('sbname').style.left = '0px';
                        Dom.get('thumbH0').style.left = '0px';
                    }
                    
                }else {
                    //Dom.get('thumbH0').style.width = arThumb[ind].bigD + 'px';
                    //Dom.get('sbname').style.left = (a-b)/2 + 'px';  
                     GE('thumbH0').style.left = T+(a-b)/2+'px';
                 }
                 
            }
              
           h1 = Dom.get('trackH'+ind)
           h1.style.width = arContainer[ind].CNerW + 'px';
           h2 = Dom.get('thumbH'+ind)
           if (bNoSlider) {
                h1.style.visibility = 'hidden'; 
                h2.style.visibility = 'hidden';
           }else {
                h1.style.visibility='visible';
                h2.style.visibility='visible';
            }
            //  if (arContainer[ind].CNerW > aSCTent[ind].W) h1.style.display = 'none'; else h1.style.display='block';
            
        }
        
       slider.RecalcSBThumb = function(ind){
       
            x = aSCTent[ind].W;
            y = arContainer[ind].CNerW;
            th = CalcWidth(x,y);  
            if (th< minThumbW) th = minThumbW;
            arThumb[ind].bigD = th;
           
            // change size of thumb
             h2 = Dom.get('thumbH'+ind)
          //  h2.style.width = th + 'px';
            
            h2_1 = Dom.get('thumbH'+ind+'_1')
            h2_1.style.width = th + 'px';    
            
       }
       
       arThumb[ind].slider = slider;  
        
    });

    
    var step = 50;

    if (typeof CSBfleXcroll != 'undefined'){
        CSBfleXcroll(ind)
        dDiv = document.getElementById('scroll'+ind+'Container');  
        dDiv.commitScroll=function(xScr,yScr){
            newOffset = - (slider.getRealValue()-step*yScr)/arThumb[ind].scaleF;        //scaleFactor;
            slider.setValue(newOffset, false, false, false) 
        };
    }
    
}

function AddDOMSlider(count, t){
        var z = (t=='H')? 1 : 0;

        var h1 = document.createElement('div');
        h1.setAttribute('id','track'+t+count)
        h1.className = "slider"+t+"-bg";

        if (z) {
            h1.style.height = arThumb[count].smallD + 'px'; //arThumb[count].smallD/2 + 'px'; 
            h1.style.width =arContainer[count].CNerW + 'px';
        }else {
            h1.style.height = arContainer[count].CNerH + 'px';
            h1.style.width = arThumb[count].smallD + 'px'; //arThumb[count].smallD/2 + 'px'; 
        }
        theRoot[count].appendChild(h1);
            
        var h2 = document.createElement('div');
        h2.setAttribute('id','thumb'+t+count);

        if (z){  
            h2.className = "slider"+t+"-thumb1";
           // h2.style.width = arThumb[count].bigD + 'px';
            h2.style.width = arThumb[count].sbtW + 'px'; 
            //h2.style.height = (arThumb[count].smallD+17) + 'px';
            h2.style.left =  '0px';
            //h2.style.bottom = '0px';   

            if (arThumb[count].sSBt.length > 0){
                h2.innerHTML = arThumb[count].sSBt;
            //    if (arThumb[count].sbtW > arThumb[count].bigD) h2.style.width = arThumb[count].sbtW + 'px'; 
              }else
                h2.innerHTML = '<img src="CommonFiles/spacer.gif" width="'+arThumb[count].bigD+'" height="'+arThumb[count].smallD+'" border="0">';
            
            theRoot[count].appendChild(h2);     //h1.appendChild(h2);    
            
            var h2_1 = document.createElement('div');
            h2_1.setAttribute('id','thumb'+t+count+'_1')
            h1.appendChild(h2_1);    //h2.appendChild(h2_1);
            h2_1.className = "slider"+t+"-thumb";
            
            x = aSCTent[count].W;
            y = arContainer[count].CNerW;
            th = CalcWidth(x,y);  //x*y/(x+y);        //parseInt( (y-x)/x );
            if (th< minThumbW) th = minThumbW;

            h2_1.style.width = th + 'px';       //arThumb[count].bigD + 'px';
            arThumb[count].bigD = th;
            h2_1.style.height = arThumb[count].smallD + 'px';
            h2_1.style.bottom = '0px';   
        
            if (arThumb[count].sSBt.length > 0){
               if (arThumb[count].bigD>arThumb[count].sbtW) 
                    //GE('sbname').style.left =  (arThumb[count].bigD-arThumb[count].sbtW)/2 + 'px';
                     GE('thumbH0').style.left =  (arThumb[count].bigD-arThumb[count].sbtW)/2 + 'px';
            }
            
        } else {
            h2.className = "slider"+t+"-thumb";
            h2.style.width = arThumb[count].smallD + 'px';
            h2.style.height = arThumb[count].bigD + 'px';
            h2.style.top = '0px';
            h2.style.left = '0px';  //- (arThumb[count].smallD/2) + 'px';
            h2.innerHTML = '<img src="CommonFiles/spacer.gif" width="'+arThumb[count].smallD+'" height="'+arThumb[count].bigD+'" border="0">';

            h1.appendChild(h2);                
        }
            
        //h1.appendChild(h2);
           
         
      // if (z) if (arContainer[count].CNerW > aSCTent[count].W) h1.style.display = 'none'; else h1.style.display='block';
      //  else   if (arContainer[count].CNerH > aSCTent[count].H) h1.style.display = 'none'; else h1.style.display='block';
         if (z) if (arContainer[count].CNerW > aSCTent[count].W) {
                h1.style.visibility = 'hidden'; 
                h2.style.visibility = 'hidden';
             }
             else {
                 h1.style.visibility='visible';
                 h2.style.visibility='visible';
             }
        else   if (arContainer[count].CNerH > aSCTent[count].H) {
                    h1.style.visibility = 'hidden'; 
                    h2.style.visibility = 'hidden'; 
               }else {
                    h1.style.visibility='visible';
                    h2.style.visibility='visible';
                }
}

function RecalcSBLoad(ind, sbType, contentW){

    RecalcSB(ind, false, contentW)
//caramba
}
function RecalcSB(ind, ctner_width, ctent_width){
    try{
        arThumb[ind].slider.RecalcSB(ind, ctner_width, ctent_width);
    }catch(e) {
        // we get into this case when no scrollbar has been created because of no need of it
        // then on resize we dont need to edit the scrollbar properties
    }
    
}

var old = window.onload;
if (typeof window.onload != 'function') {
    window.onload = LoadIt;
}
else {
     window.onload = function() {
         old();
         LoadIt();
     }
}


