/**
 *  functionality which makes every content scrollable if it is in the div ivScrollable
 */



var ivUpImagePath 		= 'fileadmin/images/scrollable/up.gif';
var ivDownImagePath 	= 'fileadmin/images/scrollable/down.gif';
var ivUpImageAltText 	= 'up';
var ivDownImageAltText 	= 'down';
var ivScrollLineHeight 	= 15;	// 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
var ivScrollLineDelay 	= 300;	// not less than 5
var ivCallback 			= 'initializeFaq';
var ivCallbackDelay 	= 200;

var ivScrollDownFlag 	= 0;
var ivScrollUpFlag 		= 0;
var weHaveBeenAtDown 	= 0;


function ivMakeScrollable() {
	$(".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).parent().attr("id");
			$(this).empty();

			var buttonHeight = parHeight;
			var specificBox = 0;
			
			if(parHeight == 0 || $(this).css("height") != '') {
				var parentHeightClosedElement = $(this).css("height");
				parentHeightClosedElement.toString();
				parHeight = parentHeightClosedElement.substr(0, eval(parentHeightClosedElement.length - 2));
				
				specificBox = 1;
				buttonHeight = eval(parHeight - 10);
			}
			
			
			var newHtml = '<div id="ivScContent' + timestamp + '" class="ivScContent" style="width:' + (parWidth - 20) + 'px;">';
			
				newHtml+= origHtml;
				newHtml+= '</div><div class="ivScButton" style="height:' + buttonHeight + 'px;"><div class="ivScDivBtnUp"><a href="#" onclick="return false;" id="scrollUp' + timestamp + '"><img src="' + ivUpImagePath + '" alt="' + ivUpImageAltText + '" border="0" /></a></div><div class="ivScDivBtnDown"><a href="#" id="scrollDown' + timestamp + '" onclick="return false;"><img src="' + ivDownImagePath + '" alt="' + ivDownImageAltText + '" border="0" /></a></div></div>';
			$(this).html(newHtml);
			
			//if we don't need the scrollbuttons, delete them again..
			//we do it in this ugly way because we experiecned problems in IE6 setting the buttons into the HTML only if needed..
			if($('#ivScContent' + timestamp).height() < parHeight && !$(this + ":parent").hasClass("slideContent") && !jQuery.browser.msie) {
				$(".ivScButton").html('');
				$('div#ivScContent' + timestamp).unbind('mousewheel');
			}

			
			$("#scrollUp" + timestamp).mouseup(function(){
				ivStopScrolling();
		    }).mousedown(function(){
				ivScrollUp(timestamp, true);
		    });
			
		
			$("#scrollDown" + timestamp).mouseup(function(){
				ivStopScrolling();
		    }).mousedown(function(){
				ivScrollDown(timestamp, true);
		    });
			
			
			// initiate scrolling via mousescroll
		    $('div#ivScContent' + timestamp).bind('mousewheel', function(event, delta) {

	            var dir = delta > 0 ? 'up' : 'down',
	                vel = Math.abs(delta);

				if(dir == 'down') {
					ivScrollDown(timestamp, false);
				} else if (dir == 'up') {
					ivScrollUp(timestamp, false);
				}

	            return false;

			});

			
			eval("if (typeof " + ivCallback + " == 'function') { window.setTimeout(\"" + ivCallback + "();\", " + ivCallbackDelay + "); }");
		}
		
	});
}


/**
 *  scroll up function
 */

function ivScrollDown (timestamp, init) {

	if (init) {
		ivScrollDownFlag = 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) {
		margin = scrolledHeight - ivScrollLineHeight;
		
		$('#ivScContent'+timestamp).css('margin-top', margin+'px');
		if (ivScrollDownFlag) {
			if (weHaveBeenAtDown) {
				window.setTimeout("ivScrollDown('" + timestamp + "', false);", ivScrollLineDelay);
			} else {
				isScrollDownActive(timestamp);
			}
		}
	}
}


function isScrollDownActive(timestamp) {
	setTimeout('returnIsScrollDownActive(\'' + timestamp + '\')', 300);
}

function returnIsScrollDownActive(timestamp) {
	if(ivScrollDownFlag) {
		weHaveBeenAtDown = true;
		window.setTimeout('ivScrollDown(\'' + timestamp + '\', false);', ivScrollLineDelay);
	}
}


/**
 *  scroll down function
 */
function ivScrollUp (timestamp, init) {
	
	if (init) {
		ivScrollUpFlag = true;
	}
	
	var scrolledHeight = parseInt($('#ivScContent'+timestamp).css('margin-top'));
	if (scrolledHeight < 0) {
		margin = scrolledHeight + ivScrollLineHeight;
		$('#ivScContent'+timestamp).css('margin-top', margin+'px');
		
		if (ivScrollUpFlag) {
			if (weHaveBeenAtUp) {
				window.setTimeout("ivScrollUp('" + timestamp + "', false);", ivScrollLineDelay);
			} else {
				isScrollUpActive(timestamp);
			}
		}
	}
}

function isScrollUpActive(timestamp) {
	setTimeout('returnIsScrollUpActive(\'' + timestamp + '\')', 300);
}

function returnIsScrollUpActive(timestamp) {
	if(ivScrollUpFlag) {
		weHaveBeenAtUp = true;
		window.setTimeout('ivScrollUp(\'' + timestamp + '\', false);', ivScrollLineDelay);
	}
}


/**
 * stop scrolling..
 */
function ivStopScrolling () {
	ivScrollDownFlag 	= 0;
	ivScrollUpFlag		= 0;
	
	weHaveBeenAtDown 	= 0;
	weHaveBeenAtUp 		= 0;
}

