/**
 *  This is the SiteController. It is mainly used to register other controllers and execute
 *  the other controller's behaviors.
 *  http://www.thinkingphp.org/2006/11/23/how-to-organize-your-cakephp-app’s-javascript-ii/
 */
SiteController = {
	controllers: [],
	menuSize: {},

	initialize: function() {
		this.registerController(this);
		this.applyBehaviors();
		this.applyAjaxBehaviors();
	},
	
	applyBehaviors: function() {
		$(this.controllers).each(function() {
			for (var fct in this.Behaviors) {
				if (typeof(this.Behaviors[fct]) == 'function' && !(Function[fct])) {
					this.Behaviors[fct]();
				}
			}
		});
	},
	
	// These behaviours will only be executed after an ajax-request
	applyAjaxBehaviors: function() {
		$(this.controllers).each(function() {
			for (var fct in this.AjaxBehaviors) {
				if (typeof(this.AjaxBehaviors[fct]) == 'function' && !(Function[fct])) {
					this.AjaxBehaviors[fct]();
				}
			}
		});
	},
	
	Behaviors: {
		externalLinks: function() {
			$('a[rel=external]').click(function() {
				window.open(this.href);
				return false;
			});
		},
		pulldown: function() {
			$('#menu-main ul').superfish({
				hoverClass	: "hover",
				delay		: 300,
				animation	: {opacity:"show"},
				speed		: "normal"
			});
		},
		fadeLogo: function() {
			$('#photos .fade').animate({ height: 51 }, 1500);
		},
		fontScaling: function() {
			$('#size a.minus').click(function() {
				SiteController.currentFontSize();
				var changed = 0;
				if (SiteController.menuSize.fontSize >= 16) {
					changed = SiteController.menuSize.fontSize-4;
				}

				if (changed < 16) {
					return false;
				}
				$('body').animate({ fontSize: changed }, '', '', function() { SiteController.resetTopMenu(changed); } )
				if (changed <= 20) {
					$('#menu-top ul.top-1').animate({width: SiteController.menuSize.w1});
					$('#menu-top ul.top-2').animate({width: SiteController.menuSize.w2});
				}
				SiteController.storeFontSize(changed);
				return false;
			});
			$('#size a.plus').click(function() {
				SiteController.currentFontSize();
				var changed = 0;
				if (SiteController.menuSize.fontSize <= 24) {
					changed = SiteController.menuSize.fontSize+4;
				}
				
				if (changed > 24) {
					return false;
				}
				// Make the horizontal menu-links smaller
				$('#menu-top ul.top-1 li a').css({'height': '2.4em', 'minHeight': '2.4em'})
				$('body').animate({ fontSize: changed })
				if (changed > 20) {
					$('#menu-top ul.top-1').animate({width: SiteController.menuSize.w1-20});
					$('#menu-top ul.top-2').animate({width: SiteController.menuSize.w2+20});
				}
				SiteController.storeFontSize(changed);
				return false;
			});
		}
		
	},
	currentFontSize: function() {
		SiteController.saveTopMenu();
		var cur = $('body').css('font-size');
		cur = cur.replace(/px/, '');
		cur = cur.replace(/em/, '');
		cur = parseInt(cur);
		// IE gets the size in EM
		if (cur == 1) {
			cur = 16;
			$('body').css('font-size', cur)
		}
		SiteController.menuSize.fontSize = cur;
	},
	saveTopMenu: function() {
		// Store original state
		if (!SiteController.menuSize.w1) {
			SiteController.menuSize = {
				w1: $('#menu-top ul.top-1').width(),
				w2: $('#menu-top ul.top-2').width(),
				h1: $('#menu-top ul.top-1 li a').height()
			};
		}
	},
	resetTopMenu: function(fontSize) {
		if (fontSize != 16) {
			return false;
		}
		// Reset original state
		$('#menu-top ul.top-1 li a').css({	'height': SiteController.menuSize.h1,
							'minHeight': SiteController.menuSize.h1 });
	},
	storeFontSize: function(fontSize) {
		if (!fontSize) {
			return;
		}
		$.get('/pages/fontsize/'+SiteController.menuSize.w1+','+SiteController.menuSize.w2+','+SiteController.menuSize.h1+','+fontSize);
	},

	registerController: function(controller) {
		this.controllers.push(controller);
	}
};
$(document).ready(function() { SiteController.initialize(); } );