var $_hi;
var $_formLock=false;
var $_changeLock=false;

//提示信息
jQuery.extend({
	//提示框
	hint: function($tx, $tm, $url, $bc, $bw, $ic, $ib, $wi, $pd) {
		if(typeof($tx) == "object") {	//数组，通过BR变成字符串
			var $txN = $tx[0];
			for(var $i=1; $i<$tx.length; $i++) {
				$txN += "<br/>" + $tx[$i];
			}
			$tx = $txN;
		}
		
	
		if(!$tm) $tm = 3;		//超时时间(秒)
		if(!$bc) $bc = '#06c';	//边框颜色
		if(!$bw) $bw = 3;		//边框宽度
		if(!$ic) $ic = '#fff';	//内容颜色
		if(!$ib) $ib = '#77a9e0';	//内容背景
		if(!$wi) $wi = 300 ;	//宽度
		if(!$pd) $pd = '20px';	//内间距

		//样式
		var $st = new Object();

		$st.fontWeight = 'bold';
		$st.color = $ic;
		$st.backgroundColor = $ib;

		$st.textAlign = "center";
		$st.border = $bw+"px "+"double "+$bc;
		$st.fontSize = "16px";
		$st.padding = $pd;
		$st.position = 'absolute';
		$st.zIndex = '999999999';
		$st.width = $wi+'px';

		//生成DIV标签
		$.removeHintFrame();
		var $div = document.createElement("div");
		$div.id = 'myHint';
		$div.innerHTML = $tx;
		for($k in $st) {
			$div.style[$k] = $st[$k];
		}

		document.body.appendChild($div);
		$div.style.left = (document.documentElement.clientWidth - $div.offsetWidth) / 2 + document.body.scrollLeft + document.documentElement.scrollLeft + 'px';
		$div.style.top = (document.documentElement.clientHeight - $div.offsetHeight) / 2 + document.body.scrollTop + document.documentElement.scrollTop + 'px';


		try {	//清除之前的定时器	
			clearTimeout($_hi);
		}catch (e){}

		if($url !=null) {
			$_hi = setTimeout("$.removeHintFrame('"+$url+"')",$tm*1000);
		}
		else {	
			$_hi = setTimeout('$.removeHintFrame()',$tm*1000);
		}
	},

	cookie : function(name, value, options) {
		if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
		value = '';
		options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
		var date;
		if (typeof options.expires == 'number') {
		date = new Date();
		date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		} else {
		date = options.expires;
		}
		expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		var path = options.path ? '; path=' + options.path : '';
		var domain = options.domain ? '; domain=' + options.domain : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
		var cookie = jQuery.trim(cookies[i]);
		// Does this cookie string begin with the name we want?
		if (cookie.substring(0, name.length + 1) == (name + '=')) {
		cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
		break;
		}
		}
		}
		return cookieValue;
		}
	},
		
	//移除提示框
	removeHintFrame : function ($goTo) {
		var $_hint = document.getElementById('myHint')
		if($_hi && $_hint) {	
			$_hint.parentNode.removeChild($_hint);
		}

		if($goTo) {
			jQuery.location($goTo);
		}
	},
	
	//重新定向URL(带Referer，支持部分浏览器)
	location : function($url) {
		$("body").append("<a id='virtual_element_location' href='"+$url+"' style='display:none'>url</a>");
		$("#virtual_element_location")[0].click ? $("#virtual_element_location")[0].click() : location.href=$url; 
	},
	
	//引用JS
	require : function($file, $cache, $func) {
		$.ajax({dataType:"script", url:$file, cache:$cache, success:$func});
	},
	
	//引入CSS
	include : function($file) {
		$.ajax({cache:true, dataType:"text", url:$file, success:function($style){
			var $html = "<style>"+$style+"</style>";
			$("head").append($html);
		}})
	},

	//Ajax通用回调函数(JSON)
	cbAjax : function($result) {
		if($result.success) {
			$success = $result.info ? $result.info : "恭喜您，操作成功！"
			$url = $result.url ? $result.url : location.href;
			$.hint($success, 1, $url);
		} else {
			$.hint($result.info);
		}
	},


	//将数字变为字符串
	implode : function($str, $arr) {
		var $result = "";
		for(var $k in $arr) {
			$result += $arr[$k] + $str;
		}

		return $result.substr(0, $result.length - $str.length);
	},

	//将一个元素移至中心位置
	goCenter : function(expression) {
		$(expression).css("position", "absolute");
		$(expression).css("zIndex", "50");

		$(expression).css("left", (document.documentElement.clientWidth - $(expression).width()) / 2 + document.body.scrollLeft + document.documentElement.scrollLeft + 'px');
		$(expression).css("top", (document.documentElement.clientHeight - $(expression).height()) / 2 + document.body.scrollTop + document.documentElement.scrollTop + 'px');

		if(parseInt($(expression).css("top")) < 0) $(expression).css("top", 0);	//超出高度
		if(parseInt($(expression).css("left")) < 0) $(expression).css("left", 0);	//超出宽度

		$(expression).fadeIn("slow");
	},

	//将带有编辑器的表单移至中心位置
	goCenterForm : function(expression, editorId) {
		$(expression).show();	
		
		if($("#"+editorId).css("display") != "none") {	//显示编辑器
			KE.create(editorId);
		}
		
		$(expression).hide();
		$.goCenter(expression);
	},

	//取得滚动条上边及左边的距率
	scroll : function() {
		var $result = new Object();
		$result.top = this._scroll_top();
		$result.left = this._scroll_left();
		return $result;
	},

	//取得滚动条距上边多少
	_scroll_top : function(){
		var scrollPos;
		if (typeof window.pageYOffset != 'undefined') {
			 scrollPos = window.pageYOffset;
		 }
		else if (typeof document.compatMode != 'undefined' &&
			 document.compatMode != 'BackCompat') {
			 scrollPos = document.documentElement.scrollTop;
		 }
		else if (typeof document.body != 'undefined') {
			 scrollPos = document.body.scrollTop;
		 }
		return scrollPos;
	},

	//取得滚动条距左边多少
	_scroll_left : function(){
		var scrollPos;
		if (typeof window.pageXOffset != 'undefined') {
			 scrollPos = window.pageXOffset;
		 }
		else if (typeof document.compatMode != 'undefined' &&
			 document.compatMode != 'BackCompat') {
			 scrollPos = document.documentElement.scrollLeft;
		 }
		else if (typeof document.body != 'undefined') {
			 scrollPos = document.body.scrollLeft;
		 }
		return scrollPos;
	},

	goAjax : function(obj, $success) {	//表单submit触发AJAX提交方法
		var $data = $(obj).serialize();
		var $url = $(obj).attr("action");
		$success = $success ? $success : $.cbAjax;
		$.ajax({type:"post", dataType:"json", data:$data, url:$url, success:$success});
		return false;
	}
});

