var SlideMenu = new Class({
	Implements: [Options, Events],
	options: {
		container: false
	},
	initialize: function(options){
		this.setOptions(options);
		
		this.RootList = $$('#nav > ul');
		this.RootListHeight = this.RootList[0].getHeight();
		this.RootListItems = this.RootList[0].getChildren('li');

		this.RootListItems.each(function(element){
			var self = this;
			
			this.MainLink = element.getChildren('a');
			this.SubList = element.getChildren('ul');
			this.SubListItem = this.SubList[0].getChildren('li');
			this.LogoHeight = $('nle-logo').getHeight();
			
			this.hmm = this.SubList[0].getChildren('li');
			
			if( this.hmm.length == 0 )
			{
				this.SubList[0].destroy();
			}
			
			if( element.hasClass('selected') == false )
			{
				element.setStyle('height', ( this.MainLink[0].getHeight() + 2 ) );
			}
			else
			{
				this.NextItem = this.MainLink.getNext();
				this.MainLink.addClass('open');
				
				if( this.NextItem[0] )
					element.setStyle('height', ( this.NextItem[0].getHeight() + this.MainLink[0].getHeight() + 2 ) );
				
				//element.setStyle('height', ( this.NextItem.getHeight() + this.getHeight() + 2 ) );
				//element.setStyle('height', ( this.MainLink[0].getHeight() + ( this.SubListItem.length * 22 ) + 2 ) );
				//console.log( this.MainLink[0].getHeight() );
				//console.log( ( this.MainLink[0].getHeight() + ( this.SubListItem.length * 22 ) + 2 ) );
			}	
			
			this.MainLink.addEvent('click',function(e){
		
			
				var selfclick = this;
				
				this.NextItem = this.getNext();
				this.SubItem = this.NextItem.getChildren('li');
				this.SubItemHeight = this.SubItem.getHeight();
				
				if( this.SubItem.length != 0 )
					e.stop();
			
				if( this.hasClass('open') )
				{
					this.foo = ( ( 31 * ($$('#nav > ul > li').length) ) + self.LogoHeight ) + 2;
					this.bar = ( this.getHeight() + 2 );
					$$('#nav ul li a').removeClass('open');
				}
				else
				{
					this.bar = ( this.NextItem.getHeight() + this.getHeight() + 2 );
					this.foo = ( ( 31 * $$('#nav > ul > li').length ) + this.NextItem.getHeight() + self.LogoHeight +2 );
					
					$$('#nav ul li a').removeClass('open');
					this.addClass('open');
				}
			
				// Close All Open Lists
				$$('#nav > ul > li').tween('height', this.getHeight() + 2 );
				// Open New List
				element.tween('height', this.bar );
				// Text Follow
				$('side-text').tween('top', this.foo );
			});
			
		});
		
		this.TextResize();
	},
	TextResize: function(){
		this.LogoHeight = $('nle-logo').getHeight();
		this.NavHeight = $('nav').getHeight();
		this.FootHeight = $('foot').getHeight();
		this.Offset = 2;
		
		$('side-text').setStyles({
			'position': 'absolute',
			'top': (this.LogoHeight + this.NavHeight + this.Offset),
			'bottom': (this.FootHeight + 2)
		});
	}	
});

