var px='px';

function el(id) {
	return document.getElementById(id);
}
function tag(tagname,attrs) {
	var ret=document.createElement(tagname);
	for(var attr in attrs) {
		ret.setAttribute(attr,attrs[attr]);
	}
	return ret;
}
function an(tonode,newnode) {
	return tonode.appendChild(newnode);
}


function ono(id,act,param) {
	return function () {
		switch(id) {
			case 'logo':
				el('logon').style.visibility=act?'visible':'hidden';
				break;
			case 'fotografinnen':
				for(var I in c) {
					if(a!=I)
						el('s'+I).className=act?'on':'off';
				}
				break;
			case 'name':
				if(act) ono('fotografinnen',true);
				el('n'+param).className=act?'on':'off';
				el('t'+param).className=act?'on':'off';
				if(a) el('t'+param).style.visibility=act?'visible':'hidden';
				if(!a) if(act) {
					sliderunning=false;
					var slideno=false;
					for(var I in slidearr) {
						if(slidearr[I]==param) slideno=I;
					}
					if(slideno!==false) {
						slidecnt=slideno*300;
						slide();
						slidecnt+=30;
						slide();
					}
					else {
						slidecnt=0;
						slide();
					}
				}
				else {
					sliderunning=true;
				}
				break;
			case 'shade':
				//ono('buttons',act);
				//ono('trigger',act,'close');
				el('buttons').style.display=act?'block':'none';
				el('bclose').className=act?'op100':'op15';
				el('shade').className=act?'op70':'op100';
				break;
			case 'buttons':
				el('buttons').style.display=act?'block':'none';
				break;
			case 'trigger':
				if(act) ono('buttons',act);
				el('bprev').className='op15';
				el('bnext').className='op15';
				el('bclose').className='op15';
				if(act) el('b'+param).className='op100';
				break;
		}
	}
}

function fakelert(msg) {
	return function () {alert(1);}
}

var slidef;

function init() {
	regomo();
	if(a) initbigshade();
	if(!a) {
		slidef=window.setInterval('slide()',20);
	}
	if(window.location.hash.substr(0,5)=='#foto') {
		hashno=parseInt(window.location.hash.substr(5));
		if(hashno) blowup(hashno);
	}
//	initsmallshades();
}

function initbigshade() {
	dw=Math.max(document.body.offsetWidth,el('textc').offsetLeft+el('textc').offsetWidth);
	dh=document.body.offsetHeight;
	for(var I in c[a]) {
		dh=Math.max(dh,c[a][I].kh+c[a][I].y);
		dw=Math.max(dw,el('bilder').offsetLeft+el('is'+I).offsetLeft+el('is'+I).offsetWidth);
	}
	el('shade').style.width=dw+'px';
	el('shade').style.height=dh+'px';
	//document.body.onclick=closedoc;
}
/*
function initsmallshades() {
	for(var I in i) {
		var ns=an(el('bilder'),tag('div',{'id':'shade'+I}));
		ns.className='smallshade';
		ns.onclick=blowup;
		ns.style.top=i[I].t+px;
		ns.style.left=i[I].l+px;
		ns.style.width=i[I].kw+px;
		ns.style.height=i[I].kh+px;
	}
}
*/
function regomo() {
	el('logo').onmouseover=ono('logo',true);
	el('logon').onmouseout=ono('logo',false);
	if(a) {
		el('fotografinnen').onmouseover=ono('fotografinnen',true);
		el('fotografinnen').onmouseout=ono('fotografinnen',false);
	}
	for(var I in c) {
		if(I!=a) {
			el('s'+I).className=a?'off':'on';
			el('n'+I).className='off';
			el('t'+I).style.visibility=a?'hidden':'visible';//'hidden';
			el('t'+I).className='off';
			el('n'+I).onmouseover=ono('name',true,I);
			el('n'+I).onmouseout=ono('name',false,I);
			el('t'+I).onmouseover=ono('name',true,I);
			el('t'+I).onmouseout=ono('name',false,I);
			if(!a) {
				var hasslide=false;
				for(var is in c[I]) hasslide=hasslide||c[I][is].s;
				if(hasslide) {
					el('slide'+I).onmouseover=ono('name',true,I);
					el('slide'+I).onmouseout=ono('name',false,I);
				}
			}
		}
	}
	if(a) {
		el('triggers').onmouseover=ono('buttons',true);
		el('triggers').onmouseout=ono('buttons',false);
		el('shade').onmouseover=ono('shade',true);
		el('shade').onmouseout=ono('shade',false);
		el('shade').onclick=closedoc;
		el('canvas').onmouseover=ono('buttons',true);
		el('canvas').onmouseout=ono('buttons',false);
		el('tprev').onmouseover=ono('trigger',true,'prev');
		el('tprev').onmouseout=ono('trigger',false,'prev');
		el('tprev').onclick=trigger;
		el('tclose').onmouseover=ono('trigger',true,'close');
		el('tclose').onmouseout=ono('trigger',false,'close');
		el('tclose').onclick=closedoc;
		el('tnext').onmouseover=ono('trigger',true,'next');
		el('tnext').onmouseout=ono('trigger',false,'next');
		el('tnext').onclick=trigger;
		el('bprev').onmouseover=ono('trigger',true,'prev');
		el('bprev').onmouseout=ono('trigger',false,'prev');
		el('bprev').onclick=trigger;
		el('bclose').onmouseover=ono('trigger',true,'close');
		el('bclose').onmouseout=ono('trigger',false,'close');
		el('bclose').onclick=closedoc;
		el('bnext').onmouseover=ono('trigger',true,'next');
		el('bnext').onmouseout=ono('trigger',false,'next');
		el('bnext').onclick=trigger;
	}
}


