﻿/*
		A link preview system that uses the rather good snapshot service available
		from websnapr.com to display thumbnail previews.

		References:

		Wesnapr: http://www.websnapr.com
		Dustan Diaz: http://www.dustindiaz.com/sweet-titles-finalized
		Arc90: http://lab.arc90.com/2006/07/link_thumbnail.php

		Modified by Alexander Groß - http://therightstuff.de/
		Added support for auto-previewing all links in blog posts except those marked with rel="no-preview"
		Added support for the rel attribute
		Added support for dasBlog Click-Through-enabled links (http://www.dasblog.info)
*/
var webSnapr =
{
	x: 0,
	y: 0,
	obj: {},
	img: null,
	lnk: null,
	timer: null,
	opacityTimer: null,
	errorTimer: null,
	hidden: true,
	linkPool: {},
	baseURI: "./",
	init: function()
	{
		var links = document.getElementsByTagName("a");
		var i = links.length || 0;
		var cnt = 0;
		while (i--)
		{
			var link = links[i];
			if (webSnapr.isValidItemLink(link))
			{
				webSnapr.addEvent(link, ["focus", "mouseover"], webSnapr.initThumb);
				webSnapr.addEvent(link, ["blur",  "mouseout"], webSnapr.hideThumb);
				webSnapr.linkPool[link.href] = cnt++;
			}
		}
		if(cnt)
		{
			webSnapr.obj = document.createElement("div");

			webSnapr.ind = document.createElement("div");
			//Safari could require this ? div.appendChild(document.createTextNode(String.fromCharCode(160)));
			webSnapr.ind.className= "imageLoaded";
			webSnapr.img = document.createElement("img");
			webSnapr.img.alt = "Preview";
			webSnapr.addEvent(webSnapr.img, ["load"], webSnapr.imageLoaded);
			webSnapr.addEvent(webSnapr.img, ["error"], webSnapr.imageError);
			webSnapr.obj.id = "fdImageThumb";
			webSnapr.obj.style.display = "none";
			webSnapr.obj.style.top = "0";
			webSnapr.obj.style.left = "0";
			webSnapr.addEvent(webSnapr.img, ["mouseout"],  webSnapr.hideThumb);
			webSnapr.obj.appendChild(webSnapr.ind);
			webSnapr.obj.appendChild(webSnapr.img);
			document.getElementsByTagName("body")[0].appendChild(webSnapr.obj);
		}
	},
	imageLoaded: function() {
		if (webSnapr.errorTimer)
		{
			clearTimeout(webSnapr.errorTimer);
		}
		if (!webSnapr.hidden)
		{
			webSnapr.img.style.visibility = "visible";
		}
		webSnapr.ind.className= "imageLoaded";
		webSnapr.ind.style.visibility = "hidden";
	},
	imageError: function(e)
	{
		if (webSnapr.errorTimer)
		{
			clearTimeout(webSnapr.errorTimer);
		}
		webSnapr.ind.className= "imageError";
		webSnapr.errorTimer = window.setTimeout("webSnapr.hideThumb()", 2000);
	},
	initThumb: function(e)
	{
		e = e || event;

		webSnapr.lnk = this;
		var positionClass = "left";

		var heightIndent;
		var indentX = 0;
		var indentY = 0;

		if(String(e.type).toLowerCase().search(/mouseover/) !== -1)
		{
			if (document.captureEvents)
			{
				webSnapr.x = e.pageX;
				webSnapr.y = e.pageY;
			}
			else if (window.event.clientX)
			{
				webSnapr.x = window.event.clientX + document.documentElement.scrollLeft;
				webSnapr.y = window.event.clientY + document.documentElement.scrollTop;
			}
			indentX = 10;
			heightIndent = parseInt(webSnapr.y - webSnapr.obj.offsetHeight, 10) + "px";
		}
		else
		{
			var obj = this;
			var curLeft = 0;
			var curTop = 0;
			if (obj.offsetParent)
			{
				curLeft = obj.offsetLeft;
				curTop = obj.offsetTop;
				while (obj = obj.offsetParent)
				{
					curLeft += obj.offsetLeft;
					curTop += obj.offsetTop;
				}
			}
			curTop += this.offsetHeight;

			webSnapr.x = curLeft;
			webSnapr.y = curTop;

			heightIndent = parseInt(webSnapr.y - webSnapr.obj.offsetHeight - this.offsetHeight, 10) + "px";
		}

		if (parseInt(document.documentElement.clientWidth + document.documentElement.scrollLeft, 10) < parseInt(webSnapr.obj.offsetWidth + webSnapr.x, 10) + indentX)
		{
			webSnapr.obj.style.left = parseInt(webSnapr.x - (webSnapr.obj.offsetWidth + indentX), 10) + "px";
			positionClass = "right";
		}
		else
		{
			webSnapr.obj.style.left = (webSnapr.x + indentX) + "px";
		}
		if (parseInt(document.documentElement.clientHeight + document.documentElement.scrollTop, 10) < parseInt(webSnapr.obj.offsetHeight + webSnapr.y, 10) + indentY)
		{
			webSnapr.obj.style.top = heightIndent;
			positionClass += "Top";
		}
		else
		{
			webSnapr.obj.style.top = (webSnapr.y + indentY) + "px";
			positionClass += "Bottom";
		}

		webSnapr.obj.className = positionClass;
		webSnapr.timer = window.setTimeout("webSnapr.showThumb()", 250);
	},
	showThumb: function(e)
	{
		webSnapr.hidden = false;
		webSnapr.obj.style.display = "block";
		webSnapr.ind.style.visibility = "visible";
		webSnapr.obj.style.opacity = webSnapr.ind.style.opacity = ".1";
		webSnapr.img.style.visibility = "hidden";

		var address = String(webSnapr.lnk.href).replace(/^.*ct\.ashx\?id=.*&url=/gi, "");
		if (address === webSnapr.lnk.href)
		{
			// Link is an unencoded admin URL with no Click-Through.
			address = encodeURIComponent(address);
		}
		webSnapr.errorTimer = window.setTimeout("webSnapr.imageError()", 15000);
		webSnapr.img.src = "http://images.websnapr.com/?url=" + address + "&rndm=" + parseInt(webSnapr.linkPool[webSnapr.lnk.href], 10);

		/*@cc_on@*/
		/*@if(@_win32)
		return;
		/*@end@*/

		webSnapr.fade(10);
	},
	hideThumb: function(e)
	{
		webSnapr.hidden = true;
		if(webSnapr.timer)
		{
			clearTimeout(webSnapr.timer);
		}
		if(webSnapr.errorTimer)
		{
			clearTimeout(webSnapr.errorTimer);
		}
		if(webSnapr.opacityTimer)
		{
			clearTimeout(webSnapr.opacityTimer);
		}
		webSnapr.obj.style.display = "none";
		webSnapr.ind.style.visibility = "hidden";
		webSnapr.img.style.visibility = "hidden";
		webSnapr.ind.className = "imageLoaded";
	},
	fade: function(opac)
	{
		var passed  = parseInt(opac, 10);
		var newOpac = parseInt(passed + 10, 10);
		if (newOpac < 90)
		{
			webSnapr.obj.style.opacity = webSnapr.ind.style.opacity = "." + newOpac;
			webSnapr.opacityTimer = window.setTimeout("webSnapr.fade('" + newOpac + "')", 20);
		}
		else
		{
			webSnapr.obj.style.opacity = webSnapr.ind.style.opacity = ".99";
		}
	},
	addEvent: function(obj, eventTypes, callback)
	{
		var eventType;
		for(var i = 0; i < eventTypes.length; i++)
		{
			eventType = eventTypes[i];

			if (obj.attachEvent)
			{
				obj["e" + eventType + callback] = callback;
				obj[eventType + callback] = function()
				{
					obj["e" + eventType + callback](window.event);
				};
				obj.attachEvent("on" + eventType, obj[eventType + callback]);
			}
			else
			{
				obj.addEventListener(eventType, callback, false);
			}
		}
	},
	isInContent: function(element)
	{
		if(element.parentNode && typeof(element.parentNode.className) !== "undefined")
		{
			if(element.parentNode.className.search(/item-content/i) !== -1)
			{
				return true;
			}
			else
			{
				return webSnapr.isInContent(element.parentNode);
			}
		}
		else
		{
			return false;
		}
	},
	isValidItemLink: function (link)
	{
		return link.getAttribute("href") &&
		 	link.rel.search(/no-preview/i) === -1 &&
			link.href.search(/\.(zip$|jpe?g$|png$|gif$|rar$|exe$|mp3$|mp4$)/i) === -1 && // Exclude links to images and archives.
			webSnapr.isInContent(link);
	}
};

//webSnapr.addEvent(window, ["load"], webSnapr.init);
