/* 公共函数 */
/*
	名称：命名空间
	功能：每一级建立为对象
*/
function $namespace(str){
	var arr=str.split(',');
	for(var i=0;i<arr.length;i++){
		arr[i]=arr[i].split(".");
		str= "";
		var strEval= "";		
		for (var j = 0; j < arr[i].length; j++){
			if (j!= 0){str += "."};
			str += arr[i][j];
			strEval += "if (typeof(" + str + ") == 'undefined'){" + str + " = {}};";
		};
		if(strEval != ""){eval(strEval)};
	}
};
/*
	名称：$.strLen(str)
	参数：str（要获取长度的字符串）
	功能：获取字符串长度
	说明：汉字为2个字符长，半角为1个字符长，全角为2个字符长
*/
$.extend({
	strLen:function(str){
		var len=0;
		for(var i =0; i<str.length;i++){
			if(str.charCodeAt(i)>255 || str.charCodeAt(i)<0){
				len +=2;
			}else{
				len +=1;
			}
		}
		return len;
	}
})
/*
	名称：$.subStrLen(str,len)
	参数：str(字符串),len(截取的长度)
	功能：截取字符串指定长度
	说明：汉字为2个字符长，半角为1个字符长，全角为2个字符长
*/
$.extend({
	subStrLen:function(str,len){
		var _str="";
		if($.strLen(str)>=len){
			_str = str.substr(0,len)+"...";
		}else{
			_str = str;
		}
		return _str;
	}
})
/*
	名称：$.cancelBubble(event)
	参数：event(事件)
	功能：阻止冒泡
	说明：判断ie,ff浏览器，适应不同浏览器
*/
$.extend({
		cancelBubble:function(e){
			if(e&&e.stopPropagation){
				e.stopPropagation();
			}else{
				window.e.cancelBubble = true;
			}
		}

})
/*
	名称：zoomImage
	参数：wid宽度、hei高度
	功能：图片缩放
	说明：图片缩放，如果图片的宽高均小于wid和hei，就不缩放，如果只有图片高度大于hei，就按图片高度/hei的比例缩小宽度，如果只有图片宽度大于hei，就按图片宽度/wid的比例缩小高度，如果两者都大于，按图片宽高大者缩放小者属性
*/
$.fn.extend({
  zoomImage:function(wid,hei){
	return this.each(function(){
		var self = this,$this=$(this);
		var $height = $this.height(),$width = $this.width();
		if($width >= wid){
			if($height >= hei){
				if($width>=$height){
					$this.width(wid);
					$this.height(wid/$width * $height);
				}else{
					$this.height(hei);
					$this.width(hei/$height * $width);
				}
			}else{
				if($width<$height){
					$this.width(wid);
					$this.height(wid/$width * $height);
				}
			}
		}else{
			if($height >= hei){
				if($width>=$height){
					$this.width(wid);
					$this.height(wid/$width * $height);
				}else{
					$this.height(hei);
					$this.width(hei/$height * $width);
				}
			}
		}
	})
  }
})

/*
	名称：prompt1
	参数：content(弹出框的文字内容)
	功能：没有按钮，弹出最普通的弹出框，一行文字，自己关闭
*/
var prompt1 = function(content,func) {
	var func = func || function(){};
	var prompt1Opt = {
		title:'提示',
		classes:'2',
		buttons: {},
		timeout:1500,
		callback:func
	}
	var prompt1Html = '<p class="bd_one_paragraph">' + content + '</p>';
    $.prompt(prompt1Html,prompt1Opt);
}
/*
	名称：prompt1success
	参数：content(弹出框的文字内容)
	功能：没有按钮，弹出最普通的弹出框，一行文字，用户操作正确，自己关闭
*/
var prompt1Success = function(content,func) {
var func = func || function(){};
var prompt1Opt = {
		title:'提示',
		classes:'2',
		buttons: {},
		timeout:1500,
		callback:func
	}
	var prompt1Html = '<p class="bd_one_paragraph popup_success">' + content + '</p>';
    $.prompt(prompt1Html,prompt1Opt);
}
/*
	名称：prompt1success
	参数：content(弹出框的文字内容)
	功能：没有按钮，弹出最普通的弹出框，一行文字，用户操作正确，自己关闭
*/
var prompt1Fail = function(content,func) {
	var func = func || function(){};
	var prompt1Opt = {
		title:'提示',
		classes:'2',
		buttons: {},
		timeout:1500,
		callback:func
	}
	var prompt1Html = '<p class="bd_one_paragraph popup_fail">' + content + '</p>';
    $.prompt(prompt1Html,prompt1Opt);
}
/*
	名称：prompt2
	参数：content(弹出框的文字内容),func(点击确定返回的方法)
	功能：确定和取消按钮，确定返回func
*/
var prompt2 = function(content, func) {
	var func = func || function(){};
	var prompt2Opt = {
		title:'提示',
		classes:'2',
		buttons: {
			确定: true,
			取消: false
		},
		submit:function(v){
			if(v){
				func()
			}
		}
	}
	var prompt2Html = '<p class="bd_one_paragraph">' + content + '</p>';
     $.prompt(prompt2Html,prompt2Opt);
}
/*
	名称：全局函数
	功能：
*/
$namespace("wlz.globalInit");
wlz.globalInit.init = function(){
	//页面初始化
	this.pageInit();
	//顶部导航栏操作（包括搜索框等）
	this.topNavInit();
	//输入框添加class
	//this.searchInputInit();
	//侧栏头像和显示其它内容动作
	this.sidebarAction();
   //空间模块左侧栏固定
   this.sideFixAction();
}

