NG.Shade = function(node, height, disablescroll) {
	if (node == null) node = document.body;
	if (node.shade == null) {
		if (height == null) height = node.offsetHeight;
		node.shade = document.createElement('div');
		NG.addClass(node.shade,'shade');
		node.shade.style.height = height + 'px';
		node.shade.style.left = '0';
		node.shade.style.position = 'absolute';
		node.shade.style.top = '0';
		node.shade.style.width = '100%';
	}
	this.disablescroll = (disablescroll == null || disablescroll);
	this.node = node;
	this.setOpacity(75);
	this.show();
}
NG.Shade.prototype.show = function() {
	this.oldOverflow = this.node.style.overflow;
	if (this.disablescroll) {
		this.node.style.overflow = 'hidden';
		scroll(0,0);
	}
	this.node.appendChild(this.node.shade);
}
NG.Shade.prototype.close = function() {
	this.node.style.overflow = this.oldOverflow;
	if (this.node.shade != null) {
		this.node.removeChild(this.node.shade);
	}
}
NG.Shade.prototype.setOpacity = function(opacity) {
	this.opacity = opacity;
	this.node.shade.style.filter = 'alpha(opacity=' + (this.opacity) + ')';
	this.node.shade.style.mozOpacity = this.opacity / 100;
	this.node.shade.style.khtmlOpacity = this.opacity / 100;
	this.node.shade.style.opacity = this.opacity / 100;
}

NG.Dialog = function(xhtml) {
	var tmp = this;
	this.shade = new NG.Shade();
	this.node = document.createElement('div');
	NG.addClass(this.node, 'dialog');
	this.node.appendChild(document.createElement('div'));
	NG.addClass(this.node.lastChild, 'inner');
	if (isStr(xhtml)) this.node.lastChild.innerHTML = xhtml;
	else this.node.lastChild.appendChild(xhtml);
	this.node.appendChild(document.createElement('div'));
	NG.addClass(this.node.lastChild, 'close');
	this.node.lastChild.innerHTML = 'X';
	NG.addEventListener(this.node.lastChild, 'click', function() { tmp.Close(); });
	document.body.appendChild(this.node);
	setTimeout(function() {
		tmp.node.style.right = Math.max(0, Math.floor((document.body.clientWidth  - tmp.node.clientWidth)  / 2)) + 'px';
	}, 10);
}
NG.Dialog.prototype.Close = function() {
	document.body.removeChild(this.node);
	this.shade.close();
}
NG.Dialog.prototype.Show = function() {
	this.shade.show();
	document.body.appendChild(this.node);
}

NG.SendToFriends = function(xhtml, current_user) {
	var tmp = this;
	if (typeof this.dialog == 'undefined') {
		var root = document.createElement('div');
		NG.addClass(root, 'sendtofriends');
		if (typeof xhtml == 'undefined') xhtml = '';
		if (isStr(xhtml)) root.innerHTML = xhtml;
		else root.appendChild(xhtml);
	
		var url = new NGUrl(window.location);
		url.addArgument('backboneop','sendtofriend', true);
		root.appendChild(document.createElement('form'));
		root.lastChild.action = url.toString();
		root.lastChild.method = 'post';
		root.lastChild.appendChild(document.createElement('div'));
		NG.addClass(root.lastChild.lastChild, 'friends');
		root.lastChild.appendChild(document.createElement('a'));
		root.lastChild.lastChild.innerHTML = 'Add More Friends';
		root.lastChild.lastChild.href = '#';
		NG.addEventListener(root.lastChild.lastChild, 'click', function() { tmp.AddFriend(); })
		root.lastChild.appendChild(document.createElement('div'));
		root.lastChild.lastChild.innerHTML = '<input class="friend-send" type="submit" value="Send" />';
		this.root = root;
		this.dialog = new NG.Dialog(root);
		if (current_user) {
			root.firstChild.firstChild.appendChild(document.createElement('div'));
			root.firstChild.firstChild.lastChild.innerHTML = '<label class="current_user">Your Name: <input name="from" type="text" class="textbox" /></label><label class="current_user">Your Email: <input name="from_email" type="text" class="textbox" /></label>';
		}
		this.AddFriend();
	} else {
		this.dialog.Show();
	}
}
NG.SendToFriends.prototype.AddFriend = function() {
	var friends = this.root.firstChild.firstChild.getElementsByTagName('div').length;
	this.root.firstChild.firstChild.appendChild(document.createElement('div'));
	this.root.firstChild.firstChild.lastChild.appendChild(document.createElement('label'));
	this.root.firstChild.firstChild.lastChild.lastChild.innerHTML = 'Name: <input name="friend[' + friends + '][name]" type="text" class="textbox" />';
	this.root.firstChild.firstChild.lastChild.appendChild(document.createElement('label'));
	this.root.firstChild.firstChild.lastChild.lastChild.innerHTML = 'Email: <input name="friend[' + friends + '][email]" type="text" class="textbox" />';
}

if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for(var i=0; i<this.length; i++) {
			if(this[i]==obj) {
				return i;
			}
		}
		return -1;
	}
}

if (!NG.getElementsByClassName) {
	NG.getElementsByClassName = function(node, classname) {
		var results = [];
		if (NG.hasClass(node, classname)) results[results.length] = node;
		for (var i = 0; i < node.childNodes.length; i++) {
			results = results.concat(NG.getElementsByClassName(node.childNodes[i], classname));
		}
		return results;
	}
}
