/*////////////////////////////////////////////////////////////////////////////  

Alex JavaScript framework, version 1.0 - 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 Framwork (www.amix.dk)
	- jQuery (www.jquery.com)
------------------------------------------------------------------------------

$Q = automatically is created with the get variables for the page


$O(str);

alex.Require(str);
alex.Empty(str);
alex.Apply(obj,obj);
alex.Remove(obj,array);
alex.Keep(obj,array);
alex.Convert(obj,function);
alex.Trace(obj);

//Get Library
alex.Get.element();
alex.Get.query();
alex.Get.client();
alex.Get.screen();
alex.Get.center(obj,obj);

//To Library
alex.To.array(obj); 
alex.To.query(obj);
alex.To.xml(obj);

//Create Library
alex.Create.window(str,obj);
allex.Create.flash(lnk,width,height,setupObj);

//String Library
alex.Pad(str);
alex.Trim(str);
alex.ZeroTrim(str);
alex.Instr(str,find);


////////////////////////////////////////////////////////////////////////////*/  

function $O(n){
	return alex.Get.element(n);
}

var alex = { //core
	version : '1.0',
	
	UIMessages : function(type,message){
		var as = "/////////////////////////////////\nAlex\n/////////////////////////////////\n";
		switch(type){
			case 'alert' : alert(as + 'Alert! ::\n' + message); break;
			case 'error' : alert(as + 'Error! ::\n' + message); break;
			case 'success' : alert(as + 'Success! ::\n' + message); break;
			case 'failure' : alert(as + 'Failure! ::\n' + message); break;
		}
	},
	
	Require : function(str){
		var re = false, org = str, type = '';str = str.split('()').join(''); 
		if(alex.InStr(str,'.js')){type = 'script'; var d = document.getElementsByTagName('script');
			for(x in d){
				var tmp = (!alex.Empty(d[x].src))?d[x].src.split('/').pop():'';
				if(d[x].src == str || tmp == str) re = true;
			}
		}else{type = 'function'; var d = 'window.'+ str; if(eval(d)) re = true;}
		if(!re) alex.UIMessages('alert','The required ' + type + ' : "' + org + '" is missing!');
		return re;
	},
	Empty : function(str){
		if((str != undefined && str != '' && str != false) || str > 0) return false;
		if(typeof str == 'object') for(each in str) return false;
		return true;
	},
	Apply : function(objA,objB){
		if(!objA || !objB) return false;
		for(x in objA) objB[x] = objA[x];
		return objB;
	},
	Remove : function(obj,a){
		if(!obj) return false;
		var newobj={},key=(a.constructor == Array)?a.join(","):a;
		for(x in obj) if(!alex.InStr(key,x)) newobj[x] = obj[x];
		return newobj;
	},
	Keep : function(obj,a){
		if(!obj) return false;
		var newobj={},key=(a.constructor == Array)?a.join(","):a;
		for(x in obj) if(alex.InStr(key,x)) newobj[x] = obj[x];
		return newobj;
	},
	Convert : function(obj,utilityFunction){
		if(!obj || typeof(utilityFunction) != 'function') return ''; var a = []; 
		for(x in obj){r = utilityFunction(x,obj[x]); if(r!=''&&r!=null) a.push(r);}
		return a;
	},
	Trace : function(obj){
		if(!obj) return ""; alex.Require('alex.Convert()');
		var c = alex.Convert(obj,function(a,b){return a+" = "+b;}).join("\n");
		alert(c);
	},
	
	//[Get]//////////
	Get : {
		element : function(n){
			if(document.getElementById) return document.getElementById(n);
			if(document.all) return document.all[n];
			if(document.layers) return document.layers[n];
		},
		query : function(){
			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(){
			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(){
			var w = (screen.availWidth)? screen.availWidth:1024;
			var h = (screen.availHeight)? screen.availHeight:768;  
			return {width:w, height:h};
		},
		center : function(objA,objB){
			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){
			if(!obj) return ""; alex.Require('alex.Convert()');
			return alex.Convert(obj,function(a,b){return a+"="+b;});
		},
		query : function(obj){
			if(!obj) return "";  alex.Require('alex.Convert()');
			var str = alex.Convert(obj,function(a,b){return a+"="+b;}).join("&");
			return (str.length > 0)?("?"+str):"";
		},
		xml : function(nodeName,obj,attrs) {
			if(!obj) return "";  alex.Require('alex.Convert()');
			var ptr=[]; nodeName = (!alex.Empty(nodeName))?nodeName : 'xmlNode';
			ptr[0] = (!alex.Empty(attrs))? alex.Convert(attrs,function(a,b){return a+'="'+b+'" ';}).join(""):'';
			ptr[1] =  alex.Convert(obj,function(a,b){if(a!="nodeName"&&a!="attributes") return "<"+a+">"+b+"</"+a+">";}).join("\n");
			return "<" + nodeName + " " + ptr[0] + ">\n" + ptr[1] + "\n</" + nodeName +">";
		}
	},
	
	Create : {
		window : function(url,opts){
			var f,newWin,winName="generalwin";  alex.Require('alex.Convert()'); alex.Require('alex.Get.center()'); alex.Require('alex.Apply()');
			var setup = {width:'640',height:'480',top:'20',left:'20',fullscreen:'no',resizable:'yes',scrollbars:'no',status:'no',titlebar:'no',toolbar:'no',location:'no'}
			
			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.Convert(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,width,height,setupObj){
			var trg = '', fcode = '',o,p,e
			var settings = {id:lnk.split(".")[0].toString().split("/").pop(),name:lnk.split(".")[0].toString().split("/").pop(),
				width:width,height:height,src:lnk,movie:lnk,codebase:'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
				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.target){trg = settings.target;settings = alex.Remove(settings,['target']);}
			if(settings.flashVars){settings.src = settings.movie = alex.To.query(settings.flashVars);settings = alex.Remove(settings,['flashVars']);}
			o = alex.Convert(alex.Keep(this.settings,['classid','codebase','width','height','id','align']),function(a,b){return a+'="'+b+'" ';}).join("")
			p = alex.Convert(alex.Remove(this.settings,['id','name','width','height','src','classid','codebase','pluginspage','type']),function(a,b){return '<param name="'+a+'" value="'+b +'" />';}).join("")
			e = alex.Convert(alex.Remove(this.settings,['classid','codebase','movie','id']),function(a,b){return a+'="'+b+'" ';}).join("")
			fcode = '<object ' + o + '>' + p + '<embed ' + e + '/></object>';
			
			if(!alex.Empty(trg)){alex.Get.element(trg).innerHTML = fcode;
			}else{document.write(fcode);}
		}
	},
	Pad : function(str){
		var v = parseInt(str);
		if(v <= 9 && v >= 0) v = "0"+v;
		return v;
	},
	Trim : function(str) { 
		str = str.toString();
		while(str.charAt(0) == " ") str = str.substring(1,str.length);
		while(str.charAt(str.length-1) == " ")str = str.substring(0,str.length-1);           
		return str;
	},
	ZeroTrim : function(str) { 
		while(parseInt(str.charAt(0)) == 0) str = str.substring(1,str.length);
		while(parseInt(str.charAt(str.length-1)) == 0 && str.indexOf('.') != -1)str = str.substring(0,str.length-1);           
		return str;
	},
	InStr : function(str,find){
		if(str.indexOf(find) != -1) return true;
		return false;
	}
}

//Setup - PHP style///////////
var $Q = alex.Get.query();
/////////////////


