﻿// BUILD SCROLLS ------------------------------------------------------------

//addOnLoad('init_scroller()');

// 1. Då dom.ready inte fungerar korrekt använder ie en egen version.
if(!jQuery.browser.msie)
    addFunctionToDomReady('init_scroller()');


var scroll_scrolls = new Array();
var mouseY = 0;

function init_scroller() {
	var allDivs = document.getElementsByTagName('DIV');
	j = 1;
	for (i = 0; i < allDivs.length; ++i) {

		if( ( allDivs[i].className == 'scroll' && allDivs[i].offsetHeight > 155 ) || allDivs[i].className == 'scroll_active' ) {
			allDivs[i].id = 'scroll_' + j;
			var firstContent = allDivs[i].getElementsByTagName("DIV");
			if (firstContent.length == 0)
				continue; 
			firstContent[0].id = 'scroll_' + j + '_content'; ++j;			
		}
	}
	for( i = 1 ; i < j ; ++i ) {
		scroll_scrolls[i] = new scroller_scroll(i);
	}
	if( !document.all ) {	document.captureEvents( Event.MOUSEMOVE | Event.MOUSEUP ) }
	document.onmousemove = scroller_getMouse;
	document.onmouseup = scroller_run.stop_drag;
}

function scroller_getMouse(e) {
	if( document.all ) {
		mouseY = event.clientY + document.documentElement.scrollTop;
	} else if ( e ) {
		mouseY = e.pageY;
	}
}

function scroller_scroll(thisScrollNum) {
	this.scroll = document.getElementById('scroll_' + thisScrollNum);
	this.scroll.className = 'scroll_active';
	
	// Set content attributes
	this.content = this.scroll.getElementsByTagName("DIV")[0];
	this.content.id = 'scroll_' + i + '_content';
	this.content.className = 'scroll_content_active';
	this.content.pos = 0;
	
	// Create list
	this.list = document.createElement('DIV');
	this.list.id = 'scroll_' + i + '_list';
	this.list.num = i;
	this.list.className = 'scroll_list';
	this.list.onclick = scroller_run.list;
	this.scroll.appendChild(this.list);
	
	// Create up-button
	this.up = document.createElement('DIV');
	this.up.id = 'scroll_' + i + '_up';
	this.up.num = i;	
	this.up.className = 'scroll_up';
	this.up.onmousedown = scroller_run.up;
	this.up.onmouseout 	= scroller_run.stop;	
	this.up.onmouseup 	= scroller_run.stop;		
	this.list.appendChild(this.up);	
	
	// Create down-button
	this.down = document.createElement('DIV');
	this.down.id = 'scroll_' + i + '_down';
	this.down.num = i;
	this.down.className = 'scroll_down';
	this.down.onmousedown = scroller_run.down;
	this.down.onmouseout 	= scroller_run.stop;
	this.down.onmouseup 	= scroller_run.stop;	
	this.list.appendChild(this.down);	
	
	// Create indicator
	this.indicator = document.createElement('DIV');
	this.indicator.id = 'scroll_' + i + '_indicator';
	this.indicator.num = i;
	this.indicator.speed = 0;
	this.indicator.pos = 18; // Change this if the CSS is changed
	this.indicator.className = 'scroll_indicator';
	this.indicator.onmousedown = scroller_run.drag_start;
	this.list.appendChild(this.indicator);
}

