/*

	ivScrollable is an easy to use content scroller based on the jquery framework
	it works with ajax applications, you can initialise it as often as needed by calling this: 
		window.setTimeout("objIvScrollable.ivMakeScrollable();", 200);
	(it is recommend to use the timeout to prevent timing problems with internet explorer) 
	
	Author: Ingo Volkmann www.godiv.de
	
	set the variables at the top of the function according to your needs

	include these files in the head section of your website after the jquery include
		<script type="text/javascript" language="javascript" src="scripts/iv_scrollable.js"></script>
		<link rel="stylesheet" type="text/css" href="styles/iv_scrollable.css" />
	
	the element which contains the content you'll scroll needs the class="ivScrollable" and a fix height
	
*/


function ivScrollable () {
	
	this.ivUpImagePath = 'assets/images/up.png';				// path to the arrow up image
	this.ivDownImagePath = 'assets/images/down.png';			// path to the arrow down image
	this.ivUpImageAltText = 'up';						// is shown, if the up arrow image is missing
	this.ivDownImageAltText = 'down';					// is shown, if the down arrow image is missing
	this.ivScrollLineHeight = 1;						// margin/padding top/bottom, line-height/height of each element inside the elements with 
														//	  the class="ivScrollable" must be a multiple of this value for scrolling line by line
	this.ivScrollLineHeightSetAutomaticHeights = false;	// true or false - true means: if you want the script resizing your scrollable content to 
														// 	  scroll line by line - the line-height will get the value of ivScrollLineHeight
	this.ivScrollLineDelay = 10;						// defines the delay between each line is shown - do not set it to less than 5
	this.ivCallback = '';								// define a callback function which is called after the scrollable element has been loaded 
														//	  - leave it blank if not needed
	this.ivCallbackDelay = 200;							// set a delay to call the callback function to prevent timing problems
	
	
	/* 
	
		DO NOT CHANGE ANYTHING BELOW THIS EXCEPT YOU REALLY KNOW WHAT YOU ARE DOING 
		
	*/

	this.ivScroll = false;
	if ($.trim(this.ivCallback) == '') {
		this.ivCallback = 'sndflkgjasbkjb';
	}

	this.ivMakeScrollable = function () {
		$(".ivScrollable").each(function(i){
			var origHtml = $(this).html();
			// look if the element is already scrollable
			if (origHtml.indexOf('class="scTdUp"') == -1) {
				var parWidth = $(this).width();
				var parHeight = $(this).height();
				var now = new Date();
				var timestamp = now.getTime();
				$(this).empty();
				var newHtml = '<div id="ivScContent' + timestamp + '" class="ivScContent" style="width:' + (parWidth - 30) + 'px;">';
					newHtml+= origHtml;
					newHtml+= '</div><div class="ivScButton" style="height:' + parHeight + 'px;"><div class="ivScDivBtnUp"><a href="#" onmouseover="objIvScrollable.ivInitScroll(\'Down\', \'' + timestamp + '\'); return false;" onmouseout="objIvScrollable.ivStopScrolling(); return false;" onclick="this.blur(); return false;"><img src="' + objIvScrollable.ivUpImagePath + '" alt="' + objIvScrollable.ivUpImageAltText + '" border="0" /></a></div><div class="ivScDivBtnDown"><a href="#" onmouseover="objIvScrollable.ivInitScroll(\'Up\', \'' + timestamp + '\'); return false;" onmouseout="objIvScrollable.ivStopScrolling(); return false;" onclick="this.blur(); return false;"><img src="' + objIvScrollable.ivDownImagePath + '" alt="' + objIvScrollable.ivDownImageAltText + '" border="0" /></a></div></div>';
				$(this).html(newHtml);
				eval("if (typeof " + objIvScrollable.ivCallback + " == 'function') { window.setTimeout(\"" + objIvScrollable.ivCallback + "();\", " + objIvScrollable.ivCallbackDelay + "); }");
				if (objIvScrollable.ivScrollLineHeightSetAutomaticHeights) {
					objIvScrollable.ivSetHeights(timestamp);
				}
			}		
		});
	}
	
	this.ivSetHeights = function (timestamp) {
		var arrStyles = new Array('marginTop', 'marginBottom', 'paddingTop', 'paddingBottom', 'height');
		$('#ivScContent' + timestamp + ' *').css('lineHeight', this.ivScrollLineHeight + 'px');
		var el = document.getElementById('ivScContent' + timestamp).getElementsByTagName('*');
		for (var i = 0; i < el.length; i++) {
			for (var a = 0; a < arrStyles.length; a++) {
				var styleValue = parseInt($(el[i]).css(arrStyles[a]));
				if (styleValue > 0) {
					var multiple = Math.ceil(styleValue / this.ivScrollLineHeight);
					$(el[i]).css(arrStyles[a], (this.ivScrollLineHeight * multiple) + 'px');
				}
			}
		}
	}

	this.ivScrollUp = function (timestamp, init) {
		if (init) {
			this.ivScroll = true;
		}
		var fullHeight = $('#ivScContent'+timestamp).height();
		var visibleHeight = $('#ivScContent'+timestamp).parent().height();
		var scrolledHeight = parseInt($('#ivScContent'+timestamp).css('marginTop'));
		if (isNaN(scrolledHeight)) {
			scrolledHeight = 0;
		}
		var heightToScrollLeft = fullHeight - (visibleHeight - scrolledHeight);
		if (heightToScrollLeft > -20) {
			var margin = scrolledHeight - this.ivScrollLineHeight;
			$('#ivScContent'+timestamp).css('margin-top', margin+'px');
			if (this.ivScroll) {
				window.setTimeout("objIvScrollable.ivScrollUp(" + timestamp + ");", this.ivScrollLineDelay);
			}
		}
	}

	this.ivScrollDown = function (timestamp, init) {
		if (init) {
			this.ivScroll = true;
		}
		var scrolledHeight = parseInt($('#ivScContent'+timestamp).css('margin-top'));
		if (scrolledHeight < 0) {
			var margin = scrolledHeight + this.ivScrollLineHeight;
			$('#ivScContent'+timestamp).css('margin-top', margin+'px');
			if (this.ivScroll) {
				window.setTimeout("objIvScrollable.ivScrollDown(" + timestamp + ");", this.ivScrollLineDelay);
			}
		}
	}

	this.ivStopScrolling = function () {
		this.ivScroll = false;
	}

	this.ivInitScroll = function  (dir, timestamp) {
		window.setTimeout("objIvScrollable.ivScroll" + dir + "(" + timestamp + ", true);", 100);
	}
	
}

var objIvScrollable = new ivScrollable();
/*
$(document).ready(function(){
	
	window.setTimeout("objIvScrollable.ivMakeScrollable();", 200);
	
});
*/

