/*
 * JavaScript version 1.5 
 * DOM 1.0
 * Prototype JavaScript framework, version 1.5.0_rc1
 *
 * Created on 2007.01.19
 *
 * File classes.js
 * 
 * @author Artur Wied <a.wied@zeitgenossen.de>
 * @version 0.1a1
 * @copyright 2006-2007 ZEITGENOSSEN
 *
 */
//ScreenLayer
screenLayer = Class.create();
screenLayer.prototype = {
	initialize: function() {
		this.bgcolor = 'rgb(100%, 100%, 100%)';
		this.duration = 0.5;
		this.opacity = 0.80;
		this.id = 'screenLayer';
		this.br = new Browser();
		var node_body = document.getElementsByTagName('body').item(0);
		var div = document.createElement('div');
		
		div.id = this.id;
		
		if(this.br.name == 'MSIE' && this.br.majorVersion < 7) {
			node_body.style.width = '100%';
			node_body.style.height = '100%';
			div.style.position = 'absolute';
			div.style.top = '0px';
			div.style.left = '0px';
			div.style.height = '100%';
			div.style.width = '100%';
			this.tim;
			
		} else {
			div.style.position = 'fixed';
			div.style.top = '0px';
			div.style.left = '0px';
			div.style.bottom = '0px';
			div.style.right = '0px';
			div.style.top = '0px';
		}
		
		div.style.backgroundColor = this.bgcolor;
		if(this.br.name == 'MSIE')
			div.style.filter = 'alpha(opacity=0)';
		else 
			div.style.opacity = '0.0';
		div.style.display = 'none';	
		div.style.zIndex = '998';
		node_body.appendChild(div);
		this.screenLayer = $(this.id);
	}, 
	showLayer: function() {
		this.screenLayer.style.display = 'block';
		new Effect.Opacity(this.id, {duration:this.duration, from:0, to:this.opacity});
		//reposetionierung für MSIE 6 
		if(this.br.name == 'MSIE' && this.br.majorVersion < 7) {
			this.screenLayer.style.top = document.documentElement.scrollTop;
			this.screenLayer.style.left = document.documentElement.scrollLeft;
			var self = this;
			document.getElementsByTagName('html').item(0).onscroll = function() {
				$('screenLayer').style.top = document.documentElement.scrollTop;
				$('screenLayer').style.left = document.documentElement.scrollLeft;
			}
		}
	},
	hideLayer: function() {
		var self = this;
		new Effect.Opacity(this.id, {duration:this.duration, from:this.opacity, to:0, 
			afterFinish: function(){
				self.screenLayer.style.display = 'none';
				//reposetionierung für MSIE 6 
				if(self.br.name == 'MSIE' && self.br.majorVersion < 7) {
					self.screenLayer.style.top = '0px';
					self.screenLayer.style.left = '0px';
					document.getElementsByTagName('html').item(0).onscroll = null;
				}
			}});
	}
}

