// Initialize mouse tracking variables
var mouse_x;
var mouse_y;
var mouse_xRef;
var mouse_yRef;

// Initialize the variable so we can trakc the current scrollpane that is active in-case more than one exists.
var jam_currentScrollPane;

// Initialize/Create the custome jam_scrollPane object.
var jam_scrollPane = new Object();

// function to initialize and style each jam_scrollPane object
jam_scrollPane = function(
						  	myId, 
						  	myZ,
							myTop,
							myLeft, 
							myWidth, 
							myHeight, 
							myVis, 
							myBg, 
							btnCon_left, 
							btnCon_width, 
							btnCon_height, 
							btnCon_bg, 
							btn_width, 
							btn_height, 
							btn_bg, 
							cntPane_mskWidth, 
							cntPane_mskHeight, 
							cntPane_overFlw, 
							cntPane_background, 
							cntPane_font, 
							cntPane_fontColor, 
							cntPane_fontSize
							) {

	jam_scrollPane.ID = document.getElementById(myId + "_jam_scrollPane");
	jam_scrollPane.ID.style.zIndex = myZ;
	jam_scrollPane.ID.style.position = "absolute";
	jam_scrollPane.ID.style.top = myTop + "px";
	jam_scrollPane.ID.style.left = myLeft + "px";
	jam_scrollPane.ID.style.width = myWidth + "px";
	jam_scrollPane.ID.style.height = myHeight + "px";
	jam_scrollPane.ID.style.visibility = myVis;
	jam_scrollPane.ID.style.backgroundImage = myBg;
	jam_scrollPane.ID.style.overflow = "visible";
	jam_scrollPane.ID.initCount = parseInt(0);
	
	var scrollBtn_container = document.getElementById(myId + "_jam_scrollButtonContainer");
	scrollBtn_container.style.zIndex = myZ+1;
	scrollBtn_container.style.position = "absolute";
	scrollBtn_container.style.top = 0 + "px";
	scrollBtn_container.style.left = myWidth + btnCon_left + "px";
	scrollBtn_container.style.width = btnCon_width + "px";
	scrollBtn_container.style.height = btnCon_height + "px";
	scrollBtn_container.style.backgroundImage = btnCon_bg;
	
	var scrollBtn_obj = document.getElementById(myId + "_jam_scrollButton");
	scrollBtn_obj.style.position = "absolute";
	scrollBtn_obj.style.top = 0 + "px";
	scrollBtn_obj.style.left = 0 + "px";
	scrollBtn_obj.style.width = btn_width + "px";
	scrollBtn_obj.style.height = btn_height + "px";
	scrollBtn_obj.style.backgroundImage = btn_bg;
	scrollBtn_obj.style.cursor = "hand";
	scrollBtn_obj.style.cursor = "pointer";

	var contentPane = document.getElementById(myId + "_jam_contentPane");
	contentPane.style.zIndex = myZ+2;
	contentPane.style.position = "absolute";
	contentPane.style.top = 0 + "px";
	contentPane.style.left = 0 + "px";
	contentPane.style.width = myWidth + "px";
	contentPane.style.height = myHeight + "px";
	contentPane.style.clip = "rect(0, " + myWidth + ", " + myHeight + ", 0)";
	contentPane.style.overflow = cntPane_overFlw;
	contentPane.style.backgroundColor = cntPane_background;
	
	var contentScroll = document.getElementById(myId + "_jam_contentScroll");
	contentScroll.style.position = "absolute";
	contentScroll.style.top = 0 + "px";
	contentScroll.style.left = 0 + "px";
	contentScroll.style.width = + (myWidth - 20) + "px";
	contentScroll.style.paddingRigth = 20 + "px";
	contentScroll.style.backgroundColor = "";
	contentScroll.style.fontFamily = cntPane_font;
	contentScroll.style.color = cntPane_fontColor;
	contentScroll.style.fontSize = cntPane_fontSize + "px";
	
	
}

function getMouse(evt) {
	mouse_x = evt.clientX + "px";
	mouse_y = evt.clientY + "px";
	mouse_xRef = evt.clientX;
	mouse_yRef = evt.clientY;
}

function windowLoaded(evt, bool) {
	// prevent IE text selection while dragging!!! Little-known trick!
	document.body.ondrag = function () { return bool; };
	document.body.onselectstart = function () { return bool; };
}

