function viewedHotelsObject()
{
	this._hotels = null;
	this._maxCount = 3;
	
	this.setCookie = function(cookieName, value, expiredays)
	{
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=cookieName+ "=" +escape(value)+
		";path=/" +
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
	}
	
	this.getCookie = function (cookieName)
	{
		if (document.cookie.length>0)
		{
			start=document.cookie.indexOf(cookieName + "=")
			if (start!=-1)
			{ 
				start=start + cookieName.length+1;
				end=document.cookie.indexOf(";",start);
				if (end==-1) end=document.cookie.length;
				return unescape(document.cookie.substring(start,end));
			} 
		}
		return "";
	}

	this.buildHotel = function (hotelIdentifier, hotelName, starRating, hotelAddress, uri, destinationId)
	{
		return new savedHotelObject(hotelIdentifier, hotelName, starRating, hotelAddress, uri, destinationId);
	}
	
	
	this.getSavedCount = function()
	{
		var count = this.getCookie("viewedCount");
		return count == "" ? 0 : count;
	}
	
	this.setSavedCount = function(count)
	{
		this.setCookie("viewedCount", count);
	}
	
	this.update = function()
	{
		for(var idx = 0; idx < this._hotels.length; idx++)
		{
			var hotel = this._hotels[idx];
			var src = hotel.source;
			this.setCookie("viewed:" + idx, src);
		}
		this.setSavedCount(this._hotels.length);
	}
	
	
	this.add = function(hotelIdentifier, name, starRating, address, uri, destinationId)
	{
		var hotel = this.buildHotel(hotelIdentifier, name, starRating, address, uri, destinationId);
		for(var idx = 0; idx < this._hotels.length; idx++)
		{
			if(this._hotels[idx]["hotelIdentifier"] == hotelIdentifier)
			{
				this._hotels[idx] = hotel;
				this.update();
				return;
			}
		}
		if(this._hotels.length >= this._maxCount)
		{
			this._hotels.splice(0,1)
		}
		this._hotels[this._hotels.length] = hotel;
		this.update();	
	}
	
	this.remove = function(hotelIdentifier)
	{
		for(var idx = 0; idx < this._hotels.length; idx++)
		{
			if(this._hotels[idx]["hotelIdentifier"] == hotelIdentifier)
				this._hotels.splice(idx,1);
		}
		
		this.update();	
		this.render();
	}
	
	this.renderRow = function(hotelIdentifier, name, starRating, address, uri, destinationId)
	{
		var element = document.getElementById("viewedhotels");
		var newdiv = document.createElement("div");
		newdiv.setAttribute("class", "hotel");
		var inner = "<div class=\"starrating\">";
		inner+= "<img src=\"" + _appPath + "/i/" + starRating + "star_white.gif\" alt=\"" + starRating + " Star Hotel\" />";
		inner+= "</div>";
		inner+= "<h4>";
		inner+= "<a href=\"" + unescape(uri) + "\">" + name + "</a>";
		inner+= "</h4>";
		inner+= "<p class=\"address\">";
		inner+= address
		inner+= "</p>";
		newdiv.innerHTML = inner; //append text to new div
		element.appendChild(newdiv);
	}

	this.render = function()
	{
		if(this._hotels.length == 0)
			document.getElementById("recentlyviewed").style.display = 'none';
		else
			document.getElementById("recentlyviewed").style.display = '';
		var element = document.getElementById("viewedhotels");
		element.innerHTML = "";
		for(var idx = 0; idx < this._hotels.length; ++idx)
			this.renderRow(this._hotels[idx]["hotelIdentifier"], this._hotels[idx]["hotelName"], this._hotels[idx]["starRating"], this._hotels[idx]["hotelAddress"], this._hotels[idx]["uri"]);
	}
	
	this._hotels = new Array();
	this._hotels.length = this.getSavedCount();
	for(var idx = 0; idx < this._hotels.length; ++idx)
	{
		this._hotels[idx] = eval(this.getCookie("viewed:" + idx));
	}

	return this;
}
var viewedHotels = new viewedHotelsObject();
