/**************************************************************************************************************************
 * 
 * slyCMS
 * 
 * Written by Andy Loeber <andreas.loeber@slysolutions.ch>
 * (c) 2010-2011 slySOLUTIONS GmbH, Zurich, Switzerland. All rights reserved.
 * 
 * YOU MAY NOT REMOVE THIS COPYRIGHT NOTICE - SEE WWW.SLYSOLUTIONS.CH FOR LICENSE DETAILS
 *************************************************************************************************************************/

//TODO: slyDIALOG darf beim Verschieben nicht über den Rand gezogen werden können
//TODO: Moving Cursor beim Verschieben

function slyDialog(config) {
	if (sly.dialog) return;
	if (!config) return;
	this.id = 'slyDialog';
	this.isDragging = false;
	sly.dialog = this;
	var _this = this;
	if (config) {
		if (config.msg) this.msg = config.msg;
		if (config.title) this.title = config.title;
		if (config.width) this.width = config.width;
		if (config.image) this.image = config.image;
		if (config.buttons) this.buttons = config.buttons;
	}
	if (this.getDialogContent) this.msg = this.getDialogContent();
	this.modalDiv = document.createElement('div');
	this.modalDiv.id = 'slyModalDiv';
	this.modalDiv.style.position = 'absolute';
	this.modalDiv.style.zIndex = 5000;
	this.modalDiv.style.top = '0px';
	this.modalDiv.style.left = '0px';
	this.modalDiv.style.backgroundColor = 'white';
	this.modalDiv.style.opacity = 0.5;
	this.modalDiv.style.filter = 'alpha(opacity=50)';
	document.body.appendChild(this.modalDiv);
	this.table = document.createElement('table');
	this.table.id = this.id;
	this.table.className = 'slyDialog';
	this.table.cellSpacing = 0;
	this.table.cellPadding = 0;
	this.table.style.top = '0px';
	this.table.style.left = '0px';
	this.table.style.fontFamily = 'Verdana';
	this.table.style.fontSize = '11px';
	if (this.width) this.table.style.width = this.width + 'px';
	this.table.style.textAlign = 'left';
	this.table.style.position = 'absolute';
	this.table.style.zIndex = 9000;
	var tbody = document.createElement('tbody');
	var tr = document.createElement('tr');
	sly.makeDraggable(this.table, tr);
	tbody.appendChild(tr);
	var td = document.createElement('td');
	td.style.width = '5px';
	td.style.background = 'url(res/scripts/slycms/images/dialog/dialog_top_left.png)';
	tr.appendChild(td);
	td = document.createElement('td');
	sly.disableSelection(td);
	td.style.background = 'url(res/scripts/slycms/images/dialog/dialog_top_center.png)';
	td.align = 'left';
	td.style.fontFamily = 'Verdana';
	td.style.fontSize = '12px';
	td.style.fontWeight = 'bold';
	td.style.cursor = 'default';
	td.innerHTML = '<table style="width:100%"><tr><td align="left" style="color:white">' + this.title + '</td><td align="right"><img onclick="sly.dialog.close()" src="res/scripts/slycms/images/dialog/close_button.png" onmouseover="this.src = \'res/scripts/slycms/images/dialog/close_button_hover.png\'" onmouseout="this.src = \'res/scripts/slycms/images/dialog/close_button.png\'" style="position:relative;top:1px;left:3px;cursor:pointer" /></td></tr></table>';
	tr.appendChild(td);
	td = document.createElement('td');
	td.style.width = '5px';
	td.style.background = 'url(res/scripts/slycms/images/dialog/dialog_top_right.png)';
	tr.appendChild(td);
	tbody.appendChild(tr);
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.style.background = '#eeeeee url(res/scripts/slycms/images/dialog/dialog_middle_left.png) repeat-y';
	tr.appendChild(td);
	td = document.createElement('td');
	td.style.verticalAlign = 'top';
	td.style.textAlign = 'justify';
	td.style.padding = '6px';
	td.style.background = '#eeeeee';
	this.contentNode = document.createElement('div');
	td.appendChild(this.contentNode);
	var html = '<p style="text-align:right; margin-bottom:0px;">';
	if (this.buttons) {
		for(var j = 0; j < this.buttons.length; j++) {
			html += '<input type="button" value="' + this.buttons[j].label + '" onclick="sly.dialog.buttons[' + j + '].event()" />';
		}
	} else {
		//html += '<input type="button" value="OK" onclick="sly.dialog.close()" />';
	}
	html += '</p>';
	var buttonDiv = document.createElement('div');
	buttonDiv.innerHTML = html;
	td.appendChild(buttonDiv);
	tr.appendChild(td);
	td = document.createElement('td');
	td.style.background = '#eeeeee url(res/scripts/slycms/images/dialog/dialog_middle_right.png) repeat-y right';
	tr.appendChild(td);
	tbody.appendChild(tr);
	tr = document.createElement('tr');
	tr.style.height = '3px';
	td = document.createElement('td');
	td.style.background = 'url(res/scripts/slycms/images/dialog/dialog_bottom_left.png)';
	tr.appendChild(td);
	td = document.createElement('td');
	td.style.background = 'url(res/scripts/slycms/images/dialog/dialog_bottom_center.png)';
	tr.appendChild(td);
	td = document.createElement('td');
	td.style.background = 'url(res/scripts/slycms/images/dialog/dialog_bottom_right.png)';
	tr.appendChild(td);
	tbody.appendChild(tr);
	this.table.appendChild(tbody);
	document.body.appendChild(this.table);
	if (this.setDialogContent) this.setDialogContent(this.contentNode);
	else this.contentNode.appendChild(document.createTextNode(this.msg));
	if (this.getDialogJavaScript) this.getDialogJavaScript();
	this.arrangeElements();
	sly.fadeInNode(_this.table, 300);
};

slyDialog.prototype.arrangeElements = function() {
	if (this.modalDiv) {
		this.modalDiv.style.width = sly.page.getSize().width + 'px';
		this.modalDiv.style.height = sly.page.getSize().height + 'px';
		var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
		if (this.table.offsetWidth <= sly.screen.width) {
			var x = 0;
			if (window.pageXOffset) x = window.pageXOffset;
			else x = iebody.scrollLeft;
			this.table.style.left = x + (sly.screen.width - this.table.offsetWidth) / 2 + 'px';
		} else {
			this.table.style.left = '20px';
		}
		if (this.table.offsetHeight <= sly.screen.height) {
			var y = 0;
			if (window.pageYOffset) y = window.pageYOffset;
			else y = iebody.scrollTop;
			this.table.style.top = y + (sly.screen.height - this.table.offsetHeight) / 2 + 'px';
		} else {
			this.table.style.top = '20px';
		}
    }
};

slyDialog.prototype.close = function() {
	if (!this.modalDiv) return;
	if (tinyMCE.activeEditor) tinyMCE.execCommand('mceRemoveControl',false,'slyNewsForm_text');
	document.body.removeChild(this.modalDiv);
	this.modalDiv = false;
	if (document.getElementById('tooltip')) {
		document.body.removeChild(document.getElementById('tooltip'));
		clearTimeout(tooltipTimeout);
	}
	var _this = this;
	sly.fadeOutNode(this.table, 200, function() {
		document.body.removeChild(_this.table);
		_this.table = false;
		sly.dialog = false;
	});
};
