/*
THIS LICENSE HEADER MUST REMAIN INTACT FOR LEGAL USE!
Pagefusion; Pagefusion Core Platform
Description: A powerful, easy-to-use, and highly customizable content management system.
Copyright (C), 2010 Pagefusion.com, All Rights Reserved.
Author: Chris Davis, chris@pagefusion.com
Support portal: www.pagefusion.com

Filename: /components/dialog/javascript/dialog.js
Description: Dialog constructor.
*/

loadFile("/libraries/core/javascript/pfDrag.js", "js");
loadFile("/libraries/core/javascript/pfResize.js", "js");
loadFile("/components/dialog/javascript/dialog_utils.js", "js");
pfDialog = function(skin_dir, isModal) {
	if (arguments.length == 0) return;
	this.isModal = isModal;

	this.container = document.createElement('div');
	this.container.className = "dialog";
	this.container.dialogBox = this;
	this.container.id = "dialog";
	this.container.style.position = "absolute";
	
	this.table_skin = document.createElement('table');
	this.table_skin.setAttribute('cellSpacing', '0');
	this.table_skin.setAttribute('cellPadding', '0');
	this.table_skin.setAttribute('border', '0');
	this.table_skin.className = skin_dir+" skin";

	// skin table
	var skin_tbody = document.createElement('tbody');
		var skin_tr1 = document.createElement('tr'); // top row
			
			var skin_tl = document.createElement('td');
			skin_tl.className = skin_dir+" tl";
			
			var tl = document.createElement('img');
			tl.src = "/skins/"+skin_dir+"/images/tl.png";
			tl_width = tl.width;
			var tl_spacer = document.createElement('img');
			tl_spacer.setAttribute('border', '0');
			tl_spacer.src = "/skins/default/images/spacer.gif";
			tl_spacer.setAttribute('width', tl_width);
			
			var skin_tc = document.createElement('td');
			skin_tc.className = skin_dir+" tc";
			
			// title table
			this.table_title = document.createElement('table');
			this.table_title.setAttribute('cellSpacing', '0');
			this.table_title.setAttribute('cellPadding', '0');
			this.table_title.setAttribute('border', '0');
			this.table_title.className = skin_dir+" title";
			this.table_title.id = "dragHandle";
			this.table_title.setAttribute('id', 'dragHandle');
			
				var title_tbody = document.createElement('tbody');
					var title_tr = document.createElement('tr');
				
						var title_l = document.createElement('td');
						title_l.className = skin_dir+" menu";
						
							// icon
							pfDialog.initDialogIcon();
							this.dialogIcon = document.createElement('img');
							this.dialogIcon.setAttribute('border','0');
						
						this.title_c = document.createElement('td');
						this.title_c.className = skin_dir+" title"; // title text goes here
						
						var title_r = document.createElement('td');
						title_r.className = skin_dir+" actions";
						
							// close icon link
							var closeLink = document.createElement('A');
							closeLink.setAttribute('href','#');
							closeLink.className = "closeLink";
							closeLink.onclick = pfDialog.closeBox;
							
							// close icon
							pfDialog.initCloseIcon();
							this.closeIcon = document.createElement('img');
							this.closeIcon.setAttribute('border','0');
							this.closeIcon.dialogBox = this;
							this.closeIcon.className = "closeIcon";
							
					
			var skin_tr = document.createElement('td');
			skin_tr.className = skin_dir+" tr";
			
				var tr = document.createElement('img');
				tr.src = "/skins/"+skin_dir+"/images/tr.png";
				tr_width = tr.width;
				
				var tr_spacer = document.createElement('img');
				tr_spacer.setAttribute('border', '0');
				tr_spacer.src = "/skins/default/images/spacer.gif";
				tr_spacer.setAttribute('width', tr_width);
			
		var skin_tr2 = document.createElement('tr'); // middle row
			var skin_ml = document.createElement('td');
			skin_ml.className = skin_dir+" ml";

			this.skin_mc = document.createElement('td');
			this.skin_mc.className = skin_dir+" mc";
			this.skin_mc.id = "dialog_mc";
			this.skin_mc.setAttribute("id", "dialog_mc");
			this.skin_mc.setAttribute("valign", "top");
			
			this.skin_mr = document.createElement('td');
			this.skin_mr.className = skin_dir+" mr";
			this.skin_mr.setAttribute("id", "dialog_mr");

				var resize_e = document.createElement('img'); // resize-e
				//resize_e.src = "/skins/"+skin_dir+"/images/resize-e.png";
				resize_e.src = "/skins/default/images/spacer.gif";
				resize_e.className = skin_dir+" resize-e";
			
		var skin_tr3 = document.createElement('tr'); // bottom row
			var skin_bl = document.createElement('td');
			skin_bl.className = skin_dir+" bl";

			this.skin_bc = document.createElement('td');
			this.skin_bc.className = skin_dir+" bc";
			this.skin_bc.setAttribute("id", "dialog_bc");
			
				var resize_s = document.createElement('img'); // resize-s
				resize_s.src = "/skins/default/images/spacer.gif";
				//resize_s.src = "/skins/"+skin_dir+"/images/resize-s.png";
				resize_s.className = skin_dir+" resize-s";
			
			this.skin_br = document.createElement('td');
			this.skin_br.className = skin_dir+" br";
			this.skin_br.id = "dialog_br";
			this.skin_br.setAttribute("id", "dialog_br");
			
				var resize_se = document.createElement('img'); // resize-se
				//resize_se.src = "/skins/"+skin_dir+"/images/resize-se.png";
				resize_se.src = "/skins/default/images/spacer.gif";
				resize_se.className = skin_dir+" resize-se";
			
		skin_tl.appendChild(tl_spacer);
		skin_tr1.appendChild(skin_tl);
						title_l.appendChild(this.dialogIcon);
					title_tr.appendChild(title_l);
					title_tr.appendChild(this.title_c);
							closeLink.appendChild(this.closeIcon);
						title_r.appendChild(closeLink);
					title_tr.appendChild(title_r);
				title_tbody.appendChild(title_tr);
			this.table_title.appendChild(title_tbody);
			skin_tc.appendChild(this.table_title);
		skin_tr1.appendChild(skin_tc);
		skin_tr.appendChild(tr_spacer);
		skin_tr1.appendChild(skin_tr);
		skin_tr2.appendChild(skin_ml);
		skin_tr2.appendChild(this.skin_mc);
		skin_tr2.appendChild(this.skin_mr);
			this.skin_mr.appendChild(resize_e);
		skin_tr3.appendChild(skin_bl);
		skin_tr3.appendChild(this.skin_bc);
			this.skin_bc.appendChild(resize_s);
		skin_tr3.appendChild(this.skin_br);
			this.skin_br.appendChild(resize_se);
		skin_tbody.appendChild(skin_tr1);
		skin_tbody.appendChild(skin_tr2);
		skin_tbody.appendChild(skin_tr3);
	this.table_skin.appendChild(skin_tbody);
	this.container.appendChild(this.table_skin);
}; // function

