var loOptions;

mediaBox = Class.create();
Object.extend(mediaBox.prototype, {

	lcBaseURL: null,
	laBoxes: new Array(),

	initialize: function(baseurl, module, moduleid) {
		this.lcBaseURL = baseurl;

		new Ajax.Request(this.lcBaseURL + '/' + module + '/ajaxgetmedia.htm', {
			method: 'post',
			postBody: 'id=' + moduleid,
			onSuccess: this.createBoxes.bind(this)
		});
	},

	createBoxes: function(t) {
		var loXML = t.responseXML.documentElement;

		var loPartElem = loXML.firstChild;
		while (loPartElem != null) {
			//var loOptions = {
			loOptions = {
				id: loPartElem.getAttribute("id"),
				position: loPartElem.getAttribute("position"),
				size: loPartElem.getAttribute("size"),
				contentWidth: loPartElem.getAttribute("contentWidth")
			};
						
			var laFiles = new Array();
			for (var i = 0; i < loPartElem.childNodes.length; i++) {
				laFiles.push(new mediaBoxFile(this.lcBaseURL, loPartElem.childNodes[i]));
			}
			// Anzeigeobject wählen
			if (laFiles.length == 1) {
			//	console.debug('mediaBoxSingle');
				this.laBoxes[this.laBoxes.length] = new mediaBoxSingle(laFiles[0], loOptions);
			}
			else if (laFiles.length < 5) {
			//	console.debug('mediaBoxMultiple');
				this.laBoxes[this.laBoxes.length] = new mediaBoxMultiple(laFiles, loOptions);
			}
			else {
			//	console.debug('mediaBoxSlider');
				this.laBoxes[this.laBoxes.length] = new mediaBoxSlider(laFiles, loOptions);
			}

			loPartElem = loPartElem.nextSibling;
		}
	}

});

