/**
 * ConfirmDialog
 * @requires kDialog2 
 */
var ConfirmDialog = kDialog2.extend({
	// Static properties
	settings: function(settings) {
		if (typeof(this.s) == 'undefined') {
			this.base();

			Object.extendProperties(this.s, {
				'+innerTriggers': [
					{node: '.ok', event: 'click', action: 'onClickOK'},
					{node: '.cancel', event: 'click', action: 'onClickCancel'},
					{node: '.close', event: 'click', action: 'hide'}
				],
				'+innerZones': [{node: '.buttons', name: 'buttons'}],
				'+zones': {
					heading: 'Confirm',
					content: '		<p class="text">Please click OK or Cancel.</p>' +
					'		<form class="buttons">' +
					'			<input type="image" src="'+Config.templatePath+'i/button_ok.gif" alt = "OK" class="ok">' +
					'			<input type="image" src="'+Config.templatePath+'i/button_cancel.gif" alt = "Cancel" class="cancel">' +
					'		</form>'
				},
				onClickOK: function() { return true; },
				onClickCancel: function() { return true; }
			});
			this.s.innerZones.push({node: '.buttons', name: 'buttons'});
			Object.extendProperties(this.s, settings);
		}
	},

	constructor: function(settings) {
		this.settings(settings);
		this.create();
	},

	// Convenience functions
	setButtons: function(html) {
		this.setZone('buttons', html);
	},

	onClickOK: function(ele, e) {
		Event.stop(e);
		if (this.s.onClickOK(this)) this.hide();
	},

	onClickCancel: function(ele, e) {
		Event.stop(e);
		if (this.s.onClickCancel(this)) this.hide();
	}
});

alertDialog = function(text, header) {
	if(!header){
		header = "Alert";
	}
	var confirmDialog = new ConfirmDialog({
		'+zones': {
			heading: header,
			content: '		<p class="text">' + text + '</p>' +
			'		<form class="buttons">' +
			'			<input type="image" src="'+Config.templatePath+'i/button_ok.gif" alt = "OK" class="ok">' +
			'		</form>'
		}
	});
}

mouseoverDialog = function(text, header, ele) {
	if(ele.confirmDialog && ele.confirmDialog.timeout){
		clearTimeout(ele.confirmDialog.timeout);
		ele.confirmDialog.timeout = null;
		return;
	}
	if(!header){
		header = "Alert";
	}
	var confirmDialog = new ConfirmDialog({
		'+zones': {
			heading: header,
			content: '		<p class="text">' + text + '</p>' +
			'		<form class="buttons">' +
			'			<input type="image" src="'+Config.templatePath+'i/button_ok.gif" alt = "OK" class="ok">' +
			'		</form>'
		},
		'position': {
			exemplarAnchor: 'bottom left',
			selfAnchor: 'top left',
			exemplar: ele,
			offsetY: 0,
			offsetX: 0
		},
		'lightBox': {
			visible: false,
			opacity: 40,
			color: '#000',
			className: 'lightbox'
		}
	});
	confirmDialog.n.superNode.onmouseover = function() {
		clearTimeout(this.timeout);
		this.timeout = null;
	}.bind(confirmDialog);
	confirmDialog.n.superNode.onmouseout = function(confirmDialog) {
		this.timeout = setTimeout(function() { this.hide(); confirmDialog.timeout=null; }.bind(this), 500);
	}.bind(confirmDialog, confirmDialog);
	ele.onmouseout = function(confirmDialog) {
		this.timeout = setTimeout(function() { this.hide(); confirmDialog.timeout=null; }.bind(this), 500);
	}.bind(confirmDialog, confirmDialog);
	ele.confirmDialog = confirmDialog;
}

var confirmRemovePost = function(el, e) {
	Event.stop(e);
	var confirmDialog = new ConfirmDialog({
		onClickOK: function() {
			window.location = this.href;
			return true;
		}.bind(el),
		startHidden: true,
		position: {
			exemplarAnchor: 'bottom left',
			selfAnchor: 'top left',
			exemplar: el,
			offsetY: 0,
			offsetX: 0
		}
	});
	confirmDialog.setText('Are you sure that you would like to delete this entry and all comments?');
	confirmDialog.show();
}

