function dlgGallery () {
	var instancename;
	var parentRef;
	this.panels = {};
	this.events = [];
		
	this.showUploadDlg = function() {
		this.panels.uploadDialog.show();
	};
	
	this.updateCallback = {
		success: function(o) {
			try {
				var tConfig = [{
					f: "%instancename%",
					r: this.instancename
				}];
				var HTML = BackDrop.tokenizer.run(o.responseText, tConfig);
				var tNode = document.createElement("DIV");
				tNode.innerHTML = HTML;
				var tNode_Divs = tNode.getElementsByTagName("DIV");
				for (var x = 0; x < tNode_Divs.length; x++) {
					if (tNode_Divs[x].id == "Application_Gallery_Container") {
						if (typeof parentRef == "string") {
							var pNode = document.getElementById(parentRef);
							pNode.innerHTML = tNode_Divs[x].innerHTML;
						} else {
							if (typeof parentRef == 'object') {
								parentRef.setBody(tNode_Divs[x].innerHTML);
							}
							else {
								alert('BackDrop could not determine the parent reference for the gallery.');
							}
						}
					}
				}
			} 
			catch (err) {
				alert("There was an error! \r\n" + err.description);
			}
		}, 
		
		failure: function(o) {
			
		},
		scope: this
	};
	
	this.init = function() {
		instancename = this.instancename;
		parentRef = this.parent;
		if (this.parentDiv) {
			parentDiv = this.parentDiv;
		}
		this.setupPanel(this.node, instancename);
	};
	
	this.render = function(divId) {
		var id, node, config;
		
		instancename = this.instancename;
		parentRef = divId;
		
		node = document.getElementById(divId);
		node.style.display = "";
		config = [{f: "%instancename%", r: instancename}];
		node.innerHTML = BackDrop.tokenizer.run(node.innerHTML, config);
		this.setupPanel(node, instancename);
	};
	
	this.setupPanel = function(node, instancename) {
		var uNode;
		var oDiv = node.getElementsByTagName("DIV");
		for(var s = 0; s < oDiv.length; s++) {
			if(oDiv[s].id.indexOf("Upload_Dialog") != -1) {
				uNode = oDiv[s];
				break;
			}
		}
		this.panels.uploadDialog = new YAHOO.widget.Dialog(uNode.id, {
			
			constraintoviewport:true, 
			visible:false, 
			draggable:true, 
			fixedcenter:true,
			zIndex: 10,
			effect: {effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25}
			});
		var buttons = [ {text: "Upload", handler: this.panels.uploadDialog.submit, isDefault:true},{text: "Cancel", handler: this.panels.uploadDialog.cancel} ];		
		this.panels.uploadDialog.cfg.queueProperty("buttons", buttons);		
		this.panels.uploadDialog.callback.upload = function(o) {
			// UPLOAD HANDLER			
			alert(o.responseText);
			var node = uNode;
			var el = node.getElementsByTagName("INPUT");
			for(var s=0; s<el.length;s++) {
				if(el[s].getAttribute("type") != "button" || el[s].getAttribute("type") != "reset" || el[s].getAttribute("type") != "submit") {
					el[s].value = "";
				}
			}
			var el = node.getElementsByTagName("TEXTAREA");
			for(var s=0;s<el.length;s++) {
				el[s].value = "";
			}
			var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.php?dialog=Gallery&action=updateimages', eval(instancename + ".updateCallback"), null);
			el = null;
			node = null;
		}
		
		this.panels.uploadDialog.render(document.body);	
	};
	
	this.showPreview = function(src, width, height) {
		//Load Gallery Panel
		var dom = YAHOO.util.Dom;
		var cWidth = dom.getClientWidth();
		var cHeight = dom.getClientHeight();
		
		var iWidth = width;
		var iHeight = height;
		
		var margin = 100;
		if(width > (cWidth - margin)) {
			width = cWidth - margin;
		}
		if(height > (cHeight - margin)) {
			height = cHeight - margin;
		}
		
		this.panels.panel1 = new YAHOO.widget.Panel('AppGalleryPreviewPanel', {
			width: width + "px", 
			height: height + "px", 
			visible: false, 
			draggable: false, 
			close:true, 
			fixedcenter:true, 
			constraintoviewport:true,
			zIndex: 11,
			modal: true
			});
		this.panels.panel1.setHeader("Preview");
		
		var nWidth = ((height - 50) * iWidth) / iHeight;
		var nHeight = height - 50;
		
		this.panels.panel1.setBody("<div style='text-align:center;'><img src='" + src + "' width='" + nWidth + "' height='" + nHeight + "'/></div>");
		this.panels.panel1.render(document.body);
		this.panels.panel1.show();	
	};
	
	this.selectCategory = function(cat) {
		this.category = cat;
		var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.php?dialog=Gallery&action=updateimages&cat=' + cat, eval(instancename + ".updateCallback"), null);
	};
	
	this.subscribe = function(evt, action) {
		this.events[evt] = action;
	};
	
	this.fire = function(evt, args) {
		if(this.events[evt]) {
			this.events[evt](args);
		}
	};
	
	this.remove = function(image,alignTo) {
		
		this.panels.delDialog = new YAHOO.widget.SimpleDialog("cfmDeleteDialog", {
			width: "20em",
			effect: {effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25},
			visible: false,
			draggable: true,
			context: [alignTo, "tl", "tl"],
			zIndex: 100
			});
		
		this.panels.delDialog.setHeader("Are you Sure?");
		this.panels.delDialog.setBody("Are you sure you want to permanently remove this image? You can not undo this action.");
		this.panels.delDialog.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_WARN);
		
		var buttons = [ { text: "Yes", handler: function() {eval(instancename + '.confirmRemove("' + image + '")');} }, {text: "Cancel", handler: this.panels.delDialog.cancel, isDefault: true}];
		this.panels.delDialog.cfg.queueProperty("buttons", buttons);
		this.panels.delDialog.render(document.body);
		this.panels.delDialog.show();
		
	};
	
	this.confirmRemove = function(image) {
		this.panels.delDialog.hide();
		var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.php?dialog=Gallery&action=removeimage&image=' + image, eval(instancename + '.removeImageResult'), null);
	};
	
	this.removeImageResult = 
	{
		success: function(o)
		{
			// Request an Update
			var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.php?dialog=Gallery', eval(instancename + ".updateCallback"), null);
		},
		failure: function(o) {alert("failure");}
	}
}

