/*
 * 51766.com用户百宝箱
 * Copyright(c) 2003-2009, 51766.com.
 * 相关js
 * http://f.51766.com/js/jquery.js
 * http://www.51766.com/www2009/js/public.js
 */
function writeBox(){
	var s='<div style="float:left;"><div class="bbhmain"><div class="bbhbox"><img src="http://www.51766.com/www2009/images/bbhhead.gif" /></div>';
	s +='<div class="bbhbox bbhper" id="box_head">';
	s +='</div>';
	
	//导航部分
	s +='<div class="bbhbox bbhlab"><ul><li><a href="javascript:getFavorite();"><img id="bbdh1" src="http://www.51766.com/www2009/images/bbhlabicon01u.gif"  /></a></li>'+
		'<li><a href="javascript:getHistory();"><img  id="bbdh2" src="http://www.51766.com/www2009/images/bbhlabicon02d.gif" /></a></li>'+
		'<li><a href="javascript:getOrder();"><img  id="bbdh3" src="http://www.51766.com/www2009/images/bbhlabicon03d.gif" /></a></li>'+
		'<li><a href="javascript:getAsk();"><img  id="bbdh4" src="http://www.51766.com/www2009/images/bbhlabicon04d.gif" /></a></li></ul></div>';
	
	//关注部分
	s+='<div class="bbhbox bbhbox01"><div class="bbhbodyout"><div class="bbhbody01" id="box_main">';
	s+=loadstr;
	
	//关注部分结束
	s+='</div></div></div>';
	//页面结束
	s +='<div class="bbhbox"><img src="http://www.51766.com/www2009/images/bbhfoot.gif" /></div>';
	s+='</div></div><div style="width:100%;height:5px"></div>';
	document.writeln(s);
	//加载关注
	getHead();
	getFavorite();
}

 //头部
 function getHead(){
 	$('#box_head').load('http://www.51766.com/www2009/userbox/box_ajax.jsp',{action:'getHead'},function() {
   		//执行计算
  		}
	);
 }
 
 //收藏(ajax实现)
 function getFavorite(){
 	$('#box_main').load('http://www.51766.com/www2009/userbox/box_ajax.jsp',{action:'getFavorite'},function() {
   		//执行跟踪加载
 		$("#fav_table").find("input").change(function(){
 				var price=0;
 				var l=$("#fav_table").find("input[type='text']");
 				for(var i=0;i<l.length;i++){
 					if($('#f_info_id'+(l[i].id).substring(3)).attr('checked')==true)
 						price += countPrice((l[i].id).substring(3),$(l[i]).attr('info_type'));
 				}
 				$('#all_price').text('￥'+price);
 			});
  		}
	);
	selectBB(1);
 }
 //计算价格
 function countPrice(num,info_type){
 	var p=0;
 	switch (info_type){
 		case '1257':
 		case '70557':
 		case '1290':
 		case '2480':
 		case '1226':
 		case '19909':
 			p=$('#num'+num).val()*$('#price'+num).val();
 			break;
 		case '2560':
 		case '1216':
 			p=$('#num'+num).val()*$('#price'+num).val()* $('#day'+num).val();
 			break;
 			
 	}
 	return p;
 }
 
 //历史(cookie实现)
 function getHistory(){
 	var r_html='<div class="bbhts01"><div class="bbhts02"><span class="sp01">强烈建议您将需要的产品收藏，历史浏览记录不能永久保存。</span><br /></div></div>';
	var his=$.cookie('51766his');
	if (his!=null && his.indexOf('||')==-1){
		his=null;
		$.cookie('51766his',null);
	}
	var mm=0;
	if (his!=null){
		r_html ='<table border="0" cellspacing="0" cellpadding="0" class="lsta01">';
		var oary=his.split('@@');
		for (var i=0;i<oary.length;i++){
			if (mm>20) break;
			var os =oary[i].split('||');
			if (os[0].length>9){
				r_html+='<tr><td class="td01"><input id="'+os[0]+'" type="checkbox" value="" /></td>'+
						'<td class="td02"><div><a href="'+os[0]+'" class="aou" target="_blank">'+os[1]+'</a></div></td>'+
						' </tr>';
				mm++;
			}
		}
		r_html +='</table>';
		r_html +='<div class="bbhls01 wrapfix"><span><a href="javascript:delHistory();"><img src="http://www.51766.com/www2009/images/bbhlsicon01.gif" />清除记录</a></span></div><div class="bbhheng01"><img src="http://www.51766.com/jr.gif" /></div>';
	}
	if (r_html=='')
		r_html='<div class="bbhls02">您还没有访问可记录历史的页面。</div><div class="bbhheng01"><img src="http://www.51766.com/jr.gif" /></div>';
 	$('#box_main').html(r_html);
 	//切换页签
 	selectBB(2);
 }
 
 function delHistory(){
	var his=$.cookie('51766his');
	if (his==null)return;
	var n_h='';
	var oary=his.split('@@');
	for (var i=0;i<oary.length;i++){
		var os =oary[i].split('||');
		if (document.getElementById(os[0])==null || document.getElementById(os[0]).checked==false){
			n_h +=oary[i]+'@@';
		}
	}
	$.cookie('51766his',n_h.substring(0,n_h.length-1));
	getHistory();
}

 //订单(ajax实现)
 function getOrder(){
 	var member_id=$.cookie('passport.memberinfo');
 	if (member_id==null || member_id==''){
 		var r_html='<div class="bbhts01"><div class="bbhts02"><span class="sp01">您还没有<a href="http://sso.51766.com/" class="au">登陆</a>或<a href="http://reg.51766.com" class="au">注册</a>用户，所以您无法查看订单状态。</span><br /></div></div>';
 		$('#box_main').html(r_html);
 	}else{
	 	$('#box_main').load('http://www.51766.com/www2009/userbox/box_ajax.jsp',{action:'getOrder'},function() {
	   		//执行计算
	  		}
		);
 	}
	selectBB(3);
 }
 //询价(ajax实现)
 function getAsk(){
 	var member_id=$.cookie('passport.memberinfo');
 	if (member_id==null || member_id==''){
 		var r_html='<div class="bbhts01"><div class="bbhts02"><span class="sp01">您还没有<a href="http://sso.51766.com/" class="au">登陆</a>或<a href="http://reg.51766.com" class="au">注册</a>用户，所以您无法查看询价状态。<br><a href="http://www.51766.com/www2009/inquiry/"><font color=blue>无需注册，立即询价!</font></a></span><br /></div></div>';
 		$('#box_main').html(r_html);
 	}else{
	 	$('#box_main').load('http://www.51766.com/www2009/userbox/box_ajax.jsp',{action:'getAsk'},function() {
	   		//执行计算
	  		}
		);
 	}
	selectBB(4);
 }
 //导航切换
 function selectBB(num){
 	for (var i=1;i<5;i++){
 		if (i==num)
 			$('#bbdh'+i)[0].src="http://www.51766.com/www2009/images/bbhlabicon0"+i+"u.gif";
 		else
 			$('#bbdh'+i)[0].src="http://www.51766.com/www2009/images/bbhlabicon0"+i+"d.gif";
 	}
 }

 //浏览历史:只记录产品、信息和网店页面，导航页面的不做记录