//BrovserInfo Erste ewntwurf
//Abhendigkeit von Prototype lösen
Browser = Class.create();
Browser.prototype = {
		initialize: function() {
			this.name = this.getBrowserName();
			this.majorVersion = this.getBrowserMajorVerion();
		},
		getBrowserName: function() {
			var nameRaw = navigator.appName;
			var out = null;
			//browser liste erweitern 
			//unterscheidung zieschen natscape und mozilla?
			switch (nameRaw) {
				case 'Microsoft Internet Explorer':
					out = 'MSIE';
					break;
				case 'Netscape':
					out = 'Mozilla';
					break;
				case 'Opera':
					out = 'Opera';
					break;
				default: 
					out = nameRaw;
			}
			return out;
		},
		getBrowserMajorVerion: function() {
			var versionRaw = navigator.appVersion;
			var name = navigator.appName;
			switch(name) {
				case 'Microsoft Internet Explorer':
					var pos = versionRaw.indexOf('MSIE');
					out = versionRaw.slice(pos+ 5, pos + 6);
					break;
				case 'Netscape':
					out = versionRaw.slice(0, 1);
					break;
				case 'Opera':
					out = versionRaw.slice(0, 1);
					break;
				default:
					out = versionRaw;
			}
			return out;
		}
}
//LoginForm Class
loginForm = Class.create();
loginForm.prototype = {
	initialize: function(formId, btnId) {
		this.status = false;
		this.btn = $(btnId);
		this.form = $(formId);
		this.passwd = $('password');
		var self = this;
		this.btn.onclick = function() {
			self.toggleForm();
		}
	},
	toggleForm: function() {
		if(this.status) {
			this.hideForm();
		} else {
			this.showForm();
			
		}
	},
	showForm: function() {
		this.form.style.visibility = 'visible';
		Effect.Appear(this.form, {duration:0.25, to:0.70, from:0.0});
		this.status = true;
		var self = this;
		window.setTimeout(function() { self.hideForm()}, 30000);
	},
	hideForm: function() {
		var self = this;
		
		Effect.Appear(this.form, {
					duration:0.25,
					to:0.0, 
					from:0.75, 
					afterFinish: function(){
						self.form.style.visibility = 'hidden';
						self.passwd.style.backgroundColor = 'white';
						self.passwd.value = '';
						self.status = false;
						
						}
					});
	}
}
//Galery Class
ImageGalery = Class.create();
ImageGalery.prototype = {
	initialize: function(imgArray, displayId, previewId, pageID, parameter) {
		this.par = parameter;
	this.img = imgArray;
	this.page = Array();
	this.pre = $(previewId);
	this.dis = $(displayId);
	this.pagelist = $(pageID);
	this.drag = Array();
	this.checkPar(); //parameter PrÃ¼ffen
	this.makePages();
	this.createPreview(0);
	var self = this;
	Droppables.add(this.dis.id, {accept: this.par.preStdClass, onDrop: function(element) { self.chengeImg(element, self)}})         
       },
       
       checkPar: function() {
       		if(this.par.imgDir == undefined)
       			this.par.imgDir = '/images/galery/';
       		if(this.par.smallImgDir == undefined)
       			this.par.smallImgDir = '/images/galery/small/';
       		if(this.par.overSuffix == undefined)
       			this.par.overSuffix = '_over';
       		if(this.par.imgFormat == undefined)
       			this.par.imgFormat = 'png';
       		if(this.par.smallImgWidth == undefined)
       			this.par.smallImgWidth = 70;
       		if(this.par.smallImgHeight == undefined)
       			this.par.smallImgHeight = 70;
       		if(this.par.tablePaddingt == undefined)
       			this.par.tablePadding = 25;
       		if(this.par.columns == undefined)
       			this.par.columns = 6;
       		if(this.par.rows == undefined)
       			this.par.rows = 6;
       		if(this.par.preStdClass == undefined)
       			this.par.preStdClass = 'previewImg';
			if(this.par.titleId == undefined)
				this.par.titleId = false;
			if(this.par.titleArray == undefined)
				this.par.titleArray = false;
       },
       makePages: function() {
       		if(this.img.length > this.par.rows) {
			var pl = this.par.rows * this.par.columns;
       			var x = Math.ceil(this.img.length / pl );
       			var s = 0;
       			var e = pl;
       			for (var i = 0; i < x; i++) {
       				this.page.push(this.img.slice(s, e))
       				s += pl;
       				if (e > this.img.length) 
       					e = this.img.length;
       				else e += pl;
       			}
       			
       		} else {
       			this.page.push(this.img)
       		}
       		
       		//delete this.img
       },
       createPreview: function(p) {
       		this.clearPreviewImg();
       		var self = this;
       		for (var i = 0; i < this.page[p].length; i++) {
       			//x y cordinaten
				var pos_x = i % this.par.columns * (this.par.smallImgWidth + this.par.tablePadding); 
				var pos_y = (((i % this.par.columns) - i) / this.par.columns) * -(this.par.smallImgHeight + this.par.tablePadding);

				//new Image
				var img = document.createElement("img");
				var imgSrc = document.createAttribute('src');
				imgSrc.nodeValue = this.par.smallImgDir + this.page[p][i] + '.' + this.par.imgFormat;
				img.setAttributeNode(imgSrc);
				
				var imgID = document.createAttribute('id');
				imgID.nodeValue =  this.page[p][i];
				img.setAttributeNode(imgID);
				
				var imgClass = document.createAttribute('class');
				imgClass.nodeValue = this.par.preStdClass;
				img.setAttributeNode(imgClass);
				
				img.style.position = 'absolute';
				img.style.top = pos_y + 'px';
				img.style.left = pos_x + 'px';
				
				img.onmouseover = function() {
					this.src = self.par.smallImgDir + this.id + self.par.overSuffix + '.' + self.par.imgFormat;
				}
				
				img.onmouseout = function() {
					this.src = self.par.smallImgDir + this.id + '.' + self.par.imgFormat;
				}
				
				this.pre.appendChild(img);
				this.drag.push(new Draggable(this.page[p][i], {revert:true}));
       		}
       },
       pageSwith: function(self, n) {
		while (this.pagelist.childNodes.length > 0) {
       			this.pagelist.removeChild(this.pagelist.childNodes[0]);
       		}
		for (var i = 0; i < this.page.length; i += 1) {
			if (n == i) {
				var txt = document.createTextNode(i + 1);
				this.pagelist.appendChild(txt);
				var sp = document.createTextNode(' ');
				this.pagelist.appendChild(sp);
			} else {
       				var a = document.createElement("a");
				var href = document.createAttribute("href");
				href.nodeValue = "javascript:" + self + ".createPreview(" + i + ");"  + self + ".pageSwith('" + self + "'," +  i  + ")";
				a.setAttributeNode(href);
				var txt = document.createTextNode(i + 1);
				a.appendChild(txt);
				this.pagelist.appendChild(a);
				var sp = document.createTextNode(' ');
				this.pagelist.appendChild(sp);
			}
		} 
       },
       chengeImg: function(element, self) {
       		self.clearDisplayImg();
       		var img = document.createElement("img");
       		var imgSrc = document.createAttribute('src');
			imgSrc.nodeValue = self.par.imgDir + element.id + '.' + self.par.imgFormat;
			img.setAttributeNode(imgSrc);
			img.style.border = '1px solid white';
			self.dis.appendChild(img);
			//Title ausgabe
			if(self.par.titleId && self.par.titleArray) {
				var id = self.searchArrayElem(self.img, element.id);
				$(self.par.titleId).firstChild.data = self.par.titleArray[id];
			}
       },
	   searchArrayElem: function  (sarray, selement) {
    		for (var i = 0; i < sarray.length; i++) {
				if (sarray[i] == selement) return i;	
    		}
    		return false;
		},
       clearDisplayImg: function() {
       		while (this.dis.childNodes.length > 0) {
       			this.dis.removeChild(this.dis.childNodes[0]);
       		}
       },
       clearPreviewImg: function() {
       		while (this.drag.length > 0) {
        		this.drag.shift().destroy();	
        	}
		/*
       		while (this.dis.childNodes.length > 0) {
       			this.dis.removeChild(this.dis.childNodes[0]);
       		}
		*/
       		while (this.pre.childNodes.length > 0) {
       			this.pre.removeChild(this.pre.childNodes[0]);
       		}
       },
       destruct: function () {
       		Droppables.remove(this.dis.id);
       		for(var i = 0; i < this.drag.length; i++) {
        		this.drag[i].destroy();
        		
        	}
       }
}