/*
	dlgGallery.methods.onUpload = function(o) {
		dlgGallery.panels.sd = new YAHOO.widget.SimpleDialog("Applications_Gallery_confirmDlg", {
			width: "20em",
			effect:{effect: YAHOO.widget.ContainerEffect.FADE,duration:0.25},
			fixedcenter:true,
			visible:false,
			draggable:false,
			zIndex: 100
					   }
					   );
		var buttons = [ {text:"Ok", handler: dlgGallery.methods.sdClose, isDefault:true}];
		dlgGallery.panels.sd.cfg.queueProperty("buttons", buttons);
		
		dlgGallery.panels.sd.setHeader("Result");
		dlgGallery.panels.sd.setBody(o.responseText);
		dlgGallery.panels.sd.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_WARN);
		
		dlgGallery.panels.sd.render(document.body);
		dlgGallery.panels.sd.show();
		
		// We need to reset the current image window.
	}
	
	dlgGallery.methods.sdClose = function() {
		dlgGallery.panels.sd.hide();
		// try to refresh requestor
		
	}
	
	dlgGallery.methods.reloadCallback = { 
		
		success: function(o) {
			alert(o.responseText);
			parentPanel.setBody("");
			parentPanel.setBody(o.responseText);
			this.init();
		}, 
		failure: function(o) {
			alert('Gallery Reload Failure');
		},
		cache:false
	};
	
	dlgGallery.methods.closeUploadDialog = function() {
		panels.uploadDialog.hide();	
	}
	
	dlgGallery.methods.showPreview = function(src, width, height) {
		//Load Gallery Panel
		var dom = YAHOO.util.Dom;
		var cWidth = dom.getClientWidth();
		var cHeight = dom.getClientHeight();
		
		var margin = 100;
		if(width > (cWidth - margin)) {
			width = cWidth - margin;
		}
		if(height > (cHeight - margin)) {
			height = cHeight - margin;
		}
		
		panels.panel1 = new YAHOO.widget.Panel('AppGalleryPreviewPanel', {
			width: width + "px", 
			height: height + "px", 
			visible: false, 
			draggable: false, 
			close:true, 
			fixedcenter:true, 
			constraintoviewport:true,
			
			});
		panels.panel1.setHeader("Preview");
		
		var nWidth = width - 50;
		var nHeight = height - 50;
		
		panels.panel1.setBody("<div style='text-align:center;'><img src='" + src + "' width='" + nWidth + "' height='" + nHeight + "'/></div>");
		panels.panel1.render('Application_Gallery_Container');
		panels.panel1.show();
	
	}
	
	dlgGallery.methods.hide = function () {
		panels.panel1.hide();
	}
	
	dlgGallery.methods.insert = function (id) {
		if(typeof Gallery_Insert_Image != 'undefined') {
			Gallery_Insert_Image(id);
		} else {
			alert("Method `Gallery_Insert_Image(string id)` Does not exist");	
		}
	}
	
	dlgGallery.methods.upload = function() {
		dlgGallery.panels.uploadDialog.show();
	}
	
	dlgGallery.methods.remove = function(image, alignTo) {
		
		panels.  v = new YAHOO.widget.SimpleDialog("cfmDeleteDialog", {
			width: "20em",
			effect: {effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25},
			visible: false,
			draggable: true,
			context: [alignTo, "tl", "tl"],
			zIndex: 100
			});
		
		panels.delDialog.setHeader("Are you Sure?");
		panels.delDialog.setBody("Are you sure you want to permanently remove this image? You can not undo this action.");
		panels.delDialog.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_WARN);
		
		var buttons = [ { text: "Yes", handler: function() {methods.confirmRemove(image); panels.delDialog.destroy()} }, {text: "Cancel", handler: panels.delDialog.cancel, isDefault: true}];
		panels.delDialog.cfg.queueProperty("buttons", buttons);
		panels.delDialog.render(document.body);
		panels.delDialog.show();
	}
	
	dlgGallery.methods.confirmRemove = function(image) {
		var cm = YAHOO.util.Connect.asyncRequest('GET', "index.php?app=Gallery&action=removeimage&image=" + image, methods.deleteCallback, null);
	}
	
	dlgGallery.methods.deleteCallback = {
		success: function(o) {
			if(typeof Applications_Gallery_Reset != 'undefined') {
				Applications_Gallery_Reset();
			} else {
				
			}
	
		}, 
		
		failure: function(o) {
			alert("could not complete your request");
		}
	}
	
	dlgGallery.methods.addFolder = function() {
		
	}
	
	dlgGallery.methods.removeFolder = function() {
		
	}
*/
