/*******************************************************
Codesigner Dialog 0.5

General dialog for Codesigner
Internet Explorer 6.0 and up
Firefox 1.5 and up


All code by Marko Niskanen, unless otherwise noted.
(c) 1997-2006 Codesign Oy and Marko Niskanen
http://www.codesign.fi/
*******************************************************/

var csDialog = Class.create();
var dialogs = new Array();
csDialog.prototype = {
	initialize : function(iframeId, doc, src, modal) {
		if (modal) {
			this.modal = 1;
		}
		this.id = iframeId;

		this.doc = doc;
		this.src = src;

		this.iframe = this.doc.getElementById(this.id);
		if (!this.iframe) {
			this.createiframe();
		} else {
			this.iframe.src= this.src;
		}

		top.dialogs[this.id] = this;
		return this;
	},
	createiframe : function() {

		var ifh = '<iframe name="' + this.id + '" id="' + this.id + '" class="dialog" scrolling="no" src="' + this.src + '" marginheight="0" marginwidth="0" frameborder="0" style="position: absolute; z-index: 750;"></iframe>';

		var divi = this.doc.createElement('div');
		divi.innerHTML = ifh;
		this.iframe = divi.firstChild;
		this.doc.body.appendChild(this.iframe);
	},
	resize : function( w, h) {
		this.height = h;
		this.width = w;
		this.iframe = this.doc.getElementById(this.id);
		if (this.iframe) {

			this.iframe.style.width = w;
			this.iframe.style.height = h;
			return true;
		}
		return false;
	},
	refresh : function() {
		this.show();
	},
	show : function() {
		if (this.modal) {
			this.doc.getElementById('hidecurtain').style.display = 'block';
		}
		this.iframe = this.doc.getElementById(this.id);
		if (this.iframe) {
			this.iframe.style.display = '';
			this.iframe.style.display = 'block';
			this.visible = true;
			this.callbackResize && this.callbackResize(this.iframe);
			return true;
		}
		return false;
	},
	hide : function() {
		if (this.modal) {
			this.doc.getElementById('hidecurtain').style.display = 'none';
		}
		this.iframe = this.doc.getElementById(this.id);
		if (this.iframe) {
			this.iframe.style.display = 'none';
			this.visible = false;
			return true;
		}
		return false;
	},
	toggleminimize : function() {
		this.iframe = this.doc.getElementById(this.id);
		if (this.minimized) {
			this.iframe.style.height = this.height;
			this.minimized = false;
		} else {
			this.iframe.style.height = '38px';
			this.minimized = true;
		}

			if (this.doc.getElementById(this.id).contentDocument){
				var doci = this.doc.getElementById(this.id).contentDocument;
				if (this.minimized) {
					doci.getElementById('dialogbody').style.display='none';
				} else {
					doci.getElementById('dialogbody').style.display='block';
				}
			} else {
				var doci = this.doc.getElementById(this.id).contentWindow.document;
				if (this.minimized) {
					doci.getElementById('dialogbody').style.display='none';
				} else {
					doci.getElementById('dialogbody').style.display='block';
				}
			}

		return true;
	},


	close : function(){
		this.hide();
		top.dialogs[this.id] = null;
		if (this.id) {
			this.iframe = this.doc.getElementById(this.id);
		}
		if (this.iframe) {
			this.iframe.id = '';
		}
		window.setTimeout(this.removeiframe.bind(this), 1000);
		return true;
	},
	removeiframe: function() {
		if (this.iframe) {
			var p = this.iframe.parentNode;
			p.removeChild(this.iframe);
			this.iframe = null;
			this.id = null;
		}
	},

	Ok : function(data){
		if (this.callbackOk) {
			this.callbackOk(data);
		}
	},
	No : function(data){
		if (this.callbackNo) {
			this.callbackNo(data);
		}
	},
	Cancel : function(data){
		if (this.callbackCancel) {
			this.callbackCancel(data);
		}
	},
	Minimize : function(data){
		this.toggleminimize();
		if (this.callbackResize) {
			this.callbackResize(this.iframe);
		}
	}
};



dialogsRefreshAll = function() {
	for (var i=0; i<top.dialogs.length; i++) {
		var dialog = top.dialogs[i];
		dialog.refresh();
	}
};

dialogHideAll = function() {
	return true;
};