var jam_scrollDelay = parseInt(0);

function scrollTracker(evt) {
	var evt = (evt) ? evt : ((window.event) ? event : null);
	
	getMouse(evt);
	windowLoaded(evt, false);

	// initialize scrollPane & tracking variables.
	var scrollPane = document.getElementById(jam_currentScrollPane+"_jam_scrollPane");
	var scrollBtn_container = document.getElementById(jam_currentScrollPane+"_jam_scrollButtonContainer");
	var scrollBtn_obj = document.getElementById(jam_currentScrollPane+"_jam_scrollButton");
	var contentPane = document.getElementById(jam_currentScrollPane+"_jam_contentPane");
	var contentScroll = document.getElementById(jam_currentScrollPane+"_jam_contentScroll");
	
	var scrollBtn_objMaxVert = parseInt(scrollBtn_obj.offsetTop + scrollBtn_obj.offsetHeight);
	var scrollBtn_start = scrollBtn_container.offsetTop;
	var scrollBtn_stop = parseInt(scrollBtn_container.offsetTop + scrollBtn_container.offsetHeight);
	var scrollBtn_offsetY = parseInt(scrollBtn_container.offsetHeight - scrollBtn_obj.offsetHeight);
	
	// Set in the class for init XY positioning (Only Occurs on init).
	if (scrollPane.initCount <= 0) {
		scrollPane.initCount++;
		scrollPane.contentScroll_initY = parseInt(contentScroll.offsetTop + contentScroll.offsetHeight) - parseInt(contentPane.offsetHeight);
		scrollPane.initOffset = Math.round(((scrollPane.contentScroll_initY/scrollBtn_offsetY)*scrollBtn_objMaxVert));
	}
	
	// Get the offsetY of the current scrollpane
	var contentScroll_offsetY = parseInt(contentScroll.offsetTop + contentScroll.offsetHeight) - parseInt(contentPane.offsetHeight);
	
	// Get the offset of the mouse in comparison to the obj
	var mouse_offsetY = parseInt(mouse_y) - parseInt(scrollPane.offsetTop) + parseInt(scrollBtn_obj.offsetHeight/2) - 3; //8.5;
	
	// Position drag_obj according to mouse pos
	if (mouse_yRef > (scrollBtn_start + 2) && mouse_offsetY < scrollBtn_stop) {
		var scrollBtn_newY = parseInt(mouse_y) - parseInt(scrollBtn_obj.offsetHeight/2) - parseInt(scrollPane.offsetTop) - 1;
		scrollBtn_obj.style.top = scrollBtn_newY + "px";
		
		
		var contentScroll_newY = Math.round(((scrollPane.contentScroll_initY/scrollBtn_offsetY)*scrollBtn_objMaxVert) - scrollPane.initOffset)*-1;
	    //alert(jam_currentScrollPane + "  |  " + initOffset);
		
		if (jam_scrollDelay % 50) {
			contentScroll.style.top = contentScroll_newY + "px";
		}
		jam_scrollDelay++;
	}
	
	// Check boundaries before moving anymore.
	if (scrollBtn_obj.offsetTop < scrollBtn_start) {
		scrollBtn_obj.style.top = parseInt(scrollBtn_container.offsetTop);
	}
	if (scrollBtn_objMaxVert > parseInt(scrollBtn_stop)) {
		scrollBtn_obj.style.top = parseInt(scrollBtn_stop - srcollBtn_obj.offsetHeight);
	}
	
	document.onmousemove = scrollTracker;
	document.onmouseup = function() {
		windowLoaded(evt, true);
		document.onmousemove = null; // addListeners;
	}
	
}

// Set all Recursive Functions.
// For Mozilla based browsers this function MUST be called within the HTML
function addListeners(evt) {
	// Must pass event through the onload function for all Mozilla type browsers to funciton properly
	// if browser event == True - use it, else if window.event use event else is null(do nothing);
	// Netscape, Mozilla, and Firefox activate using the "event" method IE, Safari and Opera use the window.event Method.
	var evt = (evt) ? evt : ((window.event) ? event : null);
	
	var mouse_x = evt.clientX + "px";
	var mouse_y = evt.clientY + "px";
	var mouse_xRef = evt.clientX;
	var mouse_yRef = evt.clientY;
	
	// Set event listeners.
	document.onmousemove = addListeners;
}