/**
* The main javascript file for maxlogic.nl
* Core object with triggers and listeners
* Author: Max Meinders
* Date: Februari 14, 2011
*/

var Core = (function() {

	var air = [];
	
	var Core = {};
	
	Core.settings = {
		speed: 'fast',
		startpage: 'pages/welcome/'
	}
	
	Core.trigger = function(eventName, params) {
		for (i in air[eventName]) air[eventName][i](params);
	}
	
	Core.bind = function(eventName, func) {
		if (air[eventName] == undefined) air[eventName] = [];
		air[eventName].push(func);
	}
		
	Core.loadPage = function(path, target) {
		$(target).empty();
		$.get(path, function(data) {
			$(target).html(data);
		});
	}
	
	Core.onhashload = function(def) {
		var path = window.location.hash;
		var apath = path.split('/');
		if ( apath[1] ) {
			$('a[href="#pages/' + apath[1] + '/"]').click();		
			if ( apath[2] ) 
				$('a[href="#pages/' + apath[1] + '/' + apath[2] + '/"]').click();		
		} else {
			$('a[href="#' + def + '"]').click();
		}
	}
	
	Core.hashgoto = function(path, track) {
		if (path == undefined) path = Core.settings.startpage;
		if (typeof track != "undefined" && track === true) _gaq.push(['_trackPageview', path]);
		var apath = path.split('/');
		var current = $('.innerCloud .box');
		for (i in apath) {		
			if (apath[i].length > 0) {
				current.children('div').hide();
				//current.find('img').hide();
				found = current.find('#' + apath[i]);
				if (found.size() > 0) {
					current = found;
					current.show();
					//current.find('img').show();
				}
			}
		}
	}
	
	return Core;

})();

var structure = {
	welcome: {
		submenu: false,
		size: 'small'
	},
	why: {
		submenu: true,
		size: 'big'
	},
	references: {
		submenu: true,
		size: 'big'
	},
	licenses: {
		submenu: true,
		size: 'big'
	},
	contact: {
		submenu: false,
		size: 'small'
	}
}	

Core.bind("menu/click", function(elm) {
	elm = $(elm);
	$('.submenu').hide();
	
	var submenu = $('.outerCloud > .box');	
	var sub = elm.attr('href').substring(1).split('/')[1];
	
	if (sub != undefined) {
		var item = submenu.find('#' + sub);		
		item.show();
	}
	
	$('.menu .blockItem.title a').removeClass('act');
	elm.addClass('act');
	
	var left = elm.position().left - 437 + (elm.width() / 2);
	$('.outerCloud > .point').animate({ marginLeft: left}, Core.settings.speed );
	if (elm.attr('submenu') == undefined)
		$('.innerCloud > .point').animate({ marginLeft: left}, Core.settings.speed );
	
	if (elm.attr('submenu') != undefined) Core.trigger("submenu/open");
	else Core.trigger("submenu/close");
		
	if (elm.attr('size') == 'small') Core.trigger("container/small");
	else Core.trigger("container/big");
});

Core.bind("menu/click", function(elm) {
	Core.hashgoto($(elm).attr('href').substring(1), false);
});

Core.bind("submenu/open", function() {
	$('.innerCloud').animate({ bottom: 96}, Core.settings.speed);	
	Core.trigger("submenu/click", $('.outerCloud .blockItem.title a:visible').first());
});

Core.bind("submenu/close", function() {
	$('.innerCloud').animate({ bottom: 6}, Core.settings.speed );
	$('.scrollleft, .scrollright').hide();
});

Core.bind("submenu/click", function(elm) {
	
	$('.outerCloud .blockItem.title a').removeClass('act');
	elm.addClass('act');	
	
	var parent = elm.parents('ol');
	var count = parent.find('li').size();
	parent.width(count * 120);
	var index = parent.find('li').index(elm.parent());
	var width = parent.width();
	
	var corr = 0;
	
	if (width > 740) {
		if (index+1 < count) $('.scrollright').show();
		else $('.scrollright').hide();
		
		if (index > 0) $('.scrollleft').show();
		else $('.scrollleft').hide();
		
		corr = Math.round( (index / (count-1)) * (width - 740) );	
		
	} else {
		$('.scrollleft, .scrollright').hide();
	}
	
	$('.submenu .blocks').animate({ left: 22 - corr}, Core.settings.speed );
	Core.hashgoto(elm.attr('href').substring(1), true);
	
	var left = elm.position().left - 415 + (elm.width() / 2) - corr;
	$('.innerCloud > .point').animate({ marginLeft: left}, Core.settings.speed );
});

Core.bind("submenu/left", function() {
	var a = $('.submenu:visible a.act').parent().prev().find('a');
	window.location.hash = a.attr('href');
	a.click();
});

Core.bind("submenu/right", function() {
	var a = $('.submenu:visible a.act').parent().next().find('a');
	window.location.hash = a.attr('href');
	a.click();
});

Core.bind("container/small", function() {
	$('.container').animate({ height: 300, marginTop: -150}, Core.settings.speed );
});

Core.bind("container/big", function() {
	$('.container').animate({ height: 500, marginTop: -250}, Core.settings.speed );
});

$(document).ready(function() {
	
	/* Attach triggers */
	$('.menu .blockItem.logo a').click(function() { 
		Core.trigger("menu/click", $('.menu .blockItem.title a').first());
	});
	
	$('.menu .blockItem.title a').click(function() { 
		Core.trigger("menu/click", this);
	});
	
	$('.outerCloud .blockItem.title a').click(function() { 
		Core.trigger("submenu/click", $(this)); 
	});
	
	$('.scrollleft a').click(function() { 
		Core.trigger("submenu/left"); 
		return false;
	});
	
	$('.scrollright a').click(function() { 
		Core.trigger("submenu/right"); 
		return false;
	});

	
	Core.onhashload('pages/welcome/');
});

