/*****************************
 *    Util - Various useful functions       *
******************************/
var Util = {
	// centers an element 
	center: function (elm) {
		elm = $(elm);
		var l = (document.viewport.getWidth() - elm.getWidth()) / 2;
		elm.setStyle({left: Math.floor(l) + "px"});
	},
	
	// returns the number of pixels that the document has already been scrolled vertically 
	scrollTop: function () {
		return window.scrollY || document.documentElement.scrollTop || 0;
	},
	
	// returns the number of pixels that the document has already been scrolled horizontally 
	scrollLeft: function () {
		return window.scrollX || document.documentElement.scrollLeft || 0;
	},
	
	// changes the document cursor by modifying the css rules - the cursor rule should be the 2nd and 3rd entry in the css file 
	setCursor: function (cursor) {
		if (!document.styleSheets[0].cssRules) {
			return;
		}
		(document.styleSheets[0].rules || document.styleSheets[0].cssRules)[1].style.cursor = cursor; // first style sheet, 2nd rule
		(document.styleSheets[0].rules || document.styleSheets[0].cssRules)[2].style.cursor = cursor; // 3rd rule
	},
	
	// reset the cursor back to the default value 
	clearCursor: function () {
		if (!document.styleSheets[0].cssRules) {
			return;
		}
		(document.styleSheets[0].rules || document.styleSheets[0].cssRules)[0].style.cursor = 'auto';
		(document.styleSheets[0].rules || document.styleSheets[0].cssRules)[1].style.cursor = 'pointer';
	},
	
	// Similar to using $ to access elements, except this returns a cached reference to the extended element. 
	cachedElms: {},
	cache: function (id) {
		var tempCache = Util.cachedElms;
		var elm = tempCache[id];
		if (!elm) {
			elm = $(id);
			tempCache[id] = elm;
		}
		return elm;
	},
	
	// removes trailing '/' on path string 
	normPath: function (path) {
		if (!path || path.charAt(path.length - 1) != '/') {
			return path;
		}

		return path.substr(0, path.length - 1);
	},
	
	// strips away any trailing '/' to guarantee only one '/' is added to the string's end 
	normDir: function (dir_path) {
		return Util.normPath(dir_path) + "/";
	},
	
	// Based on Douglas Crockford's purge from http://javascript.crockford.com/memory/leak.html ... Fixes IE's memory leaks 
	purge: function (d) {
		var a = d.attributes, i, l, n;
		if (a) {
			l = a.length;
			for (i = 0; i < l; i += 1) {
				n = a[i].name;
				if (typeof d[n] === 'function') {
					d[n] = null;
				}
			}
		}
		a = d.childNodes;
		if (a) {
			l = a.length;
			for (i = 0; i < l; i += 1) {
				Util.purge(a[i]);
			}	
		}
	},		
	
	// removes an element from the dom without creating leaks in IE 
	// passed element is assumed to be extended already
	remove: function (elm) {
		// pull an element out of the dom without memory leaking
		Util.purge(elm);
		
		//  safe way of removing the element without creating psuedo-leaks
		if (!Util.dom_trash_can) {
			Util.dom_trash_can = Util.cache('trash-can');
		}
		
		Util.dom_trash_can.insert(elm);
		Util.dom_trash_can.update();
		elm = null;
		
		return elm;
	},
	
	// check for ie or firefox on linux 
	ie6: window.external && typeof window.XMLHttpRequest == "undefined",
	ie: Prototype.Browser.IE,
	linux_ff3: navigator.userAgent.toLowerCase().indexOf('linux') > -1,
	
	// logger for IE 
	maxEntries: 10,
	log: function () {
		if (Constants.console) {
			if (!this.console_set) {
				this.cache('console').show();
				this.console_set = true;
			}
			
			var d = new Date();
			
			//if (this.maxEntries == 10) {
			Util.cache('console').innerHTML += "<span class='time'>" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "</span> - " + $A(arguments).join(" ") + "<br>";
			this.maxEntries += 1;
		}
	},
	
	// creates a random value 
	rand: function () {
		var time_part = Util.getTime();
		var rand_part = Math.floor(Math.random() * 1000000).toString();
		return time_part + rand_part;
	},
	
	// gives focus to the element 
	focus: function (elm) {
		elm = $(elm);
		try {
			elm.focus();
		} 
		catch (e) {
			// it's okay if focus fails
		}
	},
	
	// returns the number of milliseconds since midnight of January 1, 1970
	getTime: function() {
		var d = new Date();
		return d.getTime().toString();
	}
};

var RequestID = {
	nextID: 1,
	
	getID: function () {
		return RequestID.nextID++;
	}
};

/*************************
 *       Scriptaculous Effects         *
**************************/
Effect.BlindFadeUp = function (elm, opts) {
    var ef;
    ef = new Effect.BlindUp(elm, opts);
    ef = new Effect.Fade(elm, opts);
};

Effect.BlindFadeDown = function (elm, opts) {
    var ef;
    ef = new Effect.BlindDown(elm, opts);
    ef = new Effect.Appear(elm, opts);
};

/******************************
 *     Flash - displays status messages       *
*******************************/
var Flash = {
	show: function (message) {
		var container = new Element("div", {"id": "flash-container"});
		var actual_flash = new Element("p", {"id": "flash"});

		actual_flash.update(message);
		container.update(actual_flash);

		Util.cache('alert-container').update(container);
		//Util.cache('alert-container').show();
		
		var ef = new Effect.BlindFadeDown("alert-container", {queue: {scope: 'flash'}});
		
		Flash.scheduleHide();
	},

	scheduleHide: function () {
		if (Flash.hideTimer) {
			Flash.cancelHide();
		}
		// 5 sec display
		Flash.hideTimer = setTimeout(Flash.hide, 4000);
	},

	cancelHide: function () {
		clearTimeout(Flash.hideTimer);
	},

	hide: function () {
		var ef = new Effect.BlindFadeUp("alert-container", {queue: {scope: 'flash'}});
	}
};