//现实保存页面提示窗口
PPS.ShowTootipSavePages = function() {
	document.getElementById('pagessetting_savepages').style.display = "block";
};

//隐藏保存页面的提示窗口
PPS.HiddeTootipSavePages = function() {
	document.getElementById('pagessetting_savepages').style.display = "none";
};

//保存当前用户的设置
function SavePagesSetting() {

var json_plugins = "[";
var content = document.getElementById("Wrapper");

//首先查询框架
	for(var i=0;i<content.childNodes.length;i++) {
		if( content.childNodes[i].tagName != "DIV" )
			continue;
			
		var frameid = content.childNodes[i].id;
		
		for(var j=0;j<content.childNodes[i].childNodes.length;j++) {
			if( content.childNodes[i].childNodes[j].tagName == "DIV" && content.childNodes[i].childNodes[j].className.indexOf("dragWindow") > -1 ) {
				
				plugins = content.childNodes[i].childNodes[j];

				if( plugins.id == "" )
					continue;
					
				var module = PPS.DragPanel.getModuleById(plugins.id);
				json_plugins += "{";
				
				json_plugins += "\"id\":\""+module.id+"\",";
				
				json_plugins += "\"frame\":\""+content.childNodes[i].id+"\", ";
				
				json_plugins += "\"title\":\""+encodeURIComponent($('#'+module.id+'_Header_Title').html())+"\", ";
				
				json_plugins += "\"plugins\":\""+module.plugins+"\", ";
				
				json_plugins += "\"config\":\"" + module.config+"\"";
				
				json_plugins += "},";
			}
		}
	}
	
	
	//判断最后一个字符，是否为,字符
	if( json_plugins.substring(json_plugins.length-1, json_plugins.length) == "," )
		json_plugins = json_plugins.substring(0, json_plugins.length-1);
	
	json_plugins += "]";
	
	//把当前的修改，保存到数据库
	postto("", "p=pagessetting&act=savepagessetting&pagesetting=" + json_plugins);
	//隐藏保存提示的窗口
  PPS.HiddeTootipSavePages();
};

function getParams() {
	return "?timeStamp="+new Date().getTime()+"&";
}

var loading;
//get
function ajaxget(_id, _url, callback) {
	loadIn();
	$("#" + _id).ajaxStart( function() {
	});
	if(_id.trim() != "") {
			$.ajax({type:"GET", url:(_url), success: function(msg){
			var d = resulthtml(msg);
			if(d) {				
				$("#"+_id).html(msg).evalScripts();
			}
			if(typeof callback != "undefined")
				callback(msg, d);
		}});
	}	else {
		$.ajax({type:"GET", url:(_url), success: function(msg){
			if( resulthtml(msg) ) {
				if(typeof callback != "undefined")
					callback(msg);
					
				/*if(_id.trim() != "")
					document.getElementById(_id).innerHTML = msg;*/
			}
		}});
	}
	$("#" + _id).ajaxStop( function() {
		loadOut();
	});
}
function resulthtml(msg, callback) {
	if(typeof(msg) == "undefined" && msg == "")
		return true;
	
	msg = msg.trim();
					/*用户没有登陆*/
	if(msg == "1001") {
		PPS.Box.Window('用户登陆', 'act=viewlogin', {ident: 'viewLogin'});
		return false;
	} else if(msg.substring(0,4) == "1002") {
		
		PPS.Box.show("错误提示", msg.substring(5, msg.length));
		return false;
	} else if(msg.substring(0,4) == "1003") {
		PPS.Box.show("消息提示", msg.substring(5, msg.length));
		return true;
	}
	
	return true;
}
function ajaxpost(_id, _url, _data, callback, attribute) {
	loadIn();
	$.ajax({
		type: "POST",
		url: (_url),
		data: (_data),
		success: function(msg){
			if(!resulthtml(msg)) {
				if(typeof(attribute) != 'undefined') {
					if(typeof(attribute.errorcallback) != 'undefined') {
						attribute.errorcallback(msg);
					}
				}
				return;
			}
			if( typeof(callback) == "undefined" )
				document.getElementById(_id).innerHTML = msg;
			else
				callback(msg);
				
				loadOut();
	   }
	});
}

/*
divid 加载的Div ID
data 参数 例如: p=4&act=link
callback 在读取HTML 以后所要执行的方法.
evalScript 重新写 GOTO方法
*/
function GOTO(divid, data, callback, evalScript) {
	if ( typeof(evalScript) != undefined ) {
		if( data.indexOf('.php') != -1 )
			ajaxget(divid, data+'&'+getParams(), callback);
		else
			ajaxget(divid, '/include.php'+getParams()+data, callback);
	} else {
		evalScript(divid, data, callback);
	}
}



function GOTO_CAIJI(divid, data, callback) {
	
		ajaxget(divid, '/split/dianping/getstoreinfo.php'+getParams()+data, callback);
	
}

function postto(divid, data, callback, attribute) {
	ajaxpost(divid, '/include.php', 'timeStamp='+new Date().getTime()+"&"+data, callback, attribute);
}