jQuery.fn.extend({
	goCenter : function() {	//将一个元素放至中心
		$.goCenter(this);
		return this;
	},

	imgSize : function($width, $height) {	//设置图片的最宽与最高，自动缩放
		function setImgSize(){
			if($(this).width() > $width) {
				$(this).css("width", $width+"px").css("height", "auto");
			}

			if($(this).height() > $height) {
				$(this).css("height", $height+"px").css("width", "auto");
			}

		}

		var $obj;
		if($(this).attr('tagName') == 'IMG') {
			$obj = $(this);
		} else {
			$obj = $(this).find("img");
		}

		$obj.each(setImgSize).load(setImgSize);
		return this;
	}
});

//删除编辑器的值
function clearEditor(id) {
	 KE.g[id].iframeDoc.open();
	 KE.g[id].iframeDoc.write(KE.util.getFullHtml(id));
	 KE.g[id].iframeDoc.close();
	 KE.g[id].newTextarea.value = '';
}

//插入编辑器的值
function insertHtml(id, html) {
	_id = id;
	_html = html;
	setTimeout("_insterHTML(_id, _html)", 1);
}

function _insterHTML(id, html) {
	 KE.util.focus(id);
	 KE.util.selection(id);
	 KE.util.insertHtml(id, html);
}

//切换标签
function showTab($id) {
	$("[id*='tab_']").removeClass("select select02");
	$("#tab_"+$id).addClass(!$("#tab_"+$id).children("a").html() || $("#tab_"+$id).children("a").html().length > 3 ? "select" : "select02");

	$("div[id*='frame_']").removeClass("selectcontent").addClass("hiddencontent").hide();
	$("#frame_"+$id).removeClass("hiddencontent").addClass("selectcontent").show();
}

var $base = $("base").attr("href");
var $href = location.protocol + "//" + location.hostname + location.pathname + location.search;

$(document).ready(function(){	
	//自动加入图片的Title
	$("img").each(function(){
		$(this).attr("title", $(this).attr("alt"));
	});
	
	//PNG透明
	if($("script[src*='js/global.js']:last").attr("src").indexOf("png=false") < 0) {
		$.require("js/jquery.pngFix.js", true, function(){
			$("body").pngFix();
		});
	}

	//相对路径的锚点，全部相对于当前URL
	if($("script[src*='js/global.js']:last").attr("src").indexOf("href=false") < 0) {
		$("a[href^='#']").each(function(){
			$(this).attr("href", $href + $(this).attr("href"));
		});
	}
});