﻿/**
 *	2008/10/2 重写
 *	2008-10-15 修正:	options
 *						selectEditor
 * 
 *	css: 
		inplace_editor_field
		inplace_editor_field_select
		inplace_editor_button
		inplace_editor_error_div
		inplace_editor_quick_alert
	<style >
	.inplace_editor_field{
		width: 100px;
	}
	.inplace_editor_button{

	}
	</style>
 *
 */

/*
 *	obj				:	dom object
 *	paramName		:	需要更新的参数名称	
 *	url				:	提交的地址 ( 不包括参数名称 )
 *	notNull			:	能否为空 (true | false)
 *	submitOnBlur	:	失去焦点即提交 (true | false)
 *	onSuccess		:	提交后执行的方法 (function)
 *
 */
function InPlaceEditor(options) {
	var editor = new Object();
	editor.element = options.obj;
	editor.paramName = options.paramName;
	editor.url = options.url;
	editor.notNull = options.notNull;
	editor.submitOnBlur = options.submitOnBlur;
	editor.onSuccess = options.onSuccess;
	editor.data = null;
	editor.value = null;
	editor.ctx = options.ctx;
	var o = new InPlaceEditor.fn.init( editor );
	InPlaceEditor.fn.createField(o);
	//InPlaceEditor.fn.init( editor )
		//.createField();
}

/*
 *	obj				:	dom object
 *	paramName		:	需要更新的参数名称	
 *	url				:	提交的地址 ( 不包括参数名称 )
 *	data			:	下拉列表的参数 ( value: text )
 *	value			:	原始值
 *	notNull			:	能否为空 (true | false)
 *	submitOnBlur	:	失去焦点即提交 (true | false)
 *	onSuccess		:	提交后执行的方法 (function)
 *
 
	var data = [{"value":"1", "text":"测试1"}, 
				{"value":"2", "text":"测试2"}, 
				{"value":"3", "text":"编辑1"}, 
				{"value":"4", "text":"编辑2"}];
 */
function InPlaceEditorSelect( options ) {
	var editor = new Object();
	editor.element = options.obj;
	editor.paramName = options.paramName;
	editor.url = options.url;
	editor.notNull = options.notNull;
	editor.submitOnBlur = options.submitOnBlur;
	editor.onSuccess = options.onSuccess;
	editor.data = options.data;
	editor.value = options.value;
	var v = $(editor.element).attr("selectedValue");
	if (v)
	{
		editor.value = v;
	}
	editor.ctx = options.ctx;
	var o = new InPlaceEditor.fn.init( editor );
	InPlaceEditor.fn.createField(o);
	//InPlaceEditor.fn.init( editor )
		//.createField();
}