var DH_NAME='51766his';
function addHistory(link,title){
	var o_h=$.cookie(DH_NAME);
	if (o_h==null){
		o_h=link+'||'+title;
	}else{
		if (o_h.indexOf(link+'||')!=-1)
			return;
		else
			o_h = link+'||'+title+'||'+'&&'+o_h;
	}
	//只保存10条浏览历史记录
	var all=o_h.split('&&');
	var n_h=o_h;
	if (all.length>10){
		n_h=o_h.substring(0,o_h.lastIndexOf('&&'));
	}
	$.cookie(DH_NAME,n_h);
}

//添加到收藏
function addFavorite(info_id,title,url,price,info_type,cust_id){
	if(info_id=='')
		info_id=new Date().getTime();
	 $.ajax({
			   type: 'POST',
			   url: 'http://www.51766.com/www2009/userbox/box_ajax.jsp',
			   data: 'action=addFavorite&cust_id='+cust_id+'&info_id='+info_id +'&title='+escape(title)+'&price='+price+'&info_type='+info_type+'&url='+url,
			   beforeSend:function(){
			   		document.body.disabled=true;
			   },
			   success: function(msg){
			   		document.body.disabled=false;
			     	alert(msg);
			     	getFavorite();
			   },
			   error:function(msg){
			   		document.body.disabled=false;
			   		alert('添加“关注”失败！请稍后重试。');
			   }
		});
}
//添加到收藏,但不提示信息
function addFavorite2(info_id,title,url,price,info_type,cust_id){
	if(info_id=='')
		info_id=new Date().getTime();
	 $.ajax({
			   type: 'POST',
			   url: 'http://www.51766.com/www2009/userbox/box_ajax.jsp',
			   data: 'action=addFavorite&cust_id='+cust_id+'&info_id='+info_id +'&title='+escape(title)+'&price='+price+'&info_type='+info_type+'&url='+url,
			   beforeSend:function(){
			   		document.body.disabled=true;
			   },
			   success: function(msg){
			   		document.body.disabled=false;
			     	getFavorite();
			   },
			   error:function(msg){
			   		document.body.disabled=false;
			   }
		});
}
//删除收藏
function delFavorite(info_id){
	 $.ajax({
			   type: 'POST',
			   url: 'http://www.51766.com/www2009/userbox/box_ajax.jsp',
			   data: 'action=delFavorite&info_id='+info_id,
			   beforeSend:function(){
			   		document.body.disabled=true;
			   },
			   success: function(msg){
			   		document.body.disabled=false;
			     	alert(msg);
			     	getFavorite();
			   },
			   error:function(msg){
			   		document.body.disabled=false;
			   		alert('删除失败！请稍后重试。');
			   }
		});
}

 writeBox();
 
 