// http://css-tricks.com/3458-perfect-full-page-background-image/
// background-size:cover; is slow with opacity,... and need fallback anyhow ... therefore...resetImage();
// ratio and dimensions have to be calculated live
// reprogram resetImage
(function (id, requestURL, ratio, delay, speed) {
	/*! jsMorph v0.4.0 http://jsMorph.com/ | http://jsMorph.com/license */var jsMorph=function(g,a,e,f,j,i,c){var k=(function(y,p,l,E,v,u){var o={},A,H,I,m=y.style,z,q,x,w=0,D,G,F=[],s=function(n){return[n.offsetWidth,n.offsetHeight,n.offsetLeft,n.offsetTop]},r=function(){if(document.body.currentStyle){return function(n,J){return n.currentStyle[J.replace(/\-(\w)/g,function(){return arguments[1].toUpperCase()})]}}else{return function(n,J){return document.defaultView.getComputedStyle(n,null).getPropertyValue(J)}}}(),C=function(M){var K={},N,n=M.style,L=document.createElement("div"),J=L.style;J.cssText="position:absolute;left:0;top:-10ex;width:10em;height:72pt;";M.appendChild(L);N=s(L).concat(s(M));K={em:N[0]/10,pt:N[1]/72,pc:N[1]/6,"in":N[1],cm:N[1]/2.54,mm:N[1]/25.4,ex:Math.abs(N[3])/10,"%font":N[0]/1000,"%line":N[0]/1000};J.cssText+=";font-size:"+p["font-size"]+(p["font-family"]?";font-family:"+p["font-family"]:"")+";";N=s(L);M.removeChild(L);K.exn=Math.abs(N[3]/10);q=n.cssText;n.cssText+=";position:absolute;left:0%;top:0%;";N=s(M);n.cssText+=";left:-100%;top:-100%;";N=N.concat(s(M));n.cssText=q;K["%outX"]=(N[2]-N[6])/100;K["%outY"]=(N[3]-N[7])/100;return K},t=function(J,L,K){if(J[2]!="px"&&J[2]!=""){if(J[2]!="%"){J[1]*=K[J[2]]}else{if(!L.match(/^(font)/)){J[1]*=L.match(/width|left|right|padding|margin|text-ind/)?K["%outX"]:K["%outY"]}else{J[1]*=K["%"+L.split("-")[0]]}}}};y.initStyle=y.style.cssText;if(p["font-size"]){z=/([\-0-9\.]+)([a-z%]+)(!*)/.exec(p["font-size"])}for(var B in p){if(!(B.replace(/\-(\w)/g,function(){return arguments[1].toUpperCase()}) in m)){delete (p[B]);continue}I=/([\-0-9\.]+)([a-z%]*)(\s*!*)/.exec(p[B])||[];p[B]=p[B].toString().replace("!","");H=/([\-0-9\.]+)(\D*)/.exec(r(y,B.match(/^(padding|margin)$/)?B+"-left":B.match(/^(border-width)$/)?"border-left-width":B.match(/(border-radius)$/)?(x=(B.match(/^(\-\w+\-)/)||["",""])[1])+"border-"+(x=="-moz-"?"radius-topleft":"top-left-radius"):B))||["0px",0,"px"];if(!H[1]&&B.match(/^(height|width)/)){m.zoom=1;F[0]=/([\-0-9\.]+)([a-z%]*)/.exec(r(y,"padding-"+(B=="width"?"left":"top")));F[1]=/([\-0-9\.]+)([a-z%]*)/.exec(r(y,"padding-"+(B=="width"?"right":"bottom")));if((F[0]&&F[0][2]!="px")||(F[1]&&F[1][2]!="px")){t(F[0],"padding",A||(A=C(y)));t(F[1],"padding",A)}H=["",(B=="width"?y.clientWidth:y.clientHeight)-F[0][B=="width"?0:1]-F[1][B=="width"?0:1],"px"]}if((H[2]&&H[2]!="px")||(H[2]!=I[2])){t(H,B,A||(A=C(y)));t(I,B,A);if(z&&B!="font-size"){if(I[2]=="em"){I[1]*=z[1]/(z[2]!="em"?A.em:1)}if(I[2]=="ex"){I[1]*=A.exn/A.ex}}}H[1]=parseFloat(H[1]);D=parseFloat(I[1])-H[1];if(w<Math.abs(D)){w=D}o[B]={full:H[1],delta:D,unit:I[2]==""?"":"px"}}w=12+Math.abs(l/w*(!E&&E!=0?1:E));if(u.speed&&w>u.speed){}else{u.speed=w}o.speed=w;if(v){v(o,A)}return o}),b=function(l){if(!l){return new Date().getTime()-b.sT||0}else{b.sT=new Date().getTime();return 0}},d=function(r,u,B,v,C,s,q){var A,z,l=false,D,w,t,E,p,o;for(var y=C.length;y--;){D=C[y];E=D.params;p=D.prop;o=D.initProp;w=r-(D.newTime||0)-E.delay;if(s&&!s[y]&&!D.newTime){continue}A="";if(w>0){t=(B||D.obj.backwards);z=D.ease(w/E.duration);if(w<E.duration){l=true;D.done=null;for(var x in p){A+=";"+x+":"+(o[x].full+(!t?z:1-z)*o[x].delta)+o[x].unit}D.objStyle.cssText+=A}else{if(!D.done){if(E.doEnd){if(!t){for(var x in p){A+=";"+x+":"+p[x]}D.objStyle.cssText+=A}else{D.objStyle.cssText=D.obj.initStyle}}else{for(var x in p){A+=";"+x+":"+(o[x].full+(!t?o[x].delta:0))+o[x].unit}D.objStyle.cssText+=A}if(D.onMorphEnd){D.onMorphEnd(D.obj,u(),v,o.speed,q.speed,D.objStyle.cssText)}D.done=true;D.newTime=null}}if(D.onMorph){D.onMorph(D.obj,D.objStyle,r,v+1,o.speed,q.speed,z,D.objStyle.cssText)}}else{l=true}}if(l){q.timer=window.setTimeout(function(){d(u(),u,B,++v,C,s,q)},q.speed)}else{q.timer=null;if(y<=0&&q.onMorphEnd){q.onMorphEnd(s,u(),v,q.speed)}}},h=[];this.reset=function(t,l,r,s,v,u,o){for(var p=h.length;p--;){for(var q in h[p]){q=null}h[p]=null;h.pop()}if(t){this.concat(t,l,r,s,v,u,o)}return this};this.init=function(r){var q=h.length,p,o;for(var s=r?q-1:0,l=q;s<l;s++){if(h[s].obj.initStyle!=undefined&&r==undefined){p=h[s].obj.style;o=p.cssText;if(h[s].obj.initStyle!=o){p.cssText=h[s].obj.initStyle}else{o=null}}h[s]["initProp"]=k(h[s].obj,h[s].prop,h[s].params.duration,h[s].params.speed,h[s].onMorphInit,this);if(o&&r==undefined){p.cssText=o}}return this};this.concat=function(o,t,r,q,m,p,l){if(!o.pop&&!o.item){o=[o]}if(!r){r={}}for(var s=o.length;s--;){if(typeof o[s]=="string"){o[s]=document.getElementById(o[s])}h[h.length]={obj:o[s],objStyle:o[s].style,prop:t,params:{duration:r.duration||500,delay:r.delay||0,speed:r.speed!=undefined?r.speed:1,doEnd:r.doEnd!=undefined?r.doEnd:true},ease:q||function(u){return u},onMorphInit:m,onMorph:p,onMorphEnd:l};this.init(true)}return this};if(g){this.concat(g,a,e,f,j,i,c)}this.stop=function(l){window.clearTimeout(this.timer)};this.start=function(p){var o=b();if(p&&(p.pop||p.item)){arguments=p}for(var q=[],r=arguments.length;r--;){for(var l=h.length;l--;){if(h[l].obj==arguments[r]){h[l].newTime=this.timer?o:0.1;q[l]=true}}}window.clearTimeout(this.timer);d(this.timer?o:b(true),b,this.backwards,1,h,q.length?q:null,this);return this}},
	// image thingy
	imgTimer,
	resetImage = (function (id, ratio) {
		var container = document.getElementById(id), imgs = container.getElementsByTagName('img'), img0 = imgs[0].style, img1 = imgs[1].style;
		return function() {
			var dims = [container.offsetWidth,container.offsetHeight], cssText;
			if (dims[0]/dims[1]>=ratio) { // ';position:absolute;top:'+(-((imgs[0].height-dims[1])/2))+'px;left:0;width:100%;height:auto;';
				cssText = ';position:absolute;top:'+(-(imgs[0].offsetHeight-dims[1])/2)+'px;left:0;width:'+dims[0]+'px;height:'+(dims[0]/ratio)+'px;';
				img0.cssText += cssText; img1.cssText += cssText;
			}	else { // ';position:absolute;left:'+(-((imgs[0].width-dims[0])/2))+'px;top:0;width:auto;height:100%;';
				cssText = ';position:absolute;left:'+(-(imgs[0].offsetWidth-dims[0])/2)+'px;top:0;width:'+(dims[1]*ratio)+'px;height:'+dims[1]+'px;';
				img0.cssText += cssText; img1.cssText += cssText;
			}
		};
	})(id, ratio),	
	ajax = (function() {
		var req = false;
		if (window.XMLHttpRequest) req = new XMLHttpRequest();
		else if (window.ActiveXObject) {
			try{req = new ActiveXObject('Msxml2.XMLHTTP')} catch(e){
				try {req = new ActiveXObject('Microsoft.XMLHTTP');}	catch(e){}
			}
		};
		if (!req) return false;
		else return function (url, callback, methode) {
			req.open(methode||'GET', url, true);
			req.setRequestHeader('X-Referer', document.location);
			req.send(null);
			req.onreadystatechange = function(){if(req.readyState == 4) if(req.status == 200) callback(req.responseText)};
		}
	})(),
	getImgs = (function(txt) {
		var imgs = document.getElementById(id).getElementsByTagName('img');
		return function(txt, nr) {
			if (nr == undefined) loadSpinner.style.display = 'none';
			if (txt) getImgs.txt = txt.split(/\n/);
			if(getImgs.txt) while(getImgs.txt[getImgs.txt.length-1] == '') getImgs.txt.pop(); 
			else {window.setTimeout(function(){ajax(requestURL, getImgs)}, 5000); return} // auto reload
			if (getImgs.txt.length < 2) return;
			if (slides && !slides.reloadDone) {
				for(var n=0, m=slidesImgs.length; n<m; n++) {slidesImgs[n].src = imagePath+'icon/'+getImgs.txt[n];}
				slides.reloadDone = true;
			}
			// alert(getImgs.txt.length);
			if (nr == undefined) getImgs.img = getImgs.img == undefined || getImgs.img ? 0 : 1;
			if (getImgs.img == undefined)  getImgs.img = 0;
			if (nr == undefined) getImgs.current = getImgs.current == undefined ? 0 : getImgs.imgCount;
			getImgs.imgCount = nr!=undefined ? nr : (getImgs.imgCount == undefined ? 1 : ++getImgs.imgCount%getImgs.txt.length);
			
			imgs[getImgs.img].onload = function(){
				imgTimer = window.setTimeout(function(){
					moveSlides(getImgs.imgCount);fadeLayer(imgs[1],getImgs.img?0:1, speed, getImgs)
					if (slides) slides.children[1].innerHTML = (getImgs.imgCount+1)+' / '+slidesImgs.length;
				}, nr!=undefined ? 10 : delay);
			}; // note: imgs[getImgs.img].complete
			imgs[getImgs.img].src = ''; imgs[getImgs.img].src = imagePath+getImgs.txt[getImgs.imgCount];
		}
	})(),
	fadeLayer = (function() {
		var IE = document.createStyleSheet && document.all, IE8 = IE && document.querySelectorAll,
				opacity = IE8 ? '-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=' : IE ? 'filter:alpha(opacity=' : 'opacity:',
				timer = function(get) {
					if (get) return new Date().getTime()-timer.sT; 
					else {timer.sT = new Date().getTime(); return 0}
				};
		return function(obj, upDown, speed, callback, trigger) {
			var steps = Math.floor(timer(trigger)/(speed/100)), objStyle = obj.style;
			loadSpinner.style.display = 'none';
			if (steps > 100) steps = 100;
			objStyle.cssText += ';'+opacity+((upDown ? (100-steps)/100 : steps/100)*(IE ? 100 : 1))+(IE8 ? ')";' : IE ? ');' : ';');
			getImgs.bussy = steps == 100 ? false : true;
			if (steps < 100) window.setTimeout(function(){fadeLayer(obj, upDown, speed, callback, true)}, 22); else if (callback) callback();
		}
	})();
	
	// text-hide
	var text = document.getElementById('text-container'), myMorph,
			imgs = document.getElementById(id).getElementsByTagName('img');
	if (text)  {
		myMorph = new jsMorph(text,
			{width: '0px', height: '0px'},
			{duration: 700, speed:3},
			function(n) {return (n=n-1)*n*n*n*n+1},
      null,
      null,
      function(obj){obj.backwards = obj.backwards ? false : true; if (!obj.backwards) obj.children[0].style.overflow = ''}
		).concat(text,
			{'padding-top':'0px', 'padding-right':'0px', 'padding-bottom':'0px', 'padding-left':'0px'},
			{duration: 200}
		);
		
		imgs[1].onclick = text.onclick = function(e) {
			// if (slides && (slides.isOn || slides.isRunning)) return;
			var e = e||window.event, obj = e.target || e.srcElement;
			if (obj.nodeName && obj.nodeName.toLowerCase() == 'a') return;
			text.children[0].style.overflow = 'hidden';
			myMorph.start();
		}
	};
	
	var browserHeight= function() {
		if(typeof(window.innerHeight) == 'number') return function() {return window.innerHeight}; //Non-IE
		else if(document.documentElement && document.documentElement.clientHeight) return function() {return document.documentElement.clientHeight}; //IE 6+ in 'standards compliant mode'
		else if(document.body && document.body.clientHeight) return function() {return document.body.clientHeight}; //IE 4 compatible
	}(),
	currentBrowserHeight = browserHeight();

	// slide bar
	var getMousePos = function(e) {
		getMousePos = (typeof e.pageX === 'number' && !window.opera) ?
			function(e,xy) {return [e.pageX,e.pageY]} :
			function(e,xy) {return [e.clientX,e.clientY]};
		return getMousePos(e);
	},
	slides = document.getElementById('img-slides'), 
	slidesMorph, slideMorph = new jsMorph, slidesImgs, slidesWidth, slideWidth;
	
	if (slides) {
		slidesImgs = slides.children[0].children;
		slidesWidth = slides.offsetWidth;
	}
	
	var moveSlides = function(imgCount){
		if (!slides) return;
		slideWidth = slidesImgs[1].offsetLeft-slidesImgs[0].offsetLeft;
		// window.console.log(imgCount+', '+getImgs.current);
		slidesImgs[getImgs.current].className = slidesImgs[getImgs.current].className.replace(/ active/g,'');
		slidesImgs[getImgs.imgCount].className += ' active';
		slideMorph.reset(slides.children[0],
			{left:slidesWidth/2-(slideWidth*imgCount+(slideWidth/2))+'px'},
			null,
			function(n) {return -(--n*n*n*n-1)}
		).start();
		//slides.children[0].style.left = slidesWidth/2-(slideWidth*(imgCount+1))+'px';
	};
	
	if (slides) slidesMorph = new jsMorph('img-slides',
		{height:'104px'},
		{duration: 500, speed:3},
		function(n) {return -(--n*n*n*n-1)},
		null,
		null,
		function(obj){
			obj.backwards = obj.backwards ? false : true;
			obj.isRunning = false;
			// if (text && !obj.backwards) text.backwards = false;
		}
	);/*.concat('text-container',
		{bottom:'180px'},
		{duration: 500, speed:3},
		function(n) {return -(--n*n*n*n-1)}
	);*/
	
	if (slides) window.document.onmousemove = function(e) {
		var e = e||window.event, isText = text ? (text.offsetHeight ? true : false) : false;
		if (slides.isRunning) return;
		if (getMousePos(e)[1] > currentBrowserHeight/1.8) { // window.console.log(browserHeight());
			if (!slides.isOn) {
				slides.isOn = true;
				slides.isRunning = true;
				
				// if (text && isText) text.backwards = false;
				slidesMorph.start(isText ? null : slides);
			}
		} else {
			if (slides.isOn) {
				slides.isOn = false;
				slides.isRunning = true;
				// myMorph.init();
				// if (text && isText) text.backwards = true;
				slidesMorph.start(isText ? null : slides);
			}
		}
	}
	// in onload...
	if (slides) for(var n=slidesImgs.length; n--;) slidesImgs[n].me = n;
	if (slides) slides.onclick = function(e) {
		var e = e||window.event, obj = e.target || e.srcElement;
		if (obj && obj.me != undefined && !getImgs.bussy) {
			window.clearTimeout(imgTimer);
			getImgs(null, obj.me);
			loadSpinner.style.display = 'block';
		}
	}
	
	//...loading spinner
	var loadSpinner = document.getElementById('loadSpinner'),
    createSpin = function (obj, num, d, bg) {
      var newDiv = '', r = (obj.offsetHeight-d)/2;
      for (var n=num; n--;)
				newDiv += '<div style="position:absolute;'+
					'left:'+(r*Math.sin(n*2*Math.PI/num)+r)+'px;top:'+(r*Math.cos(n*2*Math.PI/num)+r)+'px;height:'+d+'px; width:'+d+'px;'+
					'background-color:'+bg+'; border-radius:'+d+'px; -moz-border-radius:'+d+'px; -o-border-radius:'+d+'px; -webkit-border-radius:'+d+'px;'+
					'line-height:0; overflow:hidden;">&nbsp;</div>';
      obj.innerHTML = newDiv;
    }(loadSpinner, 12, 16, '#aaa'),
    circles = loadSpinner.childNodes, cL = circles.length, count = 0,
    spinMorph = new jsMorph(circles, {opacity:0, width:'1px', height: '1px'}, {duration:1000, speed:8});

	window.setInterval(function() {spinMorph.start(circles[count++]); if (count >= cL) count=0}, 60);
	
	
	window.onresize = function() {
		resetImage();
		if (text) myMorph.init();
		currentBrowserHeight = browserHeight();
		if (slides) slidesWidth = slides.offsetWidth;
	};
	
	window.onload = function() {
		var imageCounter = document.createElement('div');
		ajax(requestURL, getImgs);
		resetImage(); resetImage();
		if (slides) {
			m = slidesImgs.length;
			imageCounter.className = 'image-counter';
			imageCounter.innerHTML = '1 / '+slidesImgs.length;
			slides.appendChild(imageCounter);
			
		}
	};

	
})('img-container', 'test.php'+window.location.search+(doList ? (window.location.search ? '&' : '?')+'list='+doList : ''), 1.5, 6000, 500);
