var $aLoads= [], $imgCurrent= null, $aImages= [];


function callLoads() {
	for (var i= 0; i< $aLoads.length; i++)
		$aLoads[i]();
}

function addLoad(f) {
	$aLoads.push(f);
}



function makeThumbImages(iCount, sText, sImage) {
	var x= [];
	for (var i= 0; i< iCount; i++) {
		var img= $('thumb_'+ i);
		img.cctext= sText;
		img.ccimage= sImage;
		img.onclick= enlargeImage.b(null, img, i, iCount);
		img.onmouseover= $E.css.b(null, img, 'hover', true);
		img.onmouseout= $E.css.b(null, img, 'hover', false);
		
		if (img.src.match(/^(.*\/)([^\/]+)(\/[^\/]+)$/))
			img.cclarge= RegExp.$1+ 'large'+ RegExp.$3;
		x.push(img.cclarge);
		$aImages.push([img, img.cclarge]);
	}
	
	$(sImage+ '_navi_close').onclick= function(sText, sImage) {
		$E.show(sText);
		$E.hide(sImage+ '_container');
		if ($imgCurrent) {
			$E.css($imgCurrent, 'selected', false);
			$imgCurrent.isshown= false;
		}
	}.b(null, sText, sImage);
	
	
}

function enlargeImage(img, iC, iMax) {
	if (img.isshown) return;
	img.isshown= true;
	if ($imgCurrent!= null) {
		$E.css($imgCurrent, 'selected', false);
		$imgCurrent.isshown= false;
	}
	
	$imgCurrent= img;
	$E.css(img, 'selected', true);
	
	$E.hide(img.cctext);
	$E.show(img.ccimage+ '_container');
	
	var out= $(img.ccimage+ '_out');
	out.innerHTML= '';
	
	// -- create large image
	var imgLarge= new Image();
	var cntPreload= document.createElement(imgPreload);
	cntPreload.className= 'imagepreload';
	
	var imgPreload= new Image();
	imgPreload.src= '/images/loader.gif';
    
    imgLarge.title = img.title;
	
	
	try{
		cntPreload.appendChild(imgPreload);
		$EV.obs(imgLarge, 'load', function(out, imgLarge, cntPreload, photographer) {
			out.replaceChild(imgLarge, cntPreload);
		}.b(null, out, imgLarge, cntPreload, img.title));
		imgLarge.src= $aImages[iC][1];
		out.appendChild(cntPreload);
		
	} catch(e) { // -- safari: no spreading, but still ... rubbish
		imgPreload= document.createElement('img');
		imgPreload.src= $aImages[iC][1];
		out.appendChild(imgPreload);
	}
	
	
	
	//out.appendChild($aImages[iC][1]);
	
	if (img.alt!= '') {
		var p= document.createElement('p');
		var sDesc= img.alt.replace(/\*\*/g, '<br />'); 
		p.innerHTML= sDesc;
		out.appendChild(p);
	}
	
	var prev= $(img.ccimage+ '_navi_prev');
	var next= $(img.ccimage+ '_navi_next');
	
	if (iC<= 0) 
		$E.hide(prev);
	else {
		$E.show(prev);
		prev.onclick= enlargeImage.b(null, $aImages[iC- 1][0], iC- 1, iMax);
	}
	
	if (iC+ 1< iMax) {
		$E.show(next);
		next.onclick= enlargeImage.b(null, $aImages[iC+ 1][0], iC+ 1, iMax);
	} else 
		$E.hide(next);
		
		
	
}



function submitGB() {
	var name= $EL('gname');
	var msg= $EL('gtext');
	var submit= $EL('gsubmit');
	if (submit.submitting) return;
	submit.css('disabled');
	submit.submitting= true;
	
	if (name.value!= '' && msg.value!= '') {
		var sName= name.value, sMsg= msg.value;
		name.disabled= true;
		name.css('disabled');
		msg.disabled= true;
		msg.css('disabled');
		w54.quickrequest(
			document.location.href,
			null,
			{
				name: sName,
				message: sMsg,
				insertgb: 1
			},
			function(name, msg, res) {
				
				name.disabled= false;
				msg.disabled= false;
				msg.css('disabled', false);
				name.css('disabled', false);
				submit.submitting= false;
				submit.css('disabled', false);
				name.value= '';
				msg.value= '';
				
				var d= new Date();
				var dselect= $('gdate').value.split(/\-/);
				dselect[0]= parseInt(dselect[0]); dselect[1]= parseInt(dselect[1]);
				if (dselect[0]== d.getFullYear() && dselect[1]== d.getMonth()+ 1) {
					var out= $('gout');
					var sHTML= out.innerHTML;
					out.innerHTML= res+ sHTML;
					w54.defx('highlight', out.getElementsByTagName('div')[0], {endcolor: '#000000'});
				} else {
					err('Thanks for your comment', '000000');
				}
				
				
			}.b(null, name, msg)
		);
	} else {
		err('Insert Name / EMail and Message');
		if (name.value== '')
			name.css('error');
		if (msg.value== '')
			msg.css('error');
		
		name.obs('blur', function() {
			if (this.value!='')
				this.css('error', false)
		}.b(name));
		msg.obs('blur', function() {
			if (this.value!='')
				this.css('error', false)
		}.b(msg));
		submit.submitting= false;
		submit.css('disabled', false);
	}
}


