/*////////////////////////////////////////////////////////////////////////////  
Alex JavaScript framework, version 1.3 - by: Jason Savage
------------------------------------------------------------------------------
This is a very small framework for functions that I commonly find my-self using.
Credits: 
	- Prototype Framework (www.prototypejs.org)
	- AJS Framework (www.amix.dk)
	- jQuery Framework (www.jquery.com)
////////////////////////////////////////////////////////////////////////////*/  

function $O(HTMLElementId/*:String*/) /*:HTMLObject*/{
	return alex.Get.element(HTMLElementId);
}

var alex = { //core functions
	version : '1.3',
	
	Require : function(str/*:String*/)/*:Boolean*/{
		var re = false, org = str, type = '';str = str.split('()').join(''); 
		if(alex.InStr(str,'.js')){type = 'script'; var d = document.getElementsByTagName('script');
		alex.Walk(d, function(index,value){var tmp = (!alex.Empty(value.src))?value.src.split('/').pop():''; if(value.src == str || tmp == str) re = true;});
		}else{type = 'function'; var d = 'window.'+ str; if(eval(d)) re = true;}
		if(!re) alert('The required ' + type + ' : "' + org + '" is missing!');
		return re;
	},
	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 false;
		for(x in objA) objB[x] = objA[x];
		return objB;
	},
	Remove : function(obj/*:Object*/,arr/*:Array*/)/*:Object*/{
		if(!obj) return false;
		var newobj=new Object, key=(alex.Is.array(arr))?arr.join(","):arr;
		alex.Walk(obj, function(index,value){ if(!alex.InStr(key,index)) newobj[index] = value;});
		return newobj;
	},
	Keep : function(obj/*:Object*/,arr/*:Array*/)/*:Object*/{
		if(!obj) return false;
		var newobj=new Object, key=(alex.Is.array(arr))?arr.join(","):arr;
		alex.Walk(obj, function(index,value){ if(alex.InStr(key,index)) newobj[index] = value;});
		return newobj;
	},
	Walk : function(obj/*:Object*/, callback/*:Function*/)/*:Array*/{
		if(!obj || typeof(callback) != 'function') return ''; var a = []; 
		for(x in obj){var r = callback(x,obj[x]); if(r!=''&&r!=null) a.push(r);}
		return a;
	},
	Trace : function(obj/*:Object*/)/*:void*/{
		if(!obj) return ""; alex.Require('alex.Walk()');
		var c = alex.Walk(obj,function(a,b){return a+" = "+b;}).join("\n");
		alert(c);
	},
	
	//[Get]//////////
	Get : {
		element : function(str/*:String*/)/*:HTMLObject*/{
			if(typeof str != 'string') 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];
			for(x in q){
				tmp = q[x].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 : {
		array : function(obj/*:Object*/)/*:Array*/{
			if(!alex.Is.object(obj)) return "";
			return alex.Walk(obj,function(index,value){return index+"="+value;});
		},
		query : function(obj/*:Object*/)/*:String*/{
			if(!alex.Is.object(obj)) return "";
			var str = alex.Walk(obj,function(index,value){return index+"="+value;}).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.Walk(attrs,function(index,value){return index+'="'+value+'" ';}).join(""):'';
			ptr[1] =  alex.Walk(obj,function(index,value){return "<"+index+">"+value+"</"+index+">";}).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";  alex.Require('alex.Walk()'); alex.Require('alex.Get.center()'); alex.Require('alex.Apply()');
			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(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.Walk(setup,function(n,v){return n+"="+v;}).join(",");
			newWin = window.open(url, winName +Math.floor(Math.random()*999), f);
			newWin.focus();
			return newWin;
		},
		flash : function(lnk/*:String*/,width/*:Number*/,height/*:Number*/,setupObj/*:Object*/)/*:void*/{
			var trg = '', fcode = '',o,p,e
			var movieName = lnk.split("?").shift().split("/").pop().split(".").shift();
			var settings = {id:movieName, name:movieName,width:width,height:height,src:lnk,movie:lnk,
				codebase:'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab',
				classid:'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',pluginspage:'http://www.macromedia.com/go/getflashplayer',
				type:"application/x-shockwave-flash",play:'true',loop:'true',menu:'false',devicefont:'false',quality:'high',wmode:'transparent',
				align:'middle',scale:'showall',bgcolor:'#ffffff',allowFullScreen:'false',allowScriptAccess:'sameDomain',salign:''
			}
			if(setupObj) settings = alex.Apply(setupObj,settings);
			if(settings.flashVars){settings.src = settings.movie = settings.src + alex.To.query(settings.flashVars);settings = alex.Remove(settings,['flashVars']);}
			o = alex.Walk(alex.Keep(settings,['classid','codebase','width','height','id','align']),function(a,b){return a+'="'+b+'" ';}).join("")
			p = alex.Walk(alex.Remove(settings,['id','name','width','height','src','classid','codebase','pluginspage','type']),function(a,b){return '<param name="'+a+'" value="'+b +'" />';}).join("")
			e = alex.Walk(alex.Remove(settings,['classid','codebase','movie','id']),function(a,b){return a+'="'+b+'" ';}).join("")
			fcode = '<object ' + o + '>' + p + '<embed ' + e + '/></object>';
			
			document.write(fcode);
		}
	},
	
	//[Is]//////////
	Is : {
		array : function (obj/*:**/)/*:Boolean*/{
			if(obj && obj.constructor == Array) return true;
			return false;
		},
		object : function (obj/*:**/)/*:Boolean*/{
			if(obj && obj.constructor != Array) return true;
			return 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*/{ 
		return str.replace(/^0+|0+$/g,"");
	},
	InStr : function(str/*:String*/,find/*:String*/)/*:Boolean*/{
		if(str.indexOf(find) != -1) return true;
		return false;
	}
}

//Setup - PHP style///////////
var $Q = alex.Get.query();
/////////////////////////////