// public methods
pfDialog.imagePath = "/skins/default/images/"; // preset; directory path to 'close.png' in titleBar

// show the dialog
pfDialog.prototype.show = function(posX, posY) {
	this.container.style.display = "block";
	bodyZindex.toTop(this.container);
	if (this.isModal) veil.show(true);
}; // function

// hide the dialog
pfDialog.prototype.hide = function() {
	if (this.container.style.display != "") {
		this.container.style.display = "none";
		if (this.isModal) veil.show(false);
		if (this.dialogIframe.src) {
			this.dialogIframe.src = "/libraries/core/templates/blank.html";
		} // if
	} else {
		window.close();
	} // if
}; // function

// set the dialog's position to center
pfDialog.prototype.setCenter = function(x, y) {
	
	var winWidth = getWindowWidth();
	var winHeight = getWindowHeight();
	
	var dialogWidth = this.container.offsetWidth;
	var dialogHeight = this.container.offsetHeight;
	
	var winScrollLeft = getScrollLeft();
	var winScrollTop = getScrollTop();
	
	if (x == -1) {
		x = Math.round((winWidth-dialogWidth) / 2) + winScrollLeft;
	} // if
	if (y == -1) {
		y = Math.round((winHeight-dialogHeight) / 2) + winScrollTop;
	} // if
	
	this.container.style.left = x + "px";
	this.container.style.top = y + "px";
}; // function

