/*
	### TU Ball
*/

window.addEvent('domready', function() {
	
	// Page-Position
	if( $defined($('page')) ) {

		locatepage();

		window.addEvent('resize', function() {
			locatepage();
		});

	}

	// Intro
	if( $defined($('intro')) ) {
		var width = 649;
		var height = 485;
		//var introurl = 'http://www.tu-ball.at/wp-content/themes/tuball/gfx/intro_TUBALL.swf';	// 2010
		//var introurl = 'http://www.tu-ball.at/wp-content/themes/tuball/gfx/intro_2011.swf';	// 2011
		var introurl = 'http://www.tu-ball.at/wp-content/themes/tuball/gfx/intro_2012.swf';		// 2012
		var so = new SWFObject(introurl,'theintro',width,height,'9');
		so.addParam('wmode','transparent');
		so.write('intro');

		locateskipbutton();

		window.addEvent('resize', function() {
			locateskipbutton();
		});
	}

	// Open Gallery Button
	if($defined( $("opengallery") )) {
		$("opengallery").addEvent('click', function() {
			var slimboximagesarray = [];
			var images = $$('.galleryimagelink');
			images.each(function(item, index){
				slimboximagesarray[index] = [item.href, item.title];
			});
			Slimbox.open(slimboximagesarray, 0);
		});
	}
	
	// Saalplaene
	if($defined( $('saalplan') )) {
		var maps = $$('map');
		var original_map_url = $('saal').get('src');
		if($defined(maps)) {
			maps.each(function(map) {
				var areas = map.getElements('area');
				if($defined(areas)) {
					areas.each(function(ar) {
						var id = ar.get('id').toString();
						var map_url = ar.get('alt').toString();
						var tt_url = ar.get('href').toString();

						ar.addEvents({
							
							'mouseover': function(e) {
								e.stop();
								// Change Map
								$('saal').set('src', map_url);
								// Create Tooltip if not already exists
								if( !$defined($('tooltip-'+id)) ) {
									var tt = new Element('div', {'id': 'tooltip-'+id, 'class': 'tt'});
									var tt_border = new Element('div', {'id': 'tt-border-'+id, 'class': 'tt-border'});
									var tt_content = new Element('div', {'id': 'tt-content-'+id, 'class': 'tt-content'});
									tt_border.set('opacity', 0.6);
									tt.grab(tt_border);
									tt.grab(tt_content);
									tt.fade('hide');
									tt.inject(document.body);

									// Get Tooltip-Content via Ajax
									var TooltipRequest = new Request.HTML({
										url: tt_url,
										update: 'tt-content-'+id,
										onRequest: function() {
											//ar.setStyle('cursor', 'wait !important');
										},
										onSuccess: function() {
											//ar.setStyle('cursor', 'default');
											$('tooltip-'+id).fade('in');
											var tt_dim = $('tt-content-'+id).getSize();
											$('tt-border-'+id).setStyle('height', (tt_dim.y+10).toInt() );										
										}
									}).send();
								} else {
									// already defined, just show
									$('tooltip-'+id).fade('in');
								}
								
								// Position Tooltip
								$('tooltip-'+id).setStyles({
									'top': (e.page.y-215).toInt(),
									'left': (e.page.x+15).toInt()
								});
								
							},
							
							'mouseout': function(e) {
								// Revert Map to orignal state
								$('saal').set('src', original_map_url);
								if($defined( $('tooltip-'+id) )) {
									$('tooltip-'+id).fade('out');
									$('tooltip-'+id).setStyles({
										'top': -500,
										'left': -500
									});
								}
							},
							
							'mousemove': function(e) {
								if($defined( $('tooltip-'+id) )) {
									$('tooltip-'+id).setStyles({
										'top': (e.page.y-215).toInt(),
										'left': (e.page.x+15).toInt()
									});
								}
							},
							
							'click': function(e) {
								e.stop();	
							}
							
						});
					});
				}
			});
		}
		
	}

});

function locatepage() {
	
	var screensize = document.window.getSize();
	var pagesize = $('page').getSize();

	var top = ((screensize.y-pagesize.y)/2).floor();
	var left = ((screensize.x-pagesize.x)/2).floor();

	$('page').setStyles({
		'position': 'absolute',
		'top': top,
		'left': left
	});

}

function locateskipbutton() {
	
	var introposition = $('intro').getPosition();
	$('skipintro').setStyles({
		'position': 'absolute',
		'top': introposition.y,
		'left': introposition.x
	});

}

