/*
 * project: hendrikthoma.de
 * author: GWI Media Technologies GmbH, www.gw-interactive.com
 * copyright (C) 2010
 */

var curIndex = 0;
var moods = [1, 4, 3, 0, 5, 2];

function initUrl() {
	h = self.location.hash.match(/^#!(.+)/);

	if (h != null)
		location.href = h[1];
}

function adjustUI() {
	newFooterTop = $('#content>li.active>ul').height() + 181;
	$('#footer').animate({top: newFooterTop }, 100);
}

function initAnimation() {
	numMoods = moods.length;
    images = new Array();

    for (i=0; i<numMoods; i++) {
    	images[i] = new Image();
    	images[i].src = '/img/m/1/' + moods[i] + '.jpg';
    }

    check = window.setInterval(function(){
    	allLoaded = images[0].complete;
    	for (i=1; i<numMoods; i++) {
    		allLoaded &= images[i].complete;
    	}
    	if (allLoaded) {
    		window.clearInterval(check);
    		window.setInterval(function(){
    			curIndex = (curIndex + 1  < numMoods ? curIndex + 1 : 0);
    			// create second image for animation:
    			$('<img src="" width="980" height="466" />').hide().attr('src', images[curIndex].src).insertAfter($('#mood img')[0]);
    			$($('#mood img')[0]).fadeOut('slow', function(){ $(this).remove(); });
    			$($('#mood img')[1]).fadeIn('slow');
    		}, 10000);
    	}
    }, 1000);
}

$(document).ready(function(){

	$('a.target_blank').live('click', function(e){ window.open(this.href); e.preventDefault(); })

	$('a.trackable').live('click', function(e){
		_gaq.push(['_trackEvent', 'Links', 'Click', this.href]);
	});

	$('a.hashnav').live('click', function(e){

		a_context = this.className.match(/hashnav-(.+)/);
		if (a_context)
			switch(a_context[1]) {
				case 'mainnav': // first level navigation

					// change menu:
					$('ul#content>li.active').removeClass('active');
					$(this).parents('li:first').addClass('active').find('li:first').load(this.href + '&' + Math.random(), function(){
						adjustUI();
					});
					break;

				case 'gallery':
					$('#photos-toc>li>a.active').removeClass('active');
					$(this).addClass('active');
					// load content into container:
					$('#gallery-preview').load(this.href + '&' + Math.random());
					break;
			}


		self.location.href = '/' + location.search + '#!/' + this.pathname.replace(/^\//,'') + this.search;

		e.preventDefault();
	});

	initAnimation();
	adjustUI();
});

