﻿/* getrecent */

$(document).ready(function(){


	var jdRecent=
	{
		element:$("#Recentness_View"),
		jsurl:"/common/lishiset.aspx?prdid=",
		cookiename:"_recent",
		list:$.cookie("_recent"),
		url:location.href,
		init:function()
		{
			var _matchStr=this.url.match(/\/product-(\d+).aspx/);
			var _id=(_matchStr!=null&&_matchStr[0].indexOf("aspx")!=-1)?_matchStr[1]:"";
			if(!this.list||this.list==null||this.list=="")
			{
				if(_id=="")
				{
					return this.getData(0);
				}
				else
				{
					this.list=_id;
				}
			}
			else
			{
				if(_id=="")
				{
					this.list=this.list;
				}
				else
				{
					var tmp=_id;
					var rec=this.list.split(".");
					for(i in rec)
					{
						if(rec[i]!=_id)
						{
							tmp=tmp+"."+rec[i];
						}
						if(i>=4)
						{
							break;
						}
					}
					if(tmp.split(".").length>10)
					{
						tmp=tmp.replace(/.\d+$/,"");
					}
					this.list=tmp;
				}
			}
			$.cookie(this.cookiename,this.list,{expires:7,path:"/",domain:"",secure:false});
			this.getData(this.list);
		}
		,
		clear:function()
		{
			$.cookie(this.cookiename,"",
			{
				expires:7,path:"/",domain:"",secure:false
			}
			);
		}
		,
		getData:function(list)
		{
			if(list==0)
			{
				$("#Recentness_View").html("<tr><td>暂无记录<td></tr>");
				return;
			}
			var rec=list.split(".");
			for(i in rec)
			{
				if (i==0)
				{
					$("#Recentness_View").empty()
				};
				$.getJSON(this.jsurl+rec[i],this.setData);
			}
		}
		,
		setData:function(result)
		{
			if(result.name!="")
			{
				$("#Recentness_View").append("<tr><td width='47%' align='middle'><a href='"+result.url+"'><img border='0' alt='"+decodeURI(result.name)+"' src='"+result.img+"' width='70' /></a></td><td style='padding-top: 2px' valign='top' width='53%' align='left'><p><a href='"+result.url+"'>"+decodeURI(result.name)+"</a></p><p style='margin: 6px 0px 0px' class='STYLE2'>￥"+result.price+"</p></td></tr>");
			}
		}
	};
	
	$("#clearRec").click(function(){
			jdRecent.clear();
			jdRecent.getData(0);
		}
	)
	if ($("#Recentness_View").length==1)
	{
		jdRecent.init();
	}


});

