
// ==============================================================
// 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 theHandle = []; var theRoot = []; var theScroll = []; 
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 iThSmallDim=12, iThBigDim = 30, iSTrack = 1;
   

function instantiateScroller(count, id, left, top, width, height, speed){
	if(document.getElementById) {
		    theScroll[count] = new ypSimpleScroll(id, left, top, width, height, speed);
	}
	
}

function createDragger(count, handler, root,  minX, maxX, minY, maxY, modeV, modeH){
		//add scrollers to the content
	    theRoot[count]   = document.getElementById(root);
		//document.getElementById(root).innerHTML = WriteDraggerParts(count) + theRoot[count].innerHTML; --> this is not a good way
	/**** insert this code for scrollbar
	     '<div class="scrolltrack" style="width:'+iSTrack+'px;" id="track'+count+'"></div>'+
                '<div class="thumbV" id="thumb'+count+'" style="width:'+iThSmallDim+'px;height:'+iThBigDim+'px;"><img src="CommonFiles/spacer.gif" width="'+iThSmallDim+'" height="'+iThBigDim+'" border="0"></div>';
   **/  var v1 = document.createElement('div');
        v1.setAttribute('id','track'+count)
        v1.className = "scrolltrack";
        v1.style.width = iSTrack + 'px';
        theRoot[count].appendChild(v1);
   
        var v2 = document.createElement('div');
        v2.setAttribute('id','thumb'+count)
        v2.className = "thumbV";
        v2.style.width = iThSmallDim + 'px';
        v2.style.height = iThBigDim + 'px';
        v2.innerHTML = '<img src="CommonFiles/spacer.gif" width="'+iThSmallDim+'" height="'+iThBigDim+'" border="0">';
        theRoot[count].appendChild(v2);
        
		theScroll[count].load();

            //** vertical scrollbar settings
        if (!bIsNotFullWidth) {
            maxY = maxY+15;
            theScroll[count].container.style.height = parseInt(theScroll[count].container.offsetHeight)+15+'px';    
        }    
        SetPositionsToVerticalDragger(count, minX, maxX, minY, maxY) ;
        theThumbV[count].style.top = parseInt(minY) + "px";
        SetVerticalDraggerMotion(count, minX, maxX, minY, maxY);

            //** horizontal scrollbar settings
    /*    if (!bIsNotFullHeight) {
            maxX = maxX+15;
            theScroll[count].container.style.width = parseInt(theScroll[count].container.offsetWidth)+15+'px';    
        }

        SetPositionsToHorizontalDragger(count, minX, maxX, minY, maxY);
        theThumbH[count].style.left = parseInt(minX) + "px";
        SetHorizontalDraggerMotion(count, minX, maxX, minY, maxY);
     */ 
        ShowHideScroller(count,modeV, modeH);
        
        CSBfleXcroll(count);

}	

// INITIALIZER:

function LoadIt(ind){
		if(theScroll.length>0) {
		    for(var i=0;i<theScroll.length;i++){
		        if (ind) i=ind; // if ind is defined - create croller only for that index and leave the for-cycle  with break

		        var cnerH = document.getElementById(theScroll[i].id+'Container').offsetHeight;
		        var ctentH = document.getElementById(theScroll[i].id+'Content').offsetHeight-20;
		        var cnerW = document.getElementById(theScroll[i].id+'Container').offsetWidth;
		        var ctentW = document.getElementById(theScroll[i].id+'Content').offsetWidth;
		       
		        if ( cnerH < ctentH ) {
		            modeV = ''; 
		            if (i==0) {
		                bWasFullContent_V = false;
		                bIsNotFullHeight = true;
		            }
		    }
		        else {
		            modeV = 'none';
		            if (i==0) {
		                bWasFullContent_V = true; 
		                bIsNotFullHeight = false;
		            }
		         }
		        if ( cnerW < ctentW ) {
		            modeH = ''; 
		            if (i==0) {
		                bWasFullContent_H = false;
		                bIsNotFullWidth = true;
		            }
		    }
		        else {
		            modeH = 'none';
		            if (i==0) {
		                bWasFullContent_H = true; 
		                bIsNotFullWidth = false;
		            }
		         }
		       createDragger(i, "handle"+i, "root"+i, 15, theScroll[i].clipW, 15, theScroll[i].clipH-30,modeV, modeH);
		       
		       if (ind) break;  
		    }
		    
	    }
}

var old = window.onload;
if (typeof window.onload != 'function') {
    window.onload = LoadIt();
}
else {
     window.onload = function() {
         old();
         LoadIt();
     }
}

