jiyiri.register_namespace('jiyiri.page.common');

jiyiri.require_once('jiyiri.page.common.promopicgetter');

(
function()
{
	//@todo:只能保存两个记录
	var ProductViewHistoryManager = Class.create();
	ProductViewHistoryManager._instance = null;
	ProductViewHistoryManager.GetInstance = function()
	{
		if(ProductViewHistoryManager._instance==null)
		{
			ProductViewHistoryManager._instance = new ProductViewHistoryManager();
		}
		return ProductViewHistoryManager._instance;
	};

	ProductViewHistoryManager.prototype =
	{
		initialize:function()
		{
			this._products = new Array();
			this.max_saved_num = 6;
			this.max_save_time = 6;//in hour
			this.save_name = 'pvh';
			this._Load();
		},
		Show:function(obj)
		{
			var products = this._products;
			products.reverse();
			var sb = new Array();
			for(var i=0;i<products.length;i++)
			{
				sb.push('<li>');
				sb.push('<a title="' + products[i].ProductName + '" href="' + __APP__ + '/Gift/View/id/'+ products[i].ProductId +'">');
				sb.push('<img width="60" height="60" src="' + jiyiri.page.common.PromoShop_Pic_Getter.getPic(products[i].Picture, jiyiri.page.common.PromoShop_Pic_Standard.ProductHistory_View ) + '"/>');
				sb.push('</a>')
				sb.push('</li>');
			}
			$(obj).innerHTML = sb.join('');
		},
		Add:function(product_type,product_id,product_name,product_picture)
		{
			if(!this._FindProduct(product_id))
			{
				var products = this._products;
				if(products.length>=this.max_saved_num)
				{
					//最后一个出栈
					products.pop();
				}
				var new_product = {};
				new_product.ProductId = product_id;
				new_product.ProductName = product_name;
				new_product.Picture = product_picture;
				new_product.ProductType = product_type;
				//当前商品入栈
				products.push(new_product);
				this._products = products;
				this._Save();
			}
			return true;
		},
		CleanData:function()
		{
			this._products=new Array();
			this._Save();
		},
		GetList:function()
		{
			return this._products;
		},
		_Save:function()
		{
			this._SaveInCookie(this._products);
		},
		_Load:function()
		{
			this._products = this._LoadFromCookie();
		},
		_SaveInCookie:function(value)
		{
			WriteCookie(this.save_name,Object.toJSON(value),this.max_save_time);
		},
		_LoadFromCookie:function()
		{
			var json_str = ReadCookie(this.save_name);
			var products = new Array();
			if(json_str!='')
			{
				try
				{
					eval('products='+json_str);
				}
				catch(ex)
				{
					alert(ex);
					products = new Array();
				}
			}
			return products;
		},
		_FindProduct:function(id)
		{
			var rtn=null;
			var products = this._products;
			for(var i=0;i<products.length;i++)
			{
				if( products[i].ProductId==id)
				{
					rtn = products[i];
				}
			}
			return rtn;
		}
	}
	
	
	/* 临时处理区域 */
	//Example:
	//writeCookie("myCookie", "my name", 24);
	//Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
	function WriteCookie(name,value,hours){

		var expire="";

		if(hours!=null){

		  expire=new Date((new Date()).getTime()+hours*3600000);

		  expire="; expires="+expire.toGMTString();

		}
		path = '; path=/';

		document.cookie=name+"="+escape(value)+expire+path;
	}

	function ReadCookie(name){

		var cookieValue="";

		var search=name+"=";

		if(document.cookie.length>0){

		  offset=document.cookie.indexOf(search);

		  if(offset!=-1){

			offset+=search.length;

			end=document.cookie.indexOf(";",offset);

			if(end==-1){

			  end=document.cookie.length;

			}

			cookieValue=unescape(document.cookie.substring(offset,end));

		  }

		}

		return cookieValue;

	}



	 /* 命名空间注册 */
	jiyiri.page.common.ProductViewHistoryManager = ProductViewHistoryManager;

}
)();