function AddModuleToPages(msg) {
	
	var Module_arr = eval(msg);
	var Module = Module_arr[0];
	
	if($('#'+Module.id).size() != 0) {
		PPS.Box.show('错误提示', '您是否点的太快，出现了重复的ID，请稍等1秒后在添加');
		return;
	}
	
	if( !PPS.DragPanel.CreatePanel("", Module) )
		return;

	//加载这个插件的内容
	GOTO(Module.id+'_Content', "p=" + Module.plugins + "&pluginsid="+Module.id + "&" + Module.config);
	
	//重新注册拖动组状态
	PPS.DragPanel.InitDrag();
	PPS.ShowTootipSavePages();
}

//从当前页面中删除一个Plugins
function DeletePlugins(divid) {
	PPS.Box.confirm('删除应用', '您确定要关闭这个窗口吗?\n 关闭后不可回复!', function(e) {
		if( e == 'yes' ) {
				var module = document.getElementById(divid);
				module.parentNode.removeChild(module);
				PPS.ShowTootipSavePages();
		}
	});
}

//通过传递 参数arguments 把Form Data 实现对象
function getFormData(divid, pdata, formObj, callback, attribute) {
	var data = "";
	for(var i=0;i<formObj.elements.length;i++) {
		var val = getInputValue(formObj.elements[i]);
		
		if( val == null )
			continue;

		if(formObj.elements[i].tagName != "SELECT") {
			data += formObj.elements[i].name;
			data += "=";
			
			if( formObj.elements[i].name.indexOf('FCKeditor') == -1 )
				data += encodeURIComponent(val);
			else {
				data += encodeURIComponent(FCKeditorAPI.GetInstance(formObj.elements[i].name).GetXHTML( true ));
			}
		} else {
			data += val;
		}
		if(i != (formObj.elements.length-1) ) {
			data += "&";
		}
	}

	//判断最后一个字符，是否为&字符
	if( data.substring(data.length-1, data.length) == "&" )
		data = data.substring(0, data.length-1);

		//alert(data);
	postto(divid, pdata+"&"+data, callback, attribute);
	return false;
}

function getInputValue(input) {

	if( input.type == "text" ) return input.value;
	if( input.type == "hidden" ) return input.value;
	if( input.type == "password" ) return input.value;
	if( input.type == "submit" ) return null;

	if( input.type == "checkbox" ) {
		if( input.checked )
			return input.value;
		else
			return null;
	}

	if( input.type == "radio" ) {
		if( input.checked )
			return input.value;
		else
			return null;
	}
	
	if( input.tagName == "SELECT") {
		var tmpvalue = "";
		//alert(input.value);
		for(var i=0;i<input.options.length;i++) {
			if(input.options[i].selected) {
				if(input.multiple == "multiple")
					tmpvalue += "&"+input.name+"[]="+input.options[i].value;
				else
					tmpvalue += "&"+input.name+"="+input.options[i].value;
				//tmpvalue += "&";
			}
		}
		
		return tmpvalue;
	}
	
	if(input.tagName == "TEXTAREA") {
		return input.value;
	}

}

//修改窗口标题;
function setDragWindowTitle(inobj, titleobj) {
	if( inobj.value.trim() =="" )
		return;

	$('#'+titleobj+'_Header_Title').html(inobj.value);
	PPS.ShowTootipSavePages();		
}
/*window.onerror = function() {
	alert("在您操作的过程中发生了Javascript错误\n请重新选择，如果还没有解决问题，请按F5刷新当前页面\n行: " + arguments[2]+"\n详情:" + arguments[0]);
	
	return true;
};*/
function selectTag(objli, pluginsid) {
	var ulobj = document.getElementById(pluginsid);
	for(var i=0;i<ulobj.childNodes.length;i++) {
		if( i==objli )
			ulobj.childNodes[i].className = "selectTag";
		else
			ulobj.childNodes[i].className = "";
	}
}

String.prototype.trim = function()  
{  
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}

//通过plugins返回对象 plugins
function getPlugins(pluginsid) {
	alert('当前方法已经不推荐使用，请更改参见API.更新插件');
	return {
		pluginsid: pluginsid,
		plugins: document.getElementById(pluginsid+"_plugins") ? document.getElementById(pluginsid+"_plugins").innerHTML : "",
		title: document.getElementById(pluginsid+"_title") ? document.getElementById(pluginsid+"_title").innerHTML : ""
	};	
}
//loading..
var isload = false;
function loadIn() {
	//if( isload )
		//return;
		
	//isload = true;
	//loading.show();
}

function loadOut() {
	//isload = false;
	//loading.hide();
}

function feedOpen(content) {
var _content = document.getElementById(content);
if( typeof _content == null )
	return false;
	
if( _content.style.display == "none") {
	_content.parentNode.className = "feedselected";
	_content.style.display = "block";
} else {
	_content.style.display = "none";
	_content.parentNode.className = "";
}
return false;
}

function OpenBox(pluginsid, title, _url, callback) {
	alert('当前方法已经更换新的API，请查看API，进行插件的更新.');
var createBox = function() {
	return 
		('<div class="msg" id="'+ pluginsid + '_MessageBox">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><div style="float:right"><a href="#">关闭</a></div><h3>', title, '</h3><div class="x-box-content"></div></div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>');
};

	$.html(createBox);
}

$(document).ready(function() {
	loading = $('#loading');
});