
var flickrTips = {
	fotoDiv: null,
	tilesX: 3,
	tilesY: 2,
	flickrRE: new RegExp("http://(www\\.)?flickr\\.com/photos/([^/]+)/(sets|tags|\\d+)(/([^/]+))?"),
	xmlhttp: null,
	fotos: new Object,

	addLoadEvent: function() {
		if (typeof window.onload == "function")
		{
			var temp = window.onload;
			window.onload = function() { temp(); flickrTips.init(); };
		}
		else
			window.onload = function() { flickrTips.init(); };
	},

	processPageLinks: function() {
		var alist = document.getElementsByTagName("a");
		if (!alist || !alist.length) return false;
		
		for (var i=0; i<alist.length; i++)
		{
			var a = alist[i];

			if (!this.flickrRE.test(a.href)) continue;
			if (a.getElementsByTagName("img").length) continue;

			a.style.textDecoration="none";
			a.style.borderBottom = "1px dotted #ff0084";
			a.onmouseover = this.show;			
			a.onmouseout = this.hide;
			a.onmousemove = this.move;
		}
	},

	init: function() {

		if (window.XMLHttpRequest)
			this.xmlhttp = new XMLHttpRequest;
		else if (window.ActiveXObject)
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		else
			return;

		this.fotoDiv = document.createElement("div");
		this.fotoDiv.id = "flickrTipsDiv";
		this.fotoDiv.style.position = "absolute";
		this.fotoDiv.style.left="0px";
		this.fotoDiv.style.top="200px";
		this.fotoDiv.style.visibility= "hidden";
		this.fotoDiv.style.border= "1px solid black";
		this.fotoDiv.style.padding= "3px";
		this.fotoDiv.style.backgroundColor= "white";

		this.fotos['spinner'] = document.createElement("img");
		this.fotos['spinner'].src = flickrTips.blogurl + "/wp-content/plugins/flickrtips/spinnywheel.gif";

		document.body.appendChild(this.fotoDiv);

		this.processPageLinks();
	},
	
	flickrResponse: function(e,key) {

		if (this.xmlhttp.readyState != 4) return;

		var temp = this.xmlhttp.responseText.split("\n");

		var result = new Array;

		var i=0;
		for (var y=0; y < flickrTips.tilesY; y++)
		for (var x=0; x < flickrTips.tilesX; x++)
		{
			if (temp[i].substr(0,4) != 'http')
				continue;

			var j = result.length;

			result[j] = document.createElement("img");
			result[j].src = temp[i];
			if (x) result[j].style.borderLeft = "1px solid white";
			if (y) result[j].style.borderTop = "1px solid white";

			if (++i >= temp.length) break;
		}

		flickrTips.fotos[key] = result;
		
		if (flickrTips.fotoDiv.style.visibility != "hidden") flickrTips.show(e,key);
		
	},

	move: function(e) {

		if (!e) e=window.event; 

		var x,y;

		var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
		var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1);

		if (e.pageX) { x=e.pageX; y=e.pageY; }
		else if (e.clientX) { 
			x = e.clientX;
			y = e.clientY;

			if (isIE) {
				x += document.body.scrollLeft;
				y += document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
			}
		}

		flickrTips.fotoDiv.style.left = (x+10) + "px";
		flickrTips.fotoDiv.style.top = (y+10) + "px";
	},
	
	show: function(e,fotoPage) {

		if (!e) var e = window.event;

		if (!fotoPage && !this.href) return;
		if (!fotoPage) fotoPage=this.href;	

		while (flickrTips.fotoDiv.hasChildNodes())
			flickrTips.fotoDiv.removeChild(flickrTips.fotoDiv.firstChild);

		flickrTips.fotoDiv.style.visibility='visible';

		if (!flickrTips.fotos[fotoPage])
		{
			flickrTips.fotoDiv.appendChild(flickrTips.fotos['spinner']);

			var matches = flickrTips.flickrRE.exec(fotoPage);
		
			var userID=matches[2];
			var fotoID, type;
			
			switch(matches[3]) {
				case "sets":  case "collections":  case "tags":
					type=matches[3];
					fotoID=matches[5];
					break;

				default:
					type='photos';
					fotoID=matches[3];
					break;
			}

			var flickrReq = {
				max: flickrTips.tilesX*flickrTips.tilesY,
				type: type,
				id: fotoID,
				user: userID
			};

			var flickrURL = flickrTips.blogurl + "/wp-content/plugins/flickrtips/ajaxgeturl.php?asdf=qwer";
			for (var i in flickrReq)
			{
				flickrURL += "&" + i + "=" + encodeURIComponent(flickrReq[i]);
			}

			flickrTips.xmlhttp.abort();
			flickrTips.xmlhttp.open("GET",flickrURL);
			flickrTips.xmlhttp.onreadystatechange=function() { flickrTips.flickrResponse(e,fotoPage); };
			flickrTips.xmlhttp.send("");

			return;
		}

		if (typeof flickrTips.fotos[fotoPage] == "object" && flickrTips.fotos[fotoPage].length)
		{
			var f = flickrTips.fotos[fotoPage];
			for (var i=0; i < f.length; i++)
			{
				//flickrTips.fotoImg[i] = f[i];
				flickrTips.fotoDiv.appendChild(f[i]);
				if (i % flickrTips.tilesX == flickrTips.tilesX - 1)
					flickrTips.fotoDiv.appendChild(document.createElement('br'));
			}
		}
		else
			flickrTips.hide();
	},
	
	hide: function(e) {
		flickrTips.fotoDiv.style.visibility="hidden";
	}

};