function ShowHideScroller(count,modeV, modeH){
    // vertical scrollers
    var arScr = new Array('track','up','dn', 'thumb');
    for (var i=0; i<arScr.length; i++) {
        obj = document.getElementById(arScr[i]+count);
        if (obj)
            obj.style.display = modeV;
            //obj.parentNode.removeChild(obj)
    }
    //horizontal scrollers
/*  
    var arScr = new Array('trackH','upH','dnH', 'thumbH');
    for (var i=0; i<arScr.length; i++) {
        obj = document.getElementById(arScr[i]+count);
        if (obj)
            obj.style.display = modeH;
            //obj.parentNode.removeChild(obj)
    }
 */
}

 function GetWindowSize(id,iDivNum) {
      var myWidth = 0, myHeight = 0;
      
      if (iDivNum=='0') {   
              if( typeof( window.innerWidth ) == 'number' ) {
                //Non-IE
                myWidth = window.innerWidth-15;
                myHeight = window.innerHeight-15;
              } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth-15;
                myHeight = document.documentElement.clientHeight-15;
              } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                //IE 4 compatible
                myWidth = document.body.clientWidth-15;
                myHeight = document.body.clientHeight-15;
              }
          }    
      else {
            myWidth = document.getElementById(id).offsetWidth;
            myHeight = document.getElementById(id).offsetHeight;
       }
          
      wndWidth = myWidth;
      wndHeight = myHeight;
  
}

function WriteDraggerParts(count) {
   /* var buttonsH = '<div class="scrolltrack" style="width:13px;" id="track'+count+'"></div>'+
            '<div  class="up" id="up'+count+'" >'+
            '<a href="#"  onmousedown="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseup="theScroll['+count+'].endScroll();" onmouseout="theScroll['+count+'].endScroll();" onclick="return false;"><img src="images/spacer.gif" width="3" height="13" border="0"></a>'+
            '</div>'+
            '<div class="dn"  id="dn'+count+'" >'+
            '<a href="#" onmousedown="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseup="theScroll['+count+'].endScroll()" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="images/spacer.gif" width="3" height="13" border="0"></a>'+
            '</div>'+
            '<div class="thumbV" id="thumb'+count+'" style="width:13px;height:9px;border:1px;"><img src="images/spacer.gif" width="15" height="15" qborder="0"></div>';
    var buttonsW = '<div class="scrolltrack" style="height:13px;" id="trackH'+count+'"></div>'+
            '<div  class="up" id="upH'+count+'" >'+
            '<a href="#"  onmousedown="theScroll['+count+'].scrollWest(\''+count+'\')" onmouseup="theScroll['+count+'].endScroll();" onmouseout="theScroll['+count+'].endScroll();" onclick="return false;"><img src="images/spacer.gif" width="13" height="13" border="0"></a>'+
            '</div>'+
            '<div class="dn"  id="dnH'+count+'" >'+
            '<a href="#" onmousedown="theScroll['+count+'].scrollEast(\''+count+'\')" onmouseup="theScroll['+count+'].endScroll()" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="images/spacer.gif" width="13" height="13" border="0"></a>'+
            '</div>'+
            '<div class="thumbH" id="thumbH'+count+'"  style="width:9px;height:13px;" ><img src="images/spacer.gif"  width="9" height="13" border="0"></div>';
  */
    var buttonsH = '<div class="scrolltrack" style="width:'+iSTrack+'px;" id="track'+count+'"></div>'+
                '<div class="thumbV" id="thumb'+count+'" style="width:'+iThSmallDim+'px;height:'+iThBigDim+'px;"><img src="CommonFiles/spacer.gif" width="'+iThSmallDim+'" height="'+iThBigDim+'" border="0"></div>';
         
    var buttonsW = '<div class="scrolltrack" style="height:'+iSTrack+'px;" id="trackH'+count+'"></div>'+
               '<div class="thumbH" id="thumbH'+count+'"  style="width:'+iThBigDim+'px;height:'+iThSmallDim+'px;" ><img src="CommonFiles/spacer.gif"  width="'+iThBigDim+'" height="'+iThSmallDim+'" border="0"></div>';
    
    return buttonsH ;//+ buttonsW;
}

