$(document).ready(function(){
	var history = getCookie('history');
	if (history != null)
	{
		var ids = history.split(',', 10);
		var html = '';
		
		//console.log(history);
		for (i in ids)
		{
			if (ids[i] != 'undefined')
			{
				var res = $.ajax({
					url: "/inc/getInfo.php?id="+ids[i],
					async: false,
					success: function(html){
						$("#results").append(html);
					}
				}).responseText;
				
				if (res != 'undefined') html += res;
			}
		}
		
		if (html.length > 1) $("#last_visit").html('<ul>' + html + '</li>');
		else $("#last_visit").html('Нет информации');
		//setCookie ('history', '', 30, '/');
	}
});


function setHistory(new_id){
	var old_id = getCookie('history');
	var _html = new_id + ',' + old_id;
		
	var ids = _html.split(',', 10);
	var collect = new Array;
	for (i in ids)
	{
		if (ids[i] != '') 
		{
			var _id = ids[i];
			var has = false;
			
			for (z in collect)
			{
				if (collect[z] == ids[i])
				{
					has = true;
				}
			}
			if (has === false) 
			{
				collect.push(_id);
			}
		}
	}
	var new_html = collect.toString();
	setCookie ('history', new_html, 30, '/');
}


function setCookie(name, value, expiredays, path, domain, secure) {
   if (expiredays) {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      var expires = exdate.toGMTString();
   }
   document.cookie = name + "=" + escape(value) +
   ((expiredays) ? "; expires=" + expires : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
   //document.cookie =
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
