// Copyright (c) 2000-2008 Synology Inc. All rights reserved.
MODULE_FRIEND_LINK = function(div_id, config) {
	MODULE_FRIEND_LINK.superclass.constructor.call(this, div_id, config);
};

Ext.extend(MODULE_FRIEND_LINK, SYNOBLOG_MODULE, {
	init: function() {
		var el_content = Ext.get(this.div_id+'_content');
		if(el_content != null) {
			var mgr_content = el_content.getUpdateManager();
			mgr_content.update("modules/friend_link.php", "action=get_module_content");
		}
	},
	customizeConfigDialog: function() {
		var layout = this.configDialog.getLayout();
		layout.beginUpdate();

		var content = '<div align="center" style="width:340px;" id="current_link"></div>' + 
			'<div style="margin:20px;width:340px;" id="friend_link_form"></div>';

		var cp = layout.add('center', new Ext.ContentPanel(Ext.id(), {autoCreate:true, title: blog_str_friend_link_title, background:true}));
		cp.setContent(content);
		layout.endUpdate();

		this.configDialog.getLayout().getRegion("center").showPanel(0);
		this.configDialog.on("show", function(dialog) {this.getConfigDialogData(null);}, this);
		this.getForm();
	},
	getConfigDialogData: function(action) {
		Ext.UpdateManager.updateElement('current_link', "modules/friend_link.php", "action=get_current_link");
		this.form.reset();
	},
	getForm: function() {
		this.form.column({width:340},
			new Ext.form.TextField({
				fieldLabel: blog_str_friend_link_name,
				name: 'name',
				allowBlank: false,
				maxLength: 64,
				width: 212,
				validateOnBlur: true,
				validationEvent: 'blur'
			}),
			new Ext.form.TextField({
				fieldLabel: blog_str_friend_link_link,
				name: 'link',
				allowBlank: false,
				maxLength: 512,
				width: 212,
				validateOnBlur: true,
				validationEvent: 'blur',
				vtype: 'url'
			})
		);
		this.form.addButton(blog_str_common_add, this.add_link, this);
		this.form.addButton(blog_str_common_close, this.hide, this);
		this.form.render('friend_link_form');

		SYNOBLOG_UTILS.SetTextMaxLen(this.form, 'name', 64);
		SYNOBLOG_UTILS.SetTextMaxLen(this.form, 'link', 512);
	},
	add_link: function() {
		if(!this.form.isValid()) {
			return;
		}
		this.form.submit({url:'modules/friend_link.php',
			waitTitle:' ',
			waitMsg:blog_str_man_blog_header_pic_plz_wait,
			params: {action: 'add_link'}
		});		
	},
	delete_link: function(id) {
		Ext.Ajax.request({
			url: "modules/friend_link.php",
			params: 'action=delete_link&id='+id,
			method: "POST",
			scope: this,
			success: function(res, opts) {
                this.getConfigDialogData();
			}
		});
	}
});

SYNOBLOG.instance().registerModule("friend_link", {hasConfigDialog: 1, width:420, height:210}, MODULE_FRIEND_LINK, 500, 400);