function printGB() {
	document.write('<form class="gbook" action="/" onsubmit="return false" method="post">');
	document.write('	<label for="gname">');
	document.write('		<span>Name / E-Mail:</span>');
	document.write('		<input type="text" name="name" id="gname" />');
	document.write('	</label>');
	document.write('	<label for="gtext">');
	document.write('	<span>Message:</span>');
	document.write('		<textarea cols="3" rows="3" name="text" id="gtext"></textarea>');
	document.write('	</label>');
	document.write('	<a id="gsubmit" class="submitbtt" href="javascript:submitGB()">submit</a>');
	document.write('</form>');
		
}


function gotoGB(obj) {
	if (obj.value.match(/^([0-9]{4})\-([0-9]{2})$/)) {
		document.location.href= '/Guestbook-'+ obj.value+ '.html';
	}
}

function deleteGB(id) {
	var entry= $EL('entry_'+ id);
	if (!entry) return;
	w54.quickrequest(
		document.location.href,
		null,
		{
			deletegb: 1,
			deleteid: id
		},
		function(entry, res) {
			if (res== '1') {
				entry.efx('highlight', {
					endcolor: '#ff0000',
					finish: function(entry) {
						var p= entry.parentNode;
						p.removeChild(entry);
					}.b(null, entry)
				});
			} else {
				err('cant do this');
			}
			
		}.b(null, entry)
	);
}





var $map= null; var $hArtistButtons= {}, $currentArtist= null;
function initMap() {
	$map= $('map');
	
	var aArtists= ['akimonemachine', 'blu', 'jr', 'kamisasu', 'mare', 'drinnocent', 'mrhorse', 'nixfitti', 'osgemeosnina', 'zasd', 'zevs'];
	
	for (var i= 0; i< aArtists.length; i++) {
		
		// -- preload
		var img= new Image();
		img.src= '/images/karte-'+ aArtists[i]+ '.gif';
		$hArtistButtons[aArtists[i]]= img;
		
		// -- bind
		$EV.obs($('artist_'+ aArtists[i]), 'mouseover', function(i) {
			$('map').style.backgroundImage= 'url(/images/karte-'+ this[i]+ '.gif)';
		}.b(aArtists, i));
		$EV.obs($('artist_'+ aArtists[i]), 'mouseout', function() {
			$('map').style.backgroundImage= 'url(/images/karte.gif)';
		}.b(aArtists));
	}
	
	var positions= {
		akimonemachine: [
			[334, 245],
			[357, 240],
			[401, 232]
		], 
		
		blu:  [
			[390, 245],
			[408, 227]
		], 
			
		jr: [
			[398, 213]
		], 
			
		kamisasu: [
			[448, 187]
		], 
			
		mare: [
			[324, 255]
		], 
			
		mrhorse: [
			[322, 271],
			[346, 244]
		], 
			
		nixfitti: [
			[334, 251]
		], 
			
		osgemeosnina: [
			[423, 205]
		], 
			
		drinnocent: [
			[235, 319],
			[257, 306]
		], 	
			
		zasd: [
			[351, 237]
		], 
			
		zevs: [
			[377, 256],
			[357, 217]
		]
	};
	
	var aModi= navigator.userAgent.match(/MSIE/)
		? [-145, -123]
		: [35, -5];
	
	for (var s in positions) {
		for (var i= 0; i< positions[s].length; i++) {
			var a= document.createElement('a');
			a.className='position';
			a.style.top= (positions[s][i][1]- aModi[1])+ 'px';
			a.style.left= (positions[s][i][0]- aModi[0])+ 'px';
			a.innerHTML= '&nbsp;';
			a.href= $('artist_'+ s).href;
			$EV.obs(a, 'mouseover', function(s) {
				$E.css('artist_'+ s, 'onhover', true);
				$map.style.backgroundImage= 'url(/images/karte-'+ s+ '.gif)';
			}.b(null, s));
			$EV.obs(a, 'mouseout', function(s) {
				$E.css('artist_'+ s, 'onhover', false);
				$map.style.backgroundImage= 'url(/images/karte.gif)';
			}.b(null, s));
			$map.appendChild(a);
		}
	}
	
	
}







/* -- admin --- */

function login() {
	w54.quickrequest(
		document.location.href,
		null,
		{
			name: $('gname').value,
			pw: $('gpw').value
		},
		function(res) {
			if (res== '0') {
				err('Sorry, Name or Password wrong');
			} else {
				document.location.href= '/Guestbook.html';
			}
			
		}.b(null)
	);
}

function logout() {
	w54.quickrequest(
		document.location.href,
		null,
		{
			logout: 1
		},
		function(res) {
			document.location.href= '/';
			
		}.b(null)
	);
}






/* -- misc out --- */

function err(s, sColor) {
	var err= $EL('error');
		
	if (!err) {
		err= document.createElement('div');
		err.id= 'error';
		$('bright').appendChild(err);
		err= $EL('error');
	} else if (err.timeout)
		clearTimeout(err.timeout);
	
	err.innerHTML= s;
	err.show();
	err.efx('highlight', {endcolor: '#'+ (sColor? sColor: 'ff0000')});
	err.timeout= setTimeout(err.hide, 5000);
	
	
}

