/*////////////////////////////////////////////////////////////////////////////  
alex JavaScript framework, version 1.6 - by: Jason Savage
------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////*/  

function $O(HTMLElementId/*:String*/) /*:HTMLObject*/{
	return alex.Get.element(HTMLElementId);
}

var alex = { //core functions
	version : '1.6',
	
	Empty : function(str/*:String*/)/*:Boolean*/{
		if((typeof str != 'undefined' && str != null && str != '' && str != false) || str > 0) return false;
		if(alex.Is.object(str) || alex.Is.array(str)) for(each in str) return false;
		return true;
	},
	Apply : function(objA/*:Object*/,objB/*:Object*/)/*:Object*/{
		if(!objA || !objB) return {};
		alex.Walk(objA, function(ele,indx,arr){objB[indx] = ele;});
		return objB;
	},
	Remove : function(obj/*:Object*/,arr/*:Array*/)/*:Object*/{
		if(!obj || alex.Empty(arr)) return {};
		var key=alex.To.string(arr,',');
		if(alex.Is.array(obj)) return alex.Filter(obj, function(ele,indx,arr){ return !alex.InStr(key,ele);});
		if(alex.Is.object(obj)){var newobj = {};alex.Walk(obj, function(ele,indx,arr){if(!alex.InStr(key,indx))newobj[indx]=ele;});return newobj;}
	},
	Keep : function(obj/*:Object*/,arr/*:Array*/)/*:Object*/{
		if(!obj || alex.Empty(arr)) return {};
		var key=alex.To.string(arr,',');
		if(alex.Is.array(obj)) return alex.Filter(obj, function(ele,indx,arr){ return alex.InStr(key,ele);});
		if(alex.Is.object(obj)){var newobj = {};alex.Walk(obj, function(ele,indx,arr){if(alex.InStr(key,indx))newobj[indx]=ele;});return newobj;}
	},
	IndexOf : function(obj/*:Object*/,value/*:String*/)/*:Number*/{
		if(!obj || alex.Empty(value)) return -1; 
		var f=-1; alex.ForEach(obj, function(element,index,array){if(element == value)f=index;});
		return f;
	},
	Filter : function(obj/*:Object*/, callback/*:Function*/)/*:Array*/{
		if(!obj || typeof(callback) != 'function') return []; 
		var a=[],r=false; for(x in obj){r=callback(obj[x],x,obj);if(r==true||r=='true'||r==1)a.push(obj[x]);}
		return a;
	},
	Map : function(obj/*:Object*/, callback/*:Function*/)/*:Array*/{
		if(!obj || typeof(callback) != 'function') return []; 
		var a=[],r='';for(x in obj){r=callback(obj[x],x,obj);if(r!='')a.push(r);}
		return a;
	},
	Walk : function(obj/*:Object*/, callback/*:Function*/)/*:Void*/{
		if(!obj || typeof(callback) != 'function') return []; 
		for(x in obj){callback(obj[x],x,obj);}
	},
	Trace : function(obj/*:Object*/,rtrn/*:boolean*/)/*:void*/{
		if(!obj) return "";
		var c = alex.Map(obj,function(ele,indx,arr){return indx+" = "+ele;}).join("\n");
		if(rtrn) return c; else alert(c);
	},
	Show : function (str/*:String*/)/*:Void*/{
		var ele = $O(str);
		if(ele) ele.style.display = 'block';
	},
	Hide : function (str/*:String*/)/*:Void*/{
		var ele = $O(str);
		if(ele) ele.style.display = 'none';
	},
	
	//[Get]//////////
	Get : {
		element : function(str/*:String*/)/*:HTMLObject*/{
			if(!alex.Is.string(str)) return false;
			if(document.getElementById) return document.getElementById(str);
			if(document.all) return document.all[str];
			if(document.layers) return document.layers[str];
			return false;
		},
		query : function()/*:Object*/{
			var tmp,obj={},q=document.location.href;
			if(!alex.InStr(q,'?')) return "";
			q = q.split("?")[1];
			q = (alex.InStr(q,'&'))? q.split("&"):[q];
			alex.Walk(q,function(ele,indx,arr){var tmp = ele.toString().split("=");obj[tmp[0]] = tmp[1];});
			return obj;
		},
		client : function()/*:Object*/{
			var w,h
			if (self.innerWidth){ 
				w = self.innerWidth;
				h = self.innerHeight;
			}else if (document.documentElement && document.documentElement.clientWidth){
				w = document.documentElement.clientWidth;
			 	h = document.documentElement.clientHeight;
			}else if (document.body){
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
			return {width:w, height:h};
		},
		screen : function()/*:Object*/{
			var w = (screen.availWidth)? screen.availWidth:1024;
			var h = (screen.availHeight)? screen.availHeight:768;  
			return {width:w, height:h};
		},
		center : function(objA/*:Object*/,objB/*:Object*/) /*:Object*/{
			if(!objA || !objB) return {x:0, y:0};
			var xpos = (objA.width/2) - (objB.width/2);
			var ypos = (objA.height/2) - (objB.height/2);
			return {x:xpos, y:ypos};
		}
		
	},
	
	//[To]//////////
	To : {
		string : function(obj/*:**/,delimiter/*:String*/)/*:String*/{
			delimiter=!alex.Empty(delimiter)?delimiter:',';
			if(alex.Is.object(obj)||alex.Is.array(obj)) return alex.Map(obj,function(ele,inx,arr){return ele;}).join(delimiter);
			return "" + obj + "";
		},
		array : function(obj/*:**/,delimiter/*:String*/)/*:Array*/{
			if(alex.Is.object(obj)) return alex.Map(obj,function(ele,inx,arr){return ele;});
			if(alex.Is.array(obj)) return obj;
			return (!alex.Empty(delimiter))?obj.split(delimiter):[obj];
		},
		query : function(obj/*:Object*/)/*:String*/{
			if(!alex.Is.object(obj)) return "";
			var str = alex.Map(obj,function(ele,indx,arr){return indx+"="+ele;}).join("&");
			return (str.length > 0)?("?"+str):"";
		},
		xml : function(nodeName/*:String*/, obj/*:Object*/, attrs/*:Object*/)/*:String*/{
			if(alex.Empty(nodeName) || !alex.Is.object(obj)) return "";
			var ptr=[]; nodeName = (!alex.Empty(nodeName))?nodeName : 'xmlNode';
			ptr[0] = (!alex.Empty(attrs))? alex.Map(attrs,function(ele,indx,arr){return indx+'="'+ele+'" ';}).join(""):'';
			ptr[1] =  alex.Map(obj,function(ele,indx,arr){return "<"+indx+">"+ele+"</"+indx+">";}).join("\n");
			return "<" + nodeName + " " + ptr[0] + ">\n" + ptr[1] + "\n</" + nodeName +">";
		}
	},
	
	//[Create]//////////
	Create : {
		window : function(url/*:String*/,opts/*:Object*/)/*:HTMLObject*/{
			var f,newWin,winName="generalwin"; 
			var setup = {width:'640',height:'480',top:'auto',left:'auto',fullscreen:'no',resizable:'yes',scrollbars:'yes',status:'yes',titlebar:'yes',toolbar:'yes',location:'yes'}
			if(alex.Is.object(opts)) alex.Apply(opts,setup);
			if(setup.left == 'auto') setup.left = alex.Get.center(alex.Get.screen(),setup).x;
			if(setup.top == 'auto') setup.top = alex.Get.center(alex.Get.screen(),setup).y;
			f = alex.Map(setup,function(ele,indx,arr){return indx+"="+ele;}).join(",");
			newWin = window.open(url, winName +Math.floor(Math.random()*999), f);
			newWin.focus();
			return newWin;
		}
	},
	
	//[Is]//////////
	Is : {
		array : function (obj/*:**/)/*:Boolean*/{
			return (typeof(obj) == 'object' && obj.constructor == Array)?true:false;
		},
		object : function (obj/*:**/)/*:Boolean*/{
			return (typeof(obj) == 'object' && obj.constructor != Array)?true:false;
		},
		string : function (obj/*:**/)/*:Boolean*/{
			return (typeof(obj) == 'string')?true:false;
		},
		number : function (obj/*:**/)/*:Boolean*/{
			return (typeof(obj) == 'number')?true:false;
		},
		ie : function()/*:Boolean*/{
			var ua = navigator.userAgent.toString().toUpperCase();
			return (alex.InStr(ua,'MSIE') && !alex.InStr(ua,'OPERA'))?true:false;
		}
	},
	
	Pad : function(str/*:String*/)/*:String*/{
		var v = parseInt(str);
		if(v <= 9 && v >= 0) v = "0"+v;
		return v;
	},
	Trim : function(str/*:String*/)/*:String*/{ 
		return str.replace(/^\s+|\s+$/g,"");
	},
	ZeroTrim : function(str/*:String*/)/*:String*/{ 
		str = ""+str+"";
		return str.replace(/^0+|0+$/g,"");
	},
	InStr : function(str/*:String*/,find/*:String*/)/*:Boolean*/{
		if(str.indexOf(find) != -1) return true;
		return false;
	},
	
	//[Load Cue]//////////
	LoadCue : [],
	Inititialize : function(){
		if(alex.LoadCue.length > 0){
			alex.Walk(alex.LoadCue, function(ele,i,arr){ele();});
			alex.LoadCue = [];
		}
	},
	addtoLoadCue : function(callback){
		if(typeof(callback) != 'function') return;
		alex.LoadCue.push(callback);
	}
}

//Setup///////////
var $Q = alex.Get.query();
window.onload = alex.Inititialize;
/////////////////////////////
