/* 
 * system.tools.js 
 * version 5.5.4
 * Copyright (C) Jason Mingl (Ratheous)
 *
 *	05/03/2007 : 5.5.4 - iframe_shim - changes to size / positioning
 *	05/01/2007 : 5.5.3 - Added Class.override
 *	04/30/2007 : 5.5.2 - Added Class foundation
 *	04/20/2007 : 5.5.1 - Provisional fix for get_elem_offset and relative containers
 *	04/15/2007 : 5.5.0 - Avoid reassignment of '$' - prototype.js support
 *	04/08/2007 : 5.4.9 - Added Object.extend
 *	04/02/2007 : 5.4.8 - Added system.tools.get_var_style / $S
 *	03/15/2007 : 5.4.7 - showConsole
 *	02/27/2007 : ?.?.? - Many initial releases
*/

Object.extend = function(destination, source) 
{
  for (var property in source) 
  { destination[property] = source[property]; }
  return destination;
};

var Class =  { create: function() { return function() { this.initialize.apply(this, arguments); } } };

Class.prepare = function(definition)
{
	var obj = Class.create();
	Object.extend(obj.prototype, definition);
	return obj;
};

Class.extend = function(parent, definition) 
{
	var child = Class.create();
	Object.extend(child.prototype, parent.prototype);
	Object.extend(child.prototype, definition);
	return child;
};

Class.supplement = function(child, parent, definition) 
{
	Object.extend(child.prototype, parent.prototype);
	Object.extend(child.prototype, definition);
};

Class.override = function(dest, overrides) 
{
	if(!dest.prototype.parent) dest.prototype.parent = {};
	
	for (var p in overrides)
	{
		if(dest.prototype[p])
			dest.prototype.parent[p] = dest.prototype[p];
		dest.prototype[p] = overrides[p];
	}
	return dest;
};

Class.subclass = function(parent, overrides) 
{
	var child = Class.create();
	for (var p in parent.prototype)
		child.prototype[p] = parent.prototype[p];
	child.prototype.parent = {};
	for (var p in overrides)
	{
		if(child.prototype[p])
			child.prototype.parent[p] = child.prototype[p];
		child.prototype[p] = overrides[p];
	}
	return child;
};

if(typeof system != "object")
	var system = new Object;
if(typeof system.tools != "object")
	system.tools = new Object;

String.prototype.trim = function()
{
    return this.replace(new RegExp("^\\s+|\\s+$", "gi"), "");
};

// Is the current browser a total piece of $#!7?
system.tools.is_ie = function()
{
	return (navigator.appName == "Microsoft Internet Explorer");
};
// -------------------------------------------------------

system.tools.get_var_object = function(obj, reqd)
{
	var name;
	if(typeof obj != "object")
	{
		name = obj;
		obj = document.getElementById(obj);
	}
	if(!obj)
	{
		if(reqd)
			throw new Error("get_var_object() - can't find: " + name);
		return undefined;
	}
	return obj;
}; if(!$) var $ = system.tools.get_var_object;

system.tools.get_var_style = function(v) { return($(v).style); }; var $S = system.tools.get_var_style;

// -------------------------------------------------------

system.tools.get_elem_offset = function(obj)
{
	var offset = new Object;
	offset.left = 0;
	offset.top = 0;
	obj = $(obj);
	if (obj.offsetParent)
	{
		var rel;
		while (obj.offsetParent)
		{
			// provisional solution
			rel = obj.style.position == 'relative';
			offset.left += rel ? 0 : obj.offsetLeft;
			offset.top += rel ? 0 : obj.offsetTop;
			obj = obj.offsetParent;
		}
	} 
	else
	{
		if(obj.x) offset.left += obj.x;
		if(obj.y) offset.top += obj.y;
	}
	
	return offset;
}

system.tools.set_elem_position = function(elem, x, y)
{
	elem.style.left = x + 'px';
	elem.style.top = y + 'px';
}

// ----------------------------------------------------------------------------------

system.tools.in_array = function(value, arr)
{
	for(i = 0; i < arr.length; i++)
	{
		debug("checking " + value + " against " + arr[i]);
		if(arr[i] === value);
			return true;
	}
	return false;
}

// ----------------------------------------------------------------------------------

// Use the UID to track and clear old tags
system.tools.load_dynamic_script = function(file)
{
	var head = document.getElementsByTagName('head').item(0);
	//var scriptTag = document.getElementById('loadScript');
	//if(scriptTag) head.removeChild(scriptTag);
	script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script);
};

// ----------------------------------------------------------------------------------

system.tools.switch_image = function(obj, new_src)
{
	$img_obj = system.tools.get_var_object(obj);
	$img_obj.src = new_src;
};

// ----------------------------------------------------------------------------------

system.tools.iframe_shim = function(frame, iframe_id)
{
	try 
	{
		if(!system.tools.is_ie()) return;
		frame = $(frame);
		var iframe = $(iframe_id);
		if (!iframe)
		{
			iframe = document.createElement("iFrame");
			iframe.setAttribute("id", iframe_id);
			iframe.setAttribute("src", "javascript:false;");
			iframe.setAttribute("scrolling", "no");
			iframe.setAttribute("frameborder", "0");
			iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			iframe.style.position = "absolute";
			document.body.appendChild(iframe);
		}
		if(frame.style.zIndex < 1)
			frame.style.zIndex = 1;
		iframe.style.width = frame.offsetWidth ? frame.offsetWidth + "px" : frame.style.width;
		iframe.style.height = frame.offsetHeight ? frame.offsetHeight + "px" : frame.style.height;
		iframe.style.top = frame.style.top != '' ? frame.style.top : '0';
		iframe.style.left = frame.style.left != '' ? frame.style.left : '0';
		iframe.style.zIndex = frame.style.zIndex - 1;
		iframe.style.display = "";
		return iframe;
	} 
	catch(e) {}
};

// ----------------------------------------------------------------------------------

function showConsole(opts) 
{
	var width = opts.width ? opts.width : 350;
	var height = opts.height ? opts.height : 100;
	var _top = opts.top != null ? opts.top : screen.height/2-height/2;
	var _left = opts.left != null ? opts.left : screen.width/2-width/2;
	var content = opts.content;
	var url = opts.url != null ? opts.url : '';
	var title = opts.title ? opts.title : 'Console';
	var windowname = opts.windowname ? opts.windowname : 'console';
	var sb = opts.sb ? 1:0;
	var rs = opts.rs ? 1:0;
	top.consoleRef=window.open(url, windowname, 'width='+ width +',height='+ height +(_top ? 'top='+_top+',' : '')+(_left ? 'left='+_left+',' : '')+ ',menubar=0,toolbar=0,status=0,scrollbars='+sb+',resizable='+rs);
	//docRef = top.winRef.document.open("text/html","replace");
	if(top.consoleRef && content)
	{
		top.consoleRef.document.writeln('<html><head><title>Permissions Failure</title></head><body bgcolor=white onLoad="self.focus()">'+ content +'</body></html>');
		top.consoleRef.document.close();
	}
}

if(typeof system.debug != "object")
	system.debug = new Object;
	
system.debug.trace = function() { /* blank stub */ };
system.debug.status = function() { /* blank stub */ };
	