/*
	Data-Scroller

	Getestet mit:
	- Fx3, Fx3.5, IE6, IE7, IE8, Op10, Sf5

	Letzte Änderung:
	- JeDu (2010-03-04)

	Beispielaufruf:

		mdxDataScrollerInit({
			sizes:{
				bg:{     width:798, height:135, src:'/userdata/11111/layout/topscroller/bg-sample.gif' },
				button:{ width:45,  height:63,  src:'/userdata/11111/layout/topscroller/buttons-sample.gif' },
				frame:{  width:137, height:123, src:'/userdata/11111/layout/topscroller/frame-sample.png' }
			},
			pages:5,
			data:[
				function (no, totalNo) { return '....'; },
				function (no, totalNo) { return '....'; },
				function (no, totalNo) { return '....'; },
				function (no, totalNo) { return '....'; }
			]
		});

	Konfiguration der Ausgabe-Elemente:
		element:{
			container:'dataScrollerContainer',
			table:'dataScrollerTable',
			buttonLeft:'dataScrollerButtonLeft',
			buttonLeftElem:'dataScrollerButtonLeftElem',
			buttonRight:'dataScrollerButtonRight',
			buttonRightElem:'dataScrollerButtonRightElem'
		}
*/

function mdxDataScrollerInit(config)
{
	function confError(variable) { return 'Die Konfiguration ist unvollständig.<br>' + variable + ' nicht gesetzt.'; }

	if (typeof config.element != 'object') { config.element = {}; }
	if (typeof config.element.container != 'object') { config.element.container = document.getElementById(typeof config.element.container === 'string' ? config.element.container : 'dataScrollerContainer' ); }
	if (typeof config.element.table != 'object') { config.element.table = document.getElementById(typeof config.element.table === 'string' ? config.element.table : 'dataScrollerTable' ); }
	if (typeof config.element.buttonLeft != 'object') { config.element.buttonLeft = document.getElementById(typeof config.element.buttonLeft === 'string' ? config.element.buttonLeft : 'dataScrollerButtonLeft' ); }
	if (typeof config.element.buttonLeftElem != 'object') { config.element.buttonLeftElem = document.getElementById(typeof config.element.buttonLeftElem === 'string' ? config.element.buttonLeftElem : 'dataScrollerButtonLeftElem' ); }
	if (typeof config.element.buttonRight != 'object') { config.element.buttonRight = document.getElementById(typeof config.element.buttonRight === 'string' ? config.element.buttonRight : 'dataScrollerButtonRight' ); }
	if (typeof config.element.buttonRightElem != 'object') { config.element.buttonRightElem = document.getElementById(typeof config.element.buttonRightElem === 'string' ? config.element.buttonRightElem : 'dataScrollerButtonRightElem' ); }

	if (typeof config.sizes != 'object') { config.element.container.innerHTML = confError('sizes'); return; }
	if (typeof config.sizes.bg != 'object') { config.element.container.innerHTML = confError('sizes.bg'); return; }
	if (typeof config.sizes.bg.width != 'number') { config.element.container.innerHTML = confError('sizes.bg.width'); return; }
	if (typeof config.sizes.bg.height != 'number') { config.element.container.innerHTML = confError('sizes.bg.height'); return; }

	if (typeof config.sizes.button != 'object') { config.element.container.innerHTML = confError('sizes.button'); return; }

	if (typeof config.sizes.button.width != 'number') { config.element.container.innerHTML = confError('sizes.button.width'); return; }
	if (typeof config.sizes.button.height != 'number') { config.element.container.innerHTML = confError('sizes.button.height'); return; }
	if (typeof config.sizes.button.src != 'string') { config.element.container.innerHTML = confError('sizes.button.src'); return; }

	if (typeof config.sizes.frame != 'object') { config.element.container.innerHTML = confError('sizes.frame'); return; }
	if (typeof config.sizes.frame.width != 'number') { config.element.container.innerHTML = confError('sizes.frame.width'); return; }
	if (typeof config.sizes.frame.height != 'number') { config.element.container.innerHTML = confError('sizes.frame.height'); return; }
	if (typeof config.sizes.frame.src == 'string') {
		config.sizes.frame.src = [config.sizes.frame.src];
	} else if (typeof config.sizes.frame.src != 'object' || !config.sizes.frame.src.length) { config.element.container.innerHTML = confError('sizes.frame.src'); return; }

	if (typeof config.pages != 'number') { config.pages = 5; }
	if (typeof config.data != 'object' || !config.data.length) { config.element.container.innerHTML = confError('data'); return; }

	if (typeof config.langId === 'undefined') { config.langId = 'de'; }

	var framesPerPage = Math.floor((config.sizes.bg.width - (2 * config.sizes.button.width)) / config.sizes.frame.width);

	if (config.data.length <= framesPerPage) {
		config.element.buttonLeftElem.style.visibility = 'hidden';
		config.element.buttonRightElem.style.visibility = 'hidden';
		framesPerPage = config.data.length;
	}

	var frameSpace = Math.floor(((config.sizes.bg.width - (2 * config.sizes.button.width)) - (framesPerPage * config.sizes.frame.width)) / framesPerPage);
	var pageWidth = (config.sizes.frame.width + frameSpace) * framesPerPage;

	var currentPos = 0.0;
	var finalPos = 0.0;
	var scrollInterval = null;

	config.element.table.style.width = + config.sizes.bg.width + 'px';
	config.element.table.style.height = + config.sizes.bg.height + 'px';

	config.element.buttonLeft.style.width = (config.sizes.bg.width - config.sizes.button.width - pageWidth) + 'px';
	config.element.buttonLeftElem.style.width = config.sizes.button.width + 'px';
	config.element.buttonLeftElem.style.height = config.sizes.button.height + 'px';

	config.element.buttonRight.style.width = config.sizes.button.width + 'px';
	config.element.buttonRightElem.style.width = config.sizes.button.width + 'px';
	config.element.buttonRightElem.style.height = config.sizes.button.height + 'px';

	if (config.sizes.bg.src) {
		config.element.buttonLeft.style.background = 'url("' + config.sizes.bg.src + '") no-repeat left 0';
		config.element.container.style.background = 'url("' + config.sizes.bg.src + '") no-repeat center 0';
		config.element.buttonRight.style.background = 'url("' + config.sizes.bg.src + '") no-repeat right 0';
	}

	config.element.table.style.visibility = 'visible';

	if (config.data.length === 0) {
		switch (config.langId.substr(0, 2)) {
			case 'en':
				config.element.container.innerHTML = 'No offers available at the moment.';
				break;
			case 'fr':
               			config.element.container.innerHTML = 'Pas d´offres disponibles pour le moment.';
				break;
			case 'nl':
		                config.element.container.innerHTML = 'Geen aanbiedingen beschikbaar op dit ogenblik.';
				break;
			default:
				config.element.container.innerHTML = 'Zur Zeit stehen keine Angebote zur Verfügung.';
		}
		return;
	}

	var dataList = new Array();
	var dataListCount = (framesPerPage * config.pages);

	if (config.data.length < dataListCount) { dataListCount = config.data.length; }
	for (var i = 0; i < dataListCount; i++) {
		dataList.push(config.data[i])
	}

	if (dataList.length < (framesPerPage * config.pages)) {
		dataListCount = dataList.length;
		for (var i = dataListCount; i < (framesPerPage * config.pages); i++) {
			dataList.push(dataList[i % dataListCount]);
		}
	}

	for (var i = 0; i < framesPerPage; i++) {
		dataList.push(dataList[i]);
	}

	var frameBackgroundCode = [];
	if (config.sizes.frame.src) {
		if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && typeof document.body.style.filter == "string" && !window.XMLHttpRequest){
			/* IE6 */
			for (var i = 0; i < config.sizes.frame.src.length; i++) {
				frameBackgroundCode.push(' filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + config.sizes.frame.src[i] + '\', sizingMethod=\'crop\');');
			}
		} else {
			/* all other browsers */
			for (var i = 0; i < config.sizes.frame.src.length; i++) {
				frameBackgroundCode.push(' background:url(\'' + config.sizes.frame.src[i] + '\') no-repeat 0 0;');
			}
		}
	}

	out = '';
	var dataListCount = (framesPerPage * config.pages);
	if (config.data.length < dataListCount) { dataListCount = config.data.length; }
	for (var i = 0; i < dataList.length; i++) {
		out += '<div style="float:left; text-align:left; width:' + (config.sizes.frame.width + frameSpace) + 'px; height:' + config.sizes.frame.height + 'px;">';
		out += '<div style="margin:0 ' + Math.floor(frameSpace / 2) + 'px;">' + dataList[i](i % dataListCount, i) + '</div>';
		out += '</div>'
	}
	if (typeof config.element.contentId != 'string' || document.getElementById(config.element.contentId) !== null) {
		for (var i = 0; i < 1000; i++) {
			config.element.contentId = 'dataScrollerContent' + i.toString();
			if (document.getElementById(config.element.contentId) === null) { break; }
		}
	}
	config.element.container.innerHTML = '<div style="width:' + pageWidth + 'px; position:relative; overflow:hidden;"><div id="' + config.element.contentId + '" style="position:relative; width:' + (framesPerPage * (config.pages + 1) * (config.sizes.frame.width + frameSpace)) + 'px;">' + out + '<div style="clear:both;"></div></div></div>';

	config.element.buttonLeftElem.style.background = 'url("' + config.sizes.button.src + '") no-repeat 0 0';
	config.element.buttonLeftElem.style.cursor = 'pointer';
	addEvent(config.element.buttonLeftElem, 'mouseover', function () { this.style.backgroundPosition = '0 -' + config.sizes.button.height + 'px'; });
	addEvent(config.element.buttonLeftElem, 'mouseout', function () { this.style.backgroundPosition = '0 0'; });
	addEvent(config.element.buttonLeftElem, 'click', function () { finalPos -= pageWidth; mdxDataAutoScroller(); });

	config.element.buttonRightElem.style.background = 'url("' + config.sizes.button.src + '") no-repeat -' + config.sizes.button.width + 'px 0';
	config.element.buttonRightElem.style.cursor = 'pointer';
	addEvent(config.element.buttonRightElem, 'mouseover', function () { this.style.backgroundPosition = '-' + config.sizes.button.width + 'px -' + config.sizes.button.height + 'px'; });
	addEvent(config.element.buttonRightElem, 'mouseout', function () { this.style.backgroundPosition = '-' + config.sizes.button.width + 'px 0'; });
	addEvent(config.element.buttonRightElem, 'click', function () { finalPos += pageWidth; mdxDataAutoScroller(); });

	/* Locale functions */

	function addEvent(obj, type, fn) {
		if (obj.attachEvent) {
			obj['e' + type + fn] = fn;
			obj[type + fn] = function() { obj['e' + type + fn](window.event); }
			obj.attachEvent('on' + type, obj[type + fn]);
		} else {
			obj.addEventListener(type, fn, false);
		}
	}

	function mdxDataAutoScroller() {
		if (scrollInterval == null) { scrollInterval = setInterval(mdxDataAutoScroller, 30); }

		var i = (finalPos - currentPos) / (framesPerPage * 2);
		if (
			(currentPos < finalPos && i < 0.1) ||
			(currentPos > finalPos && i > -0.1)
		) {
			currentPos = finalPos;
			clearInterval(scrollInterval);
			scrollInterval = null;
		} else {
			currentPos += i;
		}

		var newPos = -((currentPos + (pageWidth * config.pages)) % (pageWidth * config.pages));
		if (newPos > 0) {
			newPos -= (pageWidth * config.pages);
			currentPos += (pageWidth * config.pages);
			finalPos += (pageWidth * config.pages);
		}
		document.getElementById(config.element.contentId).style.left = Math.round(newPos) + 'px';
	}
}