mediaBoxFile = Class.create();
Object.extend(mediaBoxFile.prototype, {

	lcBaseURL: null,
	lnID: 0,
	lcName: null,
	lcHandler: null,
	lnFileSize: 0,
	lnUploaded: 0,
	lcType: null,
	laURLs: new Array(),
	laImages: new Array(),

	initialize: function(baseurl, loFileElem) {
		this.lcBaseURL = baseurl;
		this.lnID = parseInt(loFileElem.getAttribute("id"));
		this.lcName = loFileElem.getAttribute("name");
		this.lcHandler = loFileElem.getAttribute("handler");
		
		this.lnFileSize = parseInt(loFileElem.getAttribute("filesize"));
		this.lnUploaded = new Date(parseInt(loFileElem.getAttribute("uploaded")) * 100);
		this.lcType = loFileElem.getAttribute("type");
		this.width = loFileElem.getAttribute("width");

		if (loFileElem.hasChildNodes()) {

			var laURLs = new Array();
			$A(loFileElem.childNodes).each(function(loURLElem) {
				var lnIndex = loURLElem.getAttribute("name");
				laURLs[lnIndex] = loURLElem.firstChild.nodeValue;
			});
			this.laURLs = laURLs;
		}

		//if ((this.lcHandler == 'audio' || this.lcHandler == 'video') && typeof(SWFObject) == 'undefined') document.write('<script type="text/javascript" charset="utf-8" src="layout/js/swfobject.js"></script>');
	},

	_chkImage: function(loImg, lnIndex) {
		if (!loImg.complete) loImg.src = this.lcBaseURL + '/' + this.laURLs[lnIndex];
		return false;
	},

	draw: function(loContainer) {
		if (!loContainer) return false;
		loContainer.innerHTML = '';
		switch (this.lcHandler) {
			case 'audio':
				var loObj = new SWFObject('codebase/libs/musicplayer/xspf_player_slim.swf?song_url=' + this.laURLs['original'] + '&song_title=' + this.lcName + '&player_title=' + this.lcName, 'audioplayer', '250', '20', '6', '#FFFFFF');
	   			loObj.write(loContainer);
				break;

			case 'video':
				if(loOptions.position == 'top') {
					var loObj = new SWFObject('codebase/libs/flashplayer/FlowPlayerLight.swf', 'videoplayer', loOptions.contentWidth, (loOptions.contentWidth/4*3), '9', '#FFFFFF', true);
					//loObj.addVariable("config", "{showMenu: false, loop: false, autoPlay:false, initialScale: 'fit', videoFile: '" + this.lcBaseURL + "/" + this.laURLs['videostream'] + "', splashImageFile: '" + this.lcBaseURL + "/" + this.laURLs['fullsize'] + "'}");
					loObj.addVariable("config", "{videoFile: '" + this.lcBaseURL + "/" + this.laURLs['videostream'] + "', showMenu:false, autoPlay:false, initialScale: 'scale', loop: false}");
	
					//var loObj = new SWFObject('codebase/libs/flowplayer/FlowPlayer.swf', 'videoplayer', loOptions.contentWidth, (loOptions.contentWidth/4*3), '6', '#FFFFFF');
					//loObj.addVariable('videoFile', this.lcBaseURL + '/app/' + this.laURLs['original']);
					//loObj.addVariable('splashImageFile', this.lcBaseURL + '/' + this.laURLs['full']);
				}
				else {
					var loObj = new SWFObject('codebase/libs/flowplayer/FlowPlayer.swf', 'videoplayer', '250', '250', '6', '#FFFFFF');
					loObj.addVariable('baseURL', this.lcBaseURL);
					loObj.addVariable('videoFile', this.laURLs['videostream']);
					loObj.addVariable('splashImageFile', this.lcBaseURL + '/' + this.laURLs['full']);
				}
				//loObj.addParam("flashVars", "config={ baseURL: '" + this.lcBaseURL + "', videoFile: '" + this.laURLs['videostream'] + "', splashImageFile: '" + this.lcBaseURL + '/' + this.laURLs['full'] + "', initialScale: 'fit', showMenu: false }");
	   			loObj.write(loContainer);
				break;

			case 'image':
				var loObj = document.createElement('a');
				loObj.href = this.lcBaseURL + '/' + this.laURLs['fullsize'];
				loObj.rel = 'lightbox';
//				loObj.title = 'Vollbild';

				var loImg = document.createElement('img');
				loImg.src = this.lcBaseURL + '/' + this.laURLs['full'];
				// wir alle lieben den IE -.-
				if (document.all) window.setTimeout(this._chkImage.bind(this, loImg, 'full'), 200);
				loObj.appendChild(loImg);
//				loObj.onclick = this.showFullsize.bind(this);
				loObj.onclick = function () {myLightbox.start(this); return false;}
				loContainer.innerHTML = '';
				loContainer.appendChild(loObj);
				break;

			case 'application':
				if (this.lcType == 'x-shockwave-flash') {
					var loObj = new SWFObject('app/media/original/' + this.lcName, 'flashplayer', '250', '250', '6', '#FFFFFF');
		   			loObj.write(loContainer);
					break;
				}

			default:
				var loImg = document.createElement('img');
				loImg.src = this.lcBaseURL + '/' + this.laURLs['full'];
				// wir alle lieben den IE -.-
				if (document.all) window.setTimeout(this._chkImage.bind(this, loImg, 'full'), 200);

				loContainer.innerHTML = '';
				loContainer.appendChild(loImg);
				break;
		}
	},

	showFullsize: function() {
//		return overlib('<img src="' + this.laURLs['fullsize'] + '">',HAUTO, VAUTO, LEFT, WRAP, OFFSETX, 0, OFFSETY, 0,STICKY, CAPTION, 'Vollbild',CLOSECLICK);
	}
});



mediaBoxSingle = Class.create();
Object.extend(mediaBoxSingle.prototype, {

	loFile: {},
	loOptions: {},

	initialize: function(file) {
		this.loFile = file;
		this.loOptions = Object.extend({
	                    id: 0,
	                    position: "left",
	                    size: "big"
	                }, arguments[1] || {});

	                this.draw();
	},

	/*
	<div class="BoxImgPositionLeft">
		<div class="BoxImgBorder">
			<div class="BoxBigImage"></div>
		</div>
	</div>
	*/

	draw: function() {
		var loPartObj = $('part' + this.loOptions.id);
		if (!loPartObj) {
			//console.debug('part' + this.loOptions.id + ' not found!');
			return false;
		}

		var loBoxPosition = document.createElement('div');
		loBoxPosition.className = 'SingleImage ' + this.loOptions.position.capitalize();

		loPartObj.insertBefore(loBoxPosition, loPartObj.firstChild);

		this.loFile.draw(loBoxPosition);

        // Corners
        var LeftBottom = document.createElement('b');
		LeftBottom.className = 'LeftBottom';
		loBoxPosition.appendChild(LeftBottom);

        var LeftTop = document.createElement('b');
		LeftTop.className = 'LeftTop';
		loBoxPosition.appendChild(LeftTop);

        var RightBottom = document.createElement('b');
		RightBottom.className = 'RightBottom';
		loBoxPosition.appendChild(RightBottom);

        var RightTop = document.createElement('b');
		RightTop.className = 'RightTop';
		loBoxPosition.appendChild(RightTop);
	}
});


