var runOnLoad = function(f) {
	if (runOnLoad.loaded) {
		f(); /* If already loaded, just invoke f() now. */
	} else {
		runOnLoad.funcs.push(f); /* Otherwise, store it for later */
	}
};
runOnLoad.funcs = []; /* The array of functions to call when the document */
runOnLoad.loaded = false; /* The functions have not been run yet. */
/*
 * Run all registered functions in the order in which they were registered. It is safe to call runOnLoad.run() more than
 * once: invocations after the first do nothing. It is safe for an initialization function to call runOnLoad() to
 * register another function.
 */
runOnLoad.run = function() {
	if (runOnLoad.loaded) {
		return;
	};
	for ( var i = 0; i < runOnLoad.funcs.length; i++) {
		try {
			runOnLoad.funcs[i]();
		} catch (e) {
			/* An exception in one function shouldn't stop the rest */
		}
	};
	runOnLoad.loaded = true;
	delete runOnLoad.funcs;
	delete runOnLoad.run;
};
if (window.addEventListener) {
	window.addEventListener("load", runOnLoad.run, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", runOnLoad.run);
} else {
	window.onload = runOnLoad.run;
};


function showform() {
  Ext.Ajax.request({
	url:'formhandler.php',
	success:function(req) {
		var frm = Ext.get('frm');
		frm.dom.innerHTML = req.responseText;
	}
  });
}
function sendform() {
  Ext.Ajax.request({
	url:'formhandler.php',
	form:'contact',
	method:'POST',
	success:function(req) {
		var frm = Ext.get('frm');
		frm.dom.innerHTML = req.responseText;
	}
  });
};

function showImages() {
	var b = Ext.get('book');
	if (b) {
		var imgs = b.select('img');
		imgs.each(function(el) {
			var a = Ext.DomHelper.append('book', [
				{tag: 'a', id:'l' + el.id, onclick:'return(false);', style:'display:block; position:relative; float:left; width:14px; height:14px; background:#EDEDED; margin:18px 0 0 2px; text-decoration:none;', href:'#', html: '&nbsp;'}
			], true);
			a.on('click', function(e, ael) {
				Ext.select('#book img').each(function(im) {
					im.dom.style.display = 'none';
				});
				Ext.select('a[id^=limg]').each(function(limg) {
					limg.dom.style.backgroundColor = '#EDEDED';
				});
				ael.style.backgroundColor = '#eb690a';
				Ext.get(ael.id.substr(1)).fadeIn();
			});
			a.on('mouseover', function(e, ael) {
				ael.style.backgroundColor = '#eb690a';
			});
			a.on('mouseout', function(e, ael) {
			  if (!Ext.get(ael.id.substr(1)).isVisible()) {
				ael.style.backgroundColor = '#EDEDED';
			  }
			});
		});
		imgs.first().fadeIn();
		Ext.get('limg1').dom.style.marginLeft = '18px';
		Ext.get('limg1').dom.style.backgroundColor = '#eb690a';
	}
}

runOnLoad(function() {
	var h = Ext.lib.Dom.getViewHeight();
	var c = Ext.get('maincontent');
	var box = c.getBox();
	if (c.getTop() + box.height < h) {
		c.setHeight(h - c.getTop());
	};
	var cnt = Ext.get('content');
	if (cnt) {
		cnt.slideIn('t', { duration: 1, callback: showImages });
	};
	if (document.location.href.match(/index.html/) || document.location.href=='http://www.feinfraenkisch.de/') {
		Ext.ux.Lightbox.init();
		Ext.ux.Lightbox.register('a.lightbox', true);
	};
	var media = Ext.get('media');
	var prlnk = Ext.get('prlnk');
	if (media && prlnk) {
		if (document.location.href.match(/media\.html/)) {
			media.dom.style.display = 'block';
			media.alignTo(prlnk, 'tl-bl');
		} else {
			prlnk.on('mouseover', function() {
				media.dom.style.display = 'block';
				media.alignTo(prlnk, 'tl-bl');
			});
			media.on('mouseout', function() {
				media.dom.style.display = 'none';
			});
		}
	}
});
