/**
VNK Zoomer v1.0
(c) 2011 VNK media (http://www.vnkmedia.nl)
**/
$.fn.vnkzoomer=function(settings){
	
	VNKzoomer={
		el:null,
		cur: null,
		set: new Array(),
		cache: [],
		thumb_w: null,
		thumb_h: null,
		
		init: function(el, settings){
			/* Apply settings */
			if (typeof(settings) != 'undefined') {
				try {
					for (s in settings)
						this.set[s]=settings[s];
				} catch(e) {}
			}
			
			
			$(el).css("cursor","url(/zoom.cur), pointer");

			/* preload images */
			if(this.set['preload'] == 1){
				for(i=0; i<el.length; i++){
					if($(el).eq(i).attr('rel') != undefined)
						VNKzoomer.preload($(el).eq(i).attr('rel'));
				}
			}
			
			/* Activate Zoom function */
			$(el).click(function(){
				if($('#vnkzoomer').css('display') == "block"){
					VNKzoomer.hide(this);
				}else{
					VNKzoomer.zoom(this);
				}
			});

			/* Het aanmaken en stijlen van de Zoom div */
			$("body").append("<img id=\"vnkzoomer\" />");
			$('#vnkzoomer').css({
				'position': 'absolute',
				'cursor':'url(/zoom.cur), pointer',
				'display':'none'
			});
			
			/* Activate hide function */
			$('#vnkzoomer').click(function(){
				VNKzoomer.hide();
			});
		},
		
		preload: function(src){
			var cacheImage = document.createElement('img');
			cacheImage.src = src;
			this.cache.push(cacheImage);
  		},
		
		zoom: function(i){
			this.cur=$(i);
			var oi = $(i).attr('src');
			if($(i).attr('rel') != undefined){
				var oi = $(i).attr('rel');
			}
			
			$('#vnkzoomer').attr("src",oi);
			var original_w = $('#vnkzoomer').width();
			var original_h = $('#vnkzoomer').height();
		
			/* Set zoomimage to the thumb style */
			$('#vnkzoomer').css({
				'left': ((($(i).offset().left-1)+1)+(($(i).css('padding-left').replace(/px/,'')-1)+1)),
				'top': ((($(i).offset().top-1)+1)+(($(i).css('padding-top').replace(/px/,'')-1)+1)),
				'width': $(i).width(),
				'height': $(i).height(),
				'display': 'block',
				'z-index': '9999999'
			});
			
			/* Enlarge image to the center of the screen */
			$('#vnkzoomer').stop(true,true).animate({
				'width': original_w,
				'height': original_h,
				'left': ($(window).width()-original_w)/2+$(window).scrollLeft(),
				'top': ($(window).height()-original_h)/2+$(window).scrollTop()
			},500);
		},
		
		hide: function(t){
			/* Put image back at starting point */
			$('#vnkzoomer').stop(true,true).animate({
				'left': ((($(this.cur).offset().left-1)+1)+(($(this.cur).css('padding-left').replace(/px/,'')-1)+1)),
				'top': ((($(this.cur).offset().top-1)+1)+(($(this.cur).css('padding-top').replace(/px/,'')-1)+1)),
				'width': $(this.cur).width(),
				'height': $(this.cur).height()
			},500);
			
			/* Hide and reset Styles */
			$('#vnkzoomer').queue(function() {
				$(this).hide();
				$(this).removeAttr("src");
				$(this).removeAttr("style");
				$(this).css({
					'position': 'absolute',
					'cursor':'url(zoom.cur), pointer',
					'display':'none'
				});
				$(this).dequeue();
			});
		}
	}
	
	VNKzoomer.init($(this),settings);
}
