
/** JQuery must be loaded... */

(function($) {
	//private vars:
	var default_opts = { 
			urlType : 1, /* 1 or 2, 1=?url= & 2=/http://... */
			width : 120,
			height : 120,
			dx : 25, 
			dy : 25,
			crop : true,
			preload : false, 
			border : 'solid 1px #000000'
	}, 
	theDiv = false,  baseHref = false, isInitialized = false;

	
	
	//PRIVATE FUNCTIONS
	function linkMouseOver(e, opts, fullURL) {  
		
		//if(!e) e = window.event;
		//if(!e) return;
		if(!theDiv) {
			//create
			var id = 'dvThumbId' + Math.round(Math.random() * 10000);
			$("body").append("<div id='"+id+"'></div>");
			theDiv = $("#" + id).get(0); 
			theDiv.style.display = 'none';
			theDiv.style.position = 'absolute';
			theDiv.style.top = '0px'; 
			theDiv.style.left = '0px'; 
			theDiv.style.border = 'solid 1px #000000';
		} 
		if(opts.border && theDiv.style.border != opts.border) 
			theDiv.style.border = opts.border;
		
		theDiv.style.top =  (e.pageY + opts.dy) + 'px'; 
		theDiv.style.left = (e.pageX + opts.dx) + 'px'; 
			
		theDiv.innerHTML = "<img src='" + fullURL + "' />";	
		//theDiv.innerHTML = "<img src='http://thumbs.vdburgh.net/v1/get/"+opts.sizeAndCrop+"?url=" + escape(lnk) + "' />";
		//theDiv.innerHTML = "<img src='http://thumbs.vdburgh.net/v1/get/"+sizeAndCrop+ lnk + "' />";
		theDiv.style.display = 'block';
	}
	function linkMouseOut(e) {
		if(theDiv) theDiv.style.display = 'none';
		return; 
	}
	function initPlugin() {
		isInitialized=true;
		var baseEl = $("base");
		if(baseEl.size() > 0) {
			baseEl = baseEl.get(0);
			if (baseEl.href) {
				baseHref = baseEl.href;
			} 
		}
	}
	
	//END PRIVATE FUNCTIONS
	
	
	//PUBLIC FUNCTIONS
	
	/**
	* options: width, height, crop [true|false], preload [true|false]
	*
	* Sample: jQuery("a").createThumbs({ width: 120, height: 120 });
	*/
	$.fn.createThumbs = function(options) {
		var opts = {};
		//var element = this;
		//merge default_opts & options into opts
		$.extend(opts, default_opts, options);  
		opts.sizeAndCrop = opts.width + '/' + opts.height + '/' + (opts.crop?1:0) + '/'; 
		
		if(!isInitialized) initPlugin();
		 
		function mo(e) { 
				linkMouseOver(e, opts);  
		}
	  var tmpImages = new Array();
		return this.each(function() {		
			//setup link, and check if we can show a thumbnail
			var l = this.href;  
			if(!l) return;
			var s = new String('' + l); 
			var isAbsolute = false;
			if(s.indexOf('http://') == 0) {
				//ok
				isAbsolute = true; 
			} else if(s.indexOf('https://') == 0 || s.indexOf('ftp://') == 0 || s.indexOf("://")!=-1) { 
				return;
			} else if(!baseHref){
				//error
				return;
			}
			if(!isAbsolute)  
				s = baseHref + s;
			var lnk = s;
			var fullURL = "http://thumbs.vdburgh.net/v1/get/"+opts.sizeAndCrop+"?url=" + escape(lnk);
			if(opts.urlType == 2)
				fullURL = "http://thumbs.vdburgh.net/v1/get/"+opts.sizeAndCrop + lnk;
				
			if(opts.preload) {
				var img1 = new Image();
				img1.src = fullURL;			
				tmpImages[tmpImages.length] = img1;	
			}
			
			//attach hover event
		  $(this).hover(function(e) { linkMouseOver(e, opts, fullURL); } , linkMouseOut);
		});
	}; 

	 
})(jQuery);