mediaBoxMultiple = Class.create();
Object.extend(mediaBoxMultiple.prototype, {

	laFiles: new Array(),
	loOptions: {},

	initialize: function(files) {
		this.laFiles = files;
		this.loOptions = Object.extend({
	                    id: 0,
	                    position: "left",
	                    size: "big"
	                }, arguments[1] || {});

	                this.draw();
	},

    /*
    <div class="HrImagePosition">
				<div class="ImgPosition">
					<div class="VPosition">
						<img src="http://web.wolf/neu_pohlig.net/de/app/media/site/full_SIMG00132.jpg" alt="kinder" />
					</div>
				</div>
				<!-- Images -->
				<div class="Images">
					<div>
						<div>
							<a><img src="http://web.wolf/neu_pohlig.net/de/app/media/site/full_SIMG00132.jpg" alt="kinder" /></a>
                            <a><img src="http://web.wolf/neu_pohlig.net/de/app/media/site/full_SIMG00132.jpg" alt="kinder" /></a>
                            <a><img src="http://web.wolf/neu_pohlig.net/de/app/media/site/full_SIMG00132.jpg" alt="kinder" /></a>
                            <a><img src="http://web.wolf/neu_pohlig.net/de/app/media/site/full_SIMG00132.jpg" alt="kinder" /></a>
						</div>
					</div>
				</div>
				<!-- END Images -->
				<b class="LeftBottom"></b>
				<b class="RightBottom"> </b>
				<b class="LeftTop"> </b>
				<b class="RightTop"> </b>
			</div>
	*/

	draw: function() {
		var loPartObj = $('part' + this.loOptions.id);
		if (!loPartObj) {
			//console.debug('part' + this.loOptions.id + ' not found!');
			return false;
		}
		var loBoxPosition = document.createElement('div');
		loBoxPosition.className = 'BoxImgPosition ' + this.loOptions.position.capitalize();;

		var loBoxBorder = document.createElement('div');
		loBoxBorder.className = 'ImgPosition';
		loBoxPosition.appendChild(loBoxBorder);

		var loBoxSize = document.createElement('div');
		loBoxSize.className = 'VPosition';
		loBoxBorder.appendChild(loBoxSize);

		var loBoxThumbs = document.createElement('div');
		loBoxThumbs.className = 'Images';
		loBoxPosition.appendChild(loBoxThumbs);

            var loBoxThumbsDIV = document.createElement('div');
			loBoxThumbs.appendChild(loBoxThumbsDIV);

                var loBoxThumbsDIVDIV = document.createElement('div');
				loBoxThumbsDIV.appendChild(loBoxThumbsDIVDIV);

		loPartObj.insertBefore(loBoxPosition, loPartObj.firstChild);

		// Thumbnails zeichnen
		this.laFiles.each(function(loFile) {
			var loA = document.createElement('a');
			loA.href = 'javascript:;';
			loA.onclick = loFile.draw.bind(loFile, loBoxSize);

			var loImg = document.createElement('img');
			loImg.src = loFile.lcBaseURL + '/' + loFile.laURLs['thumb'];
			loA.appendChild(loImg);

			loBoxThumbsDIVDIV.appendChild(loA);
		});

		// Corners

        var LeftBottom = document.createElement('b');
		LeftBottom.className = 'LeftBottom';
		loBoxPosition.appendChild(LeftBottom);

        var LeftTop = document.createElement('b');
		LeftTop.className = 'LeftTop';
		loBoxPosition.appendChild(LeftTop);

        var RightBottom = document.createElement('b');
		RightBottom.className = 'RightBottom';
		loBoxPosition.appendChild(RightBottom);

        var RightTop = document.createElement('b');
		RightTop.className = 'RightTop';
		loBoxPosition.appendChild(RightTop);


		// Full Image zeichnen
		this.laFiles[0].draw(loBoxSize);
	}
});



