$(function(){
	$('#Ltree').tree({
		url: catApi,
		animate: true,
		onClick:function(node){
			addTabs(node);
		}
	});
	$("#tab").tabs({
		border:false,
		onClose:function(title) {
			var tmp;
			tmp = $.map(node_title, function(n){
				if(title.length == n.length && title == n) {
					return null;
				} else {
					return n;
				}
			});
			node_title = tmp;
		}
	});
	$("#tab_l").tabs({
		border:false
	});
	$(".history_list").live("click",function(){
		var node = new Object();
		node.text	= $(this).text();
		var h_id;
		h_id = $(this).attr('id').split("_");
		node.id	= h_id[1];
		addTabs(node);
	})
})
var node_title = [];
function addTabs(node) {
	if($.inArray(node.text, node_title) == '-1') {
		var t_id	= 'goods' + node.id;
		$('#tab').tabs('add',{
			title: node.text,
			content: '<table id="'+t_id+'" class="mydata"></table>',
			closable:true
		});
		showDataGrid(t_id, node.id);
		node_title[node_title.length] = node.text;
		set_cats_history(node);
	} else {
		$('#tab').tabs('select',node.text);
	}
}
function set_cats_history(node) {
	var c_history_id = '';
	c_history_id = getCookie('cats_history_id');
	if(c_history_id == null) {
		c_history_id = node.id;
		setCookie('cats_history_id', c_history_id, 365);
	}else{
		var flag	= 0;
		var cids	= new Array();
		cids	= c_history_id.split(",");
		for (i=0;i<cids.length ;i++ ) {
			if(node.id == cids[i]) {
				flag = 1;
			}
		}
		if(flag == 0) {
			var str = '<p class="history_list icon-hot" id="h_'+node.id+'">'+node.text+'</p>';
			$("#cats_history").append(str);
			c_history_id += ','+node.id;
			setCookie('cats_history_id', c_history_id, 365);
		}
	}
}
function showDataGrid(t_id, cid) {
	$('#'+t_id).datagrid({
		nowrap: false,
		fit:true,
		border:false,
		url:goodApi+cid,
		columns:[[
		{
			field:'pic_url',
			title:'宝贝图片',
			width:100,
			align:'center',
			formatter:function(value,rec) {
				return '<a href="'+rec.click_url+'" style="border:solid 1px #999;padding:2px;margin-top:2px;display:block" target="_blank"><img style="width:80px;height:80px;border:none" src="'+value+'" /></a>';
			}
		},
		{
			field:'title',
			title:'宝贝描述',
			width:230,
			formatter:function(value,rec) {
				return '<a href="'+rec.click_url+'" class="item-link" target="_blank">'+value+'</a><div style="margin:5px;padding-top:5px">店铺：<a class="seller-link" href="'+rec.shop_click_url+'" target="_blank">'+rec.nick+'</a></div>';
			}
		},
		{
			field:'commission_num',
			title:'热卖指数',
			width:100,
			align:'center',
			formatter:function(value,rec) {
				return '<label class="sell_num">'+value+'</label> 件';
			}
		},
		{
			field:'price',
			title:'热卖价',
			width:100,
			align:'center',
			formatter:function(value,rec) {
				return '<label class="price">￥'+value+'</label>';
			}
		},
		{
			field:'haopinlv',
			title:'好评率',
			width:100,
			align:'center',
			formatter:function(value,rec) {
				return '<label class="haopinlv">'+value+'%</label>';
			}
		},
		{
			field:'seller_credit_score',
			title:'卖家信用',
			width:140,
			align:'left',
			formatter:function(value,rec) {
				var str;
				str	= '<div><img src="'+value+'" /></div><div>宝贝与描述相符：'+rec.shop_score.item_score+'</div>';
				str	+= '<div>卖家的服务态度：'+rec.shop_score.service_score+'</div>';
				str	+= '<div>卖家发货的速度：'+rec.shop_score.delivery_score+'</div>';
				return str;
			}
		},
		{
			field:'item_location',
			title:'宝贝所在地',
			width:100,
			align:'center',
			formatter:function(value,rec) {
				return value;
			}
		}
		]],
		pagination:true,
		pageSize:20,
		singleSelect:false,
		pageList:[10,20,30,40]
	});
}
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
}
function getCookie(c_name) {
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null
}
function addFavorite (sURL, sTitle) {
	try   {
		window.external.addFavorite(sURL, sTitle);
		return false;
	} catch (e)  {
		try {
			window.sidebar.addPanel(sTitle, sURL, "");
			return false;
		} catch (e)  {
			alert("加入收藏夹失败，请使用Ctrl+D进行添加");
			return false;
		}
	}
}

