/*****
** The base path for the control panel
*****/
//var baseAdminPath = '/control/';
/**
* Onload event written by Simon Willison (http://simon.incutio.com)
* Sets a function to be executed when the page loads
* @param    string    func    The function name to execute
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//From Sitepoint.com (http://www.sitepoint.com/article/standards-compliant-world)
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}
addLoadEvent(externalLinks);

//Image rollover
function nc_preload() {
	var d = document;
	if (!d.NC_img) d.NC_img = new Array();
	var x = d.NC_img.length;
	var a = nc_preload.arguments;
	for(var i = 0; i < a.length; i ++) {
		d.NC_img[x] = new Image;
		d.NC_img[x++].src = a[i];
	}
}
function nc_imgSwap(imgName, image) {
	var d = document;

	d.NC_ROimg = new Array();
	if (d[imgName]) {
		d.NC_ROimg[0] = d[imgName].src;
		d[imgName].src = image;
	}
}
function nc_imgRestore(imgName) {
	var d = document;
	if (d.NC_ROimg) {
		d[imgName].src = d.NC_ROimg[0];
	}
}

//Form Element Creation
function createHidden(name, value) {
	return createInput('hidden', name, value);
}

function createInput(type, name, value) {
	var input = document.createElement('input');
	if (type) {
		input.setAttribute('type',type);
	}
	if (name) {
		input.setAttribute('name',name);
	}
	if (value) {
		input.setAttribute('value',value);
	}
	return input;
}

/************
* Help class for showing help popups
* 02/05/2007
***********/
var ncHelp = {
	init: function(helpPath) {
		//		new NC.Ajax(helpPath + 'help.xml', {onReady: this.setHelp.bind(this)}).request();
		new NC.Ajax(baseAdminPath + helpPath + 'gethelp', {onReady: this.setHelp.bind(this)}).request();
		this.setupLinks();

	},
    setupLinks: function() {
        this.anchors = [];
		NC.$AC(NC.$T('a')).each(function(link){
			if(link.rel && link.href && link.rel.test('^help', 'i')) {
				link.onclick = this.click.pass(link, this);
				this.anchors.push(link);
			}
		}, this);
    },
	setHelp: function(ajax) {
		if (ajax.responseXML) {
			this.help = ajax.responseXML.documentElement;
		}
	},
	click: function(link) {
		if (this.help) {
			this.linkPos = NC.$(link).getPosition();
			var rel = link.rel.split('[');
			if (rel.length > 1 && NC.type(rel[1]) == 'string') {
				this.topic = rel[1].substring(0, rel[1].length - 1);
				//See if another help topic is open
				var existing = NC.$('ncHelp');
				if (existing) {
					//If the current open help topic is the current topic close it and do nothing else
					if (this.topic == this.topicOpen) {
						this.close();
						this.topicOpen = null;
					}
					else {
						//Close the existing help topic and open the new one
						this.close()
						this.getHelpText();
					}
				}
				else {
					if (this.topic.length > 0) this.getHelpText();
				}
			}
		}
		return false;
	},
	getHelpText: function() {
		this.topicOpen = this.topic;
		var el = this.help.getElementsByTagName(this.topic);
		if (el && el.length > 0) {
			var helpTopic = el[0];
			this.title = NC.$T('title', helpTopic)[0].firstChild.data;
			this.content = NC.$T('content', helpTopic)[0].firstChild.data;
			this.show();
		}
	},
	show: function() {
		var helpTop = this.linkPos.bottom;

		var div = new NC.Element('div').setProperty('id', 'ncHelp').setStyles({position: 'absolute', top: this.linkPos.bottom + 'px', left: this.linkPos.right + 'px'}).makeDragable();
		new NC.Element('div').setProperty('id','ncHelpTitle').setHtml(this.title).addto(div);
		new NC.Element('div').setProperty('id','ncHelpContent').setHtml(this.content).addto(div);
		//		new NC.Element('div').setProperty('id','ncHelpContent').addto(div).appendChild(this.content);
		new NC.Element('a').setProperty('href','#').addClass('ncHelpClose').setHtml('Close').addto(div).onclick = this.close;
		div.addto(document.body);
		var divPos = div.getPosition();
		var windowHeight = NC.Window.getHeight();
		//Make sure that the height isn't more than the window size or 400px, whichever is smaller
		if (divPos.height > windowHeight) divPos.height = windowHeight;
		if (divPos.height > 400) divPos.height = 400;
		if (divPos.height + this.linkPos.bottom > NC.Window.getHeight()) {
			var topPos = this.linkPos.bottom - divPos.height;
			if (topPos < 0) topPos = 0;
			div.setStyles({top: topPos + 'px'});
		}

	},
	close: function() {
		document.body.removeChild(NC.$('ncHelp'));
	},
	help: null,
	title: null,
	content: null,
	topicOpen: null,
	topic: null
};
