/**
 * Plani javascript module
 *
 * jquery 1.11.4
 *
 * @package	Plani javascript Api Module
 * @author	kim seung beom
 * @copyright	Copyright (c) 2016, Plani, Inc.
 * @link	http://plani.co.kr
 * @since	 Version 2.0
 * @filesource
 */
$.info.style('/_res/plani_api/comment.css');

$.api['comment'] = function( options ) {
	this.options = $.extend({
			sequence : '',
			selector : '#',
			url_pattern : ''
		},
		options
	);

	this.form = $(this.options.selector).closest('form');
	this.layer = $('.editor_lists', this.form);
	this.options.list_url = this.options.url_pattern.sprintf('comment_list');
	this.options.last_url = '';

	this.__construct();
}

$.extend
(
	$.api['comment'].prototype, {
		__construct : function() {
			var $this=this;			

			// Called after the editor widget
			$(function(){
				new $.module['form']
				(
					$this.form, {
						success : function(module, insert_id){
							module._disposal(false);
							$this.form.find('input[type=text], input[type=password]').val('');
							$this.editor_reset();
							$this.request();
						},
						jgrow : true
					}
				);
			});

			$this.layer.delegate
			(
				'.paging-area a', 'click', function(){
					if( $(this).attr('href') != '#' ){
						$this.request( $(this).attr('href') );
					}
					return false;
				}
			)
			.delegate
			(
				'a.remove', 'click', function(){
					if( confirm( __('삭제 하시겠습니까?') ) == true ) {
						var a=$(this);

						$.request
						(
							{ 'type' : 'get', 'url' : a.attr('href') }, 
							function(msg) {
								if( msg == 'true' )
								{
									$this.request($this.options.last_url);
								}else
								{
									new $.module['modal']
									(
										a.attr('href'), {
											'title' : __('댓글 삭제'),
											'close' : false,
											'iframe' : true,
											'width' : 750,
											'onclose' : function(){
												$this.request($this.options.last_url);								
											}
										}
									);
								}
							}
						);
					}
					return false;
				}
			)
			.delegate
			(
				'a.modify', 'click', function(){
					new $.module['modal']
					(
						$(this).attr('href'), {
							'title' : __('댓글 수정'),
							'close' : false,
							'iframe' : true,
							'width' : 750,
							'onclose' : function(){
								$this.request($this.options.last_url);								
							}
						}
					);
					return false;
				}
			)
			.delegate
			(
				'a.write', 'click', function(){
					new $.module['modal']
					(
						$(this).attr('href'), {
							'title' : __('답변 등록'),
							'close' : false,
							'iframe' : true,
							'width' : 750,
							'onclose' : function(){
								$this.request();
							}
						}
					);					
					return false;
				}
			);

			$this.request();
		},
		request : function( url ) {
			var $this = this;

			if( !url ){
				url = $this.options.list_url;
			}

			$this.options.last_url = url;
			$.request
			(	
				{
					'url' : url,
					'param' : $this.form.serialize()
				},
				function( html ){
					$this.layer.html(html);
					$('.file-tooltip', $this.layer).helper_tooltip();
					try {
						if( location.hash != '' && $(location.hash, $this.layer).length > 0 ) {	
							$('html, body').animate({'scrollTop' : $(location.hash, $this.layer).offset().top}, 'slow');
							location.hash = '';
						}
					}catch (e) {
					}
				}
			);
		},
		editor_reset : function(){
			var $this = this,
				editor = $.static.editor[$this.options.sequence].editor;

			if( editor ){
				editor.setContent('');
			}

			try {
				var uploader = $.static.editor_uploader[$this.options.sequence].plupload;
				if( uploader ){				
					for( key in uploader.files ){
						uploader.removeFile(uploader.files[key]);
					}
					uploader.total.plus_size = 0;
					uploader.updateList();
				}				
			}
			catch (e){
			}
		}
	}
);