var cookiePosition = "SLPos";
var cookieOpen = "SLOpen";
var cookieName = "ShortList";
var cookieDelimiter = "$";
var cookieDataDelimiter = "|";
var SHORTLIST_DISPLAY_LENGTH = 3;

function startShortlist()
{
	showShortlist(readCookie(cookieOpen) == "1");
}

function showShortlist(visible)
{
	var currentList = readCookie(cookieName);

	if (currentList == "" || currentList == null)
	{
		document.getElementById('HotelShortList_shortlistMaster').style.height = '0px';
		writeCookie(cookieOpen, "");
	}
	else
	{
		var hotels = currentList.split(cookieDelimiter);

		writeCookie(cookieOpen, (visible ? "1" : ""));

		if (hotels.length == 0)
		{
			document.getElementById('HotelShortList_shortlistMaster').style.height = '0px';
		}
		else if (hotels.length == 1 && hotels[0] == "")
		{
			document.getElementById('HotelShortList_shortlistMaster').style.height = '0px';
		}
		else
		{
			if (visible) 
			{
				document.getElementById('footerBtm').style.paddingBottom = '182px';
				document.getElementById('HotelShortList_shortlistMaster').style.height = '180px';
			}
			else
			{
				document.getElementById('HotelShortList_shortlistMaster').style.height = '57px';
				document.getElementById('footerBtm').style.paddingBottom = '56px';
			}
		}
	}
}

function repositionShortlist()
{
	if (document.getElementById('HotelShortList_shortlistContain'))
	{
		document.getElementById('HotelShortList_shortlistMaster').style.height = "auto";
		
		if (document.getElementById('HotelShortList_shortlistNavigation'))
		{
			var h = parseInt(document.getElementById('HotelShortList_shortlistMaster').offsetHeight) - 87;
			//alert(h);
			document.getElementById('HotelShortList_shortlistNavigation').style.paddingTop = h + "px";
		}
		//alert(document.getElementById('HotelShortList_shortlistMaster').offsetHeight);
	}
}

function setVisible(item, visible)
{
	var obj = document.getElementById(item);
	
	if (obj != null && obj != 'undefined')
	{
		if (obj.style.display != 'undefined')
		{
			obj.style.display = (visible ? "inline" : "none");
		}
	}
}

function AddCookie(id, data)
{
	var currentList = readCookie(cookieName);

	if (currentList == null)
	{
		currentList = "";
	}
	if (currentList.indexOf(id + cookieDataDelimiter) < 0)
	{
		if (currentList != "")
		{
			currentList = cookieDelimiter + currentList;
		}

		writeCookie(cookieName, data + currentList);
	}
	
	if (document.getElementById('HotelShortList_shortlistMaster').style.height == '0px')
	{
		startShortlist();
	}
}

function removeFromShortlist(code)
{
	var currentList = readCookie(cookieName);
	var hotels = currentList.split(cookieDelimiter);
	var newList = "";
	
	code += cookieDataDelimiter;
	
	for(var i = 0; i < hotels.length; i++)
	{
		if (hotels[i].substr(0, code.length) != code)
		{
			newList+= cookieDelimiter + hotels[i];
		}
	}
	
	newList = newList.substring(1);
	writeCookie(cookieName, newList);

	if (newList == "")
	{
		writeCookie(cookiePosition, 0);
		showShortlist(false);
	}
	else
	{
		var position = readCookie(cookiePosition);
		var hotels = newList.split(cookieDelimiter);

		position *= 1;
		
		if (position >= hotels.length - SHORTLIST_DISPLAY_LENGTH)
		{
			position = hotels.length - SHORTLIST_DISPLAY_LENGTH;
			writeCookie(cookiePosition, position);
		}
	}
}

function shiftShortList(direction)
{
	var position = readCookie(cookiePosition);
	
	position *= 1;
	
	var currentList = readCookie(cookieName);
	var hotels = currentList.split(cookieDelimiter);
	
	if (position > hotels.length - SHORTLIST_DISPLAY_LENGTH)
	{
		position = hotels.length - SHORTLIST_DISPLAY_LENGTH;
	}

	if (position > 0 && direction == -1)
	{
		position--;
	}
	else if (position < hotels.length - SHORTLIST_DISPLAY_LENGTH && direction == 1)
	{
		position++
	}
	
	writeCookie(cookiePosition, position);
}

function highlightResult(item)
{
	var element = document.getElementById(item);
	
	if (element != null && element != 'undefined')
	{
		element.className = 'shortlisted contain';
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

function writeCookie(name, value)
{
	var now = new Date();
	var day = now.getDate();
	var month = now.getMonth();
	var year = now.getFullYear();
    
	var expiryDate = new Date(now.getFullYear()+1,now.getMonth(),now.getDate());

	document.cookie = name + "=" + value
						 + ";  expires=" + expiryDate.toGMTString() 
						 + "; path=/";
}

function eraseCookie(name) {
	var now = new Date();
	var day = now.getDate();
	var month = now.getMonth();
	var year = now.getFullYear();
    
	var expiryDate = new Date(now.getFullYear()-1,now.getMonth(),now.getDate());

	document.cookie = name + "=;  expires=" + expiryDate.toGMTString() 
						 + "; path=/";
}