

// デバッグダイアログ用
function showDialogDebugData( Id ) {
	$("#" + Id).slideToggle("slow");
}


// テーマのドリルダウンメニュー
function ThemeSelectInit() {
	for( var i = 0; i < ThemeList.length; i++ ) {
		var theme = ThemeList[i];
		$("#theme_id").append('<option value="' + theme["id"] + '" ' + theme["selected"] + '>' + theme["name"] + '</option>');
	}
	ThemeSelectProc();
}
function ThemeSelectProc() {
	var selected_theme_id = $("#theme_id option:selected").val();
	$("#subtheme_id").empty();
	for( var i = 0; i < SubThemeList.length; i++ ) {
		var subtheme = SubThemeList[i];
		if( subtheme["theme_id"] != selected_theme_id ) continue;
		$("#subtheme_id").append('<option value="' + subtheme["id"] + '" ' + subtheme["selected"] + '>' + subtheme["name"] + '</option>');
	}
}



// カラー設定のデモ
function configColorSet( code, color ) {
	$("#color_" + code).val( color );
	configColorPaletUpdate( code );
}
function configColorPaletUpdate( code ) {
	var color = $("#color_" + code).val();
	if( color.length != 7 ) return;
	$("#palet_" + code).css('background-color', color);
}



(function($){
	
	/**
	 * @name creyle
	 * @namespace */ 
	var creyle = window.creyle||{};
	window.creyle = creyle;
	/**
	 * @name creyle.plugin
	 * @namespace */ 
	creyle.plugin = creyle.plugin||{};
	/**
	 * @class
	 * @constructor
	 * @param {String} entryId
	 * @param {String} blockId
	 */
	creyle.plugin.EntryBlock = function(entryId,blockId) {
		this.entryId = entryId;
		this.blockId = blockId;
		this.element = $("#E_plugin_"+blockId);
		this.pluginCode = this.element.attr("alt");
	}
	/**
	 * @namespace
	 * @static */
	creyle.plugin.EntryBlock.Message = {
		CONFIRM_DELETE : "本当に削除しますか？",
		DELETING : "削除しています",
		DELETE_DONE : "削除しました",
		DELETE_FAIL : "削除に失敗しました",
		SAVING : "保存しています",
		SAVE_DONE : "保存しました",
		SAVE_FAIL : "保存に失敗しました"
	}
	
	/** @class */
	creyle.plugin.EntryBlock.prototype = {
		save : function(data,callbackSuccess,callbackError) {
			var msg = creyle.plugin.EntryBlock.Message;
			var ele = this.element;
			this._request("save",data,
				function(success) {
					ele.showStatus(msg.SAVE_DONE);
					if($.isFunction(callbackSuccess)) callbackSuccess.apply(ele);
				},
				function(success) {
					alert(msg.SAVE_FAIL);
					if($.isFunction(callbackError)) callbackError.apply(ele);
				}
			);
		},
		remove : function(callbackSuccess,callbackError,noconfirm) {
			var msg = creyle.plugin.EntryBlock.Message;
			var ele = this.element;
			if(!noconfirm&&!confirm(msg.CONFIRM_DELETE)) return;
			ele.showStatus(msg.DELETING);	
			this._request("delete",null,
				function(success) {
					ele.showStatus(msg.DELETE_DONE);
					ele.deleteBlock();
					if($.isFunction(callbackSuccess)) callbackSuccess.apply(ele);
				},
				function(success) {
					alert(msg.DELETE_FAIL);
					if($.isFunction(callbackError)) callbackError.apply(ele);
				}
			);
		},
		cancel : function() {
			var msg = creyle.plugin.EntryBlock.Message;
			var ele = this.element;
			ele.closeEditor();
		},
		_request : function(method,data,callbackSuccess,callbackError) {
			if(!/^(save|delete)$/.test(method)) return false;
			var ele = this.element;
			var requrl = ["/entry",this.entryId,this.blockId,"appli/plugin",this.pluginCode,method,""].join("/");
			$.ajax({
				type : "POST", url : requrl, data : data||{}, dataType : "json",
				success : function(s) {
					if( s && $.isFunction(callbackSuccess)) callbackSuccess.apply(ele,[data]);
					if(!s && $.isFunction(callbackError)) callbackError.apply(ele,[data]);
					ele.closeEditor();
				}
			});
		}
	}
	
	/**
	 * @class
	 * @constructor
	 * @param {String} pluginCode
	 * @param {Boolean} isSide
	 */
	creyle.plugin.MediaBlock = function(pluginCode,isSide) {
		this.pluginCode = pluginCode;
		this.isSide = !!isSide;
		this.element = $(["#A",isSide?"side":"top","block",pluginCode].join("_"));
	}
	
	
	/** @class */
	creyle.plugin.MediaBlock.prototype = {
		save : function(data,callbackSuccess,callbackError) {
			var ele = this.element;
			data = data||this.getValue();
			this._request("save",data,
				function(success) {
					if($.isFunction(callbackSuccess)) callbackSuccess.apply(ele,[data]);
					ele.removeClass("edit");
				},
				function(success) {
					if($.isFunction(callbackError)) callbackError.apply(ele,[data]);
					ele.removeClass("edit");
				}
			);
		},
		
		getValue : function() {
			var obj = {};
			$("form *[name]",this.element).each(function(){
				var $this = $(this);
				obj[$this.attr("name")] = $this.val();
			});
			return obj;
		},
		setValue : function(obj) {
			$.each(obj,function(k){
				$("form *[name='"+k+"']",this.element).val(obj[k]);
			});
		},
		edit : function() {
			var ele = this.element;
			ele.data("pData",this.getValue());
			ele.addClass("edit");
		},
		cancel : function() {
			var ele = this.element;
			this.setValue(ele.data("pData")||{});
			ele.removeClass("edit");
		},
		_request : function(method,data,callbackSuccess,callbackError) {
			if(!/^(save|delete)$/.test(method)) return false;
			var ele = this.element;
			var requrl = ["/appli/plugin",this.pluginCode,method,""].join("/");
			$.ajax({
				type : "POST", url : requrl, data : data||{}, dataType : "json",
				success : function(s) {
					if( s && $.isFunction(callbackSuccess)) callbackSuccess.apply(ele);
					if(!s && $.isFunction(callbackError)) callbackError.apply(ele);
				}
			});
		}
	}
	
	$.fn.getPluginInstance = function(entryId) {
		var id = this.attr("id")||"";
		if(typeof entryId == "string") {
			var blockId = id.replace(/^E_plugin_/,"");
			if(!blockId||!entryId||!this.size()) return null;
			return new creyle.plugin.EntryBlock(entryId,blockId);
		} else if(this.is(".plugin-body")) {
			return new creyle.plugin.MediaBlock(id.split("_").pop(),/^A_side_block_.+$/.test(id));
		} else {
			return null;
		}
	}

	$.fn.cancelEditing = function(entryId) {
		var ele = this;
		var b = this.getPluginInstance(entryId);
		if(!b) return false;
		b.cancel();
		return true;
	}

	$.fn.deletePluginData = function(entryId,callbackSuccess,callbackError,noconfirm) {
		var ele = this;
		var b = this.getPluginInstance(entryId);
		if(!b) return false;
		b.remove(callbackSuccess,callbackError,noconfirm);
		return true;
	}

	$.fn.savePluginData = function(entryId,data,callbackSuccess,callbackError) {
		var ele = this;
		var b = this.getPluginInstance(entryId);
		if(!b) return false;
		b.save(data,callbackSuccess,callbackError);
		return true;
	} 

	$.fn.enterEditMode = function() {
		var b = this.getPluginInstance();
		b.edit();
		return true;
	}

})(jQuery);