InPlaceEditor.fn = {
	init: function( editor ) {
		this.element = editor.element;
		this.paramName = editor.paramName;
		this.url = editor.url;
		this.notNull = editor.notNull;
		this.submitOnBlur = editor.submitOnBlur;
		this.onSuccess = editor.onSuccess;
		this.data = editor.data;
		this.value = editor.value;
		// change dudd
		this.ctx = editor.ctx || "/";
		//this.ctx = editor.ctx || "/soe";
		this.oldValue = $.trim($(this.element).text());

		//this.createField();
		return this;
	},
	createField: function( editor ) {
		$(editor.element).html("");
				
		$("<div></div>").css({ width: "100%", height: "100%", position: "absolute", top: "0", left: "0"}).addClass("inplace_editor_mask")
			//.bind('click', function(){ return false })
			.appendTo($(editor.element));
		if (editor.data){
			var sel = $("<select></select>").attr("name", editor.paramName).addClass("inplace_editor_field_select");
			for(var i = 0; i < editor.data.length; i++) {
				var item = editor.data[i];
				var option = $("<option></option>").attr("value", item.value).attr("text", item.text);
				if (editor.value && editor.value == item.value){
					option.attr("selected", "selected");
				}
				$(sel).append(option);
			}
			sel.appendTo($(editor.element));
		} else {
			$("<input type='text'></input>").attr("name", editor.paramName).attr("value", editor.oldValue)
				.addClass("inplace_editor_field")
				.bind('mouseover', function(){$(this).css({background: "#ffd"})
													 //.select()
													 .focus()})
				.appendTo($(editor.element));
		}
		if (!editor.submitOnBlur){
			$("<input type='image' align='absmiddle'></input>").attr("src", editor.ctx + "/images/default/console/main/xiugai.gif").addClass("inplace_editor_button")
				.bind('click', function(){ InPlaceEditor.fn.onSubmit( editor ) })
				.appendTo($(editor.element));
			$("<input type='image' align='absmiddle'></input>").attr("src", editor.ctx + "/images/default/console/main/fanhui.gif").addClass("inplace_editor_button")
				.bind('click', function(){ InPlaceEditor.fn.cancelSubmit( editor ) })
				.appendTo($(editor.element));
		} else {
			$(editor.element).find('.inplace_editor_field')
				.bind('blur', function(){ InPlaceEditor.fn.onSubmit( editor ) });
		}
		$("<span></span>").attr("name", "error").css({ color: "red", display: "none" }).addClass("inplace_editor_error_div")
			.appendTo($(editor.element));
		$("<span></span>").attr("name", "quick_alert").css({ color: "green", display: "none" }).addClass("inplace_editor_quick_alert")
			.html("saving ...")
			.appendTo($(editor.element));
		$(editor.element).children()
			.bind('dblclick', function(){return false});
		//$(editor.element).find('.inplace_editor_field').select().focus();
		$(editor.element).find("[@name='"+editor.paramName+"']").select().focus();
		
	},
	cancelSubmit: function( editor ) {
		$(editor.element).html(editor.oldValue);
		return;
	},
	onError: function( editor, msg ) {
		$(editor.element).find('.inplace_editor_error_div').html(msg)
						.fadeIn('slow')
						.animate({opacity: 1.0}, 2000)
						.fadeOut('slow', function() {
							//$(this).remove();
						});
	},
	onSubmit: function( editor ) {
		var fieldValue = $(editor.element).find("[@name='"+editor.paramName+"']").val();
		fieldValue = $.trim(fieldValue);
		if (editor.notNull){
			fieldValue = $.trim(fieldValue);
			if (fieldValue == ""){
				this.onError(editor, "不能提交空值");
				return;
			}
		}
		var changed = true;
		if (editor.data) {
			if (fieldValue == editor.value) {
				changed = false;
			}
		} else {
			if (fieldValue == editor.oldValue) {
				changed = false;
			}
		}
		if (changed) {
			$(editor.element).find('.inplace_editor_button').fadeOut('slow');
			var url = editor.url += (editor.url.indexOf("?") == -1 ? "?" : "&") + editor.paramName + "=" + encodeURIComponent(fieldValue);
			this.gadgetRequest( editor );
		} else {
			this.cancelSubmit( editor );
			if(editor.onSuccess){
				$(editor.onSuccess);
			}
			return;
		}

	},
	gadgetRequest: function( editor ) {
		jQuery.ajax({
				type: "GET",
				url: editor.url,
				cache: false,
				beforeSend: function(XMLHttpRequest){
					$(editor.element).find('.inplace_editor_quick_alert')
						.fadeIn('slow')
						.animate({opacity: 1.0}, 3000)
						.fadeOut('slow', function() {
							$(this).remove();
						});
				},
				success:function(ret){
					if (ret.indexOf("<title>异常处理</title>") != -1) {
						//alert("更新数据出错!");
						$(editor.element).html(ret);
						InPlaceEditor.fn.cancelSubmit( editor );
						return;
					}
					if (editor.data){
						$(editor.element).attr("selectedValue", ret);
						for(var i = 0; i < editor.data.length; i++) {
							var item = editor.data[i];
							if (item.value == ret)
							{
								ret = item.text;
							}
						}
					}
					$(editor.element).html(ret);
					if(editor.onSuccess){
						$(editor.onSuccess);
					}
				},
				error:function(){
					alert("更新数据出错! 服务器没有响应");
					InPlaceEditor.fn.cancelSubmit( editor );
				}
			});
	}
};


function gadgetRequest(id, url, success, isAppend){
	jQuery.ajax({
		type: "GET",
		url: url,
		cache: false,
		success: function(data, textStatus){
			if(typeof(data)=="string"){
				if (data != "" && jQuery.trim(data) != "")
				{
					if (isAppend)
						jQuery(data).appendTo(jQuery(id));
					else
						jQuery(id).html(data);
				} else {
					if (!isAppend)
						jQuery(id).empty();
				}
				//jQuery(id).html(data);
			}
			if(jQuery.isFunction(success))
			{
				success();
			}
		},
		error:function(){
			QuickAlert.error("连接超时! 服务器没有响应");
		}
	});
};

