﻿/**
*全新的toolbar
**/
var newToolbar = 
{
    unlogin:'<div class="toolbarmenu" style="width:150px;padding:0 3px">您好！欢迎来到YOHO!</div>'
	+'<div class="toolbarmenu"><a href="http://www.yoho.cn/MyYoho/Login.aspx?'+top.location.href+'" target="_top">登录</a></div>'
	+'<div class="toolbarmenu"><a id="toolbar_regpage" href="http://i.yoho.cn/Manage/Register.aspx" target="_top">注册</a></div>',
	quick_set:'<li class="setquick"><a href="http://i.yoho.cn/Manage/Shortcutkey.aspx" target="_top">设置快捷方式</a></li>',
	baseHtml:'<div id="toolbar"><div class="toolbarbox"><div class="toolbarlink"><a class="toolbarlogo" href="http://www.yoho.cn" target="_top"></a>'
	+'<a id="toolbar_quit" class="toolbar_quit" href="http://i.yoho.cn/Manage/RemoveCookie.aspx?'+top.location.href+'" target="_top">退出</a>'
	+'<div class="toolbar_bizbox">'//设置购物车数量
	+'<a id="toolbar_bizhome" onmouseover="newToolbar.over()" onmouseout="newToolbar.out()" class="toolbar_bizhome" href="http://buy.yoho.cn" target="_top">有货首页</a><ul id="toolbar_youhuo_login" style="display:none" onmouseover=newToolbar.over("nohide") onmouseout=newToolbar.out("youhuo_menu") >'//设置鼠标经过事件
	+'<li><a id="toolbar_order_num" href="http://i.yoho.cn/manage/biz/order.aspx" target="_top">我的订单(0)</a></li>'//设置订单数量
	+'<li><a href="http://i.yoho.cn/manage/biz/Collection.aspx" target="_top">我的收藏</a></li>'
	+'<li><a href="http://i.yoho.cn/manage/biz/reg.aspx" target="_top">缺货登记</a></li>'
        +'<li><a href="http://i.yoho.cn/manage/biz/Comment.aspx" target="_top">我关注的商品</a></li>'
        +'<li id="toolbar_myTicket_li"><a id="toolbar_myTicket" href="http://i.yoho.cn/manage/biz/Ticket.aspx" target="_top">我的抵扣券(0)</a></li>'
	+'</ul></div>'
	+'<a id="toolbar_gwc" href="http://buy.yoho.cn/manage/OrderCFMNew.aspx" target="_top" class="toolbar_gwc"><span id="toolbar_shopcart_num">(0)</span></a>'
    +'</div>'
    +'<div class="toolbarboxl" style="width:600px"><table id="toolbar_userinfo" cellpadding="0" cellspacing="0" class="toolbaruser"><tr>'
	+'<td id="toolbar_cur_user_info"></td>'//设置当前的头像和名称
    +'<td width="18" align="center"><a href="javascript:newToolbar.click()" onblur="newToolbar.blur()" ><img src="http://www.yoho.cn/images/icon_triangle_bottom_white.gif" alt="切换身份" /></a>'
	+'<div id="chageuser" style="display:none"><ul id="toolbar_userlist" class="userchage_list">'
	+'</ul><div class="setuser"><a href="http://i.yoho.cn/Manage/IdentityList.aspx" target="_top"><strong>设置身份</strong></a></div></div></td></tr></table>'
	+'<span id="toolbar_unlogin"></span>'
    +'<div class="toolbarmenu" id="toolbar_mypage_outer"><a id="toolbar_mypage" href="#" target="_top">我的主页</a></div>'//设置我的首页
    +'<div class="toolbarmenu" id="toolbar_person_cen"><a href="http://i.yoho.cn/Manage/Index.aspx" target="_top">个人中心</a></div>'
    +'<div class="toolbarmenu" id="toolbar_msg"><a id="toolbar_msg_num" href="http://i.yoho.cn/Manage/Message.aspx" target="_top">信箱(0)</a></div>'//设置信箱的数量
    +'<div id="toolbarquick" class="toolbarmenu toolbarquick"><a id="toolbar_quick_f" onmouseover=newToolbar.over("fast") onmouseout=newToolbar.out("fast") href="http://i.yoho.cn/Manage/logAppend.aspx" target="_top">&nbsp;&nbsp;&nbsp;写日志</a>'
    +'<ul id="toolbar_quick_con"  onmouseover=newToolbar.over("nohide_q") onmouseout=newToolbar.out("q_menu") style="display:none"></ul></div>'//设置快捷内容
    +'<div class=" toolbar_sqbox toolbarmenu"><a href="http://www.yoho.cn/MyYoho/Center.aspx" target="_top">找朋友</a></div>'
    +'<div class=" toolbar_sqbox toolbarmenu"><a href="http://city.yoho.cn/" target="_top">看帖子</a></div>'
    +'<div class=" toolbar_sqbox toolbarmenu"><a href="http://www.yoho.cn/active/index.aspx" target="_top">玩活动</a></div></div></div><div class="toolshadow">&nbsp;</div></div>',
	ishide:true,
	ishide1:true,
     init:function()//初始化
     {
	   
	   var s;
       s = document.createElement("link");
 	   s.charset="utf-8";
	   s.rel="stylesheet"; 
	   s.type="text/css";
	   s.media="screen";
	   s.href = "http://style.yoho.cn/web/toolbar/toolbar_1211.css";
 	   document.getElementsByTagName("head")[0].appendChild(s);
	   document.body.style.margin = "0";
	   newToolbar._genHTML();	   
       if(newToolbar.islgoin())
	   {
	    newToolbar.loadScript("http://i.yoho.cn/yohodata/toolbar/get.ashx?callback=newToolbar.cnget&rnd="+Math.random());
		newToolbar.loadScript("http://buy.yoho.cn/ajax/shoppingcart.ashx?type=6&callback=newToolbar.buyget&rnd="+Math.random());
	   }
	   else
	   {
		newToolbar.$("toolbar_gwc").style.display = "none";
		newToolbar.$("toolbar_quit").style.display = "none";
		newToolbar.$("toolbar_mypage_outer").style.display = "none";
		newToolbar.$("toolbar_person_cen").style.display = "none";
		newToolbar.$("toolbar_msg").style.display = "none";
		newToolbar.$("toolbarquick").style.display = "none";
		newToolbar.$("toolbar_userinfo").style.display = "none";
		newToolbar.$("toolbar_unlogin").innerHTML=newToolbar.unlogin;
		newToolbar.checkReg();
	   }
	 },
	 $:function(name)
	 {
	   return document.getElementById(name);
	 },
	 click:function()//多身份点击
	 {
		if(newToolbar.$("chageuser").style.display=="none")
			newToolbar.$("chageuser").style.display = "";
		else
			newToolbar.$("chageuser").style.display = "none";
	 },
	 
	 over:function(type)//鼠标悬停
	 {
        if(type=="fast")
        {
          newToolbar.$("toolbar_quick_con").style.display = "";
        }
		else if (type=="nohide_q")
		{
		  newToolbar.ishide = false;
		  newToolbar.$("toolbar_quick_con").style.display = "";
		}
		else if (type=="nohide")
		{
		  
		  newToolbar.ishide1 = false;
		  newToolbar.$("toolbar_youhuo_login").style.display = "";
		}
        else
        {
			if(newToolbar.islgoin())
				newToolbar.$("toolbar_youhuo_login").style.display = "";
        }         
	 },
	 
	 out:function(type)//鼠标离开
	 {
        if(type=="fast")
        {
		    window.setTimeout('if(newToolbar.ishide){newToolbar.$("toolbar_quick_con").style.display = "none"}',300);
        }
		else if(type == "q_menu")
		{
		  newToolbar.ishide = true;
		  newToolbar.$("toolbar_quick_con").style.display = "none";
		}
		else if(type == "youhuo_menu")
		{
		  newToolbar.ishide1 = true;
		  newToolbar.$("toolbar_youhuo_login").style.display = "none";
		}
        else
        {
		    window.setTimeout('if(newToolbar.ishide1){newToolbar.$("toolbar_youhuo_login").style.display = "none"}',300);
        }         
	 },
     //判断注册界面
	checkReg:function()
	{
	  
	  var ret = false;
	  var data = ["city","space","i","www","shop","m","ezine"];//社区这边的域名
	  /*var date;
 	  var two_day = 1000*60*60*24;
      var cur_date = new Date().getTime();
	  date = new Date(cur_date+two_day);
	  document.cookie = "yohoreg="+data.join("|")+";path=/;expires=" + date.toGMTString()+";domain=.yoho.cn";*/
	  for(var i = 0; i< data.length; i++)
	  {
	    if(data[i]+".yoho.cn"==window.location.host)
		{
			ret = true;
		}
	  }
	  if(!ret)
	  {
	    newToolbar.$("toolbar_regpage").href = "http://i.yoho.cn/Manage/Regquick.aspx";
	  }
	},
     blur:function()//失去焦点
     {
	   window.setTimeout('newToolbar.$("chageuser").style.display="none";',300);	    
	 },

	 _genHTML:function(html)//生成toolbar结构
	 {
       document.body.insertAdjacentHTML("afterBegin",newToolbar.baseHtml); 
	 },
     cnget:function(data)//获取社区数据展示
     {
       //绑定用户身份
       if(data.ids.length>1)
       { 
		var arr = newToolbar._getCookie("yoho").split("&");
		var cur_id = 0;
		for(var i = 0;i<arr.length;i++)
		{
		  if(arr[i].indexOf("nowUid=")>-1)
		  {
		    cur_id = arr[i].replace("nowUid=","");			
			break;
		  }
		}
		var html = "";
     	for(var i=0;i<data.ids.length;i++)
     	{		  
           //判断当前用户
		   if(!data.ids[i])break;
		   if(data.ids[i].id == cur_id)
		   {
		     var ico = data.ids[i].ico;
			 var html_str = '';
		     if(ico.indexOf(".gif")>-1)
		       ico = ico.replace(".gif","16.gif");
		     if(ico.indexOf(".jpg")>-1)
		       ico = ico.replace(".jpg","16.jpg");
			 if(ico.indexOf(".png")>-1)
		       ico = ico.replace(".png","16.png");
             html_str += '<img id="toolbar_cur_img" src="'+ico+'" width="16" height="16" align="absmiddle" /><a href="javascript:newToolbar.click()" onblur="newToolbar.blur()" ><strong id="toolbar_cur_name">'+data.ids[i].name+'</strong></a>';
             newToolbar.$("toolbar_cur_user_info").innerHTML = html_str;
			 
			 if(data.ids[i].domain&&data.ids[i].domain!="")
			 {
			     newToolbar.$("toolbar_mypage").href = "http://i.yoho.cn/"+data.ids[i].domain;
			 }
		     else
			 {
				 newToolbar.$("toolbar_mypage").href = "http://i.yoho.cn/"+cur_id;
			 }
		   }
		   //绑定HTML
		   	 var ico = data.ids[i].ico;
			 var url="";
		     if(ico.indexOf(".gif")>-1)
		       url = ico.replace(".gif","32.gif");
		     if(ico.indexOf(".jpg")>-1)
		       url = ico.replace(".jpg","32.jpg");
			 if(ico.indexOf(".png")>-1)
		       url = ico.replace(".png","32.png");
			 
			 var li_style = "";
			 if(data.ids[i].id == cur_id)
				li_style = '<li class="on">';
			 else
				li_style = '<li>';
			 html+=li_style+'<a href= javascript:newToolbar.changeUser("'+data.ids[i].id+'","'+data.ids[i].name+'");top.location.reload()>'
			 +'<img src="'+url+'" align="left" />'
			 +'<small class="font_gray">'+data.ids[i].id+'</small>'
			 +'<br /><strong>'+data.ids[i].name+'</strong> </a></li>';
        }
		newToolbar.$("toolbar_userlist").innerHTML = html;
       }
       else
       {
          //绑定当前用户的HTML
		 var ico = data.ids[0].ico;
		 if(ico.indexOf(".gif")>-1)
		   ico = ico.replace(".gif","16.gif");
		 if(ico.indexOf(".jpg")>-1)
		   ico = ico.replace(".jpg","16.jpg");
		 if(ico.indexOf(".png")>-1)
		   ico = ico.replace(".png","16.png");
		 var html_str_one = '<img id="toolbar_cur_img" src="'+ico+'" width="16" height="16" align="absmiddle" /><a href="javascript:newToolbar.click()" onblur="newToolbar.blur()" ><strong id="toolbar_cur_name">'+data.ids[0].name+'</strong></a>';
         newToolbar.$("toolbar_cur_user_info").innerHTML = html_str_one;			 
		 if(data.ids[0].domain&&data.ids[0].domain!="")
		 {
			 newToolbar.$("toolbar_mypage").href = "http://i.yoho.cn/"+data.ids[0].domain;
		 }
		 else
		 {
			 newToolbar.$("toolbar_mypage").href = "http://i.yoho.cn/"+data.ids[0].id;
		 }
       }
       //绑定邮箱数量
       if(data.msg)
       {
			newToolbar.$("toolbar_msg_num").innerHTML = "信箱("+data.msg+")";
       }

       //绑定用户快捷方式
       if(data.links&&data.links.length>0)
       {
		  if(data.links[0].name.length>7)
			data.links[0].name = data.links[0].name.substr(0,5)+"...";
	      newToolbar.$("toolbar_quick_f").href = data.links[0].url;
		  newToolbar.$("toolbar_quick_f").innerHTML = "&nbsp;&nbsp;&nbsp;"+data.links[0].name;
          var html = "";
		  for(var i = 1;i < data.links.length;i++)
          {
		    if(!data.links[i])break;
            if(data.links[i].name == '上传图片')
			{
				html += '<li class="toolbar_addpic"><a href="'+data.links[i].url+'" target="_top">'+data.links[i].name+'</a></li>';
			}
                        else if(data.links[i].name == '写日志')
			{
			   html += '<li class="toolbar_diary"><a href="'+data.links[i].url+'" target="_top">'+data.links[i].name+'</a></li>';
			}
			else if(data.links[i].name == '上传音乐')
			{
			   html += '<li class="toolbar_addmusic"><a href="'+data.links[i].url+'" target="_top">'+data.links[i].name+'</a></li>';
			}
			else if(data.links[i].name == '收藏')
			{
				html += '<li class="toolbar_collect"><a href="'+data.links[i].url+'" target="_top">'+data.links[i].name+'</a></li>';
			}
			else
			{
				if(data.links[i].name.length>7)
					data.links[i].name = data.links[i].name.substr(0,5)+"...";
				html += '<li class="toolbar_zdy"><a href="'+data.links[i].url+'" target="_top">'+data.links[i].name+'</a></li>';
			}
          }		 
       }
	    html += newToolbar.quick_set;
		newToolbar.$("toolbar_quick_con").innerHTML = html;
     },
     
     buyget:function(data)//获取有货数据展示
     {
     	//绑定有货数据
     	if(data.sc)
     	{
          newToolbar.$("toolbar_shopcart_num").innerHTML = "("+data.sc+")";
        }

        //绑定订单数量
     	if(data.order)
     	{
          newToolbar.$("toolbar_order_num").innerHTML = "我的订单("+data.order+")";
        }
        //抵扣券数量
        if(data.ticket)
        {
          newToolbar.$("toolbar_myTicket").innerHTML = "我的抵扣券("+data.ticket+")";
		  if(data.ticket!="0")
		    newToolbar.$("toolbar_myTicket_li").className = "toolbar_biz_ticket";
        }
     },

     //判断是否已登录
     islgoin:function()
     {
    	 if(newToolbar._getCookie("yoho")&&newToolbar._getCookie("yoho").length>4)
    	 {
            return true;
         }
    	 else
    	 {
           return false;
         }
     },
     /*
     addLoadEvent:function(fn) //页面加载
 	 {
 		var win = window,doc = document,UNDEF = "undefined";
 		if (typeof win.addEventListener != UNDEF) 
 		{
 			win.addEventListener("load", fn, false);
 		}
 		else if (typeof doc.addEventListener != UNDEF) 
 		{
 			doc.addEventListener("load", fn, false);
 		}
 		else if (typeof win.attachEvent != UNDEF) 
 		{
 			win.attachEvent("onload", fn);
 		}
 		else if (typeof win.onload == "function") 
 		{
 			var fnOld = win.onload;
 			win.onload = function() 
 			{
 				fnOld();
 				fn();
 			};
 		}
 		else 
 		{
 			win.onload = fn;
 		}
 	 },*/
    //dom加载完
	domready:function(fn)
	{
		// 参数不正确
		if(typeof fn != "function")
			return false;
		if(typeof document.addEventListener == "function") 
		{ // 非 IE 浏览器
			document.addEventListener("DOMContentLoaded", fn, false);
			return;
		}
		var _this = arguments.callee;
		if(_this.ready) // 如果 DOM 已经加载完毕, 则直接执行
			return fn();
	 
		if(!_this.list) // 创建一个待执行函数数组
			_this.list = [];
	 
		_this.list.push(fn);
	 
		if (_this.done) return; // 正在循环检测则返回
		(function() 
		{ // 循环检测
			_this.done = true;
			try 
			{
				document.documentElement.doScroll("left");
			}
			catch(error) 
			{
				setTimeout(arguments.callee, 0);
				return;
			}
			// DOM 加载完毕, 执行所有待执行函数
			_this.ready = true;
			for (var i=0, l=_this.list.length; i<l; i++)
			{
				_this.list[i]();
			}
		})();
    },
 	 
 	//获取cookie
 	_getCookie:function(sName)
 	{
 		var aCookie = document.cookie.split("; ");
 		for (var i=0; i < aCookie.length; i++)
 		{
 			var aCrumb = aCookie[i].split("=");
 			if (sName == aCrumb[0])
 			return aCookie[i].replace("yoho=","");
 		}
 		return null;
 	},

    //设置cookie
    _setCookie:function(sName,sValue)
    {
 	    var date;
 		var two_year = 1000*60*60*24*365*2;
 		var cur_date = new Date().getTime();
 		if(newToolbar._getCookie("yoho").indexOf("expires=1")>-1)
 		{
 			date = new Date(cur_date+two_year); 		
 			document.cookie = sName + "=" + sValue + ";path=/;expires=" + date.toGMTString()+";domain=.yoho.cn";
 		}
 		else
 		{
 	    	document.cookie = sName + "=" + sValue + ";path=/;domain=.yoho.cn";
 	    }
 	}, 

 	//加载脚本
 	loadScript:function(url)
 	{
		var s;
 		s = document.createElement("script");
 		s.charset="utf-8";
 		document.getElementsByTagName("head")[0].appendChild(s);
 		s.src = url;
 		return s;
 	},

 	//改变当前用户
 	changeUser:function(id,name)
 	{
 		var str = newToolbar._getCookie("yoho");
 		var arr = str.split("&");
 		var one = true,two = true;//只是遍历一遍
 		for(var i=0;i<arr.length;i++)
 		{
 		  if(arr[i].indexOf("nowUid=")==0&&one)
 		  {
 		     arr[i] = "nowUid="+id;
 		     one = false; 
 		  }
 		  if(arr[i].indexOf("userName=")==0&&two)
 		  {
 		     arr[i]="userName="+encodeURIComponent(name);
 		     two = false;
 		  }
 		}
 		newToolbar._setCookie("yoho",arr.join("&"));
 	},
	//改变购物车数量
	changeShopCartNum:function(num)
	{
		newToolbar.$("toolbar_shopcart_num").innerHTML="("+num+")";
	}
}

//兼容老toolbar切换用户
var toolbar = 
{
   ChangeUser:function(id,name)
   {
      newToolbar.changeUser(id,name);
	  top.location.reload();
   }
}

//兼容老方法，判断是否登录
var isLand = function()
{
   return newToolbar.islgoin();
}


//初始化
newToolbar.domready(newToolbar.init); 

//插入HTML代码，兼容性
if (window.navigator.userAgent.indexOf("MSIE")<0) 
{
   
	HTMLElement.prototype.insertAdjacentHTML=function(where, html)
	{ 
	   var e=this.ownerDocument.createRange(); 
	   e.setStartBefore(this); 
	   e=e.createContextualFragment(html); 
	   switch (where)
	   { 
			case 'beforeBegin': this.parentNode.insertBefore(e, this);break; 
			case 'afterBegin': this.insertBefore(e, this.firstChild); break; 
			case 'beforeEnd': this.appendChild(e); break; 
			case 'afterEnd': 
			if(!this.nextSibling) this.parentNode.appendChild(e); 
			else this.parentNode.insertBefore(e, this.nextSibling); break; 
	   } 
	}; 
} 

