var PTTools = {
	morph:function(root, callback){
		typeof root=="object"?null:root=document.getElementById(root);
		var standard= new Array();
		var args;
		var toggle=false;
		var obj = this;
		var frames = 30;
		var counter = 0;
		var ist=new Array();
		var soll=new Array();
		var erweiterung=new Array();
		var work=new Array();
		var interval;
		var setAbsolute=false;
		var timer=function(freqcallback){
			typeof freqcallback== "function"?fn = function(){obj.change(freqcallback)}:fn = function(){obj.change()}
			window.clearInterval(interval);
			interval = window.setInterval(function(){obj.change(freqcallback)}, 20);
		};
		this.change=function(freqcallback){
			if(typeof freqcallback=="function")freqcallback();
			for(var i=0; i<args.length;i++){
				root.style[args[i].att]=styleWork[work[i]](i);
			}
			counter<frames?counter++:clearT();
		};
		var clearT=function(){
			window.clearInterval(interval);
			counter = 0;
			if(typeof callback=="function")callback();
		};
		var workonStyle = function(att){
			return att.replace(/-/g,"");
		}
		var styleWork = {
				eese:function(numb){
					return erweiterung[numb].replace(/\?/gi, Math.round((soll[numb]-ist[numb])*Math.sin(counter/frames*(Math.PI/2))+ist[numb]));
				},
				linear:function(numb){
					return erweiterung[numb].replace(/\?/gi, Math.round((soll[numb]-ist[numb])*counter/frames+ist[numb]));
				},
				color:function(numb){
					var r=(soll[numb][0]-ist[numb][0])*counter/frames+ist[numb][0];
					var g=(soll[numb][1]-ist[numb][1])*counter/frames+ist[numb][1];
					var b=(soll[numb][2]-ist[numb][2])*counter/frames+ist[numb][2];
					return "rgb("+r+"%, "+g+"%, "+b+"%)";
				}
		}
		var GiveDec = function (Hex){
		   if(Hex == "A")
			  Value = 10;
		   else if(Hex == "B")
			  Value = 11;
		   else if(Hex == "C")
			  Value = 12;
		   else if(Hex == "D")
			  Value = 13;
		   else if(Hex == "E")
			  Value = 14;
		   else if(Hex == "F")
			  Value = 15;
		   else
			  Value = eval(Hex);
		   return Value;
		};

		var toRGB=function(color){
			if(color.indexOf("rgb")>-1){
				color = color.replace(/rgb\((.+)\)/gi,"$1");
				color = color.split(",");
				for(i in color)color[i]=color[i]*100/255;
				return color//"rgb("+color[0]+"%, "+color[1]+"%, "+color[2]+"%)";
			}else if(color.indexOf("#")>-1){
				color = color.toUpperCase();
				var a = GiveDec(color.substring(1, 2));
				var b = GiveDec(color.substring(2, 3));
				var c = GiveDec(color.substring(3, 4));
				var d = GiveDec(color.substring(4, 5));
				var e = GiveDec(color.substring(5, 6));
				var f = GiveDec(color.substring(6, 7));
				
				var x = (a * 16) + b;
				var y = (c * 16) + d;
				var z = (e * 16) + f;
				
				return toRGB("rgb("+x+","+y+","+z+")");
			}else if(color.indexOf("transparent")>-1){
				return "rgb(100%,100%,100%)";
			}else{
				return color;
			}
		}
		return{
			start:function(toggle, argumente, freqcallback, lastcallback){
				callback = lastcallback;
				counter = 0;
				if(!soll.length||toggle==false){
					args = argumente;
					for(var i=0; i<args.length;i++){
						//if(args[i].att=="top"||args[i].att=="left"||args[i].att=="right"||args[i].att=="bottom") root.style.position = "relative";
						var prob = window.getComputedStyle(root, "").getPropertyValue(args[i].att);
						if(args[i].att.match(/color/gi)){
							work[i]="color";
							ist[i] = toRGB(prob);
							soll[i]=toRGB(args[i].value);
						}else if(args[i].att.indexOf("%")>-1){
							work[i]="linear";
							ist[i]=parseInt(prob);
							soll[i]=args[i].value;
						}else{
							work[i]="eese";
							ist[i]=parseInt(prob);
							if(args[i].value=="max"){
								var tempIst=root.style[args[i].att];
								root.style[args[i].att]="auto";
								var tempSoll = root["offset"+args[i].att.slice(0,1).toUpperCase()+args[i].att.slice(1,args[i].att.length)];
								soll[i]=tempSoll;
								root.style[args[i].att] = tempIst;
							}else if(args[i].value=="min"){
								var tempIst=root.style[args[i].att];
								root.style[args[i].att]="auto";
								var tempSoll = 0;
								soll[i]=tempSoll;
								root.style[args[i].att] = tempIst;
							}else{
								soll[i]=args[i].value;	
							}
						}
						////DEFAULT STANDART ein mal setzen
						if(standard.length<args.length)standard[i] = ist[i];
						//
						erweiterung[i] = prob.replace(/[-\d+\.]+/gi, "?");
						args[i].att = workonStyle(args[i].att);
					}
				}else{
					var temp = soll;
					soll = ist;
					ist = temp;
				}
				timer(freqcallback);
			}
		}
	},
	drag:function(root, redx, redy, border, dolly, callOnUp, callOnMove){
		typeof root=="object"?null:root=document.getElementById(root);
		var thisObject = this;
		var startTop;
		var startLeft;
		var fn;
		var initDrag = function(e){
			if(dolly){
				var clone = root.parentNode.insertBefore(root.cloneNode(true), root.nextSibling);
				clone.className += " clone";
			}
			/*thisObject.morph =new PTTools.morph(root, function(){
				if(dolly)root.parentNode.removeChild(root.nextSibling);
				root.style.position = "static";
			});*/
			root.style.position = "absolute";
			startTop = parseInt(window.getComputedStyle(root, "").getPropertyValue("top"));
			startLeft = parseInt(window.getComputedStyle(root, "").getPropertyValue("left"));
			top = e.clientY-startTop;
			left = e.clientX-startLeft;
			fn = function(e){moveIt(e, top, left)};
			document.addEventListener("mousemove", fn, false);
			document.addEventListener("mouseup", fireDrag, false);
		}
		var moveIt=function(e, top, left){
			if(redx)root.style.left = Math.min(Math.max(e.clientX-left,border[3]!=''?border[3]:0), border[1]!=''?border[1]:1024)+"px";
			if(redy)root.style.top =Math.min(Math.max(e.clientY-top, border[0]!=''?border[0]:0), border[2]!=''?border[2]:768)+"px";
			if(typeof callOnMove=="function")callOnMove();
		}
		var fireDrag = function(){
			document.removeEventListener("mousemove", fn, false);
			document.removeEventListener("mouseup", fireDrag, false);
			if(typeof callOnUp=="function")callOnUp();
		}
		return{
			startDrag:function(e){
				initDrag(e);
			}
		}	
	},
	resizeOnScreen:function(root){
		typeof root=="object"? root=root: root = document.getElementById(root);
		refObjHeight = document.getElementById("mainContent").offsetHeight;
		refObjWidth = document.getElementById("mainContent").offsetWidth;
		var getScreenHeight = function(){
			document.documentElement.clientWidth ? wWidth = document.documentElement.clientWidth : wWidth = window.innerWidth;
			document.documentElement.clientHeight ? wHeight = document.documentElement.clientHeight : wHeight = window.innerHeight;
		}
		var anpassen = function(){
			getScreenHeight();
			wWidth = Math.max(refObjWidth, wWidth);
			wHeight = Math.max(refObjHeight, wHeight);
			var screen_w_h = wWidth/wHeight;
			var pic_w = 1280;
			var pic_h = 960;
			var pic_w_h = pic_w/pic_h;
			var scale_w_h;
			if(pic_w_h>=1){
				if (pic_w_h == screen_w_h) {
					scale_w_h = Stage.width/pic_w;
				} else if (pic_w_h<screen_w_h) {
					scale_w_h = wWidth/pic_w;
				} else if (pic_w_h>screen_w_h) {
					scale_w_h = wHeight/pic_h;
				}
			}else{
				if (pic_w_h == screen_w_h) {
					scale_w_h = wWidth/pic_w;
				} else if (pic_w_h>screen_w_h) {
					scale_w_h = wWidth/pic_w;
				} else if (pic_w_h<screen_w_h) {
					scale_w_h = wHeight/pic_h;
				}
			}
			if(root.hasChildNodes()){
				root.style.overflow = "hidden";
				root.firstChild.width = "auto";
				root.firstChild.style.width = Math.round(scale_w_h*pic_w)+"px";
				root.firstChild.style.height = Math.round(scale_w_h*pic_h)+"px";
				root.style.width = wWidth+"px";
				root.style.height = wHeight+"px";
			}
		}
		return{
			refreshIt:function(e){
				anpassen(e);
			}	
		}
	},
	picGrow:function(i, arr){
		var thisObj = this;
		root = arr[i];
		
		if(typeof root.style=="object"){
			root.style.cursor="pointer";
			root.style.cursor="hand";
		}
		this.popup = document.getElementById("popUps");
		init=function(){
			var newDiv = document.createElement("div");
			newDiv.id="bilderPopUp";
			newDiv.className="bilderPopUp";
			newDiv.style.left = "100px";
			newDiv.style.top = "100px";
			newDiv.style.cursor="move";
			newDiv.style.position = "absolute";
			this.newImg = document.createElement("img");
			this.newImg.src = root.src.replace(/^(.+)\/thumb_(.+)(\..+)$/gi, '$1/$2$3');
			this.newImg.onmousedown=function(e){
				PTTools.startdrag(e, this, newDiv);
				return false;
			}
			//this.mainimg.onmouseup=function(e){
			//	yPos = wind.style.top;
				//xPos = wind.style.left;
				//return false;
			//}
			var  menBut = document.createElement("a");
			menBut.href="#";
			menBut.className = "whiteBg closeBut";
			menBut.onclick=function(){
				delit();
				return false;
			}
			newclose = document.createTextNode(unescape("SCHLIE%DFEN"));
			menBut.appendChild(newclose);
			
			
			var  menButZ = document.createElement("a");
			menButZ.href="#";
			menButZ.className = "whiteBg prevBut";
			menButZ.onclick=function(){
				if(i>0){
					newImg.src = arr[i-1].src.replace(/^(.+)\/thumb_(.+)(\..+)$/gi, '$1/$2$3');
					i=i-1;
				}else{
					newImg.src = arr[arr.length-1].src.replace(/^(.+)\/thumb_(.+)(\..+)$/gi, '$1/$2$3');
					i=arr.length-1
				}
				return false;
			}
			newprev = document.createTextNode(unescape("ZUR%DCCK"));
			menButZ.appendChild(newprev);
			
			var  menButP = document.createElement("a");
			menButP.href="#";
			menButP.className = "whiteBg nextBut";
			menButP.onclick=function(){
				
				if(i<arr.length-1){
					newImg.src = arr[Number(i)+1].src.replace(/^(.+)\/thumb_(.+)(\..+)$/gi, '$1/$2$3');
					i=i+1;
				}else{
					newImg.src = arr[0].src.replace(/^(.+)\/thumb_(.+)(\..+)$/gi, '$1/$2$3');
					i=0;
				}
				return false;
			}
			newprev = document.createTextNode("VOR");
			menButP.appendChild(newprev);
			
			if(document.getElementById("bilderPopUp")==null){
				light();
				
				newDiv.appendChild(newImg);
				
				newDiv.appendChild(menBut);
				
				if(arr.length>1){
					newDiv.appendChild(menButZ);
					newDiv.appendChild(menButP);
				}
				
				thisObj.popup.appendChild(newDiv);
			}
			
			//alert(root.parentNode.offsetLeft);
			//alert(PTTools.getCompStyle(root, "left"));
		}
		light = function(){
			var knoten = document.getElementById("lightbox");
			knoten.style.display = "block";
			knoten.onclick=function(){
				delit();
				return false;
			}
		}
		delit=function(){
			document.getElementById("bilderPopUp").parentNode.removeChild(document.getElementById("bilderPopUp"));
			document.getElementById("lightbox").style.display = "none";
		}
		init();
	},
	getCompStyle:function(element, pseudoElt){
		if(window.getComputedStyle){
			return window.getComputedStyle(element, null).getPropertyValue(pseudoElt);
		}else if(element.currentStyle){
			//return element.runtimeStyle[pseudoElt]+element.runtimeStyle.marginLeft;
			return element.currentStyle[pseudoElt];
		}else{ 
			return false;
		}
	},
	startdrag:function(e, element, toMove){
		if(!e)e = window.event;
		thisobj = this;
		this.drag = true;
		function remove(){
			if(this.removeEventListener){
				document.removeEventListener("mousemove", move, false);
				document.removeEventListener("mouseup", remove, false);
			}else if(this.detachEvent){
				document.detachEvent("onmousemove", move);
				document.detachEvent("onmouseup", remove);
			}
			thisobj.drag=false;
		}
		
		if(document.addEventListener){
			document.addEventListener("mousemove", move, false);
			document.addEventListener("mouseup", remove, false);
		}else if(document.attachEvent){
			document.attachEvent("onmousemove", move);
			document.attachEvent("onmouseup", remove);
		}
		tx = parseInt(toMove.style.left);
		x = e.clientX;
		ty = parseInt(toMove.style.top);
		y = e.clientY;
		function move(e){
			if(!e)e = window.event;
			if(thisobj.drag){
				/*if(e.layerX){
					toMove.style.top = Math.min(Math.max(ty+e.clientY-y, 0), document.documentElement.clientHeight-toMove.offsetHeight)+"px";
					toMove.style.left = Math.min(Math.max(tx+e.clientX-x, 0), document.documentElement.clientWidth-toMove.offsetWidth)+"px";
				}else*/
				toMove.style.top = ty+e.clientY-y+"px";
				toMove.style.left = tx+e.clientX-x+"px";
				/*if(document.documentElement.clientHeight){
					toMove.style.top = ty+e.clientY-y+"px";
					toMove.style.left = Math.min(Math.max(tx+e.clientX-x, 0), document.documentElement.clientWidth-toMove.offsetWidth)+"px";
				}else{
					toMove.style.top =ty+e.clientY-y+"px";
					toMove.style.left = Math.min(Math.max(tx+e.clientX-x, 0), window.innerWidth-toMove.offsetWidth)+"px";
				}*/
			}
		}
		return{
			getPos:function(){
				var xpos="100px";
				return xpos;
			}
		}
		
		
	}
};