// JavaScript Document

/**
 * Checking for dependencies
 */

if ( ! Prototype )
	throw new Exception('Can\'t work without prototype');

if ( ! hensam )
	var hensam = { Projects : { NewVision : { } } };
else if ( ! (hensam.Projects ) )
	hensam.Projects = { NewVision : { } } ;
else if ( ! (hensam.Projects.NewVision ) )
	hensam.Projects.NewVision = { };

hensam.Projects.NewVision.Navigation = Class.create({
	
	element : null, 
	elementName : 'navigation',
	cookie : null,
	cookieName : 'hensam.NewVision.Navigation',
	additionalOffset : { common : 91, start : 9, news : 145 },
	calcSilent : false,
	
	positionAdjusting : function() {
				
		if ( this.element )
		{
			if ( navigator.appName == 'Microsoft Internet Explorer' )
			{
				this.element.setStyle('visibility: hidden');
			}
			var topOffset = this.element.up().getHeight();
			topOffset /= 2;
				
			if ( window.isStartpage && isStartpage )
			{
				topOffset -= this.additionalOffset.start;
			}
			else if ( window.isNews && isNews )
			{
				topOffset -= this.additionalOffset.news;
			}
			else
			{
				topOffset -= this.additionalOffset.common;
			}
			
			var offset = topOffset - this.element.cumulativeOffset().top;
			
			if ( this.calcSilent )
			{				
				this.calcSilent = false;
				
				if ( offset == 0 )
				{
					this.element.style.visibility = '';
					return;
				}
			}
			
			if ( this.cookie )
			{
				this.cookie.set(this.cookieName, topOffset, 365);
			}
			
			if ( ! window.Scriptaculous || ! window.Effect.Move ) 
			{
				topOffset += 'px';
				this.element.setStyle({top: topOffset}).setStyle('visibility: visible;');
			}
			else
			{
				new Effect.Move(this.element, { y: offset, mode: 'relative', duration: 0.5, afterFinish : function() { $('navigation').style.visibility = '';} });
			}
		}
		else
		{
			this.element = $(this.elementName);
			this.positionAdjusting();
		}
	},
	
	setElement : function(elementName) {
		this.element = $(elementName);
	},
	
	domReady : function() {
		this.element = $(this.elementName);
		
		if ( navigator.appName != 'Microsoft Internet Explorer' )
		{
			this.element.setStyle('visibility: visible');
		}
						
		if ( this.cookie && this.cookie.read(this.cookieName))
		{
			var topOffset = parseInt(this.cookie.read(this.cookieName))+'px';
			
			this.element.setStyle({top: 0});
			this.element.setStyle({top: topOffset});
			
			this.calcSilent = true;
		}
		
		this.positionAdjusting();		
	},
	
	initialize : function() {
		
		document.observe('dom:loaded', this.domReady.bindAsEventListener(this));
		Event.observe(window, 'resize', this.positionAdjusting.bindAsEventListener(this));
		
		if ( hensam.Snippets && hensam.Snippets.Cookies )
		{
			this.cookie = new hensam.Snippets.Cookies();
		}
	}
});

var tmp = new hensam.Projects.NewVision.Navigation();
