(function($){

var current_small;
var current_large;
var zoomed = false;

var mouse_down = false;

var image_frame;
var preload_frame;

var last_selected = false;

var zoom_button;
var zoom_in_img;
var zoom_out_img;

var current_width = 0;
var current_height = 0;
var mouse_start_x = 0;
var mouse_start_y = 0;
var curr_offset_x = 0;
var curr_offset_y = 0;
	
$.fn.imgZoomer = function(options){
	var defaults = {
		zoom_button: "",
		zoom_in: "",
		zoom_out: ""
	};
	var opts = $.extend(defaults, options);
	
	image_frame = $(this);
	zoom_button = $(opts.zoom_button);
	zoom_in_img = ROOTURL+opts.zoom_in;
	zoom_out_img = ROOTURL+opts.zoom_out;
	
	$(this).append('<div id="imgZoomer_preload" style="height: 0px; width: 0px; overflow: hidden;"></div>');
	preload_frame = $(this).find("div#imgZoomer_preload");
	
	image_frame.hover(function(){
		image_frame.css("cursor","pointer");
	},function(){});
	
	image_frame.mousedown(function(e){
		mouse_start_x = e.pageX - curr_offset_x;
		mouse_start_y = e.pageY - curr_offset_y;
		mouse_down = true;
		return false;
	});
	
	$(document).mouseup(function(e){
		mouse_down = false;
		return false;
	});
	
	$(document).mousemove(function(e){
		if( mouse_down == true && zoomed == true ){
			curr_offset_x = e.pageX - mouse_start_x;
			curr_offset_y = e.pageY - mouse_start_y;
			
			if(curr_offset_x > 0){ curr_offset_x = 0; }
			if(curr_offset_y > 0){ curr_offset_y = 0; }
			
			if(curr_offset_x < -current_width+image_frame.width() ){ curr_offset_x = -current_width+image_frame.width(); }
			if(curr_offset_y < -current_height+image_frame.height() ){ curr_offset_y = -current_height+image_frame.height(); }
			
			image_frame.css("background-position",curr_offset_x+"px "+curr_offset_y+"px");
			return false;
		}
	});
	
	image_frame.mouseup(function(e){
		if( mouse_down == true && zoomed == false ){
			zoom_in();
		}
		
		mouse_down = false;
		return false;
	});
	
	preload_img(zoom_in_img);
	preload_img(zoom_out_img);
	
	zoom_button.click(function(){
		if( zoomed == false){
			zoom_in();
		}else{
			zoom_out();
		}
		return false;
	});

}

$.fn.imgZoomerThumbs = function(options){
	var defaults = {
		on_selected: "selected"
	};
	var opts = $.extend(defaults, options);
	
	$(this).each(function(){
		preload_img( $(this).attr("href") );
		$(this).click(function(){
			replace_image( $(this).attr("href"), $(this).attr("rel") );
			if( last_selected != false ){ last_selected.removeClass(opts.on_selected); }
			$(this).find("img").addClass(opts.on_selected);
			last_selected = $(this).find("img");
			return false;
		});
	});
	

}

function replace_image( img_small, img_large ){
	current_small = img_small;
	current_large = img_large;
	
	image_frame.css("background",'none');
	image_frame.css("background-image",'url("'+current_small+'")');
	image_frame.css("background-position","0px 0px");
	
	zoom_button.attr("src",zoom_in_img);
	zoomed = false;
	
	preload_img(current_large);
}

function zoom_in(){
	var preloadImg = new Image();
	preloadImg.src = current_large;
	
	current_width = preloadImg.width;
	current_height = preloadImg.height;
	
	curr_offset_x = -((current_width / 2) - (image_frame.width() / 2));
	curr_offset_y = -((current_height / 2) - (image_frame.height() / 2));

	image_frame.css("background",'none');
	image_frame.css("background-image",'url("'+current_large+'")');
	image_frame.css("background-position",curr_offset_x+"px "+curr_offset_y+"px");
	zoom_button.attr("src",zoom_out_img);
	zoomed = true;
}

function zoom_out(){
	image_frame.css("background",'none');
	image_frame.css("background-image",'url("'+current_small+'")');
	image_frame.css("background-position","0px 0px");
	zoom_button.attr("src",zoom_in_img);
	zoomed = false;
}

function preload_img( img ){
	if( image_frame.find("div#imgZoomer_preload").find("img[src="+img+"]").length == 0 ){
		image_frame.find("div#imgZoomer_preload").append('<img src="'+img+'" height="1" width="1" alt="" />');
	}
}


})(jQuery);

