/*
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), 2009 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.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
loadFile("/components/dialog/javascript/dialog_utils.js", "js");

var Drag = {

	obj: null,

	init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
		o.onmousedown	= Drag.start;

		o.hmode = bSwapHorzRef ? false : true ;
		o.vmode = bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX = typeof minX != 'undefined' ? minX : null;
		o.minY = typeof minY != 'undefined' ? minY : null;
		o.maxX = typeof maxX != 'undefined' ? maxX : null;
		o.maxY = typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	}, // function
	
	init_s: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
		o.onmousedown	= Drag.start_s;

		o.hmode = bSwapHorzRef ? false : true ;
		o.vmode = bSwapVertRef ? false : true ;
		
		o.root = oRoot && oRoot != null ? oRoot : o ;
		
		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX = typeof minX != 'undefined' ? minX : null;
		o.minY = typeof minY != 'undefined' ? minY : null;
		o.maxX = typeof maxX != 'undefined' ? maxX : null;
		o.maxY = typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStartS	= new Function();
		o.root.onDragEndS	= new Function();
		o.root.onDragS		= new Function();
	}, // function
	
	start : function(e)	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		
		//window.parent.document.dialog.current_x.value = o.lastMouseX;
		//window.parent.document.dialog.current_y.value = o.lastMouseY;
		
		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		} // if

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		} // if

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	}, // function
	
	start_s : function(e)	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		//var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStartS(y);

		//o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		
		//window.parent.mydialog.current_y.value = o.lastMouseY;
		
		/*
		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		} // if
		*/

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		} // if

		document.onmousemove	= Drag.resize_s;
		document.onmouseup		= Drag.end_s;

		return false;
	}, // function
	
	drag : function(e) {
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
		
		//window.parent.document.mydialog.current_x.value = nx;
		//window.parent.document.mydialog.current_y.value = ny;
		
		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	}, // function
	
	resize_s : function(e) {
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		//var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		//var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		//var nx, ny;
		// var ny;
		
		//if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		//if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		//nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		//if (o.xMapper)		nx = o.xMapper(y)
		//else if (o.yMapper)	ny = o.yMapper(x)

		//Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style["bottom"] = ny + "px";
		//Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDragS(ny);
		return false;
	}, // function
	
	resize_e : function() {
		
	}, // function
	
	resize_se : function() {
		
	}, // function
	
	end : function() {
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
								parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	}, // function
	
	end_s : function() {
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEndS(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
								parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	}, // function

	fixE : function(e) {
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		
		return e;
	} // function
};

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.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";
			
				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.src = closeIcon;
							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";
			
			var skin_mr = document.createElement('td');
			skin_mr.className = skin_dir+" mr";
			
			var skin_mr_a = document.createElement('a');
			
		var skin_tr3 = document.createElement('tr'); // bottom row
			var skin_bl = document.createElement('td');
			skin_bl.className = skin_dir+" bl";

			var skin_bc = document.createElement('td');
			skin_bc.className = skin_dir+" bc";
			skin_bc.setAttribute('align', 'center')
			var skin_bc_a = document.createElement('a');
			//var skin_br_a_space = document.createTextNode('\u00a0');
			
			var skin_br_a = document.createElement('a');
			
			var skin_br = document.createElement('td');
			skin_br.className = skin_dir+" br";
		
		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_mr.appendChild(skin_mr_a);
		skin_tr2.appendChild(skin_mr);
		skin_tr3.appendChild(skin_bl);
		skin_bc.appendChild(skin_bc_a);
		skin_tr3.appendChild(skin_bc);
		//skin_br_a.appendChild(skin_br_a_space);
		skin_br.appendChild(skin_br_a);
		//skin_br_a.setAttribute('onmousedown', 'resizeDialog();');
		skin_tr3.appendChild(skin_br);
		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);
	
	Drag.init(this.table_title, this.container, 0, null, 0);
	
} // 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() {
	this.container.style.display = "none";
	if (this.isModal) veil.show(false);
} // function

// vanish the dialog
pfDialog.prototype.vanish = function() {
	this.container.style.visibility = "hidden";
	if (this.isModal) veil.show(false);
} // function

// set the dialog's position to center
pfDialog.prototype.setCenter = function(x, y) {
	
	var winWidth = getWindowWidth();
	var winHeight = getWindowHeight();
	
	//alert("winHeight: "+winHeight);
	
	var dialogWidth = this.container.offsetWidth;
	var dialogHeight = this.container.offsetHeight;
	
	var winScrollLeft = getScrollLeft();
	var winScrollTop = getScrollTop();
	
	//alert("(winHeight ("+winHeight+") - dialogHeight ("+dialogHeight+") / 2) + winScrollTop ("+winScrollTop+")");
	
	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.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 (!dialogIframe) {
		var dialogIframe = document.createElement("IFRAME");
		dialogIframe.setAttribute('id', obj_id);
		dialogIframe.setAttribute('frameBorder', 'no');
		dialogIframe.setAttribute('scrolling', scrolling);
		dialogIframe.style.display = "block";
		obj.skin_mc.innerHTML = "";
		obj.skin_mc.appendChild(dialogIframe);
	} // if
	
	if (width) {
		dialogIframe.style.width = width;
		dialogIframe.setAttribute('width', width);
	} else {
		dialogIframe.style.width = "100%";
		dialogIframe.setAttribute('width', "100%");
	} // if
	
	if (height) {
		dialogIframe.style.height = height;
		dialogIframe.setAttribute('height', height);
	} else {
		dialogIframe.style.height = 200;
		dialogIframe.setAttribute('height', 200);
	} // if
	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.table_skin.style.width = width + "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");