function SetPositionsToVerticalDragger(count, minX, maxX, minY, maxY) {
   
    //** vertical scrollbar settings	
	/*	var thisup = document.getElementById("up"+count);
		thisup.style.left = parseInt(maxX) + "px";
		thisup.style.top = 0 + "px";
		thisup.style.width = '3px';
		//thisup.style.visibility = 'hidden';
	
		var thisdn = document.getElementById("dn"+count);
		thisdn.style.left = parseInt(maxX) + "px";
		thisdn.style.top = parseInt(minY+maxY) + "px";
		thisdn.style.width = '3px';
		//thisdn.style.visibility = 'hidden';
		
		var thistrack = document.getElementById("track"+count);
		thistrack.style.left = parseInt(maxX) + "px";
		thistrack.style.top = 15 + "px";
		thistrack.style.height = parseInt(minY+maxY-15) + "px";
		thistrack.style.width = "3px";  //15px";
  */
		var thistrack = document.getElementById("track"+count);
		thistrack.style.left = parseInt(maxX) + "px";
		thistrack.style.top = 0 + "px";
		thistrack.style.height = parseInt(minY+maxY+15) + "px";
		thistrack.style.width = iSTrack + "px";  //15px";
		
  		theThumbV[count]  = document.getElementById('thumb'+count);
		theThumbV[count].style.left = parseInt(maxX) + "px";
		//theThumbV[count].style.border =0;
		theThumbV[count].style.top = parseInt(minY) + "px"; 
		scrollV = iThBigDim;   //( parseInt(document.getElementById(theScroll[count].id+'Container').offsetHeight) *  parseInt(maxY) ) / theScroll[count].docH
	    //if(scrollV < 5) scrollV = 5;
	    theThumbV[count].style.height = Math.round(scrollV) + "px";
	    theThumbV[count].style.width = iThSmallDim+"px";
	  /*   theThumbV[count].style.borderWidth = "1px";
        theThumbV[count].style.borderStyle = "solid";
       theThumbV[count].style.borderColor = "#FFFFFF #000000 #000000 #FFFFFF";
       */ 
}
function SetPositionsToHorizontalDragger(count, minX, maxX, minY, maxY) {
    //** horizontal scrollbar settings
        var thisleft = document.getElementById("upH"+count);
		thisleft.style.left = "0px";
		thisleft.style.top = parseInt(maxY+30) + "px";
		var thisright = document.getElementById("dnH"+count);
		thisright.style.top = parseInt(maxY+30) + "px";
		thisright.style.left = parseInt(maxX-15) + "px";
		
		theThumbH[count]  = document.getElementById("thumbH"+count);
		theThumbH[count].style.left = parseInt(minX) + "px"; 
		theThumbH[count].style.border = 0;
		theThumbH[count].style.top = parseInt(maxY+30) + "px";
			scrollH = ( parseInt(document.getElementById(theScroll[count].id+'Container').offsetWidth) *  parseInt(maxX-15) ) / theScroll[count].docW
	    if(scrollH < 5) scrollH = 5;
	    theThumbH[count].style.width = Math.round(scrollH) + "px";
	    theThumbH[count].style.borderWidth = "1px";
        theThumbH[count].style.borderStyle = "solid";
        theThumbH[count].style.borderColor = "#FFFFFF #000000 #000000 #FFFFFF";
	
		
		var thistrackH = document.getElementById("trackH"+count);
		thistrackH.style.left = parseInt(minX) + "px";
		thistrackH.style.top = parseInt(maxY+30) + "px";
		thistrackH.style.width = parseInt(maxX-15) + "px";
		thistrackH.style.height = "15px";

}
function SetVerticalDraggerMotion(count, minX, maxX, minY, maxY) {
    //** vertical scrollbar settings
        iSbHeight = parseInt( (theThumbV[count].offsetHeight)? theThumbV[count].offsetHeight : theThumbV[count].style.height )- 15 ;	
        Drag.init(theThumbV[count], null, maxX, maxX, minY, maxY-iSbHeight);
		
		// the number of pixels the thumb can travel vertically (max - min)
		thumbTravelV[count] = theThumbV[count].maxY - theThumbV[count].minY;

		// the ratio between scroller movement and thumbMovement
//*		ratioV[count] = theScroll[count].scrollH / thumbTravelV[count];
	
		theThumbV[count].onDrag = function(x, y) {
		    xx = Math.abs(parseInt(theScroll[count].content.style.left))
		    ratioV[count] = theScroll[count].scrollH / thumbTravelV[count];
		    theScroll[count].jumpTo( xx , Math.round((y - theThumbV[count].minY) * ratioV[count]) );
		}
}
function SetHorizontalDraggerMotion(count, minX, maxX, minY, maxY){
    //** horizontal scrollbar settings
        iSbWidth = parseInt( (theThumbH[count].offsetWidth)? theThumbH[count].offsetWidth : theThumbH[count].style.width )- 15 ;
		Drag.init(theThumbH[count], null, minX, maxX-iSbWidth-30, maxY+30, maxY+30);
		
		// the number of pixels the thumb can travel vertically (max - min)
		thumbTravelH[count] = theThumbH[count].maxX - theThumbH[count].minX;

		// the ratio between scroller movement and thumbMovement
//*		ratioH[count] = theScroll[count].scrollW / thumbTravelH[count]; 
		
		theThumbH[count].onDrag = function(x, y) {
		    yy = Math.abs(parseInt(theScroll[count].content.style.top));
		    ratioH[count] = theScroll[count].scrollW / thumbTravelH[count]; 
		    theScroll[count].jumpTo(Math.round((x - theThumbH[count].minX) * ratioH[count]), yy );
        }
}