pfDialog.prototype.setModal = function(op) {
	this.isModal = op;
}; // function

pfDialog.prototype.setIcon = function(icon) {
	this.dialogIcon.src = icon;
}; // function

pfDialog.prototype.setTitle = function(title) {
	this.title_c.innerHTML = title;
}; // function

pfDialog.prototype.setCloseIcon = function(closeIcon) {
	this.closeIcon.src = closeIcon;
}; // function

pfDialog.prototype.setUrl = function(obj, obj_id, url, width, height, scrolling) {
	if (!this.dialogIframe) {
		this.dialogIframe = document.createElement("IFRAME");
		this.dialogIframe.setAttribute('id', obj_id);
		this.dialogIframe.setAttribute('frameBorder', 'no');
		this.dialogIframe.setAttribute('scrolling', scrolling);
		this.dialogIframe.style.display = "block";
		obj.skin_mc.innerHTML = "";
		obj.skin_mc.appendChild(this.dialogIframe);
	} // if

	this.dialogIframe.style.width = "100%";
	this.dialogIframe.setAttribute('width', "100%");

	this.dialogIframe.style.height = "100%";
	this.dialogIframe.setAttribute('height', "100%");
	
	if (width) {
		this.skin_mc.style.width = width;
		this.skin_mc.setAttribute('width', width);
	} else {
		this.skin_mc.style.width = "100%";
		this.skin_mc.setAttribute('width', "100%");
	} // if
	
	if (height) {
		this.skin_mc.style.height = height;
		this.skin_mc.setAttribute('height', height);
	} else {
		this.skin_mc.style.height = "300";
		this.skin_mc.setAttribute('height', "300");
	} // if
	
	this.dialogIframe.src = url;
}; // function

pfDialog.prototype.getUrl = function() {
	if (this.dialogIframe) {
		dialogTraceObj.show(this.dialogIframe);
		var url = this.dialogIframe.src;
		if (this.dialogIframe.contentWindow) {
			try {url = this.dialogIframe.contentWindow.location.href;}
			catch(e) {}
		} // if
		return url;
	} // if
}; // function

pfDialog.prototype.setContent = function(htmlContent) {
	if (!this.dialogIframe) {
		this.skin_mc.innerHTML = htmlContent;
	} else {
		this.dialogIframe.style.display = "none";
		this.skin_mc.innerHTML = htmlContent;
	} // if
}; // function

pfDialog.prototype.setWidth = function(width) {
	this.skin_mc.style.width = width + "px";
}; // function

pfDialog.prototype.setHeight = function(height) {
	this.skin_mc.style.height = height + "px";
}; // function

pfDialog.prototype.getContentNode = function() {
	// expose 'skin_mc' DOM node for direct manipulation
	return this.skin_mc;
}; // function

// private methods
pfDialog.className = "dialog"; // CSS className
pfDialog.closeIcon = null;
pfDialog.maxDepth = 5; // optimize search of parent nodes

pfDialog.initCloseIcon = function() {
	// pre-fetch this icon so it doesn't distort dialog box size
	if (pfDialog.closeIcon == null) {
		pfDialog.closeIcon = new Image();
		pfDialog.closeIcon.src = "/skins/default/images/close_banner.png";
	} // if
}; // function

pfDialog.initDialogIcon = function() {
	// pre-fetch this icon so it doesn't distort dialog box size
	if (pfDialog.dialogIcon == null) {
		pfDialog.dialogIcon = new Image();
		pfDialog.dialogIcon.src = "/skins/default/images/info.png";
	} // if
}; // function

pfDialog.closeBox = function(e) {
	if (!e) e = window.event;
	var node = e.target ? e.target : e.srcElement;
	var count = 0;
	while ((node != null) && (count < pfDialog.maxDepth)) {
		if (node.dialogBox) {
			node.dialogBox.hide();
			return false;
		} // if
		node = node.parentNode;
		count++;
	} // while
	return false;
}; // function

loadFile("/components/dialog/javascript/dialog_init.js", "js");