/* 

	Easy Scroll v1.0
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	please visit http://cssglobe.com/post/1495/easy-scroll-accessible-content-scroller
	
*/

function insertAfter(newElement,targetElement) {

    //target is what you want it to go after. Look for this elements parent.	
    var parent = targetElement.parentNode;
    
    //if the parents lastchild is the targetElement...
    if(parent.lastchild == targetElement) {	
    //add the newElement after the target element.	
    parent.appendChild(newElement);	
    } else {	
    // else the target has siblings, insert the new element between the target and it's next sibling.	
    parent.insertBefore(newElement, targetElement.nextSibling);	
    }

}


this.easyscroll = function(){
	
    // id of the container element 
    var id = "scroll";
    
    // navigation buttons text
    var nav = ["<img src='../img/galeria-buton-up.jpg' border='0' />", "<img src='../img/galeria-buton-down.jpg' border='0' />"];
    
    //	id for each navigation button (OPTIONAL)
    var navId = ["btnUp", "btnDown"];

    // movement speed
    var speed = 5;
    
    // desired height of the container element (in pixels)
    var height = 317;
    
    //
    // END CONFIG
    // do not edit below this line (unless you want to of course :) )
    //

    var obj = document.getElementById(id);
    
    if ( obj )
    {
    
        obj.up = false;
        obj.down = false;
        obj.fast = false;
        
        var container = document.createElement("div");
        var parent = obj.parentNode;
        container.id="easyscroll";
        parent.insertBefore(container,obj);
        parent.removeChild(obj);	
        
        container.style.position = "relative";
        container.style.height = height + "px";
        container.style.overflow = "hidden";
        obj.style.position = "absolute";
        obj.style.top = "0";
        obj.style.left = "0";
        container.appendChild(obj);
        
        var btns = new Array();
        var ul = document.createElement("ul");
        ul.id="easyscrollnav";
        var li = document.createElement("li");
                li.innerHTML = nav[0];
                li.id = navId[0];
                btns.push(li);
                ul.appendChild(li);
                
        parent.insertBefore(ul,container);

        var ul = document.createElement("ul");
        ul.id="easyscrollnav2";
        var li = document.createElement("li");
            li.innerHTML = nav[1];
            li.id = navId[1];
            btns.push(li);
            ul.appendChild(li);
        insertAfter(ul,container);
        
        btns[0].onmouseover = function(){
                obj.up = true;		
        };
        btns[0].onmouseout = function(){
                obj.up = false;		
        };		
        btns[1].onmouseover = function(){
                obj.down = true;			
        };
        btns[1].onmouseout = function(){
                obj.down = false;		
        };		
        btns[0].onmousedown = btns[1].onmousedown = function(){
                obj.fast = true;
        };	
        btns[0].onmouseup = btns[1].onmouseup = function(){
                obj.fast = false;
        };		
        
                
        this.start = function(){				
            var newTop;
            var objHeight = obj.offsetHeight;
            var top = obj.offsetTop;
            var fast = (obj.fast) ? 2 : 1;
            if(obj.down){		 
                newTop = ((objHeight+top) > height) ? top-(speed*fast) : top;	
                obj.style.top = newTop + "px";
            };	
            if(obj.up){		 
                newTop = (top < 0) ? top+(speed*fast) : top;
                obj.style.top = newTop + "px";
            };
        };	
        obj.interval = setInterval("start()",50);		
    }
};


//
// script initiates on page load. 
//

this.addEvent = function(obj,type,fn){
    if(obj.attachEvent){
        obj['e'+type+fn] = fn;
        obj[type+fn] = function(){obj['e'+type+fn](window.event );}
        obj.attachEvent('on'+type, obj[type+fn]);
    } else {
        obj.addEventListener(type,fn,false);
    };
};
addEvent(window,"load",easyscroll);
