var History = new function() {
	var $ = jQuery;	
	
	this.NAME_MAX_LENGTH = 30;
	this.LIST_MAX_COUNT = 3;//防止cookies过大问题，只记录5条
	this.TEMPLATE = [
		    '<li class="word-ellipsis">',
		    	'<a href="${URL}" target="_blank" title="${TITLE}">',
					'${NAME}',
		    	'</a>',
		    '</li>'
		].join('');
	
	this.init = function() {		
		var expiration = 60 * 60 * 24 * 30;
		var type = $('#searchtype').val().toUpperCase();
		var historyType = 'HISOTRY_' + type;
		DocumentCookie.load(document);
		var historyCookie = DocumentCookie.get(historyType);
		historyCookie.setExpiration(expiration);
		historyCookie.setDomain('pc120.com');
		historyCookie.setPath('/');
		
		var json = eval('(' + (historyCookie.getVal() || '{history:[], path:""}') + ')');
		
		// 访问详细页，记录history
		if (window.CONSTANTS) {
			if (!json.path) {
				json.path = CONSTANTS.URI.PATH;
				if (type == 'LEAK') {
					json.path = json.path.replace(/os\/|soft\//, '_SUBTYPE_/');
				};	
			};
			var subtype;
			if (type == 'LEAK') {
				subtype = /hotfix\/(os|soft)/.exec(CONSTANTS.URI.PATH)[1];
			};
			var key = CONSTANTS.BASE64 || CONSTANTS.KEY;
			var data = {
				subtype : subtype,
				key : key,
				name : $.trim(CONSTANTS.URL || CONSTANTS.NAME)
			};
			
			if (data.key && data.name) {
				for (var i = 0; i < json.history.length; i++) {
					var history = json.history[i];
					if (history.key == data.key) {
						json.history.splice(i, 1);
						break;
					};
				};
				json.history.unshift(data);
				if (json.history.length > History.LIST_MAX_COUNT) {
					json.history.splice(History.LIST_MAX_COUNT,1);
				};
		
				historyCookie.setVal(JSON.stringify(json));
				historyCookie.write();
			}
		};
		
		var list = [];
		for (var i = 0; i < json.history.length; i++) {
			var history = json.history[i];
			var url = json.path.replace('_KEY_', history.key);
			if (type == 'LEAK') {
				url = url.replace('_SUBTYPE_', history.subtype);
			};
			var title = history.name;
			var name = F.wordEllipsis(title, History.NAME_MAX_LENGTH);
			if (title && name) {
				list.push(History.TEMPLATE.replace('${URL}', url).replace('${TITLE}',title).replace('${NAME}', name));
			};
		};
		$('#browserHistoryList').html('<ul class="list">' + list.join('') + '</ul>');
	};
	
	this.update = function(e) {
		var anchor = $(e);
		var url = anchor.attr('href');
		var title = $.trim(anchor.prev().html());
		
		var list = $('#browserHistoryList li');
		if (list.length == History.LIST_MAX_COUNT) {
			$(list[History.LIST_MAX_COUNT - 1]).remove();
		};		
		
		list = $('#browserHistoryList li');		
		for (var i = 0; i < list.length; i++) {
			var li = $(list[i]);
			if (li.find('a').attr('href').indexOf(url) > -1) {
				li.remove();
				li = null;				
				delete li;
				break;
			};
		};
		
		var name = F.wordEllipsis(title, History.NAME_MAX_LENGTH);	
		list.parent().prepend(History.TEMPLATE.replace('${URL}', url).replace('${TITLE}',title).replace('${NAME}', name));
	};
};
F.main(function() {
	History.init();
});