var nleLayout = new Class({
	Implements: [Options, Events],
	options: {
		wrap: false,
		logo: false,
		nav: false,
		text: false,
		foot: false,
		image: false,
		images: $empty
	},
	initialize: function(options){
		this.setOptions(options);
		
		window.addEvents({
			'domready': (function(){
				this.resize();
				this.sideinit();
				this.nav();
			}).bind(this),
			'resize': (function(){
				this.resize();
				this.sideinit();
			}).bind(this)
		});
		
	},
	nav: function(){
		var first = $$('#nav > ul > li');
		var selected = $$('#nav > ul > li.selected > ul');
		
		if( selected.length != 0 ){
			var navSize = this.options.nav.getSize().y - selected[0].getSize().y;
			}
		else
			var navSize = this.options.nav.getSize().y;
		
		first.each(function(item){
			var childs = item.getChildren();
		
			if( childs.length > 1 )
			{
				var listSize = childs[1].getSize();
				
				childs[0].addEvent('click', (function(e){
					e.stop();
					
					if( item.hasClass('open') )
					{
						if( Browser.Engine.trident ){
			
							item.setStyle('height', 31 );
							}
						else
							item.tween('height', 31 );
						
						item.removeClass('open');
						this.sideinit( navSize );
					}
					else
					{
						
						first.removeClass('open');
						
						if( Browser.ie7 || Browser.ie6)
						{
							first.setStyle('height', 31 );
							item.setStyle('height', listSize.y+31 );
						}
						else
						{
							first.tween('height', 31 );
							item.tween('height', listSize.y+31 );
						}
						item.addClass('open');
						
						this.sideinit( navSize + listSize.y );
					}
				
				}).bind(this));
			}
		},this);
		
		
	},
	sideinit: function( navHeight ){
		var wrapSize = this.options.wrap.getSize();
		var logoSize = this.options.logo.getSize();
		var navSize = this.options.nav.getSize();
		var textSize = this.options.text.getSize();
		var footSize = this.options.foot.getSize();
		
		this.options.text.setStyles({
			'bottom': (footSize.y + 2)
		});
		
		if( navHeight == null )
		{
			var newTextHeight = ( wrapSize.y ) - ( logoSize.y + this.options.nav.getSize().y + footSize.y + 4 );
			
			this.options.text.setStyles({
				'height': newTextHeight
			});
		}
		else
		{
			var newTextHeight = ( wrapSize.y ) - ( logoSize.y + navHeight + footSize.y + 4 );
			
			if( Browser.Engine.trident )
				this.options.text.setStyle('height', newTextHeight);
			else
				this.options.text.tween('height', newTextHeight);
		}
		
	},
	resize: function(){
		var winsize = window.getSize();
		
		$('wrap').setStyles({
			'width': winsize.x,
			'height': winsize.y
		});
		
		if( this.options.image == false )
		{
			this.options.image = this.getImage();
		}
		
		this.options.image.setStyles({
			'height': winsize.y
		});
		
	},
	getImage: function(){
		var foo = location.href.split('/');
		var foo = foo[3].toLowerCase();
		
		if(this.options.images[foo] != null)
			var image_name = this.options.images[foo];
		else if($(document.body).hasClass('site-1'))
			var image_name = 'no-front.jpg';
		else if($(document.body).hasClass('site-4'))
			var image_name = 'no-front.jpg';
		else
			var image_name = 'back-front.jpg';
		
		var img = new Element('img', {
			'src': '/img/nle-back/'+image_name
		}).inject( this.options.wrap );
		
		return img;
	}
});

