var SynkiPlugin_Attachment = {
ub: new URLBuilder,
ids: {
	show_file_manager: "show_file_manager",
	_parent: "file_manager",
	show_upload_button_wrap: "show_upload_field_wrap",
	show_upload_button: "show_upload_field",
	upload_field_wrap: "file_upload_filed",
	upload_field: "_Filedata",
	attach_list: "file_manager_attach_list",
	insert_image_button: "attach_insert_images",
	insert_file_button: "attach_insert_files",
	manager_close_button: "file_manager_close",
	checked_name: "checked_attach",
	attach_count: "attach_count",
	header_close_button: "header_close_button",
	doing: "file_manager_doing"
},

panel_html: function() {
	var html = '<div class="file_manager_wrap">';
	html += '<form enctype="multipart/form-data" method="post">';
	html += '<div class="file_manager_header">' + MSG[34] + '</div>';
	html += '<div id="' + this.ids.doing + '" class="file_manager_doing"><img src="/images/ico_saving.gif"> ' + MSG[323] + '</div>';
	html += '<div class="attach_count_wrap">';
	html += '<span id="' + this.ids.attach_count + '">0</span> ' + MSG[35]
	html += '&nbsp;&nbsp;&nbsp;&nbsp;<img src="/images/alert_close.gif" id="' + this.ids.header_close_button + '" class="header_close_button">';
	html += '</div>';

	//File Upload Button Toggle & File Upload Input
	html += '<div class="attach_upload_field">';
	html += '	<div id="' + this.ids.show_upload_button_wrap + '"><img src="/images/ico_attach.gif"/> <a id="' + this.ids.show_upload_button + '">' + MSG[145] + '</a></div>';
	html += '	<div class="Hide" id="' + this.ids.upload_field_wrap + '"><input size="30" id="' + this.ids.upload_field + '" name="' + this.ids.upload_field + '" type="file" value="' + MSG[536] + '" /><input type="submit" value="' + MSG[536] + '" onclick="SynkiPlugin_Attachment.file_control_create();return false" /></div>';
	html += '</div>';

	//Attachment List & Item Info
	html += '<div class="attach_list_wrap">';
	html += '	<div class="attach_list"><ul id="' + this.ids.attach_list + '" /></ul></div>';
	html += '	<div class="attach_info"><!-- <div class="Pop_Img attach_preview" id="attach_preview_div"/></div> --></div>';
	html += '</div>';

	html += '<div class="clear_both"><div class="Hide" id="file_manager_upload_progress"><img alt="working..." src="/images/loading.gif"></div></div>';

	//Buttons
	html += '<div class="file_manager_buttons">';

//		if(show_type == "editor"){
		html += '	<input class="insert_buttons attach_edit_mode" id="' + this.ids.insert_image_button + '" type="button" value="' + MSG[36] + '" />';
		html += '	<input class="insert_buttons attach_edit_mode" id="' + this.ids.insert_file_button + '" type="button" value="' + MSG[37] + '" />';
//		}

	html += '	<input class="Pop_In" id="' + this.ids.manager_close_button + '" type="button" value="' + MSG[38] + '" />';
	html += '</div>';

	html += '</div>';
	html += '</form>';

	return html;
},

bind_event: function(Wizard, is_offline) {
	var show_upload_button = document.getElementById(this.ids.show_upload_button);
	if(is_offline) add_event(show_upload_button, "click", function() { new Shout().error({msg: MSG[538]})});
	else add_event(show_upload_button, "click", CallBack(this, this.show_upload_field));
//
////		var file_upload_filed = document.getElementById(this.ids.upload_field);
////		add_event(file_upload_filed, "change", CallBack(this, this.file_control.create()});
//
	var insert_images_button = document.getElementById(this.ids.insert_image_button);
	add_event(insert_images_button, "click", CallBack(this, function(){this.insert_image(Wizard)}));

	var insert_files_button = document.getElementById(this.ids.insert_file_button);
	add_event(insert_files_button, "click", CallBack(this, function(){this.insert_link(Wizard)}));

	var manager_close_button = document.getElementById(this.ids.manager_close_button);
	add_event(manager_close_button, "click", CallBack(this, function(){this.hide_file_manager(Wizard)}));

	var header_close_button = document.getElementById(this.ids.header_close_button);
	add_event(header_close_button, "click", CallBack(this, function(){this.hide_file_manager(Wizard)}));
},

bind_toggle_progress: function(){
	jQuery("#file_manager_upload_progress").ajaxStart(function(){
		jQuery(this).show();
	});
	jQuery("#file_manager_upload_progress").ajaxComplete(function(){
		jQuery(this).hide();
	});
},

show_file_manager: function(_event) {
	var t = "File Attach";
	var a = "#TB_inline?height=320&width=496&inlineId=file_manager&modal=true";
	var g = false;
	tb_show(t,a,g);
	_event.target.blur();
	return false;
},

hide_file_manager: function(Wizard) {
	try {
		Wizard.do_cancel();
	} catch(e) {
	}
},

show_upload_field: function() {
	document.getElementById(this.ids.show_upload_button_wrap).style.display = "none";
	document.getElementById(this.ids.upload_field_wrap).style.display = "block";
},

hide_upload_field: function() {
	document.getElementById(this.ids.show_upload_button_wrap).style.display = "block";
	document.getElementById(this.ids.upload_field_wrap).style.display = "none";
},

makeup_list: function() {
	this.make_file_manager_list();
	this.make_page_file_list();
},
make_file_manager_list: function() {
	// userd global variable "__attachments"
	document.getElementById(this.ids.attach_list).innerHTML = "";

	var html = "";
	for(i=0; i<__attachments.length; i++) {
		html += this.list_create(__attachments[i], i);
	}
	document.getElementById(this.ids.attach_list).innerHTML = html;
	this.write_count();
},
make_page_file_list: function() {
	// content_renderer.js
	// used global variable "__attachments"
	A_RENDER_CONTENT("attachments_list", AttachmentListPane, {
		attachments: __attachments
	});
	CONTENT_RENDERER();
},

file_create_success: function(attachment) {
	this.attachment_create_array(attachment);
	this.makeup_list();
},

file_destroy_success: function(file_name) {
	this.attachment_destroy_array(file_name);
	this.makeup_list();
},




//File upload & delete
file_control_create: function() {
	var target = "#" + this.ids.attach_list;
	var fileElement = jQuery("#" + this.ids.upload_field);

	var file_value = jQuery(fileElement).val();
	var term = (jQuery.os.Windows)? "\\" : "/";
	var file_name = file_value.split(term).pop();
	try {
		file_name = file_name.replace(/[\s]/g, "_");
	}
	catch(m) {
		alert(m.message);
	}

	var pure_file_name = file_name.replace(/.\w+?$/, "");

	var shout = new Shout();
	if(SynkiHelper.vaildate_name(pure_file_name)){
		jQuery.log(pure_file_name);
		shout.error({msg:MSG[277]});

		return false;
	}

	this.show_doing();

	if(tom_and_jerry){
		if(jQuery.browser.msie) return false;
		w_store.file_upload(object, page.name);
		jQuery(target).prepend(this.list_create(file_name));
		return false;
	}
	else{
		jQuery(".upload_progress")
		.ajaxStart(function(){
			jQuery(this).show();
		})
		.ajaxComplete(function(){
			jQuery(this).hide();
		});

		jQuery.ajaxFileUpload
		(
			{
				url: this.ub.attachment_path({ workspace_id: __page.workspace_id, page_id: __page.id}, true),
				data: file_name,
//				secureuri:false,
				dataType: "xml",
				fileElement:fileElement,
				success: function (data, status, responseText){
					// sorry about below exception handling on "success callback"
					// but this shit lib works weird.
					// permission error
					var _p = /Permission.Denied/g
					if (_p.test(responseText)) {
						shout.error({msg:MSG[275]});
						return false;
					}
					// disk full error
					var _p = /out.of.disk/g
					if (_p.test(responseText)) {
						shout.error({msg:MSG[276]});
						return false;
					}

					var attach_obj = {
						"workspace_id": __page.workspace_id,
						"page_id": __page.id,
						"file_name": file_name,
						"user_name": "",
						"file_size": "",
						"created_at": "",
						"new_file": true
					}
					SynkiPlugin_Attachment.file_create_success(attach_obj);
				},
				error: function (data, status, e){
//					jQuery.log(data);
//					jQuery.log(status);
//					jQuery.log(e);

//					jQuery.log(data + ", " + e)
					shout.error({msg:"'" + data + "'" +  MSG[540] + " " + e});
				},
				complete:function(data){
					SynkiPlugin_Attachment.hide_doing();

//					jQuery.log(this);
//					jQuery.log(data);
				}
			}
		)

		return false;
	}
},
file_control_destroy: function(file_name) {
	var shout = new Shout();
	var url = this.ub.attachment_path({ workspace_id: __page.workspace_id, page_id: __page.id, file_name:file_name}, false);

	shout.confirm({msg:MSG[146]}, function(){
		SynkiPlugin_Attachment.show_doing();
		// jQuery(obj).removeClass("attach_selected");
		// jQuery(obj).addClass("delete_attach");
		jQuery.ajax({
			type: "POST",
			url: url,
			data: "_method=delete",
			file_name: file_name,
			error: function() {
				jQuery(obj).removeClass("delete_attach");
				shout.error({msg:MSG[303]});
			},
			success: function(data) {
				var _p = /Permission.Denied/g
				if (_p.test(data)) {
					shout.alert({msg:MSG[226]});
					return false;
				}
				SynkiPlugin_Attachment.hide_doing();
				SynkiPlugin_Attachment.file_destroy_success(file_name);
			}
		});
	});

	return false;
},



// progress controler
show_doing: function() {
	document.getElementById(this.ids.doing).style.display = "inline";
},
hide_doing: function() {
	document.getElementById(this.ids.doing).style.display = "none";
},



//File Manager List Window
file_replace_regExp: new RegExp("\\.|\\[|\\]", "mgi"),
list_create: function(attachment, _index) {
	var workspace_id = attachment.workspace_id;
	var page_id = attachment.page_id;
	var file_name = attachment.file_name;
	var user_name = attachment.user_name;
	var file_size = attachment.file_size;
	var created_at = attachment.created_at;
	var new_file = attachment.new_file;

	var attach_id = file_name.replace(this.file_replace_regExp, "_");

	var li = "<li id='attach_list_" + attach_id + "'>";
	if(new_file) {
		li += "<div style='float:left;margin-right:5px' class='check_img'><input type='checkbox' name='" + this.ids.checked_name + "' value='" + _index + "' checked='checked'/></div>";
	}
	else {
		li += "<div style='float:left;margin-right:5px' class='check_img'><input type='checkbox' name='" + this.ids.checked_name + "' value='" + _index + "' /></div>";
	}
	li += "<div class='file_manager_filename'>" + file_name + "</div>";
//	li += "<div onclick=\"attachment.preview('" + page_name + "', '" + file_name + "', '" + user_name + "', '" + file_size + "', '" + created_at + "');\" ondblclick=\"attachment.selected_check(this.parentNode, '" + page_name + "')\" style='float:left;' class='file_manager_file_list'>" + file_name + "</div>";
	li += "<div class='file_manager_del_button' onclick=\"SynkiPlugin_Attachment.file_control_destroy('" + file_name + "')\"><img src='/images/delete.gif'></div></li>";

	return li;
//	document.getElementById(this.ids.attach_list).innerHTML += li;
},
list_destroy: function(file_name) {
	var attach_id = file_name.replace(this.file_replace_regExp, "_");
	jQuery("#attach_list_" + attach_id).remove();
},
write_count: function() {
	document.getElementById(this.ids.attach_count).innerHTML = __attachments.length;
},


//Buttons Action
insert_image: function(Wizard) {
	var html = this.checked_list("image");
	if (html) {
		Wizard.do_ok("insert_html", html);
	}
},
insert_link: function(Wizard) {
	var html = this.checked_list("link");
	if (html) {
		Wizard.do_ok("insert_html", html);
	}
},
checked_list: function(type) {
	var shout = new Shout();

	var checkeds = document.getElementsByName(this.ids.checked_name);
	var html = "";

	for (i=0; i<checkeds.length; i++) {
		var _element = checkeds[i];
		if (_element.checked) {
			var file_name = __attachments[_element.value].file_name;
			var _link = this.ub.attachment_path({ workspace_id: __page.workspace_id, page_id: __page.id, file_name: file_name}, false);
			if (type == "image") {
				if(!SynkiHelper.is_image(file_name)){
					shout.error({msg:file_name + " " + MSG[283]});
					break;
				}
				html += "<img src='" + _link + "' /><br />";
			}
			else if (type == "link"){
				html += "<a href=" + _link + ">" + file_name + "</a><br />";
			}
		}
	}

	return html;
},


//Attachment Array Controler
attachment_create_array: function(attachment) {
	__attachments.splice(0, 0, attachment);
},
attachment_destroy_array: function(file_name) {
	for(var i=0; i<__attachments.length; i++) {
		if(__attachments[i].file_name == file_name) {
			var _index = i;
			break;
		}
	}
	__attachments.splice(_index, 1);
}

};

//SynkiPageEditor.register_plugin(SynkiPlugin_Attachment);
