<!--

/************************************************
 * JavaScript Sliding Function v.0.2.1
 * (c) 2006 Oliver Hader <oh@inpublica.de>
 * Last modification: 2006-11-28
 * All rights reserved.
 ************************************************/

var ohSlider = {
	version: '0.2.1',
	objects: [],
	notNeccessaryFunc: null,
	stdClassName: '',

	addJSON: function(json) {
		for (var object in json) this.addObject(object, json[object]);
	},

	addObject: function(objectId, behaviour) {
		if (!behaviour) behaviour = 'top';
		if (this.isNeccessary(objectId))
			this.objects.push(new ohSliderObject(objectId, behaviour));
	},

	isNeccessary: function(objectId) {
		var isNeccessary = false;
		var object = document.getElementById(objectId);
		if (object && typeof object == 'object') {
			var parent = object.parentNode;
			isNeccessary = object.offsetHeight > parent.offsetHeight ? true : false;
			if (!isNeccessary && this.notNeccessaryFunc != null) this.notNeccessaryFunc(object);
		}
		return isNeccessary;
	},

	setStdClassName: function(className) {
		this.stdClassName = className;
	},

	getStdClassName: function() {
		return this.stdClassName;
	}
};

function ohSliderObject(objectId, behaviour) {
 	var ohSlider = this;
	this.doDebug = false;

	this.objectId = objectId;
	this.object = document.getElementById(objectId);
	this.slideStyle = behaviour == 'bottom' ? 'marginBottom' : 'marginTop';
	this.slideRef = null;

	this.scrollSpeed = 0;		// and float between [-1.00; +1.00] to indicate direction and speed
	this.scrollFactor = -5;		// pixel factor for each scroller call by internal timer
	this.scrollValue = 0;		// scrollValue = scrollSpeed*scrollFactor;
	this.scrollTimer = 50;		// value in milliseconds after which the slide function is triggered
	this.watchdog = null;

	this.eventListener = function(event) {
		if (!event) event = window.event;

		if (event.type == 'mousemove') {
			ohSlider.updateScrollValue(event.clientY);
			if (ohSlider.slideRef == null) ohSlider.slideRef = setInterval(function() { ohSlider.slideExecutor(); }, ohSlider.scrollTimer);

		} else if (event.type == 'mouseout') {
			if (!ohSlider.isInsideObjectBounding(event.clientX, event.clientY)) {
				ohSlider.killSlideTrigger();
			}
		}

		return false;
	}

	this.updateScrollValue = function(mousePosY) {
			// returns a value between [-1.00; +1.00] to indicate direction and speed
		this.scrollSpeed = (this.parentData.breaking-mousePosY)/this.parentData.delta;
		this.scrollValue = Math.round(this.scrollSpeed*this.scrollFactor);
	}

	this.killSlideTrigger = function() {
		if (this.slideRef != null) clearInterval(this.slideRef);
		this.slideRef = null;
	}

	this.slideExecutor = function() {
		var margin = parseInt(this.object.style[this.slideStyle]);
		if (isNaN(margin)) margin = 0;
		margin += this.scrollValue;

		if (margin > 0)
			margin = 0;
		else if (margin < this.parentData['height']-this.object.offsetHeight)
			margin = this.parentData['height']-this.object.offsetHeight;

		this.object.style[this.slideStyle] = margin+'px';
	}

	this.isInsideObjectBounding = function(x, y) {
		return this.objectData.left <= x && x <= this.objectData.right && this.objectData.top <= y && y <= this.objectData.bottom
		? true
		: false;
	}

	this.setObjectBounding = function() {
		var parentTop = 0;
		var parentLeft = 0;
		var parent = this.object.offsetParent;

		while (parent) {
			parentTop += parent.offsetTop;
			parentLeft += parent.offsetLeft;
			parent = parent.offsetParent;
		}

		this.objectData = new Object();
		this.objectData.top = this.object.offsetTop+parentTop;
		this.objectData.bottom = this.objectData.top+this.object.offsetHeight;
		this.objectData.left = this.object.offsetLeft+parentLeft;
		this.objectData.right = this.objectData.left+this.object.offsetWidth;

		if (this.doDebug) {
			var debug = '';
			for (var i in this.objectData) debug += i+': '+this.objectData[i]+', ';
			alert(debug);
		}
	}

	this.createParent = function() {
		this.parent = this.object.parentNode;

		var grandParentTop = 0;
		var grandParent = this.parent.offsetParent;

		while (grandParent) {
			grandParentTop += grandParent.offsetTop;
  			grandParent = grandParent.offsetParent;
		}

		this.parentData = new Object();
		this.parentData.height = this.parent.offsetHeight;
		this.parentData.delta = this.parentData.height/2;
		this.parentData.upper = this.parent.offsetTop+grandParentTop;
		this.parentData.breaking = this.parentData.upper+this.parentData.delta;
		this.parentData.lower = this.parentData.upper+this.parentData.height;
	}

	this.registerEvents = function() {
		this.object.onmousemove = function(event) { ohSlider.eventListener(event); }
		this.parent.onmouseout = function(event) { ohSlider.eventListener(event); }
	}

	this.setScrollFactor = function(scrollFactor) {
		this.scrollFactor = scrollFactor;
	}

	this.setScrollTimer = function(scrollTimer) {
		this.scrollTimer = scrollTimer;
	}

	this.setObjectBounding();
	this.createParent();
	this.registerEvents();

	return this;
};

// -->