jQuery.noConflict()

jQuery.thumbnailviewer={
	loadmsg: '<img src="template/default/spinningred.gif" />', //HTML for loading message. Make sure image paths are correct

	dsetting: {trigger:'mouseover', untrigger:'mouseout', preload:'yes', fxduration:500, enabletitle:'yes'}, //default settings
	buildimage:function($, $anchor, setting){
		var imghtml='<img src="'+setting.imgsrc+'" style="border-width:0" />'
		imghtml='<div>'+imghtml+((setting.enabletitle && $anchor.attr('title')!='')? '<br />'+$anchor.attr('title') : '')+'</div>'
		return $(imghtml)
	},

	showimage:function($image, setting){
		$image.stop().fadeIn(setting.fxduration, function(){
			if (this.style && this.style.removeAttribute)
				this.style.removeAttribute('filter') //fix IE clearType problem when animation is fade-in
		})
	},

	hideimage:function($image, setting){
		$image.stop().fadeOut(setting.fxduration)
	}

}

jQuery.fn.addthumbnailviewer=function(options){
	var $=jQuery

	return this.each(function(){ //return jQuery obj
		if (this.tagName!="A")
			return true //skip to next matched element

		var $anchor=$(this)
		var s=$.extend({}, $.thumbnailviewer.dsetting, options) //merge user options with defaults
		s.fxduration=parseInt(s.fxduration)
		if (s.preload=="yes"){
			var hiddenimage=new Image()
			hiddenimage.src=s.imgsrc
		}
		var $loadarea=$('#'+s.targetdiv)
		var $hiddenimagediv=$('<div />').css({position:'absolute',visibility:'hidden',left:-10000,top:-10000}).appendTo(document.body) //hidden div to load enlarged image in
		
		// Make the picture appear
		var triggerevt=s.trigger+'.thumbevt' //"click" or "mouseover"
		$anchor.unbind(triggerevt).bind(triggerevt, function(){
			$loadarea.data('$curanchor', $anchor)
			if ($loadarea.data('$queueimage')){ //if a large image is in the queue to be shown
				$loadarea.data('$queueimage').unbind('load') //stop it first before showing current image
			}
			$loadarea.html($.thumbnailviewer.loadmsg)
			var $hiddenimage=$hiddenimagediv.find('img')
			
			// Load the real stuff
			$loadarea.show()
			var $hiddenimage=$('<img src="'+s.imgsrc+'" />').appendTo($hiddenimagediv) //populate hidden div with enlarged image
			var $targetimage=$.thumbnailviewer.buildimage($, $anchor, s).hide() //create/reference actual enlarged image
			$hiddenimage.bind('loadevt', function(e){ //when enlarged image has fully loaded
				$loadarea.empty().append($targetimage) //show enlarged image
				$.thumbnailviewer.showimage($targetimage, s)
			})
			$hiddenimage.bind('unloadevt', function(e){ //when enlarged image has fully loaded
				$.thumbnailviewer.hideimage($targetimage, s)
			})
			$loadarea.data('$queueimage', $hiddenimage) //remember currently loading image as image being queued to load

			if ($hiddenimage.get(0).complete) {
				$hiddenimage.trigger('loadevt')
			} else {
				$hiddenimage.bind('load', function(){$hiddenimage.trigger('loadevt')})
			}
			return false
		})
		
		// Make the picture follow the mouse
		$anchor.mousemove(function(e){
			var placeY, placeX;
			if ( (e.pageY + $loadarea.height() + 50) > $(window).height() ) {
				// If not enough place right/above
				placeY = e.pageY - ($loadarea.height() + 50)
			} else {
				// Default right/below
				placeY = e.pageY + 50
			}
			placeX = e.pageX + 15
			//alert($loadarea.width())
			$loadarea.css({
				top: placeY + "px",
				left: placeX + "px"
			})
		})
		
		// Make the picture disappear
		var untriggerevt=s.untrigger+'.thumbunevt' //"click" or "mouseover"
		$anchor.unbind(untriggerevt).bind(untriggerevt, function(){
			var $hiddenimage=$hiddenimagediv.find('img')
			if ($hiddenimage.get(0).complete) {
				$hiddenimage.trigger('unloadevt')
			}
			$loadarea.hide()
		
			return false
		})
	})
}

jQuery(document).ready(function($){
	
	// Link all image of the document with zoom feature
	var $anchors=$('a[rel="enlargeimage"]') //look for links with rel="enlargeimage"
	$anchors.each(function(i){
		var options={}
		var rawopts=this.getAttribute('rev').split(',') //transform rev="x:value1,y:value2,etc" into a real object
		for (var i=0; i<rawopts.length; i++){
			var namevalpair=rawopts[i].split(/:(?!\/\/)/) //avoid spitting ":" inside "http://blabla"
			options[jQuery.trim(namevalpair[0])]=jQuery.trim(namevalpair[1])
		}
		$(this).addthumbnailviewer(options)
	})
	
})
