/****h* libs/poma_scrollbox
 * FUNCTION
 *		the poma_scrollbox library provides all functionality to create scrolling
 *		content like photo slideboxes or newstickers.
 ****
 */


/****f* poma_scrollbox/poma.scrollbox
 * SYNOPSIS
 */
poma.scrollbox = function(id, params)
/*
 * FUNCTION
 *		constructor. creates news instance of class scrollbox. the second - optional - parameter
 *		requires an javascript object with the following (optional) properties:
 *      * speed -- sliding speed 1 .. 10, higher numbers for faster sliding (default: 4)
 *      * lr -- allow left/right scrolling true/false (default: false)
 *		* ud -- allow up/down scrolling true/false (default: true)
 *		* usebuttons -- use buttons for scrollbox instead of using mouse true/false (default: false)
 * INPUTS
 *		* id -- id of DOM element to use as scrollbox
 *		* params -- javascript object for optional parameters
 ****
 */
{
	// box elements
	this.id = id;
	this.box = poma.dom.$id(id);

	if (!poma.dom.$id(id + '-slider')) {
		return false;
	} else {
		this.slider = poma.dom.$id(id + '-slider');
	}

	if (isNaN(parseInt(this.slider.style.left))) {
		this.slider.style.left = poma.dom.getComputedStyle(this.slider, 'left');
	}
	if (isNaN(parseInt(this.slider.style.top))) {
		this.slider.style.top = poma.dom.getComputedStyle(this.slider, 'top');
	}

	var cNode = (poma.useragent.inName('msie', false)) ? 0 : 1;

	if (poma.dom.$id(id + '-left') && poma.dom.$id(id + '-right')) {
		this.sliderLImg = poma.dom.$id(id + '-left').childNodes[cNode].childNodes[0];
		this.sliderLImgSrcActive = this.sliderLImg.src;
		var filePath = this.convertFilePath(this.sliderLImgSrcActive);
		this.sliderLImgSrcInActive = filePath;
		this.sliderLImg.src = filePath;

		this.sliderRImg = poma.dom.$id(id + '-right').childNodes[cNode].childNodes[0];
		this.sliderRImgSrcActive = this.sliderRImg.src;
		filePath = this.convertFilePath(this.sliderRImgSrcActive);
		this.sliderRImgSrcInActive = filePath;
	}

	if (poma.dom.$id(id + '-up') && poma.dom.$id(id + '-down')) {
		this.sliderUImg = poma.dom.$id(id + '-up').childNodes[cNode].childNodes[0];
		this.sliderUImgSrcActive = this.sliderUImg.src;
		var filePath = this.convertFilePath(this.sliderUImgSrcActive);
		this.sliderUImgSrcInActive = filePath;
		this.sliderUImg.src = filePath;

		this.sliderDImg = poma.dom.$id(id + '-down').childNodes[cNode].childNodes[0];
		this.sliderDImgSrcActive = this.sliderDImg.src;
		filePath = this.convertFilePath(this.sliderDImgSrcActive);
		this.sliderDImgSrcInActive = filePath;
	}

	// calculate position and center of box
	var xy = poma.dom.getPos(this.box);

	this.left = xy[0];
	this.top = xy[1];

	this.centerX = this.box.offsetWidth / 2;
	this.centerY = this.box.offsetHeight / 2;

	this.mx = 0;
	this.my = 0;

	// misc settings
	params = (typeof params == 'undefined' ? [] : params); 

	this.xallow = (typeof params['lr'] == 'undefined' || !!params['lr']);
	this.yallow = (typeof params['ud'] == 'undefined'
					? !!(poma.dom.$id(id + '-up') && poma.dom.$id(id + '-down'))
					: !!params['ud']);

	this.usebuttons = (typeof params['usebuttons'] != 'undefined' && params['usebuttons']);

	// interval timer settings
	this.timer = null;
	this.timeout = 50;

	// scroll speed when using left/right handle
	this.speed = (typeof params['speed'] == 'undefined' ? 4 : params['speed']);

	// settings when mouse movement is used to scroll
	this.maxSpeed = 10;
	this.centerRX = this.centerX * 0.1;
	this.centerRY = this.centerY * 0.1; 

	this.cbaseX = ((this.centerX - this.centerRX) / this.maxSpeed);
	this.cbaseY = ((this.centerY - this.centerRY) / this.maxSpeed);

	// install event handlers
	var me = this;

	if ((poma.dom.$id(id + '-up') && poma.dom.$id(id + '-down'))||
		(poma.dom.$id(id + '-left') && poma.dom.$id(id + '-right'))) {
		// use buttons for scrolling
		this.setHandleCallback(id + '-left', function() { me.moveSlider([-me.speed, 0]); });
		this.setHandleCallback(id + '-right', function() { me.moveSlider([me.speed, 0]); });
		this.setHandleCallback(id + '-up', function() { me.moveSlider([0, -me.speed]); });
		this.setHandleCallback(id + '-down', function() { me.moveSlider([0, me.speed]); });
	}

	if (!this.usebuttons && !poma.dom.$id(id + '-up') && !poma.dom.$id(id + '-down')) {
		// use sensitive handle areas for scrolling
		this.setHandleCallback(id, function() {
			var x = (poma.mouse.X - me.left - me.centerX);
			var y = (poma.mouse.Y - me.top - me.centerY);
			var rX = me.centerRX;
			var rY = me.centerRY;

			if (Math.abs(x) < rX) { x = 0; } else { x = (x < 0 ? x + rX : x - rX); }
			if (Math.abs(y) < rY) { y = 0; } else { y = (y < 0 ? y + rY : y - rY); }

			x = (me.xallow ? Math.min(Math.ceil(x / me.cbaseX), me.maxSpeed) : 0);
			y = (me.yallow ? Math.min(Math.ceil(y / me.cbaseY), me.maxSpeed) : 0);

			me.moveSlider([x, y]);
		});
	}

	return true;
}

