/**
*	@package		Belvedere
*	@subpackage		Javascript
*	@author			Ben Sekulowicz-Barclay
*	@copyright		Copyright 2009, Ben Sekulowicz-Barclay.
*	@version		1.0
*
************************************************************************************************************************ **/

var belvedere = Class.create({	
			
	fontsize: 0,
	viewsize: 0,
	
	/* ****************************************************************************************************************** */
	
	initialize: function() {
		// When the DOM is available
		document.observe('dom:loaded', function(e) {
			
			// Get our body element niceley
			this.body = $$('body').first();
			
			// Call our three monitoring functions
			this.observeKeys();
			this.observeFontsize();
			this.observeViewsize();
			
			// Cal our onDomLoad function
			this.onDomLoad();
													
		}.bindAsEventListener(this));
	},
	
	/* *********************************************************************************************************************
	
	INTERNAL: OBSERVE EVENTS
	
	********************************************************************************************************************* */
	
	onDomLoad: function() { },
	
	/* ****************************************************************************************************************** */
	
	observeKeys: function() {
		// Create the keypress observer ...
		document.observe('keypress', function(e) {
			
			// Execute the function
			this.onKeys((e.which || e.keyCode));
			
		}.bindAsEventListener(this));
	},
	
	/* ****************************************************************************************************************** */
	
	onKeys: function(i) { },
	
	/* ****************************************************************************************************************** */
	
	observeFontsize: function() {
		// If the resize element doesn't exist, create it ...
		if (!$('belvedere-fontsize')) {
			this.body.insert({top: '<div id="belvedere-fontsize" style="height:1em;left:0;position:absolute;top:0;width:1em;"></div>'});
		}
		
		// If the height matches that on record, stop here ...
		if ($('belvedere-fontsize').getHeight() == this.fontsize) { return; }
		
		// Assign the new height
		this.fontsize = $('belvedere-fontsize').getHeight();
	
		// Call all of the functions we need ...
		this.onFontsize(this.fontsize);
					
		// Create the timer and check periodically
		new PeriodicalExecuter(this.observeFontsize.bind(this), 0.5);
	},
	
	/* ****************************************************************************************************************** */
	
	onFontsize: function(i) { },
	
	/* ****************************************************************************************************************** */
	
	observeViewsize: function() {		
		// Execute the event function immediately
		this.onViewsize(document.viewport.getHeight(), document.viewport.getWidth());
		
		// Create the resize observer ...
		Event.observe(window, 'resize', function() {
			
			// Execute the function
			this.onViewsize(document.viewport.getHeight(), document.viewport.getWidth());
			
		}.bindAsEventListener(this));
	},
	
	/* ****************************************************************************************************************** */
	
	onViewsize: function(h, w) { },
	
	/* *********************************************************************************************************************
	
	INTERNAL: SET AND GET COOKIES
	
	********************************************************************************************************************* */
	
	_cookieSet: function(n, v, d) {
		// Set the initial cookie expiry 
		var e = ""
		
		// if we set a date value in the set method ...
		if (d) {
			var date = new Date();
			date.setTime(date.getTime() + ( d * 24 * 60 * 60 * 1000));
			e = "; expires=" + date.toGMTString();
		}
		
		// Create the cookie
		document.cookie = n + "=" + v + e + "; path=/";
	},
	
	/* ****************************************************************************************************************** */
	
	_cookieGet: function(n) {
		// Create our cookie name and string vars
		var n = n + "=";		
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') { c = c.substring(1,c.length); }
			if (c.indexOf(n) == 0) { return c.substring(n.length, c.length); }
		}
		
		// Return nothing if we get here ...
		return null;
	}
	
	/* ****************************************************************************************************************** */
});