/**
 * Alliance Software Content Management System
 *
 * @copyright Copyright &copy; 2004, Alliance Software
 * No unauthorised use without the express written permission
 * of Alliance Software
 *
 * @package core
 *
 */

/**
 * Add a function to the window.onload event
 */
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 * Add a function to the window.onresize event
 */
function addResizeEvent(func){
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize = func;
	} else {
		window.onresize = function() {
			oldonresize();
			func();
		}
	}
}

function addEvent(element, type, func) {
	if (element.addEventListener)
		element.addEventListener(type, func, false);
	else
		 element.attachEvent('on' + type, func);
}

/** Display a popup window showing a specific URL */
function showWindow(url,name,width,height){
	var options = "width="+width+",height="+height+",status=yes,toolbar=no,"
		+"directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes,"
		+"left=100,top=100";
	var hwnd = open(url, name, options);
}

/** Post form to a popup window */
function submitFormToWindow(formName,name,width,height){
	var options = "width="+width+",height="+height+",status=yes,toolbar=no,"
		+"directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes,"
		+"left=100,top=100";
	var hwnd = open("", name, options);
	var a = window.setTimeout("document."+formName+".submit();", 500);
	// Set focus to window
	setTimeout('setFocus(\''+name+'\')', 500);
}

/** Set focus to a named window */
function setFocus(name){
	hwnd = open('', name);
	hwnd.focus();
}

/** Return an object */
function getObject(objectId)
{
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId);
	} else if (document.all && document.all[objectId]) {
		// MSIE 4 DOM
		return document.all[objectId];
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}

if (typeof $ == 'undefined') {
	/* No jQuery, so let's make our own really simple $ function */
	$ = function() //use this
	{
		var elements = new Array();

		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string') {
				if (document.getElementById) {
					element = document.getElementById(element.substring(1));
				} else if (document.all) {
					element = document.all[element];
				}
			}


			elements.push(element);
		}

		return elements;
	}
}

/** Return the style of an object */
function getStyleObject(objectId) {
	var object = getObject(objectId);
	if (false != object) {
		return object.style;
	}
	return false;
}

/**
 * Switch a layer between hidden and type
 * NOTE: type = '' returns it to default type
 */
function toggleLayer(wLayer, type){
	if ('undefined' == typeof(type)) { type = ''; }
	var s = getStyleObject(wLayer);
	if (false != s) {
		s.display=(s.display=='none')?type:'none';
	}
}
/** specifically show a layer */
function showLayer(wLayer, type){
	if ('undefined' == typeof(type)) { type = ''; }
	var s = getStyleObject(wLayer);
	if (false != s) {
		s.display=type;
	}
}
/** specifically hide a layer */
function hideLayer(wLayer){
	var s = getStyleObject(wLayer);
	if (false != s) {
		s.display='none';
	}
}


//---------------------------------------------------------
// Functions for embeding active content 
// (from TinyMCE 'media' plugin)
// http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media 
// File: embed.js

/**
 * Write embed code for active content to document using a 
 * document.write()
 * @param string $cls class id for object
 * @param string $cb codebase url used to prompt user to download
 * plugin from if it can't be found locally 
 * is not found then user can be promted to download from this url
 * @param string $mt content type of data
 * @param array(string) $p extra parameters 
 */
function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	document.write(h);
}

/**
 * Write content to be handled by Flash
 * @param array(string) $p extra parameters 
 */
function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

/**
 * Write content to be handled by Shock Wave
 * @param array(string) $p extra parameters 
 */
function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

/**
 * Write content to be handled by Quick Time
 * @param array(string) $p extra parameters 
 */
function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

/**
 * Write content to be handled by Real Media
 * @param array(string) $p extra parameters 
 */
function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

/**
 * Write content to be handled by Windows Media 
 * @param array(string) $p extra parameters 
 */
function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

//---------------------------------------------------------

/** Money format prototype for numbers */
Number.prototype.formatMoney = function(c, currencyCode)
{
	if (typeof(currencyCode) == 'undefined') {
		currencyCode = 'AUD';
	}

	// TODO: For now just hardcode currency to symbol relationship here.
	// There's more to consider however basead on localization (eg. grouping
	// of numbers, decimal separator, position of symbol etc)
	switch (currencyCode)
	{
		case 'GBP':
			var symbol = '&pound;';
			break;
		case 'EUR':
			var symbol = '&euro;';
			break;
		case 'AUD':
		case 'USD':
		default:
			var symbol = '$';
			break;
	}

	var n = this,
		c = isNaN(c = Math.abs(c)) ? 2 : c,				// decimal precision
		i = parseInt(n = (+n || 0).toFixed(c)) + '',	// integer part
		j = 3 < i.length ? i.length % 3 : 0,			// prep 1000's separator
		d = (n - i).toFixed(c).slice(1);				// decimal part
	return symbol + (j ? i.substr(0, j) + ',' : '')
		+ i.substr(j).replace(/(\d{3})(?=\d)/g, '$1,')	// add 1000's separator
		+ d;
}





//