var scroller_run = {
	// The variabel named "num" that keeps appearing in different areas of the script
	// is used to define which scroll that is currently active. This is only useful
	// if you have multiple scrolls on one and the same page but is still necessary
	// for the script to work.

	indicatorMin: 18, // Lowest allowed position for the indicator
	indicatorMax: 108, // Highest allowed position for the indicator
	indicatorMaxDif: 48, // The amount that is to be taken off the scroll-list height and set the highets allowed position for the indicator
	contentSpeed: 6, // Scroll speed
	contentSpeedList: 20, // Scroll speed when you click on the list
	pageSize: 25, // When you scroll by clicking the list this variable determines how much will be showed of the previous page
	interval: 40, // Interval speed
	tInterval: 10, // Timeout speed
	state: 0,
	runing: 0, // Indicates whether the scroll is running or not.
	runing_drag: 0, // Indicates whether the scroll is beign draged or not.	
	timer: null,
	timer_drag: null,
	
	play: function() {
		scroller_run.runing = 1; // Do I even use this?
	},
	
	stop: function() {
		clearInterval(scroller_run.timer);
		scroller_run.runing = 0; // Do I even use this?
	},
	
	stop_drag: function() {
		scroller_run.runing_drag = 0; // Do I even use this?
	},	
	
	// If you click the down-arrow
	down: function() {
		scroller_run.timer 	= setInterval('scroller_run.nextFrame(' + this.num + ')', scroller_run.interval);
	},

	// If you click the up-arrow	
	up: function() {
		scroller_run.timer 	= setInterval('scroller_run.previousFrame(' + this.num + ')', scroller_run.interval);
	},	
	
	// If you start draging the indicator
	drag_start: function() {
		scroller_run.runing_drag = 1;
		scroller_run.timer_drag = setTimeout('scroller_run.drag(' + this.num + ')', scroller_run.tInterval);
		return false;		
	},
	
	// If you keep draging the indicator
	drag: function(num) {
		var scroll 		= document.getElementById('scroll_' + num);
		var content 	= document.getElementById('scroll_' + num + '_content');		
		var indicator = document.getElementById('scroll_' + num + '_indicator');
		if( scroller_run.runing_drag == 1 ) {
			indicator.pos = ( mouseY - scroll.offsetTop - ( indicator.offsetHeight / 2 ) );
			content.pos = ( ( indicator.pos / ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) ) * ( content.offsetHeight - scroll.offsetHeight ) ) * -1;

			// Minimum reach
			if( indicator.pos < scroller_run.indicatorMin ) {
				indicator.pos = scroller_run.indicatorMin;
				content.pos = 0;
			}
			// Maximum reach			
			else if( indicator.pos > ( scroll.offsetHeight - scroller_run.indicatorMaxDif )  ) {
				indicator.pos = ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) ;
				content.pos = ( content.offsetHeight - scroll.offsetHeight ) * -1;
			}
			indicator.style.top = indicator.pos + 'px';
			content.style.top = content.pos + 'px';
			scroller_run.timer_drag = setTimeout('scroller_run.drag(' + num + ')', scroller_run.tInterval);	
		}
		return false;
	},
	
	// If you click on the list
	list: function() {
		var scroll = document.getElementById('scroll_' + this.num);
		var indicator = document.getElementById('scroll_' + this.num + '_indicator');
				indicatorTop = indicator.offsetTop + indicator.parentNode.parentNode.offsetTop;
				minTop = indicator.parentNode.parentNode.offsetTop + scroller_run.indicatorMin;
				maxTop = indicator.parentNode.parentNode.offsetTop + ( scroll.offsetHeight - scroller_run.indicatorMaxDif )  + indicator.offsetHeight;
				thisScroll = document.getElementById('scroll_' + this.num);
				numOfSteps = ( thisScroll.offsetHeight - scroller_run.pageSize ) / scroller_run.contentSpeed;
		//alert( 'mouseY: ' + mouseY + ' indicatorTop: ' + indicatorTop + ' indicator.offsetHeight: ' + indicator.offsetHeight + ' maxTop: ' + maxTop );
		if( mouseY > ( indicatorTop + indicator.offsetHeight ) && mouseY < maxTop ) {
			// Under the indicator
			scroller_run.timer = setTimeout('scroller_run.list_next(' + this.num + ', numOfSteps)', scroller_run.tInterval);

		} else if( mouseY < ( indicatorTop ) && mouseY > minTop ) {
			// Over the indicator
			scroller_run.timer = setTimeout('scroller_run.list_previous(' + this.num + ', numOfSteps)', scroller_run.tInterval);
		}
	},
	
	// If you clicked below the indicator
	list_next: function(num,steps) {
		if( steps > 0 ) {
			scroller_run.nextFrame(num,1);
			setTimeout('scroller_run.list_next(' + num + ', ' + ( --steps ) + ')', scroller_run.tInterval);
		} else {
			clearTimeout(scroller_run.timer);
		}
	},
	
	// If you clicked abowe the indicator	
	list_previous: function(num,steps) {
		if( steps > 0 ) {
			scroller_run.previousFrame(num,1);
			setTimeout('scroller_run.list_previous(' + num + ', ' + ( --steps ) + ')', scroller_run.tInterval);
		} else {
			clearTimeout(scroller_run.timer);
		}
	},	
	
	// Moves the scroll content up	
	nextFrame: function(num,onList) {
		var indicator 		= document.getElementById('scroll_' + num + '_indicator');
		var content				= indicator.parentNode.parentNode.getElementsByTagName("DIV")[0];
				contentTop 		= content.offsetTop;
		var scroll 				= document.getElementById('scroll_' + num);
				scrollHeight 	= scroll.offsetHeight;
				if( indicator.speed == 0 ) {
					indicator.speed = ( ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) - scroller_run.indicatorMin ) / ( ( content.offsetHeight - scrollHeight ) / scroller_run.contentSpeed );
				}
		if( indicator.offsetTop < ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) && indicator.offsetTop >= scroller_run.indicatorMin ) {
			indicator.pos = indicator.pos + indicator.speed;
			indicator.style.top = indicator.pos  + 'px';
			content.style.top = ( contentTop - scroller_run.contentSpeed ) + 'px';
		}
		if( indicator.offsetTop > ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) - ( indicator.speed - 1 ) || content.style.top <= content.offsetHeight ) {
			indicator.pos = ( scroll.offsetHeight - scroller_run.indicatorMaxDif ); indicator.style.top = indicator.pos + 'px';
		}	
	},
	
	// Moves the scroll content down	
	previousFrame: function(num,onList) {
		var indicator 		= document.getElementById('scroll_' + num + '_indicator');
		var content				= indicator.parentNode.parentNode.getElementsByTagName("DIV")[0];
				contentTop 		= content.offsetTop;
		var scroll 				= document.getElementById('scroll_' + num);
				scrollHeight 	= scroll.offsetHeight;
				if( indicator.speed == 0 ) {
					indicator.speed = ( ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) - scroller_run.indicatorMin ) / ( ( content.offsetHeight - scrollHeight ) / scroller_run.contentSpeed );
				}
		if( ( indicator.offsetTop <= ( scroll.offsetHeight - scroller_run.indicatorMaxDif ) && indicator.offsetTop > scroller_run.indicatorMin ) ) {
			indicator.pos = indicator.pos - indicator.speed;
			indicator.style.top = indicator.pos  + 'px';
			content.style.top = ( contentTop + scroller_run.contentSpeed ) + 'px';
		}
		if( indicator.offsetTop < scroller_run.indicatorMin + ( indicator.speed - 1 ) || content.style.top >= 0 ) { 
			indicator.pos = scroller_run.indicatorMin; indicator.style.top = indicator.pos + 'px';
		}
	}	
}