function backendRequest(url,success,complete,alertBody){
	var timeout = "";
	jQuery.ajax({
		type: "GET",
		url: url,
		cache: false,
		beforeSend: function(XMLHttpRequest){
				QuickAlert.loading("正在发送请求...", alertBody);
		},
		success: function(data, textStatus){
			var s = false;
			if (data) 
			{
				var text = "操作成功!";
				var r = data.split("|");
				if (r[0] && r[0] == "1") 
				{
					text = r[1] || text;
					QuickAlert.ok(text);
					s = true;
				} 
				else if (r[0] && r[0] == "0")
				{
					text = r[1] || "操作没有成功!";
					QuickAlert.error(text, alertBody);
				}
				else 
				{
					if (data.indexOf('403.htm') != -1)
						QuickAlert.error("无权访问!", alertBody);
					else 
						QuickAlert.error("没有返回状态信息", alertBody);
				}
			}
			else 
			{
				QuickAlert.error("没有返回状态信息", alertBody);
			}
			if (s)
				if(jQuery.isFunction(success))
					success();
		},
		complete: function(request, textStatus){
			if(jQuery.isFunction(complete))
				complete();
		},
		error:function(){
			QuickAlert.error("连接超时! 服务器没有响应", alertBody);
		}
	});
};

function backendPost(_url,_data, success,complete,alertBody){
	var timeout = "";
	jQuery.ajax({
		type: "POST",
		url: _url,
		cache: false,
		data: _data,
		beforeSend: function(XMLHttpRequest){
				QuickAlert.loading("正在发送请求...", alertBody);
		},
		success: function(data, textStatus){
			var s = false;
			if (data) 
			{
				var text = "操作成功!";
				var r = data.split("|");
				if (r[0] && r[0] == "1") 
				{
					text = r[1] || text;
					QuickAlert.ok(text);
					s = true;
				} 
				else if (r[0] && r[0] == "0")
				{
					text = r[1] || "操作没有成功!";
					QuickAlert.error(text, alertBody);
				}
				else 
				{
					QuickAlert.error("没有返回状态信息", alertBody);
				}
			}
			else 
			{
				QuickAlert.error("没有返回状态信息", alertBody);
			}
			if (s)
				if(jQuery.isFunction(success))
					success();
		},
		complete: function(request, textStatus){
			if(jQuery.isFunction(complete))
				complete();
		},
		error:function(){
			QuickAlert.error("连接超时! 服务器没有响应", alertBody);
		}
	});
};


var QuickAlert = {
	alert:function (parentBody){
		return new QuickAlertHandler(parentBody);
	},
	loading: function (text, parentBody){
		this.alert(parentBody).loading(text);
	},
	ok:function (text, parentBody, duration){
		this.alert(parentBody).ok(text, duration);
	},
	error:function (text, parentBody, duration){
		this.alert(parentBody).error(text, duration);
	},
	remove:function (parentBody){
		this.alert(parentBody).remove();
	}
};
QuickAlertHandler = function(parentBody){
	this.id = "QuickAlert_Main";
	this.mainClass = "QuickAlert_Main";
	this.loadingClass = "loading";
	this.status_okClass = "status_ok";
	this.status_errorClass = "status_error";
	this._alert = this.init(parentBody);
};
QuickAlertHandler.prototype.init = function (parentBody){
	var parent;
	if (parentBody == null)
	{	parent = jQuery("body");}
	else
	{	
		parent = jQuery(parentBody);
		parent.addClass("relative");
	}
	
	var alert = parent.find("[name='"+this.id+"']");
	if (alert.length <= 0)
	{
		jQuery("<div></div>").attr("name", this.id)
			.appendTo(parent)
			.wrap("<div class='QuickAlert_Main'></div>");
		alert = parent.find("[name='"+this.id+"']");
	}
	return alert;
};
QuickAlertHandler.prototype.loading = function (text){
	this._alert.addClass(this.loadingClass)
			.html(text);
	this._alert.parent()
			.fadeIn('slow')
			.animate({opacity: 0.5});
};
QuickAlertHandler.prototype.ok = function (text, duration){
	this._alert.addClass(this.status_okClass)
			.html(text);
	this.fadeOut(duration);
};
QuickAlertHandler.prototype.error = function (text, duration){
	this._alert.addClass(this.status_errorClass)
			.html(text);
	this.fadeOut(duration);
};
QuickAlertHandler.prototype.remove = function (){
	this._alert.parent().remove();
};
QuickAlertHandler.prototype.fadeOut = function (duration){
	duration = duration || 1500;
	this._alert.parent()
			//.fadeIn('slow')
			.animate({opacity: 0.5}, duration)
			.fadeOut('slow', function() {
					$(this).remove();
			});
};