var confirmRemoveComment = function(el, e) {
	Event.stop(e);
	var confirmDialog = new ConfirmDialog({
		onClickOK: function() {
			window.location = this.href;
			return true;
		}.bind(el),
		startHidden: true,
		position: {
			exemplarAnchor: 'bottom right',
			selfAnchor: 'top right',
			exemplar: el,
			offsetY: 0,
			offsetX: 0
		}
	});
	confirmDialog.setText('Are you sure that you would like to delete this comment and all children comments?');
	confirmDialog.show();
}

//instantiate and use the object
EventSelectors.register({
	'a.answered:click' : function(el, e) {
		Event.stop(e);
		var confirmDialog = new ConfirmDialog({
			onClickOK: function() {
				window.location = this.href;
				return true;
			}.bind(el),
			startHidden: true,
			position: {
				exemplarAnchor: 'bottom right',
				selfAnchor: 'top right',
				exemplar: el,
				offsetY: 0,
				offsetX: 0
			}
		});
		confirmDialog.setText('Are you sure that you would like to close this question?');
		confirmDialog.show();
	},
	'a.group_remove:click' : function(el, e) {
		Event.stop(e);
		var confirmDialog = new ConfirmDialog({
			onClickOK: function() {
				window.location = this.href;
				return true;
			}.bind(el),
			startHidden: true,
			position: {
				exemplarAnchor: 'bottom left',
				selfAnchor: 'top left',
				exemplar: el,
				offsetY: 0,
				offsetX: 0
			}
		});
		confirmDialog.setText('Are you sure that you would like to delete this group?');
		confirmDialog.show();
	},
	'a.group_leave:click' : function(el, e) {
		Event.stop(e);
		var confirmDialog = new ConfirmDialog({
			onClickOK: function() {
				window.location = this.href;
				return true;
			}.bind(el),
			startHidden: true,
			position: {
				exemplarAnchor: 'bottom left',
				selfAnchor: 'top left',
				exemplar: el,
				offsetY: 0,
				offsetX: 0
			}
		});
		confirmDialog.setText('Are you sure that you would like to leave this group?');
		confirmDialog.show();
	}/*,
	'a.post_remove:click' : function(el, e) {
		Event.stop(e);
		var confirmDialog = new ConfirmDialog({
			onClickOK: function() {
				window.location = this.href;
				return true;
			}.bind(el),
			startHidden: true,
			position: {
				exemplarAnchor: 'bottom left',
				selfAnchor: 'top left',
				exemplar: el,
				offsetY: 0,
				offsetX: 0
			}
		});
		confirmDialog.setText('Are you sure that you would like to delete this post and all comments?');
		confirmDialog.show();
	},
	'a.comment_remove:click' : function(el, e) {
		Event.stop(e);
		var confirmDialog = new ConfirmDialog({
			onClickOK: function() {
				window.location = this.href;
				return true;
			}.bind(el),
			startHidden: true,
			position: {
				exemplarAnchor: 'bottom right',
				selfAnchor: 'top right',
				exemplar: el,
				offsetY: 0,
				offsetX: 0
			}
		});
		confirmDialog.setText('Are you sure that you would like to delete this comment and all children comments?');
		confirmDialog.show();
	}*/
}, true);var makeLoginBox = function(node, e) {
	new FormDialog({
		'+zones': {
			heading: 'Loading...',
			text: 'Loading...'
		},
		position: {  
			exemplarAnchor: 'bottom right',
			selfAnchor: 'top right',
			exemplar: node,
			offsetY: 0,
			offsetX: 0
		},
		groupId: 'AuthLogin',
		groupLimit: 1,
		startHidden: true,
		moduleName: 'AuthLogin',
		parentThis: this,
		classNames: ['kDialog2', 'authLogin']
	}, node);
	if(e){
		Event.stop(e);
	}
	return false;
}
EventSelectors.register({
    'a.loginBox:click': function(node, e) {
		if(String(node.onclick).indexOf('makeLoginBox') == -1){
			makeLoginBox(node,e);
		}
	}
});