
var PostBatchCommands = Class.create(AjaxRequest, {
	initialize: function($super, options) {
		$super(options);
		if (!$('batchCommands')) return;
		this.checkPosts = new checkItems('.checkPost', 'checkAllPost');
		this.linkHref = $('batchCommands').getAttribute('href');
		this.linkToAjaxWindow('batchCommands', {
			onBeforeRequest: this._beforeBatchCommands,
			onWindowOpen: this._setupForms
		});
	},
	
	_beforeBatchCommands: function(element) {
		var checkedItems = this.checkPosts.checkedItems(element);
		if (checkedItems.length == 0) {
			alert('선택된 글이 없습니다.');
			return false;
		} else {
			var q = (this.linkHref.match(/\?/)) ? '&' : '?';
			element.setAttribute('href', this.linkHref + q + 'checked=' + checkedItems.join(','));
		}
		return true;
	},

	_setupForms: function() {
		var containerID = this.windowContainer.id;
		$$('#'+containerID+' a').each(function(element){
			this.linkToAjaxWindow(element, {
				onWindowClose: this._reloadPage
			});
		}.bind(this));

		$$('#'+containerID+' form#deleteForm').each(function(element){
			this.formToAjaxRequest(element, {
				onAfterRequest: this._reloadPage
			});
		}.bind(this));

		$$('#'+containerID+' form#moveForm').each(function(element){
			this.formToAjaxRequest(element, {
				onAfterRequest: this._reloadPage
			});
		}.bind(this));
	},
	
	_reloadPage: function() {
		if (this.options.boardUrl) {
			document.location.href = this.options.boardUrl;
		} else {
			window.location.reload(); 
		}
		
	}
});

var Comments = Class.create(AjaxRequest, {
	initialize: function($super, options) {
		$super(options);
		this.formToAjaxRequest('commentForm', {
			onAfterRequest: this._commentFormCallback
		});
		this.resetCommentLinks();
	},

	updateComments: function() {
		var url = this.options.postUrl + "/comment";
		var pars = null;
		var placeholder = 'commentListContainer';
		var myAjax = new Ajax.Updater(placeholder, url, {
				method: 'get',
				parameters: pars,
				evalScripts: true,
				onComplete: function(request){
					this.resetCommentLinks();
				}.bind(this)
			}
		);
	},

	resetCommentLinks: function() {
		$$('#commentList .commentMenu a').each(function(element){
			this.linkToAjaxWindow(element, {
				onWindowOpen: this._resetWindowLinks
			});
		}.bind(this));
		$$('#commentList .pager a').each(function(element){
			this.linkToAjaxUpdater(element, 'commentList', {
				onAfterRequest: this.resetCommentLinks
			});
		}.bind(this));
	},
	
	_resetWindowLinks: function() {
		var containerID = this.windowContainer.id;

		$$('#'+containerID+' a').each(function(element){
			this.linkToAjaxWindow(element, {
				onWindowOpen: this.resetCommentLinks,
				onWindowClose: this.updateComments
			});
		}.bind(this));

		$$('#'+containerID+' form').each(function(element){
			this.formToAjaxRequest(element, {
				onAfterRequest: this._commentFormCallback
			});
		}.bind(this));
	},

	_commentFormCallback: function(element, result) {
		this.updateComments();
		$('commentForm').reset();
		$$('#commentForm .captchaImage').each(function(element){
			var keyName = getQueryVariable(element.src, 'key');
			element.src = moduleBaseUrl + 'captcha?key='+keyName+"&"+(new Date()).getTime();
		});
	}
}); 


document.observe('dom:loaded', function(){
	var userInfo = $$('.userInfo');
	if (userInfo.length > 0) {
		var userInfoMenu = new dropDownMenu('userInfo', 'mouse');
		userInfo.each(function(element){
			userInfoMenu.addMenuElements(element); 
		});
	}
});