function trigger(e) {
	if(!e) e=window.event;
	if(e) {
		var t=e.srcElement||e.target;
		var newno=t.id.substr(1)=='prev'?prevno:nextno;
		blowdown();
		blowup(newno);
	}
//	blowno(
}

var prevno,nextno;

function blowup(no) {
	if(isNaN(no)) {
		if(!no) {
			no=window.event;
			no=no.srcElement.id.substr(5);
		}
		else {
			no=no.target.id.substr(5);
		}
	}
	el('shade').style.display='block';
	el('canvas').style.display='block';
	el('triggers').style.display='block';
	icnt=0;
	for(var I in c[a]) {
		el(I).style.display='none';
		icnt++;
	}
	var ai=el('i'+no)
	ai.style.display='block';
	var l=screencenterx()-Math.round(c[a]['i'+no].gw/2);
	var t=screencentery()-Math.round(c[a]['i'+no].gh/2);
	ai.style.left=l+px;
	ai.style.top=t+px;
	ai.style.width=c[a]['i'+no].gw;
	ai.style.height=c[a]['i'+no].gh;
	ai.style.visibility='visible';
	el('triggers').style.left=l+px;
	el('triggers').style.top=t+px;
	el('triggers').style.width=c[a]['i'+no].gw+px;
	el('triggers').style.height=c[a]['i'+no].gh+px;
	el('canvas').style.left=(l-40)+px;
	el('canvas').style.top=(t-40)+px;
	el('canvas').style.width=(c[a]['i'+no].gw+80)+px;
	el('canvas').style.height=(c[a]['i'+no].gh+80)+px;
	el('grossebilder').style.display='block';
	prevno=(no+icnt-2)%icnt+1;
	el('bpreva').href='#foto'+prevno;
	el('tprev').href='#foto'+prevno;
	nextno=no%icnt+1;
	el('bnexta').href='#foto'+nextno;
	el('tnext').href='#foto'+nextno;
	on=true;
}

function blowdown() {
	el('shade').style.display='none';
	el('canvas').style.display='none';
	el('triggers').style.display='none';
	for(var I in c[a]) {
		el(I).style.width='10px';
		el(I).style.height='10px';
		el(I).style.top='0px';
		el(I).style.left='0px';
		el(I).style.visibility='hidden';

		//el('i'+I).style.display='none';

	}
}

function screencenterx() {
	var halfw=document.body.clientWidth;
	if(isNaN(halfw)) {
		halfw=window.innerWidth;
		if(isNaN(halfw)) {
			halfw=1000;
		}
	}
	halfw=Math.round(halfw/2);
	var scrollw=document.body.scrollLeft;
	if(isNaN(scrollw)) {
		scrollw=document.documentElement.scrollLeft;
		if(isNaN(scrollw)) {
			scrollw=window.pageXOffset;
			if(isNaN(scrollw)) {
				scrollw=0;
			}
		}
	}
	return halfw+scrollw;
}
function screencentery() {
	var halfh=document.body.clientHeight;
	if(isNaN(halfh)) {
		halfh=window.innerHeight;
		if(isNaN(halfh)) {
			halfh=700;
		}
	}
	halfh=Math.round(halfh/2);
	var scrollh=document.body.scrollTop;
	if(isNaN(scrollh)) {
		scrollh=document.documentElement.scrollTop;
		if(isNaN(scrollh)) {
			scrollh=window.pageYOffset;
			if(isNaN(scrollh)) {
				scrollh=0;
			}
		}
	}
	return halfh+scrollh;
}

function closer(e) {
/*	if(!e) e=window.event;
	alert(e);
	e.returnValue=false;
	return false;
	e.preventDefault();*/
}

function closedoc(e) {
	if(!e) e=window.event;
	if(e) {
		var t=e.srcElement||e.target;
		//alert(t.className);
		if((t.className!='smallshade') && (t.className!='bild') && on && (t.className!='grossbild')) blowdown();
	}
}

var pollcnt=0;

function poll() {
	pollcnt++;
	var fertig=true;
	for(var I in c[a]) {
		var is=c[a][I];
		var io=el(I);
		if(io) {
			if(io.complete) {
				el('shade'+I).style.display='none';
			}
			else {
				fertig=false;
			}
		}
	}
	if((pollcnt>200)||fertig) {
		window.clearInterval(pollf);
	}
}

function setopa(el,opa) {
	el.style['opacity']=opa;
	intopa=Math.round(opa*100);
	el.style['-ms-filter']="progid:DXImageTransform.Microsoft.Alpha(Opacity="+intopa+")";
	el.style['filter']='alpha(opacity='+intopa+')';
}

var slidecnt=0;
var aslide=null;
var sliderunning=true;

function slide() {
	var frame=slidecnt%300;
	if(frame==0) {
		if(aslide) aslide.style.display='none';
		for(var I in slidearr) {
			el('slide'+slidearr[I]).style.display='none';
		}
		var aslideno=Math.floor(slidecnt/300)%slidearr.length;
		aslide=el('slide'+slidearr[aslideno]);
		setopa(aslide,0);
		aslide.style.visibility='visible';
		aslide.style.display='block';
	}
	else if(frame<=30) {
		setopa(aslide,frame/30);
	}
	else if(frame>=270) {
		setopa(aslide,(300-frame)/30);
	}
	if(sliderunning) slidecnt++;
}
