var menu = document.getElementById("favMenu");
var block;
var target;

function splitFavs()
{
	if (document.cookie != null)
	{
		var aCookie = document.cookie.split(";");
		for (var i=0; i < aCookie.length; i++)
		{
			var aCrumb = aCookie[i].split("=");
			if ((aCrumb.length == 2) && (("Favs" == new String(aCrumb[0])) || (" Favs" == new String(aCrumb[0]))) && (new String(aCrumb[1]).length != 0))
			{
				return unescape(aCrumb[1]).split(";");
			}
		}
	}
	return null;
}

function setFile(imgFile, title, url)
{
	url = url.replace(/=/, '%3D')

	if (title.length != 0)
	{
		var files = splitFavs();
		var filestr = "";
		if (files != null)
		{
			for (var i=0;i<files.length;i++)
			{
				var bCrumb = files[i].split("=");
				if ((url == unescape(bCrumb[0])) && (title == unescape(bCrumb[1]))) return;
				filestr += files[i] + ";";
			}
		}
		
		filestr = escape(filestr + escape(url) + "=" + escape(title));
	
		
		var expiry = (new Date(new Date().valueOf() + 5*24*60*60*1000)).toUTCString();
		document.cookie = "Favs=" + filestr + "; path=/; expires=" + expiry + ";";
		
		target = document.getElementById('favCount');
		target.style.xpos = getRealPos(target,"Left") + (target.offsetWidth -12);
		target.style.ypos = getRealPos(target,"Top");
				
		block = imgFile.style;	
		block.xpos = parseInt(getRealPos(imgFile,"Left"));
		block.ypos = parseInt(getRealPos(imgFile,"Top"));
		
		if ((imgFile.parentNode != null) && (imgFile.parentNode.parentNode != null) && (imgFile.parentNode.parentNode.parentNode != null) && (imgFile.parentNode.parentNode.parentNode.style.overflow == "scroll"))
		{
			//if we are in a scrolling box, need to get the position relative to the container
			block.ypos = block.ypos - parseInt(imgFile.parentNode.parentNode.parentNode.scrollTop, 10);	
			//need to move the image outside the container
			imgFile.originalParent = imgFile.parentNode;
			imgFile.originalSibling = imgFile.nextSibling;
			
			document.body.appendChild(imgFile);
		} 
		else 
		{
			block.left = block.xpos;
			block.top = block.ypos;
		}
		
		block.yinc = ((block.ypos - target.style.ypos) / -1) / 15;
		block.xinc = ((block.xpos - target.style.xpos) / -1) / 15;				
		block.position = "absolute";		
		block.count = 0;
			
		slideFile();
	}
}

function getFiles()
{
	var counter = document.getElementById("favCount");
	counter.innerHTML = "&nbsp;";
	
	menu.innerHTML = "";
	menu.style.display = "none";
	files = splitFavs();
	if (files != null)
	{
		for (var i=0;i<files.length;i++)
		{
			var bCrumb = unescape(files[i]).split("=");
			var regExReplace = new RegExp(String.fromCharCode(152), "g");
			menu.innerHTML += "<div style=\"padding-bottom: 4px\"><a href=\"javascript:linkFav(" + i + ");\">" + bCrumb[1].replace(regExReplace,"'") + "</a></div>";
		}
		menu.innerHTML += "<div class=\"horDots\" style=\"width:100%\"></div><div style=\"padding-bottom: 4px\"><a href=\"javascript:DeleteFavs();\">Delete all favourites</a></div>";
		counter.innerHTML = files.length + " x <img src=\"/_system/Iliffe/images/readmore.gif\">";
	}		
}	

function showMenu()
{
	var display = menu.style.display == "none" ? "" : "none";
	if (display == "")
	{	
		getFiles();
		if (menu.innerHTML != "")
			menu.style.display = display;
			
		document.onclick = hideMenu;
	}
}

function hideMenu()
{
	menu.style.display = "none";
}

function DeleteFavs()
{
	var expiry = new Date(new Date().valueOf() + 5*24*60*60*1000);
	document.cookie = "Favs=; path=/; expires=" + expiry.toUTCString();
	
	for(var i=0;i<document.images.length;i++)
	{
		var img = document.images[i];
		if ((img.onclick != null) && (new String(img.onclick).indexOf("setFile") != -1))
		{
			img.style.position = "relative";
			img.style.display = "";
			img.style.left = 0;
			img.style.top = 0;
			
			if (img.originalParent != null)
			{
				if (img.originalSibling == null)
				{
					img.originalParent.appendChild(img);
				} else {
					img.originalParent.insertBefore(img, img.originalSibling);
				}				
			}
		}	
	}
	
	getFiles();
}

function linkFav(position)
{
	files = splitFavs();
	if (files != null)
	{
		var bCrumb = unescape(files[position]).split("=");
		var filestr = "";
		for (var i=0;i<files.length;i++)
		{
			if (position != i)
				filestr += files[i] + ";";
		}
		filestr = filestr.substring(0, filestr.length-1);
		
		var expiry = new Date(new Date().valueOf() + 5*24*60*60*1000);
		document.cookie = "Favs=" + escape(filestr) + "; path=/; expires=" + expiry.toUTCString();
		document.location.href = bCrumb[0].replace(/%3D/,'=');
	}
}

function isFav(imgFile, title, url)
{
	url = url.replace(/=/, '%3D')
	files = splitFavs();
	if (files != null)
	{
		for (var i=0;i<files.length;i++)
		{
			var bCrumb = unescape(files[i]).split("=");
			if ((url == bCrumb[0]) && (title == bCrumb[1]))
			{
				imgFile.style.display = "none";
			}			
		}
	}	
}

function slideFile() {
	if (block.count < 15) {	
		block.xpos += block.xinc;
		block.ypos += block.yinc;
		block.left = Math.round(block.xpos);
		block.top = Math.round(block.ypos);
		block.display = "";
		block.count += 1;
			
		setTimeout("slideFile()",10);
	}
	else 
	{
		block.count = 0;
		block.display = "none";
		getFiles();	
	}
}

function getRealPos(el,which) {
	iPos = 0
	while (el!=null) {
	 	iPos += el["offset" + which]
		el = el.offsetParent
	}
	return iPos
}

getFiles();