/**
* corefunctions 0.3.0 beta 
* NOT APPROVED YET
*/

var corefunctions = (function(){
	
	loadevent(injection);
	
	function loadevent (func) 
	{
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}
	
	function injection() 
	{
		convertRelByTagName('a');
		convertRelByTagName('area');
	}
	
	function convertRelByTagName (type)
	{
		if (document.getElementsByTagName) {
			var links = document.getElementsByTagName ( type );
			for (i=0,len=links.length; i<len; i++) {
				if (type == 'a' && typeof links[i].rel != 'undefined'  && links[i].rel.match(/type:/)) {
					links[i].onclick = function() {
						var o = eval('({' + this.rel + '})');
						if (o.type == 'popup'){
							return popup(this.href,o);
						}
					}
				} else if (type == 'a' && typeof links[i].rel != 'undefined'  && links[i].rel.match(/^external$/)) {
					this.target = '_blank';
				} else if (type == 'area' && typeof links[i].className != 'undefined' && links[i].className.match(/type:/)) {
					var o = eval('({' + links[i].className + '})');
					if (o.type == 'normal') links[i].target = '_blank';
					links[i].onclick = function() {
						var o = eval('({' + this.className + '})');
						if (o.type == 'popup'){
							return popup(this.href,o);
						}
					}
				} else if (type == 'area' && typeof links[i].rel != 'undefined'  && links[i].className.match(/^external$/)) {
					this.target = '_blank';
				}
			}
		}
	}
	
	function popup (url,o)
	{
	
		o.screen = {};
		o.screen.width = screen.width - 10;
		o.screen.height = screen.height - 28;
		
		// Failsafe, set defaults
		if (typeof o.name == 'undefined') o.name = "default";
		if (typeof o.position == 'undefined') o.position = false;
		if (typeof o.width == 'undefined') o.width = screen.width;
		if (typeof o.height == 'undefined') o.height = screen.height;
		if (typeof o.top == 'undefined') o.top = 0;
		if (typeof o.left == 'undefined') o.left = 0;
		if (typeof o.toolbar == 'undefined') o.toolbar = 0;
		if (typeof o.scrollbars == 'undefined') o.scrollbars = 0;
		if (typeof o.menubar == 'undefined') o.menubar = 0;
		if (typeof o.resizable == 'undefined') o.resizable = 0;
		if (typeof o.status == 'undefined') o.status = 0;
		if (typeof o.location == 'undefined') o.location = 0;
		if (typeof o.directory == 'undefined') o.directory = 0;
		
		if (o.position == "center") {
			o.left = (o.screen.width - o.width) / 2;
			o.top = (o.screen.height - o.height) / 2;
		} else if (o.position == "full") {
			o.width = o.screen.width;
			o.height = o.screen.height;
			o.left = 0;
			o.top = 0;
		} else if (o.position == "topleft") {
			o.left = 0;
			o.top = 0;
		} else if (o.position == "topright") {
			o.left = o.screen.width - o.width;
			o.top = 0;
		} else if (o.position == "botleft") {
			o.left = 0;
			o.top = o.screen.height - o.height;
		} else if (o.position == "botright") {
			o.left = o.screen.width - o.width;
			o.top = o.screen.height - o.height;
		}
		
		// Convert false and true to int
		o.toolbar = o.toolbar === 1 || o.toolbar === true ? 1 : 0;
		o.scrollbars = o.scrollbars === 1 || o.scrollbars === true ? 1 : 0;
		o.menubar = o.menubar === 1 || o.menubar === true ? 1 : 0;
		o.resizable = o.resizable === 1 || o.resizable === true ? 1 : 0;
		o.resizable = o.resizable === 1 || o.status === true ? 1 : 0;
		o.location = o.location === 1 || o.location === true ? 1 : 0;
		o.directory = o.directory === 1 || o.directory === true ? 1 : 0;
		
		window.open(url,o.name,"width="+o.width+",height="+o.height+",toolbar="+o.toolbar+",scrollbars="+o.scrollbars+",menubar="+o.menubar+",resizable="+o.resizable+",status="+o.status+",location="+o.location+",directory="+o.directory+",left="+o.left+",top="+o.top+"'");	
		
		return false;
	}
	
	return {}
	
})();
