var dtm = {
	openHome: function(){ dtm.showProgress(); dtm.homeIndex(); },
	openAbout: function(){ dtm.showProgress(); dtm.homeAbout(); },
	openContact: function(){ dtm.showProgress(); dtm.homeContact(); },
	openServices: function(id){ dtm.openContent("Service", id); },
	//openTip: function(id){ dtm.openContent("Tip", id); },
	openTips: function(id){ if(typeof(id) == 'undefined') id = -1; dtm.openContent("Tip", id); },
	openProduct: function(id){ dtm.openProducts(id); },
	openProducts: function(id){ if(typeof(id) == 'undefined') id = 1; dtm.openContent("Product", id); },
	// deprecate
	openSeminar: function(id){ dtm.openContent("Seminar", id); },
	openTrainning: function(id){ dtm.openContent("Trainning", id); },
	openNews: function(id){ dtm.openContent("News", id); },
	openPromotions: function(id){ if(typeof(id) == 'undefined') id = 0; dtm.openContent("Promotion", id); },
	openContent: function(type, id){ dtm.showProgress(); setTimeout( function(){ dtm.contentIndex(type, id); }, 50); },

	newContent: function(type_name){ dtm.showProgress(); dtm.contentNew(type_name);},
	getContent: function(){
		var picture_url = Ext.getCmp("content_picture").getValue();
		return { 
			title: Ext.getCmp("content_title").getValue(), 
			content_type_id: Ext.getCmp("content_type_id").getValue(),
			short_description: Ext.getCmp("content_short_desc").getValue(), 
			description: Ext.getCmp("content_description").getValue(),
			picture_small: picture_url, 
			picture_medium: picture_url, 
			picture_large: picture_url,
			show_home: Ext.getCmp("content_firstpage").getValue()?1:0, 
			reference: Ext.getCmp("content_reference").getValue()
		};
	},
	searchContent: function(page){
		var params, elem;
		params = new Array();

		elem = $('search_text_again'); 
		if(!elem) elem = $('search_text');
		if(elem.value =='') 	return false;
		params[0] = elem.value;

		elem = $('search_column');
		if( elem ) params[1] = elem.value;
		else params[1] = 'all';

		if( typeof(page) == 'undefined'  ) params[2] = 1;
		else params[2] = page;
	
		params[3] = false;
		dtm.showProgress();
		dtm.contentSearch( params );
		return false;
	},
	createContent: function(){ 
		if( !Ext.getCmp("content_form").getForm().isValid() ){ 	alert('กรุณากรอกข้อมูลให้ครบ'); return; 	};
		dtm.showProgressContent(); 
		var content = dtm.getContent();  
		dtm.contentCreate(content); 
	},
	viewContent: function(id){ dtm.showProgress(); 	dtm.contentView(id); },
	editContent: function(id){ 	dtm.showProgress(); dtm.contentEdit(id); },
	saveContent: function(){ 
		if( !Ext.getCmp("content_form").getForm().isValid() ){ 	alert('กรุณากรอกข้อมูลให้ครบ'); return; 	};
		dtm.showProgressContent(); 
		var content = dtm.getContent(); 
		content.content_id = Ext.getCmp("content_id").getValue(); 
		dtm.contentSave(content); 
	},
	deleteContent: function(id){ if (confirm("คุณต้องการลบข้อมูลใช่หรือไม?่\nโปรดระวังคุณไม่สามารถกู้ข้อมูลคืนได้")){dtm.showProgress(); dtm.contentDelete(id); }},
	listContent: function( type, page, order_col, order ){ dtm.showProgress(); dtm.contentList( type, page, order_col, order ); },

	openLog: function(){ dtm.showProgress(); dtm.logIndex();},
	showLog: function(){ dtm.showProgress(); dtm.logShow( $('log_year').value, $('log_month').value);},

	ajaxfilemanagerurl: "/js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php?language=th",
	ajaxfilemanager: function (field_name, url, type, win) {
		var ajaxfilemanagerurl = dtm.ajaxfilemanagerurl;
		switch (type) { case "image": break; case "media": break; case "flash": break; case "file": break; default: return false; 	}
		tinyMCE.activeEditor.windowManager.open( { url: dtm.ajaxfilemanagerurl,	width: 782, height: 440, inline : "yes", close_previous : "no" }, { window : win, input : field_name });
		return false;
	},

	login: function(){ dtm.adminIndex(); dtm.showProgress(); },
	loggingIn: function(){ dtm.adminLogin( $('username').value, $('password').value, $('etag').value ); dtm.showProgress(); },
	logout: function(){ dtm.adminLogout(); dtm.showProgress(); },

	changePassword: function(){ dtm.showProgress(); dtm.adminChangePassword();},
	savePassword: function(){ dtm.hideMessage();
		if( $('new_password').value != $('confirm_password').value ) { dtm.showNoticeMessage('รหัสผ่านใหม่ไม่เหมือนกัน');
		}else if( $('new_password').value == '' ) { dtm.showNoticeMessage('กรุณาพิมพ์รหัสผ่านใหม่');
		}else if( $('old_password').value == $('confirm_password').value ) { dtm.showNoticeMessage('รหัสผ่านเดิมและรหัสผ่านใหม่เหมือนกัน');
		}else{ dtm.showProgress(); dtm.adminSavePassword( $('old_password').value, $('new_password').value, $('confirm_password').value ); }
		
	},

	showProgress : function(){ $('progress_info').innerHTML = 'โปรดรอซักครู่, กำลังโหลดอยู่...'; $('progress').show(); dtm.startTime(); dtm.hideMessage(); return false;},
	hideProgress : function(){ $('progress').hide(); dtm.stopTime(); return false;},
	showProgressContent : function(){ $('progress_content').show(); return false;},
	hideProgressContent : function(){ $('progress_content').hide(); return false;},
	hideMessage: function(){  $('error').hide(); $('notice').hide(); return false;},
	showNoticeMessage: function(mess){ $('notice').show(); $('notice').innerHTML = mess; return false;},
	showErrorMessage: function(mess){ $('error').show(); $('error').innerHTML = mess; return false;},
	startTime: function(){ window.setTimeout( dtm.timeOut, 10000); return false;},
	stopTime: function(){	window.clearTimeout();	return false;},
	timeOut : function(){	if( $('progress').visible() ) $('progress_info').innerHTML ='การเชื่อมต่อไปยังเซิร์ฟเวอร์ช้า อาจจะมีสาเหตุมาจากอินเตอร์ของคุณช้าหรือเครื่องเซิร์ฟเวอร์มีปัญหา';return false;},
	createDraggable : function(event){ var elem = $('content_edit'); if( !elem.drag ) elem.drag = new Draggable( elem , { zindex: 999999, scroll: window }); return false;},
	destroyDraggable : function(event){ var elem = $('content_edit'); if( elem.drag && !elem.drag.dragging){ elem.drag.destroy(); elem.drag = null; } return false;},
	openFileManager: function( element){ tinyMCE.activeEditor.windowManager.open({ url: dtm.ajaxfilemanagerurl,	width: 782, height: 440, inline : "yes", close_previous : "no" },{ window: window, input : element}); return false;},
	onLoad: function(){ if(typeof(openContent) != 'undefined' ) window.setTimeout( openContent, 100); Ext.QuickTips.init(); },
	openForm: function(callback, type_id, id, title, sdescription, picture, description, firstpage, reference){
		var dlg = Ext.getCmp("dtm_content_editer");
		if(dlg){
			dlg.close();
			dlg.destroy();
		}
		dlg = new Ext.Window({
			id: "dtm_content_editer",
			title: "DTM Content",
			//autoWidth: true,
			//autoHeight: true,
			width: 790,
			height: 665,
			minWidth: 100,
			minHeight: 100,
			layout: "form",
			modal: true,
			resizable: true,
			maximizable: false,
			closeAction: "hide",
			hideMode: "offsets",
			constrainHeader: true,
			items: [
			{
				xtype: "form",
				id: "content_form",
				autoScroll: true,
				border: false,
				bodyBorder: false,
				items: [
					{ xtype: "hidden", id: "content_id", value: id || "" },
					{ xtype: "hidden", id: "content_type_id", value: type_id || "0" 	},
					{ xtype: "textfield", id: "content_title", fieldLabel: "หัวเรื่อง", 	width: 300, 	allowBlank: false, value: title || "" },
					{ xtype: "textfield", id: "content_short_desc", fieldLabel: "รายละเอียดแบบย่อ", width: 500, allowBlank: false, value: sdescription || "" },
					{ xtype: "textfield", id: "content_picture", fieldLabel: "ภาพประกอบ",	width: 400, 	/*allowBlank: false,*/	value: picture || ""	},
					{
						xtype: "tinymce",
						id: "content_description",
						fieldLabel: "รายละเอียด",
						width: 300,
						height: 450,
						tinymceSettings: {
							language : "th",
							mode : "exact", 
							//elements : element, 
							theme : "advanced", skin : "o2k7", skin_variant : "silver",
							plugins: "safari,pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
							theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
							theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
							theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|",
							theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
							theme_advanced_toolbar_location : "top",
							theme_advanced_toolbar_align : "left",
							theme_advanced_statusbar_location : "bottom",
							theme_advanced_resizing : false,
							extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",

							file_browser_callback : "dtm.ajaxfilemanager",
							external_link_list_url : "/js/tiny_mce/lists/link_list.js",
							external_image_list_url : "/js/tiny_mce/lists/image_list.js"
						},
						value: description || "<h1>Test</h1><p>DTM content...</p>"
					},
					{ xtype: "checkbox", id: "content_firstpage", fieldLabel: "แสดงหน้าแรก", width: 500, 	checked: (typeof firstpage != 'undefined')?firstpage:false },
					{ xtype: "textfield", id: "content_reference", fieldLabel: "อ้างอิง", width: 200, value: reference || "" },
					{ 
						xtype: "box",
						id: "content_progress",
						autoEl: { 
							tag: 'div', 
							html: '<div id="progress_content" style="display: none; background-color:#f00; color: #fff;">'
								+ '<img src="/images/progress.gif" border="0" width="20" height="20"/>' 
								+ '<span id="progress_content_info" name="progress_content_info">โปรดรอซักครู่, กำลังโหลดอยู่...</span></div>' 
						}
					}
				],
				buttons: [
					{
						text: "บันทึก",
						handler: callback || function(){}
					},{
						text: "ล้าง",
						handler: function(){
							Ext.getCmp("content_title").setValue("");
							Ext.getCmp("content_short_desc").setValue("");
							Ext.getCmp("content_picture").setValue("");
							Ext.getCmp("content_description").setValue("");
							Ext.getCmp("content_firstpage").setValue(false);
							Ext.getCmp("content_reference").setValue("");
						}
					},
					{
						text: "ปิด",
						handler: function(){
							Ext.getCmp("dtm_content_editer").close();
						}
					}
				]
			}					
			]
		});
		
		Ext.getCmp( "content_picture" ).on( "focus", function() {
			dtm.openFileManager('content_picture');
		});
		
		dlg.show();
	}
};