/***********************
 * UTILITY FUNCTIONS 
 ***********************/
/**
 * $ - Shortcut for document.getElementById
 * @param id	string	ID of the element you want to retrieve
 * @return		object	Element object that matches the id
 */
function $(id){ return document.getElementById(id); }
/**
 * $n - Shortcut for document.getElementsByName
 * @param name	string		Name of the element(s) you want to retrieve
 * @return		collection	List of elements matching the name
 */
function $n(name){ return (this==window?document:this).getElementsByName(name); }
/**
 * $t - Shortcut for document.getElementsByTagName
 * @param tag	string		TagName of the element(s) you want to retrieve
 * @return		collection	List of elements matching the tag
 */
function $t(tag){ return (this==window?document:this).getElementsByTagName(tag); }
/**
 * slice - Shortcut for Array.prototype.slice.call(obj, idx)
 * @param obj	mixed		Object to call array.slice on
 * @param idx	integer		Index at which to begin slicing
 * @return		array		New array containing values from the idx to the end of the obj
 *		Note: Useful for transforming arguments object and collections into regular arrays
 *		ex.	function(){ var args = slice(arguments); alert(args instanceof Array); }
 */
function slice(obj,idx){
	var ret = obj;
	if(/internet\sexplorer/i.test(navigator.appName)){
		if(typeof obj.length=='undefined'){ obj.length = getLength(obj); }
		ret = Array.prototype.map.call(obj,function(item){return item;});
	}
	return Array.prototype.slice.call(ret,idx || 0);
}
/**
 * next - Returns the next non-whitespace node in the DOM
 * @param elm	object		Element node from which to start
 * @return		object		Next non-whitespace element in the DOM
 */
function next(elm){ 
	if (!elm) {
		return elm;
	}
	elm = elm.nextSibling; 
	return (elm.nodeType==1) ? elm : next(elm); 
}
/**
 * prev - Returns the previous non-whitespace node in the DOM
 * @param elm	object		Element node from which to start
 * @return		object		Previous non-whitespace element in the DOM
 */
function prev(elm){ elm=elm.previousSibling; return (elm.nodeType==1) ? elm : prev(elm); }
/**
 * Array.indexOf - adds indexOf() support to browsers that don't have it.
 */
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj,start){
		var len = this.length;
		start = Number(start) || 0;
		start = (start < 0) ? Math.ceil(start) : Math.floor(start);
		if(start < 0){ start+= len; }
		for(;start < len;start++){
			if(start in this && this[start]===obj){ return start; }
		}
		return -1;
	}
}
/**
 * Array.forEach - adds forEach() support to browsers that don't have it.
 */
if(!Array.forEach){
	Array.prototype.forEach = function(fn /*, bind*/){
		var len = this.length;
		if(typeof(fn) != 'function'){ throw new TypeError(); }
		var bind = arguments[1];
		for(var i=0;i < len;i++){
			if(i in this){ fn.call(bind, this[i], i, this); }
		}
	};
}
/**
 * Array.map - adds map() support to browsers that don't have it.
 */
if(!Array.map){
	Array.prototype.map = function(fn /*, bind*/){
		var len = this.length;
		if(typeof(fn) != 'function'){ throw new TypeError(); }
		var ret = [], bind = arguments[1];
		for (var i=0;i < len;i++){
			if(i in this){ ret[i] = fn.call(bind, this[i], i, this); }
		}
		return ret;
	};
}
/**
 * Array.filter - adds filter() support to browsers that don't have it
 */
if(!Array.filter){
	Array.prototype.filter = function(fn /*, bind*/){
		var len = this.length;
		if(typeof(fn) != 'function'){ throw new TypeError(); }
		var ret = [], bind = arguments[1], val=null;
		for(var i=0;i < len;i++){
			if(i in this){
				val=this[i];
				if(fn.call(bind,val,i,this)){ ret.push(val); }
			}
		}
		return ret;
	};
}
/**
 * getLength - Finds the total number of all non-function properties owned by the object (see hasOwnProperty)
 * @param obj    object    optional;The object whose properties to count. (default: this,  example: getLength.call(myobj))
 * @return        int        The length of the object or 0 if obj was invalid
 */
function getLength(obj){
    var i=0, obj = (obj || this);
    if(!obj){ return 0; }
    for(var key in obj){
        if(obj.hasOwnProperty(key) && typeof obj[key] !='function'){i++;}
    }
    return i;
}

/**
 * fixEvent - Returns an event object with common properties/methods normalized for easier cross browser usage.
 * @param e		object	optional;Event object to normalize
 * @return		object	Event object after normalization
 */
function fixEvent(e){
	evnt = e || window.event;
	if(!evnt.target){ evnt.target = evnt.srcElement; }
	evnt.preventDefault = (evnt.preventDefault)? evnt.preventDefault : function(){ this.returnValue = false; };
	evnt.stopPropagation = (evnt.stopPropagation)? evnt.stopPropagation : function(){ this.cancelBubble = true; }
	return evnt;
}

/* Regular expression to match empty or undefined values */
var empty = /^(?:\s*|undefined)$/i;

function setLocale(url) {
	new async_rpc(url, function () { window.location = window.location.href.replace(window.location.hash,"") + "#currency"; window.location.reload(true); });
}


/* domLoader - detects when the DOM is ready in the browser (typically before onload would fire).
 * Also allows registration of functions to execute once ready.
 */
var domLoader = {
	'isReady':false,
	'timer':null,
	'queue':[],
	'register':function(fn){
		var self = this;
		if(this.isReady){ return fn(); }
		else if(!this.timer){
			registerEvent(window,'load', function(){ self.checkDom(); });
			this.timer = window.setInterval(function(){ self.checkDom(); },13);
		}
		this.queue.push(fn);
	},
	'checkDom':function(){
		if(this.isReady){ return true; }
		if(document && document.getElementById && document.getElementsByTagName && document.body){
			window.clearInterval(this.timer);
			for(var i=0;i < this.queue.length;i++){
				this.queue[i]();
			}
			this.timer = null;
			this.queue = [];
			this.isReady = true;
			return true;
		}
		return false;
	}
};

function validateSearch(){
	var search = $('search');
	var valid = true;
	if (search && search.value == '') {
		valid = false;
		alert('Please enter a term to search for.');
		search.focus();
	}
	return valid;
}

function hoverOn(el){
	if (!el) { return; }
	el.className = el.className+' hover';
}

function hoverOff(el){
	if (!el) { return; }
	el.className = el.className.replace(/\s?hover\s?/ig,'');
}

function vRoll(name, height, speed) {
	if(! speed) {
		speed = 250;
	}
	fxHeight = height;
	var targ = domcheck(name);
	if ( targ.style.display == 'none' ) {
		new Activa_FX('RollDown', targ, speed);
	} else {
		new Activa_FX('RollUp', targ, speed);
	}
}

function showLogin() {
	vRoll('login', '30');
}
