/**
 * WinkBall namespace
 * @see http://adam.kahtava.com/journal/2008/03/17/namespacing-your-javascript/
 */
var winkball;
if (!winkball) winkball = {};

(function() {

	// Add some IE version detection to prototype
	Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
	Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
	Prototype.Browser.IE8 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 8;

	/*
	 * Creates a cookie called 'name' with 'value' and optional expiry date 'days'
	 */
	winkball.createCookie = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	};

	/*
	 * Reads a cookie with 'name'
	 */
	winkball.readCookie = function(name) {
		var nameEQ = name + "=";
		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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	};

	/*
	 * Removes a cookie with 'name'
	 */
	winkball.eraseCookie = function(name) {
		winkball.createCookie(name,"",-1);
	};

	/*
	 * Puts the scrollTop value for element with 'id' into cookie
	 */
	winkball.recordScrollTop = function(id) {
		var node = document.getElementById(id);
		if (!node) return;
		var scrollTopValue = node.scrollTop;
		winkball.createCookie("scrollTop-"+id, scrollTopValue);
	};

	/*
	 * Reads the scrollTop for 'id' from a cookie and sets scrollTop for element with 'id'
	 */
	winkball.restoreScrollTop = function(id) {
		var scrollTopValue = winkball.readCookie("scrollTop-"+id);
		if (scrollTopValue) {
			var node = document.getElementById(id);
			if (node) node.scrollTop = scrollTopValue; // in case page is not fully loaded
		}
	};

	 
 })();

(function() {

	function padNumber(number) {
		if(number < 10) {
			number = "0" + number;
		}
		return number;
	}

	/**
	 * I log a message to the console if available.
	 * 
	 * @param {String} message The message to log
	 * @param {String} type The type of the message (debug, info, warn or error)
	 * @param {Boolean} nodate True for not printing datetime
	 */
	function log(message, type, nodate) {

		if(typeof(nodate) == "undefined") {
			var date = new Date();
			message = padNumber(date.getHours()) + ":" + padNumber(date.getMinutes()) + ":" + padNumber(date.getSeconds()) + " - " + message;
		}

		if(typeof(window['console']) != "undefined") {
			if(console[type]) { // firebug
				console[type](message);
			} else if(console.log) { // generic console (Firefox, Safari)
				console.log(message);
			}
		}
	}

	winkball.log = {

		debug: function(message) {
			log(message, "debug");
		},
	
		info: function(message) {
			log(message, "info");
		},
	
		warn: function(message) {
			log(message, "warn");
		},
	
		error: function(message) {
			log(message, "error");
		},

		dir: function(object) {
			this.dumpObject(object);
		},

		dumpObject: function(object) {
			if(Object.isString(object)) {
				this.info(object);
			} else {
				log(object, "dir", true);
			}
		},
	
		/**
		 * Removes lines referencing prototype.js and anonymous closures from stack traces to make the output
		 * a bit more readable.  This of course makes the assumption that prototype.js is perfect.
		 * 
		 * @param {Exception} e An exception object
		 * @param {boolean} doNotReplace Pass true if you don't want the extra lines to be removed
		 */
		dumpException: function(e, doNotReplace) {
			if(typeof(e) == "string") {
				this.error(e);
				return;
			}

			for(var key in e) {
				try {
					if(!doNotReplace && e[key].replace) {
						e[key] = e[key].replace(/^[apply|call](.)*$/gm, "");
						e[key] = e[key].replace(/^(.)*prototype\.js(.)*$/gm, "");
					}

					this.error(key + " = " + e[key]);

				} catch(e) {}
			}
		}
	};

})();