/****f*
 * SYNOPSIS
 */
poma.scrollbox.prototype.setHandleCallback = function(id, cb)
/*
 * FUNCTION
 * 		installs callback for specified box handle. this method is not to be used
 *		directly. instead it is called from scrollbox constructor to attach callback
 *		methods to scrollbox handles
 * INPUTS
 *		* id -- id ov handle
 *		* cb -- callback method to be executed when handle is triggered
 ****
 */
{
	var me = this;

	if (poma.dom.$id(id)) {
		poma.dom.$id(id).onmouseover = function() {
			if (me.timer == null) {
				me.timer = window.setInterval(cb, me.timeout);
			}
		}
		poma.dom.$id(id).onmouseout = function() {
			if (me.timer != null) {
				window.clearInterval(me.timer);
				me.timer = null;
			}
		}
	}
}

/****f*
 * SYNOPSIS
 */
poma.scrollbox.prototype.convertFilePath = function(path)
/*
 * FUNCTION
 *      converts the path of an left-/right-/up-/down-image into its inactive state 
 * INPUTS
 *		* path -- path to convert into inactive state
 ****
 */
{
	var tmp = path.split('/');
	var tmpPath = '';
	var tmpFile = '';

	for (var i=0; i<tmp.length; i++) {
		if (i == tmp.length - 1) {
			tmpFile = tmp[i];
		} else {
			tmpPath += tmp[i] + '/';
		}
	}

	tmpFile = tmpFile.split('.');
	tmpFile = tmpFile[0] + '-inactive.' + tmpFile[1];
	tmp = tmpPath + tmpFile;

	return tmp;
}

/****f*
 * SYNOPSIS
 */
poma.scrollbox.prototype.moveSlider = function(param)
/*
 * FUNCTION
 * 		move scrollbox' content slider. this method is not to be called directly, instead
 *		it is set as callback method through the constructor.
 * INPUTS
 *		* param -- additional parameters for sliding
 ****
 */
{
	var hval = param[0];
	var vval = param[1];

	var tmp, diff;

	diff = this.slider.offsetWidth - this.box.offsetWidth;

	var l = parseInt(this.slider.style.left);

	if (hval != 0 && l <= 0 && l + diff >= 0) {
		tmp = parseInt(this.slider.style.left) - hval;

		if (tmp > 0) {
			tmp = 0;
		} else if (tmp + diff < 0) {
			tmp = this.box.offsetWidth - this.slider.offsetWidth; 
		}

		if (!isNaN(tmp)) {
			this.slider.style.left = tmp + 'px';
		}

		if (l != 0) {
			this.sliderLImg.src = this.sliderLImgSrcActive;
		}
		if (l == 0) {
			this.sliderLImg.src = this.sliderLImgSrcInActive;
		}

		if (Math.abs(l) != diff) {
			this.sliderRImg.src = this.sliderRImgSrcActive;
		}
		if (Math.abs(l) == diff) {
			this.sliderRImg.src = this.sliderRImgSrcInActive;
		}
	}

	diff = this.slider.offsetHeight - this.box.offsetHeight;

	var h = parseInt(this.slider.style.top);
	if (vval != 0 && h <= 0 && h + diff >= 0) {
		tmp = parseInt(this.slider.style.top) - vval;

		if (tmp > 0) {
			tmp = 0;
		} else if (tmp + diff < 0) {
			tmp = this.box.offsetHeight - this.slider.offsetHeight;
		}

		if (!isNaN(tmp)) {
			this.slider.style.top = tmp + 'px';
		}

		if (h != 0) {
			this.sliderUImg.src = this.sliderUImgSrcActive;
		}
		if (h == 0) {
			this.sliderUImg.src = this.sliderUImgSrcInActive;
		}

		if (Math.abs(h) != diff) {
			this.sliderDImg.src = this.sliderDImgSrcActive;
		}
		if (Math.abs(h) == diff) {
			this.sliderDImg.src = this.sliderDImgSrcInActive;
		}
	}
}
