﻿// JScript File

function $$$() {
    var elements = new Array();
    
    for (var i = 0; i < arguments.length; i++) {
	    var element = arguments[i];
        if (typeof element == 'string')
	        element = document.getElementById(element);
        if (arguments.length == 1)
	        return element;
        elements.push(element);
        }
    return elements;
}

function hscroll(viewportId,holderId,innerholderId,itemwidth,itemheight,spacewidth,itemcount,aspeed,attachedDivs,rightmargin){
   
    var viewportDiv=document.getElementById(viewportId); 
    var holderDiv=document.getElementById(holderId);
    var innerDiv=document.getElementById(innerholderId);
    

     
    if (rightmargin==null) {rightmargin=0};
    

    var itemWidth=itemwidth-spacewidth;
    var spaceWidth=spacewidth;
    var width=itemwidth*itemcount - spacewidth + rightmargin;
    
    var viewportwidth=viewportDiv.offsetWidth;
    
    if (viewportwidth==0) viewportwidth=parseInt(viewportDiv.style.width.replace('px',''));
    
    this.items=itemcount;
    var speed = (aspeed!=null)?aspeed:1;
    
    holderDiv.style.whitespace = 'nowrap';
    holderDiv.style.left = '0px';
    
    innerDiv.style.whitespace = 'nowrap';
    innerDiv.style.left = '0px';

    holderDiv.scrollLeft = 0;
    
    if (1==2){
        holderDiv.onscroll=function(){
            for (i=0;i<attachedDivs.length;i++){
                        attachedDivs[i].scrollTop= holderDiv.scrollTop;
            }
        }
        holderDiv.style.overflow='scroll';
        holderDiv.style.width=holderDiv.offsetWidth + 26 + 'px';
        holderDiv.style.height=holderDiv.offsetHeight + 16 + 'px';
    }
     
    var viewportitems = parseInt((viewportwidth+rightmargin)/itemwidth);
    
    var currleftitem = 0;
    var maxleftitem = this.items-viewportitems;
    
    var currpos=0;
    var targetpos=0;
    
    var currVerticalpos=0;
    var targetVerticalpos=0;
    
    var movedownlock=0;
    
   

    this.moveDown = function(places){
        if (movedownlock==0){
        movedownlock=1;
        verticalMove=itemheight*places;
        this.slideVertical(-verticalMove);
        }
    }
    
    this.moveUp = function(places){
        verticalMove=itemheight*places;
        this.slideVertical(verticalMove);  
    }

    this.moveRight=function(places){
        
        if (currleftitem-viewportitems <=itemcount){
        if (places==null) places = 1;
        this.slide(Math.min(currleftitem+Math.abs(places), maxleftitem) - currleftitem);
        }
    }
    
    this.slideVertical=function(offset){
        currVerticalpos=-holderDiv.scrollTop;
        targetVerticalpos=currVerticalpos+offset;
        
        if (holderDiv.slideVerticalint){window.clearInterval(holderDiv.slideVerticalint);holderDiv.slideVerticalint=null;}
        holderDiv.slideVerticalint=window.setInterval(
            function(){
                if (currVerticalpos > targetVerticalpos) {
                    step = -Math.ceil(Math.sqrt(Math.abs(targetVerticalpos-currVerticalpos)*speed));
                }
                else if (currVerticalpos < targetVerticalpos){
                    step = Math.abs(Math.sqrt(Math.abs(targetVerticalpos-currVerticalpos)*speed));
                }
                
                currVerticalpos+=step;
                
                if ((step<0 && currVerticalpos<targetVerticalpos) || (step>0 && currVerticalpos>targetVerticalpos)) { 
                    currVerticalpos=targetVerticalpos;             
                }
                
                holderDiv.scrollTop = -currVerticalpos;
                if (attachedDivs){
                    for (i=0;i<attachedDivs.length;i++){
                        attachedDivs[i].scrollTop= holderDiv.scrollTop;
                    }
                }
                
                if (currVerticalpos==targetVerticalpos){
                    if (currVerticalpos <= -holderDiv.offsetHeight){currVerticalpos=-holderDiv.offsetHeight}
                    if (currVerticalpos > 0){currVerticalpos=0};
                    window.clearInterval(holderDiv.slideVerticalint);
                    holderDiv.slideVerticalint = null;
                    movedownlock=0;
                }   
            }
            ,24
        )
    }
    
    this.moveLeft=function(places){
        if (places==null) places = 1;
        this.slide(Math.max(currleftitem-Math.abs(places), 0) - currleftitem);   
    }
   
    this.moveTo=function(place){
        if (place!=null)
        this.slide(Math.max(0,Math.min(place, maxleftitem))-currleftitem);   
    }


    this.slide=function(offset){
        if ((currleftitem+offset) <= maxleftitem && (currleftitem+offset) >= 0 && offset!=0){
            currleftitem+=offset;
//            alert(currleftitem);
            targetpos=-(itemWidth+spaceWidth)*currleftitem;
            
                    
            if (holderDiv.slideint){window.clearInterval(holderDiv.slideint);holderDiv.slideint=null;}

                holderDiv.slideint=window.setInterval(
                    function(){                               
                        if (currpos > targetpos) {
                            step = -Math.ceil(Math.sqrt(Math.abs(targetpos-currpos)*speed));
                        }
                        else if (currpos < targetpos){
                            step = Math.abs(Math.sqrt(Math.abs(targetpos-currpos)*speed));
                        }
                        
                        currpos+=step;
                        
                        if ((step<0 && currpos<targetpos) || (step>0 && currpos>targetpos)) { 
                            currpos=targetpos;             
                        }
                        
                        holderDiv.scrollLeft = -currpos;
                        if (attachedDivs){
                            for (i=0;i<attachedDivs.length;i++){
                                attachedDivs[i].scrollLeft= holderDiv.scrollLeft;
                            }
                        }
                        
                        if (currpos==targetpos){
                            window.clearInterval(holderDiv.slideint);
                            holderDiv.slideint = null;
                        }
                    }            
                    ,24
                );
            }
            
        } 
    } 

