window.addEvent('domready', function() {
	// preload array
	var preload = new Array();

	// init main menu
	var menuItems = $$('#menubar img');
	$each(menuItems, function(img, i) {
		path = img.getProperty('src');

		preload[preload.length] = new Image();
		preload[preload.length - 1].src = path.replace('_no.png', '_hi.png');

		// img.setProperty('onMouseOut', 'this.src=\''+path+'\'');
		// img.setProperty('onMouseOver', 'this.src=\''+path.replace('_no.png', '_hi.png') +'\'');
	});

	// metanavi hovers
	var metaNavi = $$('#footer img');
	$each(metaNavi, function(img, i) {
		path = img.getProperty('src');

		preload[preload.length] = new Image();
		preload[preload.length - 1].src = path.replace('_no.png', '_hi.png');

		// img.setProperty('onMouseOut', 'this.src=\''+path+'\'');
		// img.setProperty('onMouseOver', 'this.src=\''+path.replace('_no.png', '_hi.png') +'\'');
	});

	var mainMenuItems = $$('#menubar > li');
	var mainMenuImages = $$('#menubar > li > a > img');
	var activeMenuItem = 0;
	var extended = null;
	$each(mainMenuItems, function(li, i) {
		mainMenuImages[i].removeProperty('onmouseout');

		li.addEvent('mouseenter', function(event) {
			li.addClass('active');
			mainMenuImages[i].src = mainMenuImages[i].src.replace('_no.png', '_hi.png');
			activeMenuItem = i;
			$each(mainMenuItems, function(l, j) {
				if (j != i && mainMenuImages[j].src.indexOf('_hi.png') > 0) {
					extended = j;
					mainMenuItems[j].removeClass('active');
				}
			});
		});

		if (!mainMenuItems[i].hasClass('initial')) {
			li.addEvent('mouseleave', function(event) {
				if (extended != i)
					li.removeClass('active');
			});
		}

		if (!mainMenuItems[i].hasClass('initial') && mainMenuImages[i].src.indexOf('_no.png') > 0) {
			li.addEvent('mouseleave', function(event) {
				mainMenuImages[activeMenuItem].src = mainMenuImages[activeMenuItem].src.replace('_hi.png', '_no.png');
				if (extended != null)
					mainMenuItems[extended].addClass('active');
			});
		}
	});

	peopleHover();
	positionMeta();

});

window.addEvent('load', function() {
	positionMeta();
});

function positionMeta() {
	$('footer').setStyles( {
		'position' :'relative',
		'top' :0,
		'width' :'100px'
	});
	//if (window.getScrollHeight() == window.getHeight())
	{
		$('footer').setStyles( {
			'position' :'absolute',
			'top' :(window.getScrollHeight() - 22),
			'width' :'1000px'
		});
	}
}

window.addEvent('resize', function(e) {
	positionMeta();
});

/**
 * makes gallery items hover to show a description for each image
 * 
 */
function peopleHover() {

	// get elements
	var galerie = $$('.peopleElement');
	var descs = $$('.peopleDescription');

	$each(galerie, function(el, i) {

		// change styles on hover
		el.addEvent('mouseenter', function(event) {
			descs[i].setStyle('display', 'block');
		});

		// change styles back to initial status
		el.addEvent('mouseleave', function(event) {
			descs[i].setStyle('display', 'none');
		});
	});
}

/**
 * searches for a given string, loads the results via http request and views
 * them in a specified element
 * 
 * @param form
 * @param page
 */
function searchList(form, page, listPhpUrl) {

	params = page == null ? $('listForm') : listPhpUrl + '&parent_id=' + form.parent_id.value
			+ '&needle=' + form.needle.value + '&page=' + page;

	var myHTMLRequest = new Request.HTML( {
		url :'/list.php',
		onRequest : function() {
			$('listItems').set('html', '<img src="images/loading.gif" />');
		},
		onSuccess : function(html) {
			$('listItems').empty();
			$('listItems').adopt(html);
			positionMeta();
		}
	}).post(params);
}


/**
 * searches for a given string, loads the results via http request and views
 * them in a specified element
 * 
 * @param form
 * @param page
 */
function searchListInfopanel(form, page, listPhpUrl) {

	params = page == null ? $('listForm') : listPhpUrl + '&state=infopanel&parent_id=' + form.parent_id.value
			+ '&needle=' + form.needle.value + '&page=' + page;

	var myHTMLRequest = new Request.HTML( {
		url :'/list.php',
		onRequest : function() {
			$('listItems').set('html', '<img src="images/loading.gif" />');
		},
		onSuccess : function(html) {
			$('listItems').empty();
			$('listItems').adopt(html);
			positionMeta();
		}
	}).post(params);
}


function loadMapHovers() {

	// make me fly
	if (navigator.appVersion.indexOf("MSIE") == -1) {
		document.body.addEvent('click', function(event) {
			if (event.target.get('tag') != "img"
					&& !event.target.hasClass('dot')
					&& !event.target.hasClass('flyer')) {
				$each($$('#flyer div'), function(el, i) {
					el.setStyle('display', 'none');
					$$('#dots div')[i].removeClass('dotactive');
				});
			}
		});
	}

	// make dots hover on menu mouseover
	$each($$('#cities a'), function(el, i) {

		el.addEvent('mouseover', function(event) {
			$$('.' + el.title)[0].addClass('dotred');
		});

		el.addEvent('mouseleave', function(event) {
			$$('.' + el.title)[0].removeClass('dotred');
		});
	});

	// make menu hover on dot mouseover
	$each($$('#dots div'), function(el, i) {

		el.addEvent('mouseover', function(event) {
			el.addClass('dotred');
			$$('#cities li a img')[i].src = $$('#cities li a img')[i].src
					.replace('_no', '_hi');
		});

		el.addEvent('mouseleave', function(event) {
			el.removeClass('dotred');
			$$('#cities li a img')[i].src = $$('#cities li a img')[i].src
					.replace('_hi', '_no');
		});
	});

}

/**
 * shows the flyer and highlights the dots on the splashpage and the map
 * 
 * @param name
 */
function showFlyer(city) {

	// hide all flyers
	$each($$('#flyer div'), function(el, i) {
		el.setStyle('display', 'none');
	});

	// fade in selected flyer
	el = $$('.' + city)[1];
	el.setStyle('height', 0);
	el.setStyle('display', 'block');
	var myFx = new Fx.Tween(el, {
		duration :100
	});
	myFx.start('height', 0, 173);

	// remove dot hovering
	$each($$('#dots div'), function(el, i) {
		el.removeClass('dotred');
		el.removeClass('dotactive');
	});

	// make selected dot stay active
	$$('.' + city)[0].addClass('dotred dotactive');
}

function startCallback() {
	return true;
}

function completeCallback(response) {
	$('response').innerHTML = response;
}

AIM = {
	frame : function(c) {
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="' + n
				+ '" name="' + n + '" onload="AIM.loaded(\'' + n
				+ '\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof (c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof (c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}

		if (typeof (i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}
}