//页面初始化
wlz.globalInit.pageInit = function(){
	if(top.location.href.indexOf(".131.com")<0){
		top.location.href = self.location.href;
	}
	$(".album_list li>img").zoomImage(100,75);	
	wlz.globalInit.getMessageCount();
    window.setInterval(wlz.globalInit.getMessageCount,15000);
}

//获取页面cookie
wlz.globalInit.getPageCookie=function(name){
	if (document.cookie.length>0)
	{ 
		var cookieStart=document.cookie.indexOf(name + "=");
		if (cookieStart!=-1)
		{ 
			cookieStart=cookieStart + name.length+1; 
			cookieEnd=document.cookie.indexOf(";",cookieStart);
			if (cookieEnd==-1) 
			{
				cookieEnd=document.cookie.length;
			}
			return unescape(document.cookie.substring(cookieStart,cookieEnd));
		} 
	}
	return ""
}

//设置页面cookie
wlz.globalInit.setPageCookie=function(name,value,expiresd,path,domain,secure){
	var expdate = new  Date(),
		expires = arguments[2] || null,
		path  	= arguments[3] || "/",
		domain  = arguments[4] || null,
		secure  = arguments[5] || false;		
	expires?expdate.setMinutes(expdate.getMinutes() + parseInt(expires)):"";		
	var cookietemp =escape(name) + '=' + escape(value) + (expires ? '; expires=' + expdate.toGMTString() : '') + (path ? '; path=' + path : '')+ (domain ? '; domain=' + domain : '')+(secure ? '; secure' : '');
	document.cookie=cookietemp;
}

//判断页面cookie是否存在
wlz.globalInit.checkPageCookie=function(name,value,expiresd){
	var cookieInfo=wlz.globalInit.getPageCookie(name);
	if (cookieInfo==null && cookieInfo=="") 
	{
		setCookie(name,value,expiresd);
		return "";
	}
	else
	{
		return cookieInfo;
	}
}
	
//保存初始页面Title
var pTitle=document.title; 
var msgCount=0;
var obj;

//获得别人发送的消息数量
wlz.globalInit.getMessageCount = function(){
	$.ajax({
		type: "get",
		url: "/AshxService/Message/Receive.ashx?act=getcount",
		data: "",
		success: function(msg) {
			$("#spanCount").html(msg);
			$("#messCount").html(msg);
			$("#tipsClose").click(function(){
				$("#navItemMessTips").hide();
				$("#messCount").show();
				wlz.globalInit.setPageCookie("MessTipState","False|"+msgCount,1);
			})
			var flickerT=new flickerTitle();
			if((msg-0)>0){
				//判断是显示数字信息提示还是气泡信息提示
			    var cookieInfo=wlz.globalInit.checkPageCookie("MessTipState","True|"+msg,1);
			    if(cookieInfo=="")
			    {
			   		$("#navItemMessTips").show();
					$("#messCount").hide();
			    }
			    else
			    {
					var cookieValue=cookieInfo.split('|');
					if(cookieValue[0]=="True")
					{
						$("#navItemMessTips").show();	
						$("#messCount").hide();
						wlz.globalInit.setPageCookie("MessTipState","True|"+msg,1);
					}
					else if(cookieValue[0]=="False")
					{
						if((cookieValue[1]-0)<msg)
						{
							$("#navItemMessTips").show();	
							$("#messCount").hide();
							wlz.globalInit.setPageCookie("MessTipState","True|"+msg,1);
						}
						else
						{
							$("#navItemMessTips").hide();	
							$("#messCount").show();
							wlz.globalInit.setPageCookie("MessTipState","False|"+msg,1);	
						}
					}
				}
				//让页面标题闪动
			   if(msgCount!=msg){
				   msgCount=msg;
				   flickerT.flickerStop();
				   flickerT.msgCount=msg;
				   flickerT.flickerStart();
			   }
			}else{
				$("#navItemMessTips").hide();
				$("#messCount").hide();
				document.title=pTitle;
				flickerT.flickerStop();
			}
		}
	});
}

