function TemplateDialog(templateInfo) {
	this.templateInfo = templateInfo;
	this.templateId = templateInfo.id;
	this.dialogId = 'templateDialog_' + templateInfo.id;
	this.openerId = 'viewTemplate_' + templateInfo.id;
	this.templateTabs = 'templateTabs_' + templateInfo.id;
	this.templateInfoTabId = 'templateInfoTab_' + templateInfo.id;
	this.templateTextTabId = 'templateTextTab_' + templateInfo.id;
	YAHOO.util.Event.addListener(window, 'load', this.initialize, this, true);
}

TemplateDialog.prototype = {
	
	templateInfo : null,
	
	dialogId : null,
	
	templateId : null,
	
	openerId : null,
	
	templateTabs : null,
	
	templateInfoTabId : null,
	
	templateTextTabId : null,
	
	dialog : null,
	
	showBtn : null,
	
	initialize : function() {
		 this.showBtn = getEl(this.openerId);
		 YAHOO.util.Event.addListener(this.openerId, 'click', this.showDialogLayout, this, true);
	},
	
	showDialog : function(){
	    if(!this.dialog){ // lazy initialize the dialog and only create it once
	    	this.buildDialog();			
	        this.dialog = new YAHOO.ext.BasicDialog(this.dialogId, { 
	                autoTabs:true,
					autoScroll:true,
	                width:500,
	                height:300,
	                shadow:true,
	                minWidth:300,
	                minHeight:250,
	                proxyDrag: true
	        });
	        this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
	        this.dialog.addButton('Close', this.dialog.hide, this.dialog);
	    }
	    this.dialog.show(this.showBtn.dom);
	},
	
	showDialogLayout : function() {
		if(!this.dialog){ // lazy initialize the dialog and only create it once
			this.buildDialog();	
            this.dialog = new YAHOO.ext.LayoutDialog(this.dialogId, { 
                    width:600,
                    height:400,
                    shadow:true,
                    minWidth:300,
                    minHeight:300,
                    center: {
                        autoScroll:true,
                        tabPosition: 'top',
                        closeOnTab: true,
                        alwaysShowTabs: true
                    }
            });
            this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
            this.dialog.addButton('Close', this.dialog.hide, this.dialog);
			if(this.templateInfo.hasFile == 1) {
				this.dialog.addButton('Download File', this.showFile, this);
			}
            
            var layout = this.dialog.getLayout();
            this.dialog.beginUpdate();
			
			var infoTab = new YAHOO.ext.ContentPanel(this.templateInfoTabId, {title: 'Template Info'});
			infoTab.setUrl('ajaxRequests.php?pageAction=getTemplate&template=' + this.templateInfo.id + '&value=all', null, true);
			layout.add('center', infoTab);
			var reportTab = new YAHOO.ext.ContentPanel(this.templateTextTabId, {title: 'Build Report'});
			reportTab.setUrl('ajaxRequests.php?pageAction=getTemplate&template=' + this.templateInfo.id + '&value=TemplateText', null, true);
            
			layout.add('center', reportTab);
			layout.getRegion('center').showPanel(this.templateInfoTabId);
            this.dialog.endUpdate();
        }
        this.dialog.show(this.showBtn.dom);
	},
	
	buildDialog : function() {
		var containerDiv = document.createElement('div');
		var headerDiv = document.createElement('div');
		var bodyDiv = document.createElement('div');
  		
		containerDiv.setAttribute('id', this.dialogId);
		headerDiv.className = 'ydlg-hd';
		bodyDiv.className = 'ydlg-bd';
  		headerDiv.innerHTML = this.templateInfo.username + '\'s ' + this.templateInfo.className + 'Template';
		
		containerDiv.appendChild(headerDiv);
		containerDiv.appendChild(bodyDiv);
  		document.body.appendChild(containerDiv);
		this.buildBody(bodyDiv);
	},
	
	buildBody : function(bodyDiv) {
		var toolBarDiv = document.createElement('div');
		toolBarDiv.setAttribute('id', 'templateTB_' + this.templateInfo.id);
		bodyDiv.appendChild(toolBarDiv);
		var templateTextTab = document.createElement('div');
		templateTextTab.setAttribute('id', this.templateTextTabId);
		templateTextTab.setAttribute('class', 'dlg-tab-content');
		templateTextTab.className = 'dlg-tab-content';
		var templateInfoTab = document.createElement('div');
		templateInfoTab.setAttribute('id', this.templateInfoTabId);
		templateInfoTab.setAttribute('class', 'dlg-tab-content');
		templateInfoTab.className = 'dlg-tab-content';
		bodyDiv.appendChild(templateInfoTab);
		bodyDiv.appendChild(templateTextTab);
	},
	
	showFile : function() {
		window.open('' + this.templateInfo.filePath, 'Template File');
	}
	
}
