var instr_timer = 0;
var instrList = [];
function prepareInstrList(){
    var list = document.getElementById("instr_box");
    if(list && typeof list == "object"){
        var elems = list.firstChild;
        while(elems){
            if(typeof elems == "object" && elems.nodeType === 1 && elems.nodeName.toLowerCase() == "div"){
                if(elems.className == "instruments"){
                    elems.id = "idforinstr";
                    var holder = elems.firstChild;
                    while(holder){
                        if(typeof holder == "object" && holder.nodeType === 1 && holder.nodeName.toLowerCase() == "div"){
                            if(holder.className == "inst_holder"){
                                holder.style.display = "block";                                 
                                holder.style.width = getRealWidth(holder) + "px";
                                prepareInstrArray(holder);
                            }
                        }
                        holder = holder.nextSibling;
                    }
                }
                if(elems.className == "arrow_right"){
                    elems.onmouseover = function(){
                        this.className = "arrow_right_hover";
                        /*clearInterval(instr_timer);                                                
                        instr_timer = setInterval("moveInstrList('r')",5);*/                        
                    }
                    elems.onmouseout = function(){
                        this.className = "arrow_right";
                        /*clearInterval(instr_timer);*/
                    }    
                    elems.onclick = function(){
                        moveInstrRight();
                    }                
                }
                if(elems.className == "arrow_left"){
                    elems.onmouseover = function(){
                        this.className = "arrow_left_hover";
/*                        clearInterval(instr_timer);                                                
                        instr_timer = setInterval("moveInstrList('l')",5);*/                        
                    }
                    elems.onmouseout = function(){
                        this.className = "arrow_left";
                    /*    clearInterval(instr_timer);*/
                    }   
                    elems.onclick = function(){
                        moveInstrLeft();
                    }                                      
                }                
            }
            elems = elems.nextSibling;
        }
        var corr = document.getElementById("idforinstr");
        if(corr && typeof corr == "object"){
            if(instrList && instrList instanceof Array && instrList.length > 0){
                corr.scrollLeft = instrList[0][0]; 
            }else{
                corr.scrollLeft = 0;
            }
        }
    }
}

function moveInstrRight(){
    var main = document.getElementById("idforinstr");
    var found = false;
    if(main && typeof main == "object" && instrList && instrList instanceof Array && instrList.length > 0){
        for(var i=0; i<instrList.length; i++){
            if(!found && (parseInt(instrList[i][0], 10) + parseInt(instrList[i][1], 10)) > (parseInt(main.scrollLeft, 10) + parseInt(main.offsetWidth, 10))){
                main.scrollLeft = ((parseInt(instrList[i][0], 10) + parseInt(instrList[i][1], 10)) - parseInt(main.offsetWidth, 10));
                found = true;
            }
        }
    }
}

function moveInstrLeft(){
    var main = document.getElementById("idforinstr");
    var found = false;
    if(main && typeof main == "object" && instrList && instrList instanceof Array && instrList.length > 0){
        for(var i=instrList.length-1; i>=0; i--){
            if(!found && parseInt(main.scrollLeft, 10) > parseInt(instrList[i][0], 10)){
                main.scrollLeft = parseInt(instrList[i][0], 10);
                found = true;
            }
        }
    }
}

function prepareInstrArray(obj){
    var w = 0;    
    if(obj && typeof obj == "object" && instrList && instrList instanceof Array){
        var list = obj.firstChild;        
        while(list){
            if(list.nodeType === 1 && !isNaN(parseInt(list.offsetWidth)) && list.className !== "clear_style"){
                if(window.getComputedStyle){
                    w += !isNaN(parseInt(window.getComputedStyle(list, null).marginLeft))?parseInt(window.getComputedStyle(list, null).marginLeft):0;
                    w += !isNaN(parseInt(window.getComputedStyle(list, null).marginRight))?parseInt(window.getComputedStyle(list, null).marginRight):0;                    
                }else{
                    w += !isNaN(parseInt(list.currentStyle.marginLeft))?parseInt(list.currentStyle.marginLeft):0; 
                    w += !isNaN(parseInt(list.currentStyle.marginRight))?parseInt(list.currentStyle.marginRight):0;
                }                
                instrList.push([w,list.offsetWidth]);
                w += parseInt(list.offsetWidth);                
            }
            list = list.nextSibling;
        }
    }
}


function moveInstrList(act){
    var get_scroll = document.getElementById("idforinstr");
    if(get_scroll && typeof get_scroll == "object" && act && (act === "l" || act === "r")){
        if(act === "l"){get_scroll.scrollLeft -= 5;}
        if(act === "r"){get_scroll.scrollLeft += 5;}            
    }
}