//让页面Title前面的图标实现动画效果
function flickerTitle()
{
    var n=1;
	this.isStart=false;
	this.msgCount=0;
	
	this.flicker=function(msgSum)
	{
	   return (function() {
         if(n%2==0)
		     document.title="◇您有"+msgSum+"封未读信件◇  "+pTitle;
	     else
		     document.title="◆您有"+msgSum+"封未读信件◆  "+pTitle; 
	     n++;
      }); 
	}
	this.flickerStart=function()
	{
		var functRef=this.flicker(this.msgCount);
		obj=window.setInterval(functRef,500);
	}
	
	this.flickerStop=function()
	{try{
	    window.clearInterval(obj);
	}catch(e){}
	}
}

//根据url是否包含哪个值来判断加载cur的class
wlz.globalInit.addCurByUrl = function(){
	if (location.href.toLowerCase().indexOf("friend") == 17) { $("#headerNav li.cur").removeClass("cur"); $("#navItemFriend").addClass("cur");}
	else if (location.href.toLowerCase().indexOf("message") == 17) { $("#headerNav li.cur").removeClass("cur"); $("#navItemUnion").addClass("cur");}
	else if (location.href.toLowerCase().indexOf("wzone") > -1) { }
	else if (location.href.toLowerCase().indexOf("set") == 17) { }
	else if (location.href.toLowerCase().indexOf("diary") == 17) { }
	else if (location.href.toLowerCase().indexOf("http://i.131.com/t") == 0) { }
	else if (location.href.toLowerCase().indexOf("photo") == 17) { }
	else if (location.href.toLowerCase().indexOf("share") == 17) { }
	else if (location.href.toLowerCase().indexOf("card") == 17) { }
	else if (location.href.toLowerCase().indexOf("footprint") == 17) { }
	else if (location.href.toLowerCase().indexOf("gamecenter") ==17) { $("#headerNav li.cur").removeClass("cur"); $("#navItemUnion").addClass("cur");}
	else { $("#headerNav > ul > li:first").addClass("cur"); }
}
/*
	名称：鼠标经过和移除文字和下拉箭头区域事件
	参数：overObj 鼠标经过和移除的区域，包括链接和下拉箭头两部分
		 arrObj 下拉箭头
		 parentObj 父区域，添加class
		 showObj 显示的下拉框的id
	注：参数均为jQ对象
*/
wlz.globalInit.topNavItemOverOut = function(overObj,arrObj,parentObj,showObj){
	$("#headerNav li a,#headerNav li span").mouseover(function(){
			parentObj.addClass("over");
		}).mouseout(function(){
			parentObj.removeClass("over");
			showObj.hide();
	})
	arrObj.click(function(){
		showObj.show();
	})
	showObj.mouseover(function(){
		$(this).show();
		parentObj.addClass("over");
	})
}
//顶部导航栏操作（包括搜索框等）
wlz.globalInit.topNavInit = function(){
	wlz.globalInit.addCurByUrl();
	//顶部导航栏鼠标经过和点击效果，下拉列表消失绑定#wrap鼠标经过，导航栏单项和下拉框阻止冒泡
	var showNavList=true;
	$("#headerNav>ul>li").not($("#headerNav>ul>li.cur")).bind("mouseover",function(e){
			$(this).addClass("over");
			$.cancelBubble(e);
	}).bind("mouseout",function(e){
			$(this).removeClass("over");
			$.cancelBubble(e);
	})
	$("#navItemIndexArr,#navItemFriendArr,#navItemMessArr").each(function(){
		$(this).click(function(){
			$("#navItemIndexBd,#navItemFriendBd,#navItemMessBd").hide();
			$(this).next().show();
		})
	})
	$("#navItemIndexBd,#navItemFriendBd,#navItemMessBd").each(function(){
		$(this).bind("mouseover",function(e){
			$(this).show();
			$.cancelBubble(e);
		})
	})
	$("#navItemMessTips").mouseover(function(){
		$("#navItemMess").mouseout();	
	})
	$("body").mouseover(function(){
		$("#navItemIndexBd,#navItemFriendBd,#navItemMessBd").hide();
	})
	
	$("#linkHd").hover(function(){
		$("#linkBd").show();
	},function(){
		$("#linkBd").hide();
	})
	$("#linkBd").hover(function(){
		$(this).show();
	},function(){
		$(this).hide();
	})
	$("#inputNavSearch").focus(function(){
		$(this).addClass("cur");
	}).blur(function(){
		if($(this).val()==""){
			$(this).removeClass("cur");
		}
	}).keypress(function(evt){
		evt = window.event?window.event:evt;
		if(evt.keyCode == 13 && $(this).val()!=""){
			if($("#inputNavSearch").val()!=""){
				document.location.href = "/friend/find/" + encodeURI($("#inputNavSearch").val());
			}
		}
	})
	$("#searchBtn").click(function(){
		if($("#inputNavSearch").val()!=""){
			document.location.href = "/friend/find/" + encodeURI($("#inputNavSearch").val());								
		}
	})
}
//侧栏头像和显示其它内容动作
wlz.globalInit.sidebarAction = function(){
	$("#sideBar>ul>li").mouseover(function(){
		$(this).find(".tips").show();
   	}).mouseout(function(){
		$(this).find(".tips").hide();   
	})
   $("#sidebarUserAvatar").mouseover(function(){
		$(this).addClass("sidebar_user_avatar_over");
	}).mouseout(function(){
		$(this).removeClass("sidebar_user_avatar_over");
	})
   $("#sidebarTit").click(function(){
		$("#indexSidebarBdMid").toggle();
	})
	//鼠标经过经验条停留一段时间显示提示信息
	var expOverTime=0,interval;
	var showExpTips = function(){
		expOverTime++;
		if(expOverTime>8){
			expOverTime=0;
			$("#expTips").show();
		}
	}
	$("#expWrap,#expWrap span").bind("mouseover",function(e){
		clearInterval(interval);
		interval = setInterval(showExpTips,100);
		$.cancelBubble(e);	
	})
	$("#expTips").bind("mouseover",function(e){
		$(this).show();
		$.cancelBubble(e);	
	})
	$("body").mouseover(function(){
		$("#expTips").hide();
		clearInterval(interval);
	})
}
/*
	功能：固定玩空间左侧栏和返回顶部链接
	说明：玩空间左侧栏固定位置
		 返回顶部链接位置固定，滚动才显示，滚回到顶部消失
*/
wlz.globalInit.sideFixAction = function(){
	var changeLeftTop = function(){
		var contentLeft,contentTop,backLeft,backTop,absoluteTop;
		contentLeft = $("#containerMain").offset().left;
		contentTop = $("#containerMain").offset().top;
		backLeft = contentLeft+$("#containerMain").width()+5;
		backTop = $(window).height()-$("#backToTop").height()-25;
		absoluteTop = $(window).height()+$(window).scrollTop()- $("#backToTop").height();
		$("#backToTop").css({"top":backTop,"right":"25px"});
		$("#indexSideBar").css({"top":contentTop,"left":contentLeft});
		if($.browser.msie && $.browser.version=="6.0"){
			$("#indexSideBar").css({"position":"absolute"});
			$("#backToTop").css({"position":"absolute"});
		}else{
			$("#backToTop").css({"position":"fixed"});
			$("#indexSideBar").css({"position":"fixed"});
		}
	}
	this.changeLeftTop = changeLeftTop;
	changeLeftTop();
	$(window).resize(function(){
		changeLeftTop();
	});
	$(window).scroll(function(){
		$("#backToTop").show();
		if(document.documentElement.scrollTop==0){
			$("#backToTop").hide();
		}
		if($.browser.msie && $.browser.version=="6.0"){
			var absoluteTop = $(window).height()+$(window).scrollTop()- $("#backToTop").height(),contentTop = $("#containerMain").offset().top + $(window).scrollTop();
			if(absoluteTop > $("#containerMain").offset().top+$("#containerMain").height){
				absoluteTop = $("#containerMain").offset().top+$("#containerMain").height;
			}
			$("#backToTop").css({"top":absoluteTop});
			$("#indexSideBar").css({"top":contentTop});
		}
	})
	/*else{
		changeLeftTop()
	}*/
}
//初始化StringBuilder类的新实例
function StringBuilder(value)
{
	this.strings = new Array("");
	this.append(value);
}

// 添加值到StringBuilder对像的结尾。
StringBuilder.prototype.append = function (value)
{
	if (value)
	{
		this.strings.push(value);
	}
}

// 清除字符串缓冲区
StringBuilder.prototype.clear = function ()
{
	this.strings.length = 1;
}

// 此实例转换为String
StringBuilder.prototype.toString = function ()
{
	return this.strings.join("");
}

//alert(new Date().format("yyyy-MM-dd")); 
//alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss")); 

Date.prototype.format = function(format) //author: meizz 
{ 
  var o = { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(),    //day 
    "h+" : this.getHours(),   //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
    "S" : this.getMilliseconds() //millisecond 
  } 
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o)if(new RegExp("("+ k +")").test(format)) 
    format = format.replace(RegExp.$1, 
      RegExp.$1.length==1 ? o[k] : 
        ("00"+ o[k]).substr((""+ o[k]).length)); 
  return format; 
}
