/** 
 * no frame, but top_msg need ajax.js
 * @author fire-j_chenwy created on 2010-01-20 at xm.fanzhi 
 * @version 1.1
 * */
/*--FireCookie is base as below--*/
var FireCookie = function(){this.fire = document && document.cookie;}
FireCookie.prototype.get = function(k){
	if(!this.fire) return null; if(!k) return null;
	var cookies = document.cookie.split("; ");
	if(cookies) {
		for(var i = 0; i < cookies.length; i++) {
			var cookie_name = cookies[i].split("=")[0];
			if(cookie_name == k) {
				return cookies[i].split("=")[1];
			}
		}
	}
}

FireCookie.prototype.set = function(k, v, t){
	if(!this.fire) return false;
	var setting = {"name": k || "", "value" : v || null};
	var expireDate = new Date();
	expireDate.setSeconds(expireDate.getSeconds() + t);
	var str = setting.name + "=" + setting.value + "; expires=" + expireDate.toGMTString();
	document.cookie = str;
}

var __fireCookie = null;

/*-TopMsg is message size-*/
var TopMsg = {"live": 90,"interval" : 1000*3*60,"timer" : null,"ajaxObj" : null,"htmlId" : function(){return document.getElementById("userMsgSizeId");}};
function _top_message(uid){
	var k_value = "headMsgSize_" + uid;
	if(!__fireCookie) {
		__fireCookie = new FireCookie();
	}
	var hms = __fireCookie.get(k_value);
	if(hms) {
		var html = "消息(" + hms + ")";
		TopMsg.htmlId().innerHTML = html;
	} else {
		if(!TopMsg.ajaxObj) {
			TopMsg.ajaxObj = new Ajax({
				url :'headMsgAjax',
				method :'POST',
				onSuccess: function(data){
					if(data) {
						var _result = 0;
						if(data.length > 0) {
							_result = eval('('+data[0]+')').result;
							__fireCookie.set(k_value, _result + "", TopMsg.live);
							var html = "消息(" + _result + ")";
							TopMsg.htmlId().innerHTML = html;
						}
					}
				},
				onFailure : function(){
					_top_message_clear();
				}
			});
		}
		TopMsg.ajaxObj.send();
	}
}

function _top_message_zero(uid){
	var k_value = "headMsgSize_" + uid;
	__fireCookie.set(k_value, "0", TopMsg.live);
}

function _top_message_clear(){
	if(TopMsg.timer) {
		window.clearInterval(TopMsg.timer);
	}
}
//;(function($){
//	$.extend({
//		"cookie": function(k,v,t){
//			if(!document.cookie) {
//				return false;
//			}
//			var setting = {"name": k || "", "value" : v || null};
//			if(setting.name.length > 0) {
//				if(setting.value) {
//					var expireDate = new Date();
//					expireDate.setSeconds(expireDate.getSeconds() + t);
//					var str = setting.name + "=" + setting.value + "; expires=" + expireDate.toGMTString();
//					document.cookie = str;
//				} else {
//					var cookies = document.cookie.split("; ");
//					if(cookies) {
//						for(var i = 0; i < cookies.length; i++) {
//							var cookie_name = cookies[i].split("=")[0];
//							if(cookie_name == setting.name) {
//								return cookies[i].split("=")[1];
//							}
//						}
//					}
//				}
//			} 
//		}
//	});
//})(jQuery);