﻿/**
 * params: d string ie:8月2日
 */
function BBCool_PlayItem(title, mid, aid, vo, d) {
	this.$title = title;
	this.$time = d;
	this.$mid = mid;
	this.$aid = aid;
	this.$vo = vo;
}

BBCool_PlayItem.prototype.getTitle = function() {
	return this.$title;
};

BBCool_PlayItem.prototype.getMid = function() {
	return this.$mid;
};

BBCool_PlayItem.prototype.getAid = function() {
	return this.$aid;
};

BBCool_PlayItem.prototype.getVo = function() {
	return this.$vo;
};

BBCool_PlayItem.prototype.getTime = function() {
	return this.$time;
};

BBCool_PlayItem.prototype.serialize = function() {
	var servalue = "";
	for ( var prop in this) {
		if ((prop.charAt(0) != '$') || ((typeof this[prop]) == 'function')) {
			continue;
		}
		if (servalue != "") {
			servalue += "|";
		}
		servalue += prop + "#" + escape(this[prop]);
	}
	return servalue;
}

BBCool_PlayItem.prototype.unserialize = function(servalue) {
	var a = servalue.split("|");
	for ( var i = 0; i < a.length; i++) {
		var b = a[i].split("#");
		this[b[0]] = unescape(b[1]);
	}
};

function BBCool_Storage(timeout, path, domain) {
	this.cookieName = "bbcool_storage";
	this.domain = domain;
	this.timeout = timeout;
	this.path = path;
};

BBCool_Storage.prototype.listPlayItem = function(cb) {
	cookie = new Cookie(document, this.cookieName, this.timeout, this.path,
			this.domain);
	cookie.load();

	if (cookie["List_count"] == undefined || cookie["List_count"] == null) {
		return;
	}

	var listCount = parseInt(cookie["List_count"]);
	for ( var i = 1; i <= listCount; i++) {
		var servalue = cookie["item" + i];
		if (servalue == undefined || servalue == null) {
			continue;
		}
		var item = new BBCool_PlayItem();
		item.unserialize(servalue);
		if (cb != null && typeof (cb) == "function") {
			cb(i, item);
		}
	}
}

BBCool_Storage.prototype.addPlayItem = function(title, mid, aid, vo, d) {
	var cookie = null;
	do {
		cookie = new Cookie(document, this.cookieName, this.timeout, this.path,
				this.domain);
		cookie.load();

		var listCount = 0;
		if (cookie["List_count"] != undefined && cookie["List_count"] != null) {
			listCount = parseInt(cookie["List_count"]);
		}

		if (listCount >= 5) {
			this.delPlayItem(1);
		}
	} while (listCount >= 5);

	var newItem = new BBCool_PlayItem(title, mid, aid, vo, d);
	var servalue = newItem.serialize();
	cookie["item" + (listCount + 1)] = servalue;
	cookie["List_count"] = listCount + 1;
	cookie.store();
}

BBCool_Storage.prototype.delPlayItem = function(i) {
	cookie = new Cookie(document, this.cookieName, this.timeout, this.path,
			this.domain);
	cookie.load();

	if (cookie["List_count"] == undefined || cookie["List_count"] == null) {
		return;
	}

	var listCount = cookie["List_count"];
	if (listCount <= 0 || listCount > 5) {
		return;
	}
	if (i < 0 || i > listCount) {
		return;
	}

	if (i < listCount) {
		for ( var k = i + 1; k <= listCount; k++) {
			cookie["item" + (k - 1)] = cookie["item" + k];
		}
		delete cookie["item" + listCount];
	} else {
		delete cookie["item" + i];
	}
	listCount = listCount - 1;
	cookie["List_count"] = listCount;
	cookie.store();
}

var bbcool = {};
bbcool.Storage = {
	clearPlayList : function() {
		var list = document.getElementById("historyList");
		for ( var i = list.childNodes.length - 1; i >= 0; --i) {
			list.removeChild(list.childNodes[i]);
		}
	},

	cbHistoryList : function(i, item) {
		var closeImg = document.createElement("img");
		closeImg.setAttribute("width", "15px");
		closeImg.setAttribute("height", "15px");
		closeImg.setAttribute("src", "http://www.bbcool.cn/images/clos.gif");
		var closeLink = document.createElement("a");
		closeLink.setAttribute("href", "javascript:void();");
		if (window.attachEvent) {
			closeLink.attachEvent("onclick", function() {
				bbcool.Storage.deleteHistoryAndReload(i);
			});
		}
		closeLink.appendChild(closeImg);

		var titleLink = document.createElement("a");
		titleLink.setAttribute("class", "two");
		titleLink.setAttribute("href", "http://www.bbcool.cn/details/"
				+ item.getMid() + ".htm");
		titleLink.setAttribute("target", "_blank");
		titleLink.appendChild(document.createTextNode(item.getTitle()));

		var dateSpan = document.createElement("span");
		var t = item.getTime();
		var d = new Date();
		d.setTime(t);
		var strText = (1 + d.getMonth()) + "月" + d.getDate() + "日";
		dateSpan.appendChild(document.createTextNode(strText));

		var continuePlayLink = document.createElement("a");
		continuePlayLink.setAttribute("class", "three");
		continuePlayLink.setAttribute("href",
				"http://www.bbcool.cn/player.do?aid=" + item.getAid() + "&vo="
						+ item.getVo() + "&mid="+item.getMid());
		continuePlayLink.setAttribute("target", "_blank");
		continuePlayLink.appendChild(document.createTextNode("继续观看"));

		var li = document.createElement("li");
		li.appendChild(closeLink);
		li.appendChild(titleLink);
		li.appendChild(dateSpan);
		li.appendChild(continuePlayLink);

		document.getElementById("historyList").appendChild(li);
	},

	loadHistoryList : function() {
		bbcool.Storage.clearPlayList();
		bbcool.Storage.listHistory(bbcool.Storage.cbHistoryList)
	},

	deleteHistoryAndReload : function(i) {
		bbcool.Storage.deleteHistory(i);
		bbcool.Storage.loadHistoryList();
	},

	recordHistory : function(title, mid, aid, vo) {
		var storage = new BBCool_Storage(2400, "/", null);
		var d = new Date().getTime();
		storage.addPlayItem(title, mid, aid, vo, d);
	},

	listHistory : function(cbList) {
		var storage = new BBCool_Storage(2400, "/", null);
		storage.listPlayItem(cbList);
	},

	deleteHistory : function(i) {
		var storage = new BBCool_Storage(2400, "/", null);
		storage.delPlayItem(i);
	}
};

