/**
 * scrollFrame.js
 * JavaScript used in the information panel pages for musicforbodies.net
 * @author Peter Edwards <tech@e-2.org>
 * @version 1.0
 * @package m4b
 */
 
/**
 * scrollFrame
 * scroller object encapsulating all functions used for the scrollbars
 * @package m4b
 */
var scrollFrame = {
    /**
		 * initialisation function (constructor)
		 */
		init:   function(docH)
		{
				scrollFrame.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
        /**
				 * height of scroller on scrollbar
				 * if an image is used for the scroller, set to the height of the image.
				 * Otherwise, use the following to set the size of the scroller automatically:
				 * scrollFrame.scrollH = (scrollFrame.contH * scrollFrame.scrollAreaH) / scrollFrame.docH;
         * if (scrollFrame.scrollH < 15) scrollFrame.scrollH = 15;
				 */
        scrollFrame.scrollH = 9;
        document.getElementById("scroller").style.height = Math.round(scrollFrame.scrollH) + "px";
        
        // what is the effective scroll distance once the scoller's height has been taken into account
        scrollFrame.scrollDist = Math.round(scrollFrame.scrollAreaH - scrollFrame.scrollH);
        
        // make the scroller div draggable
        Drag.init(document.getElementById("scroller"), null, 0, 0, 0, scrollFrame.scrollDist);
				
				// make the panel draggable
        var test1 = document.body.scrollWidth;
        var test2 = document.body.offsetWidth;
				var minX = 30;
				var minY = 30;
				var maxY = 400;
        if (test1 > test2) {
				    maxX = document.body.scrollWidth - 50;
        } else {
				    maxX = document.body.offsetWidth - 50;
        }
				//alert("minX: "+minX+"\nmaxX: "+maxX+"\nminY: "+minY+"\nmaxY: "+maxY);
				Drag.init(document.getElementById("panel-dragbar"), document.getElementById("panel"), minX, maxX, minY, maxY);
				Drag.init(document.getElementById("staticpanel-dragbar"), document.getElementById("staticpanel"), minX, maxX, minY, maxY);
    
				// setup dragging on the frame
				scrollFrame.setup(docH);
				
				// add up button click functions
				document.getElementById("upbutton").onmousedown = scrollFrame.up;
				document.getElementById("upbutton").onmouseup = scrollFrame.stop;
				// add down button click functions
				document.getElementById("downbutton").onmousedown = scrollFrame.down;
				document.getElementById("downbutton").onmouseup = scrollFrame.stop;
    },
    setup: function(docH)
		{
        // cancel the event capturing if present
				document.getElementById("scroller").onDrag = null;
				// reset the position of the scrollbar slider
				document.getElementById("scroller").style.top = 0;
				scrollFrame.docH = docH;
        scrollFrame.contH = 420;
				scrollFrame.currentY = 0;
				scrollFrame.currentDocY = 0;
        // add ondrag function
        document.getElementById("scroller").onDrag = function (x,y) {
            var scrollY = parseInt(document.getElementById("scroller").style.top);
            var docY = (scrollY * (scrollFrame.docH - scrollFrame.contH) / scrollFrame.scrollDist);
						frames['panel-frame'].scrollTo(0, docY);
						scrollFrame.currentDocY = docY;
						scrollFrame.currentY = scrollY;
        }
		},
		to: null,
		/**
		 * up
		 * mousedown action for the up button. Called repeatedly using a timeout until
		 * the mouseup event fires.
		 */
		up: function()
		{
        if (scrollFrame.currentDocY == 0) {
						scrollFrame.stop();
						return;
				} else {
				    // set the position of the scroller
						var scrollY = ((scrollFrame.currentY - 1) <= 0)? 0: scrollFrame.currentY - 1;
						document.getElementById("scroller").style.top = scrollY + "px";
						scrollFrame.currentY = scrollY;
						// set the position of the scroling frame
            var docY = (scrollY * (scrollFrame.docH - scrollFrame.contH) / scrollFrame.scrollDist);
						frames['panel-frame'].scrollTo(0, docY);
						scrollFrame.currentDocY = docY;
				    scrollFrame.to = setTimeout("scrollFrame.up()", 10);
				}
		},
		/**
		 * down
		 * mousedown action for the down button. Called repeatedly using a timeout until
		 * the mouseup event fires.
		 */
		down: function()
		{
        var currentY = parseInt(document.getElementById("scroller").style.top);
				currentY = isNaN(currentY)? 0: currentY;
        var textY = scrollFrame.currentDocY;//0 - parseInt(document.getElementById("scrollingText").style.top);
				var maxY = scrollFrame.docH - scrollFrame.contH;
				textY = isNaN(textY)? 0: (textY >= maxY)? maxY: textY;
				if (scrollFrame.currentY == scrollFrame.scrollDist) {
						scrollFrame.stop();
						return;
				} else {
						// set the position of the scrollbar
						var scrollY = ((scrollFrame.currentY + 1) > scrollFrame.scrollDist)? scrollFrame.scrollDist: currentY + 1;
						document.getElementById("scroller").style.top = scrollY + "px";
						scrollFrame.currentY = scrollY;
						// set the position of the scrolling frame
            var docY = (scrollY * (scrollFrame.docH - scrollFrame.contH) / scrollFrame.scrollDist);
						frames['panel-frame'].scrollTo(0, docY);
						scrollFrame.currentDocY = docY;
				    scrollFrame.to = setTimeout("scrollFrame.down()", 10);
				}
		},
    /**
		 * stop
		 * mouseup action for up and down buttons. Cancels timeout
		 */		
		stop: function()
		{
		    clearTimeout(scrollFrame.to);
		}
}