mediaBoxSlider = Class.create();
Object.extend(mediaBoxSlider.prototype, {

	// Div Container
	loContainer: null,
	loImageLayer: null,
	loBoxSize: null,
    loBoxThumbsDIVDIV: null,

	// Dateien
	laFiles: new Array(),
	loOptions: {},

	slide: null,
	width: 160,

	initialize: function(files) {
		this.laFiles = files;
		this.loOptions = Object.extend({
	                    id: 0,
	                    position: "left",
	                    size: "big",
	                    imageWidth: 35,
	                    imageHeight: 26,
	                    currentIndex: 0,
	                    slideStep: 10
	                }, arguments[1] || {});

	                this.draw();
	},

	draw: function() {
		var loPartObj = $('part' + this.loOptions.id);
		if (!loPartObj) {
			//console.debug('part' + this.loOptions.id + ' not found!');
			return false;
		}

        var loBoxPosition = document.createElement('div');
		loBoxPosition.className = 'BoxImgPosition '+ this.loOptions.position.capitalize();

		var loBoxBorder = document.createElement('div');
		loBoxBorder.className = 'ImgPosition';
		loBoxPosition.appendChild(loBoxBorder);

		this.loBoxSize = document.createElement('div');
		this.loBoxSize.className = 'VPosition';
		loBoxBorder.appendChild(this.loBoxSize);

		var loBoxThumbs = document.createElement('div');
		loBoxThumbs.className = 'Images';
		loBoxPosition.appendChild(loBoxThumbs);

            var loBoxThumbsDIV = document.createElement('div');
			loBoxThumbs.appendChild(loBoxThumbsDIV);

                var loBoxThumbsDIVDIV = document.createElement('div');
				loBoxThumbsDIVDIV.style.padding = 0;
				loBoxThumbsDIV.appendChild(loBoxThumbsDIVDIV);

		// Pfeil links
		var loArrowLeft = document.createElement('a');
		loArrowLeft.href = 'javascript:;';
		loArrowLeft.onmouseover = this.slideStart.bind(this, 1);
		loArrowLeft.onmouseout = this.slideStop.bind(this);
		loArrowLeft.className = 'BoxImageSliderArrowLeft';
		loBoxThumbsDIVDIV.appendChild(loArrowLeft);

		// Container
		this.loContainer = document.createElement('div');
		this.loContainer.className = 'BoxImageSlider';
		this.loContainer.style.width = this.width + 'px';
		this.loContainer.style.height = this.loOptions.imageHeight + 'px';
		loBoxThumbsDIVDIV.appendChild(this.loContainer);

		loPartObj.insertBefore(loBoxPosition, loPartObj.firstChild);

		// Pfeil rechts
		var loArrowRight = document.createElement('a');
		loArrowRight.href = 'javascript:;';
		loArrowRight.onmouseover = this.slideStart.bind(this, -1);
		loArrowRight.onmouseout = this.slideStop.bind(this);
		loArrowRight.className = 'BoxImageSliderArrowRight';
		loBoxThumbsDIVDIV.appendChild(loArrowRight);

		// Imagelayer
		this.loImageLayer = document.createElement('div');
		this.loImageLayer.id = 'imagelayer' + this.loOptions.id;
		Element.setStyle(this.loImageLayer, {
			display: 'block',
			position: 'relative',
			left: '0px',
			width: (this.laFiles.length * this.loOptions.imageWidth) + 'px',
			height: this.loOptions.imageHeight + 'px'
		});
		/*
		this.loImageLayer.setStyle({
			display: 'block',
			position: 'relative',
			left: '0px',
			width: (this.laFiles.length * this.loOptions.imageWidth) + 'px',
			height: this.loOptions.imageHeight + 'px'
		});
		*/
		this.loContainer.appendChild(this.loImageLayer);

		// Bilder einfügen
		var lnImageCount = Math.ceil(this.width / this.loOptions.imageWidth);
		if (lnImageCount > this.laFiles.length) lnImageCount = this.laFiles.length;
		for (var i = 0; i < lnImageCount; i++) {
			var loImg = document.createElement('img');
			loImg.id = 'image-' + this.loOptions.id + '-' + i;
			//loImg.className = this.imageclass;
			this.setImage(loImg, i);

			loImg.width = this.loOptions.imageWidth;
			loImg.height = this.loOptions.imageHeight;

			loImg.style.position = 'absolute';
			loImg.style.left = (i*this.loOptions.imageWidth) + 'px';

			this.loImageLayer.appendChild(loImg);
		}

        // Corners

        var LeftBottom = document.createElement('b');
		LeftBottom.className = 'LeftBottom';
		loBoxPosition.appendChild(LeftBottom);

        var LeftTop = document.createElement('b');
		LeftTop.className = 'LeftTop';
		loBoxPosition.appendChild(LeftTop);

        var RightBottom = document.createElement('b');
		RightBottom.className = 'RightBottom';
		loBoxPosition.appendChild(RightBottom);

        var RightTop = document.createElement('b');
		RightTop.className = 'RightTop';
		loBoxPosition.appendChild(RightTop);


		// Erstes Bild zeichnen:
		this.drawCurrentImage();
	},

	next: function() {
		this.setIndex(this.loOptions.currentIndex+1);
	},

	prev: function() {
		this.setIndex(this.loOptions.currentIndex-1);
	},

	slideStart: function(direction) {
		//if (this.slide != null) return false;
		var lnLeft = Math.floor(parseInt(Element.getStyle(this.loImageLayer, 'left'))  + (direction*this.loOptions.slideStep));
		//alert(lnLeft);
		var lnLayerWidth = parseInt(Element.getStyle(this.loImageLayer, 'width')) - this.width;
		if (lnLeft < -lnLayerWidth) lnLeft = -lnLayerWidth;
		else if (lnLeft > 0) lnLeft = 0;

		this.loImageLayer.style.left = lnLeft + 'px';
		//alert(this.loImageLayer.style.left);
		this.checkWrap();

		if (this.slide == null) this.slide = window.setInterval(this.slideStart.bind(this, direction), 50);
	},

	slideStop: function() {
		window.clearInterval(this.slide);
		this.slide = null;
	},

	setIndex: function(lnIndex) {
		if (lnIndex < 0 || lnIndex > this.laFiles.length) return false;
		this.loOptions.currentIndex = lnIndex;
		this.drawCurrentImage();
	},

	updateCurrentImage: function() {
		var lnCurrentLeft = this.loOptions.currentIndex * this.loOptions.imageWidth;
		for (var i = 0; i < this.loImageLayer.childNodes.length; i++) {
			var loImg = this.loImageLayer.childNodes[i];
			var lnLeft = parseInt(loImg.style.left);
			if (lnLeft == lnCurrentLeft) Element.setStyle(loImg, {opacity: 1.0});
			else Element.setStyle(loImg, {opacity: 0.5});
		}
	},

	drawCurrentImage: function(el) {
		this.laFiles[this.loOptions.currentIndex].draw(this.loBoxSize);
		this.updateCurrentImage();
	},

	checkWrap: function() {
		var lnLeft = parseInt(this.loImageLayer.style.left);
		var loImgFirst = this.loImageLayer.firstChild;
		var loImgLast = this.loImageLayer.lastChild;
		var lnLeftFirst = parseInt(loImgFirst.style.left);
		var lnLeftLast = parseInt(loImgLast.style.left);
		var lnImgID = 0;

		//$('debug').innerHTML = (-lnLeft) + ' ' + refLast;

		while (-1*(lnLeft+this.loOptions.imageWidth) > lnLeftFirst) {
			this.loImageLayer.removeChild(loImgFirst);
			loImgFirst.style.left = (lnLeftLast + this.loOptions.imageWidth) + 'px';
			lnImgID = (lnLeftLast+this.loOptions.imageWidth) / this.loOptions.imageWidth;
			this.setImage(loImgFirst, lnImgID);

			this.loImageLayer.appendChild(loImgFirst);

			loImgFirst = this.loImageLayer.firstChild;
			loImgLast = this.loImageLayer.lastChild;
			lnLeftFirst = parseInt(loImgFirst.style.left);
			lnLeftLast = parseInt(loImgLast.style.left);
		}
		while (-lnLeft < lnLeftLast-this.width) {
			this.loImageLayer.removeChild(loImgLast);
			loImgLast.style.left = (lnLeftFirst - this.loOptions.imageWidth) + 'px';
			lnImgID = (lnLeftFirst-this.loOptions.imageWidth) / this.loOptions.imageWidth;
			//loImgLast.src = this.images[lnImgID][0];
			this.setImage(loImgLast, lnImgID);

			this.loImageLayer.insertBefore(loImgLast, loImgFirst);

			loImgFirst = this.loImageLayer.firstChild;
			loImgLast = this.loImageLayer.lastChild;
			lnLeftFirst = parseInt(loImgFirst.style.left);
			lnLeftLast = parseInt(loImgLast.style.left);
		}

		this.updateCurrentImage();
	},

	setImage: function(loImg, lnIndex) {
		var loFile = this.laFiles[lnIndex];
		loImg.src = loFile.lcBaseURL + '/' + loFile.laURLs['thumb'];
		loImg.title = loFile.lcName;
		loImg.onclick = this.setIndex.bind(this, lnIndex);
	}

});