// JavaScript Document
if ( ! window.Prototype )
	throw new Exception('Can\'t work without prototype');
	
//if ( ! window.Scriptaculous )
//	throw new Exception('Can\'t work without scriptaculous');
	
if ( ! window.BrowserDetect )
	throw new Exception('Can\'t work without BrowserDetect');
	
if ( ! window.hensam )
	var hensam = { Projects : { mehrdafon : {} } };
else if ( ! (hensam.Projects ) )
	hensam.Projects = { mehrdafon : {} } ;
else if ( ! hensam.Projects.mehrdafon  )
	hensam.Projects.mehrdafon = {};
	
hensam.Projects.mehrdafon.ToTop = Class.create({
	icon : false,
											   
	addLinkIcon : function() {
		
		var linkIcon = new Element('a', { 'id': 'linkIcon' } );
		linkIcon.update('nach oben <img src="/images/misc/pointer-top.png" alt="nach oben" />');		
		
		$('main').appendChild(linkIcon);
				
		this.icon = linkIcon;
		this.icon.show().observe('click', this.linkIconClickedEventHandler.bindAsEventListener(this));
		this.repositionLinkIcon();
		
		if ( window.supersleight ) 
		{
			window.supersleight.limitTo(this.icon);
			window.supersleight.run();
		}
	},
	
	repositionLinkIcon : function() {
		var top = document.viewport.getHeight() + document.viewport.getScrollOffsets().top -100;
		this.icon.setStyle('top: '+top+'px');
	},
	
	linkIconClickedEventHandler : function(event) {
		event.stop();		
		window.scrollTo(0, 0);
	},
	
	resizeEventHandler : function(event) {
		if ( this.icon )
		{
			this.repositionLinkIcon();
		}
		else if ( $('content').getHeight() < document.viewport.getHeight() )
		{
			this.addLinkIcon();
		}
	},
	
	scrollEventHandler : function(event) {
		if ( this.icon )
		{
			this.repositionLinkIcon();
		}
	},
	
	domLoadedEventHandler : function(event) {
		var content = $('content');
		if ( ! content )
		{
			return;
		}
		var viewportHeight = document.viewport.getHeight();
		var contentHeight = content.getHeight();
		
		if ( document.viewport.getHeight() < $('content').getHeight() )
		{
			this.addLinkIcon();
		}
		
		Event.observe(window, 'resize', this.resizeEventHandler.bindAsEventListener(this));
		Event.observe(window, 'scroll', this.scrollEventHandler.bindAsEventListener(this));
	},
	
	initialize : function() {
		document.observe('dom:loaded', this.domLoadedEventHandler.bindAsEventListener(this));
	}
});

new hensam.Projects.mehrdafon.ToTop();