window.addEvent('domready', function(){
	
	var layout = new nleLayout({
		'wrap': $('wrap'),
		'logo': $('nle-logo'),
		'nav': $('nav'),
		'text': $('side-text'),
		'foot': $('foot'),
		'images': {
			'manufacturingprojects': 'framleidsla-back.jpg',
			'manufacturing': 'framleidsla-back.jpg',
			'framleidsla': 'framleidsla-back.jpg',
			'news': 'frettir-back.jpg',
			'frettir': 'frettir-back.jpg',
			'corporateprofile': 'fyrir-back.jpg',
			'fyrirtaekid': 'fyrir-back.jpg',
			'projectgreeniceland': 'green-back.jpg',
			'graentisland': 'green-back.jpg',
			'investors': 'fjarfestar-back.jpg',
			'fyrirfjarfesta': 'fjarfestar-back.jpg',
			'forinvestors': 'fjarfestar-back.jpg',
			'rafbilavaeding': 'rafbila-back.jpg',
			'vehicleelectrification': 'rafbila-back.jpg',
			'salarafbila': 'sala-raf-back.jpg',
			'evdistribution': 'sala-raf-back.jpg',
			'hafasamband': 'samband-back.jpg',
			'contact': 'samband-back.jpg',
			'energyprojects': 'orka-back.jpg',
			'skyndibilar': 'skyndibilar-back.jpg',
			'evcarsharing': 'skyndibilar-back.jpg',
			'omoss': 'no-omoss.jpg',
			'elbiler': 'no-elbiler.jpg',
			'forhandlere': 'no-forhandlere.jpg',
			'hvorforelbiler': 'no-annad3.jpg',
			'miljo': 'no-miljo.jpg',
			'provekjoring': 'no-provekjoring.jpg',
			'kontaktoss': 'no-kontakt.jpg',
			'nyheter': 'no-nyheter.jpg',
			'nyhetsbrev': 'no-video.jpg',
			'video': 'no-video.jpg',
			'andretjenester': 'no-annad1.jpg',
			'aboutus': 'no-omoss.jpg',
			'electricvehicles': 'no-elbiler.jpg',
			'dealers': 'no-forhandlere.jpg',
			'whyelectric': 'no-annad3.jpg',
			'enviroment': 'no-miljo.jpg',
			'testdrive': 'no-provekjoring.jpg',
			'contactus': 'no-kontakt.jpg',
			'newsletter': 'no-nyhedsbrev.jpg',
			'video': 'no-video.jpg',
			'otherservices': 'no-annad1.jpg'
		}
	});
	
	
	
	$$('.staff-index .item .video-link').each(function(item){
		var VideoID = item.get('rel');
		var VideoWidth = 600;
		var VideoHeight = 338;
		
		var WinSize =  window.getSize();
		
		if( VideoID == '' )
		{
			item.setStyle('display','none');
		}
		
		var video = '';
		video += '<object width="'+VideoWidth+'" height="'+VideoHeight+'">';
		video += '<param name="allowfullscreen" value="true" />';
		video += '<param name="allowscriptaccess" value="always" />';
		video += '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+ VideoID +'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />';
		video += '<embed src="http://vimeo.com/moogaloop.swf?clip_id='+ VideoID +'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+VideoWidth+'" height="'+VideoHeight+'"></embed>';
		video += '</object>';
		
		item.addEvent('click', function(){
			
			if( $('overlay') )
				$('overlay').destroy();
			
			var overlay = new Element('div', {
				'id': 'overlay',
				'styles':
				{
					'width': WinSize.x,
					'height': WinSize.y,
					'opacity': 0
				},
				'events': {
					'click': function(){
						$('video-popup').fade('out');
						$('overlay').fade('out');
						(function(){
							$('video-popup').destroy();
							$('overlay').destroy();
						}).delay(500);
					}
				}
			}).inject( $(document.body) );
			
			overlay.fade('0.5');
			
			var popup = new Element('div', {
				'id': 'video-popup',
				'styles':
				{
					'width': VideoWidth,
					'height': VideoHeight,
					'top': ( (WinSize.y/2) - ((VideoHeight+20)/2) ),
					'left': ( (WinSize.x/2) - ((VideoWidth+20)/2) ),
					'opacity': 0
				}
			});
			
			var close = new Element('div', {
				'class': 'close',
				'events': {
					'click': function(){
						$('video-popup').fade('out');
						$('overlay').fade('out');
						(function(){
							$('video-popup').destroy();
							$('overlay').destroy();
						}).delay(500);
					}
				}
			});
			
			popup.inject( $(document.body) );
			popup.fade('in');
			
			(function(){
				popup.set('html',video);
				close.inject( popup );
			}).delay(750);
			
			
			
		});
		
		
		
	});
	
	
	$$('.media-index .item').each(function(item){
		var WinSize = window.getSize();
		var VideoWidth = 600;
		var VideoHeight = 338;
		
		var image = item.getChildren('.img-path');
		var pdf = item.getChildren('.pdf-file');
		var pdf = pdf[0].get('html');
		var vimeo = item.getChildren('.vimeo-id');
		var vimeo = vimeo[0].get('html');
		var text = item.getChildren('.text');
		var text = text[0].get('html');
		var title = item.getChildren('.title');
		var title = title[0].get('html');
		var slink = item.getChildren('.source-link');
		var slink = slink[0].get('html');
		
		var VideoID = 0;
		var VideoID = vimeo;
		
		var videoobject = '';
		videoobject += '<object width="'+VideoWidth+'" height="'+VideoHeight+'">';
		videoobject += '<param name="allowfullscreen" value="true" />';
		videoobject += '<param name="allowscriptaccess" value="always" />';
		videoobject += '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+ VideoID +'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />';
		videoobject += '<embed src="http://vimeo.com/moogaloop.swf?clip_id='+ VideoID +'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+VideoWidth+'" height="'+VideoHeight+'"></embed>';
		videoobject += '</object>';
		
		
		
		item.addEvent('click', function(){
			if( $('video-popup') )
				$('video-popup').destroy();
			
			if( $('overlay') )
				$('overlay').destroy();
			
			var overlay = new Element('div', {
				'id': 'overlay',
				'styles':
				{
					'width': WinSize.x,
					'height': WinSize.y,
					'opacity': 0
				},
				'events': {
					'click': function(){
						$('video-popup').fade('out');
						$('overlay').fade('out');
						(function(){
							$('video-popup').destroy();
							$('overlay').destroy();
						}).delay(500);
					}
				}
			}).inject( $(document.body) );
			
			overlay.fade('0.5');
			
			var popup = new Element('div', {
				'id': 'video-popup',
				'styles':
				{
					'width': VideoWidth,
					//'height': VideoHeight,
					'top': ( (WinSize.y/2) - ((VideoHeight+20)/2) ),
					'left': ( (WinSize.x/2) - ((VideoWidth+20)/2) ),
					'opacity': 0
				}
			});
				
				
				var ttl = new Element('div', { 'class': 'title', 'html': title } ).inject(popup);
				
				if( vimeo != '' )
					var video = new Element('div', { 'class': 'video', 'html': videoobject } ).inject(popup);
				
				if( text != '' )
					var txt = new Element('div', { 'class': 'text', 'html': text } ).inject(popup);
				
				if( pdf != '' )
					var link = new Element('a', { 'class': 'link', 'target': '_blank', 'href': pdf, 'html': 'Download PDF' } ).inject(popup);
				else if( slink != '' )
					var link = new Element('a', { 'class': 'link', 'target': '_blank', 'href': slink, 'html': 'View Web' } ).inject(popup);
					
			var close = new Element('div', {
				'class': 'close',
				'events': {
					'click': function(){
						$('video-popup').fade('out');
						$('overlay').fade('out');
						(function(){
							$('video-popup').destroy();
							$('overlay').destroy();
						}).delay(500);
					}
				}
			});
			
			close.inject( popup );
			popup.inject( $(document.body) );
			popup.fade('in');
			
			//video.set('html', video);
			
		});
		
	});
	
});

