
var currentSlide;

Cufon.replace('#main h2', {fontFamily: 'Sentinel Bold'});
Cufon.replace('#services h3,#clients h3,#thinking h3,#people h3, #client-list h3', {fontFamily: 'Sentinel Bold'});
Cufon.replace('#main #description p', {fontFamily: 'BentonSans Regular'});
Cufon.replace('#main #description p strong', {fontFamily: 'BentonSans Bold'});


var init = function()
{
	Cufon.now();
	$('bg').getElement('img').addEvent('load', function() {
		  var img = this; 
		  img.setStyle('opacity', 0);
		  (function(){img.fade('in');}).delay(200); 
	});
	window.addEvent('resize', fixFooter);
	window.addEvent('resize', fixBg);
	//fixFooter.periodical(100);
	fixBg();
	fixLinks();
	fixFooter();
	initTabs();
	initSlides();
	initNavigation();
	initMap();
}

var initNavigation = function()
{
	if (!$('section-home')) {
		var container = $('aside');
		//container.set('tween', {duration: 'normal', transition: 'sine:out', fps: 24});
	
		var originalPosition = container.getPosition().y;
		var containerSize = container.getSize().y;

		var footerPosition = $('footer').getPosition().y;
		
		window.addEvent('scroll', function(e) {(function(){
			var windowPosition = window.getScroll().y;
			var diff = windowPosition - originalPosition + 10;
	
			if (diff < 0) {
				diff = 0;
			}
			
			if (windowPosition + containerSize + $('footer').getSize().y > footerPosition) {
				diff = footerPosition - containerSize - originalPosition;
				container.setStyles({
					position: 'relative',
					top: diff - 40
				});
			} else {
				container.setStyles({
					position: 'fixed',
					top: 'auto'
				});				
			}
		}).delay(100);});	
	}
}

var fixLinks = function()
{
	document.getElements('a[href^=mailto:firstname]').each(function(a) {
	      var name = a.get('title').toLowerCase();
	      a.set('text', a.get('text').replace('firstname', name));
	      a.set('href', a.get('href').replace('firstname', name));
	      // for ie6
	      a.set('text', a.get('text').replace('firstname', name));
	});
	
}

var initTabs = function()
{
	var tabs = $('tabs');
	if (tabs) {
		tabs.getElements('a').addEvent('click', function(e) {
			e.stop();
			var hash = this.get('href').substr(1);
			var id = 'tab-' + hash;
			var el = $(id);
			if (el) {
				el.getAllPrevious().addClass('hide');
				el.getAllNext().addClass('hide');
				el.removeClass('hide');
				tabs.getElements('li').removeClass('selected');
				this.getParent().addClass('selected');
				var span = el.getElement('span');
				if (span) {
					var previous = $('bg').getElement('img').get('src');
					var url = span.get('text');
					if (url != previous) {
						changeBackground(url);
					}
				}
			}
		});
	}
}

var changeBackground = function(url)
{
	var img = new Element('img', {'src': url, 'width': '100%'});
	img.setStyle('opacity', 0);
	var bg = $('bg');
	fixBgSize(img);
	img.addEvent('load', function(e) {
		var out = bg.getElement('img');
		//out.fade('out');
		//img.fade('in');
		var fx = new Fx.Tween(out, {onComplete: function() {out.dispose();img.fade('in');}});
		fx.start('opacity', 1, 0);
	});
	img.inject(bg);
}

var fixBgSize = function(img)
{
    var proportion = 1280 / 720;
    var winSize = window.getSize();
    var winProportion = winSize.x / winSize.y;
    if (winProportion >= proportion) {
      img.setStyles({
        width: winSize.x,
        height: 'auto'
      });
    } else {
      img.setStyles({
        height: winSize.y,
        width: 'auto'
      });
    }

}
var fixBg = function()
{
	fixBgSize($('bg').getElement('img'));
}

var fixFooter = function() 
{
	var footer = $('footer');
	var windowHeight = window.getSize().y
	var footerHeight = footer.getSize().y;
	var footerTop = footer.getPosition().y;
	var diff = windowHeight - footerTop - footerHeight; 
	if (diff < 0) {
		footer.setStyles({
			position: 'relative',
			top: 0
		});
	} else {
		footer.setStyles({
			position: 'fixed',
			bottom: 0,
			top: 'auto',
			width: '100%'
		});		
	}
	if ($('wrapper').getSize().y + 20 > footerTop) {
		footer.setStyles({
			position: 'relative',
			top: 0
		});
	}
    
}


var initSlides = function()
{
	var container = $('slides')
	if (container) {
		container.getElements('.slide').each(function(s) {
			new Fx.Tween(s).set('opacity', 0);
		});
		if ($('next-slide')) {
			$('next-slide').addEvent('click', function(e) {
				e.stop();
				nextSlide();
			});
		}
		if ($('prev-slide')) {
			$('prev-slide').addEvent('click', function(e) {
				e.stop();
				prevSlide();
			});
		}
		checkSlides();
		currentSlide = window.location.hash;
		checkSlides.periodical(100);
	}
}

var checkSlides = function()
{
	var hash = window.location.hash;
	if (currentSlide != hash) {
		currentSlide = hash;
		var id = hash.substr(1);
		var slide = $('slide-' + id);
		if (!hash || !slide) {
			var slide = $('slides').getElement('.slide');
		}
		if (slide) {
			changeSlide(slide);
		}
	}		
}

var changeSlide = function(slide)
{
	if (slide) {
		var id = slide.get('id');
		var container = $('slides');
		var current = container.getElement('.selected');
		if (current) {
			current.fade('out');
		}
		slide.fade('in');
		container.getElements('.slide').removeClass('selected');
		slide.addClass('selected');
		var position = 0;
		container.getElements('.slide').each(function(el, i) {
			if (el.get('id') == id) {
				position = i;
			}
		});
		$('slide-controls').setStyle('background-position', '50% ' + (position * -100) + 'px')
		
	}		
}

var nextSlide = function()
{
	var container = $('slides');
	var current = container.getElement('.selected');
	if (current) {
		var slide = current.getNext();
	}	
	if (slide) {
		//slide = container.getElement('.slide');
		var id = slide.get('id');
		window.location.hash = '#' + id.substr(6);
	}
}
var prevSlide = function()
{
	var container = $('slides');
	var current = container.getElement('.selected');
	if (current) {
		var slide = current.getPrevious();
	}	
	if (slide) {
		//slide = container.getElement('.slide');
		var id = slide.get('id');
		window.location.hash = '#' + id.substr(6);
	}
}


var initMap = function()
{
	var container = $('contact-map');
	if (container) {
		var latlng = new google.maps.LatLng(60.167988, 24.939351);
		var options = {
		    zoom: 15,
		    center: latlng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  };
		var map = new google.maps.Map(document.getElementById("contact-map"), options);
			
		var marker = new google.maps.Marker({
		    position: latlng, 
		    map: map, 
		    title:"Nordkapp"
		});   
		var infoWindow = new google.maps.InfoWindow({
	        content: '<div style="width:400px;height:150px;"></div>'
	    });
		google.maps.event.addListener(marker, 'click', function() {
			infoWindow.open(map, marker);
		});
		infoWindow.open(map, marker);
	}
}

