//
// create closure
//
(function($) {
	//
	// plugin definition
	//
	$.fn.zoom = function(options) {
		debug(this);
		// build main options before element iteration
		var opts = $.extend( {}, $.fn.zoom.defaults, options);

		// iterate and reformat each matched element
		this.each( function() {
			var a       = $(this);
			var img_href = $(this).attr("href");
			var img     = $("img", this);
			var thumb_img_height = img.height();
			var thumb_img_width = img.width();

			$(this).wrap('<div style="position:relative; width:'+(thumb_img_width+2)+'px"></div>');
			$(this).parent("div").append('<div class="position_bottom" style="display:block;"></div>');
			$(this).parent("div").append('<div class="zoom_mask_div" style="display:none;"></div>');
			$(this).parent("div").append('<div class="zoom_div" style="display:none;"><img style="position: relative;" src="'+img_href+'"></div>');
			$(this).parent("div").children(".zoom_div").css("left", thumb_img_width+10);
			
			if(!thumb_img_height) {
				var img_pos = $(this).parent("div").position();
				var div_pos = $(this).parent("div").children("div.position_bottom").position();

			}
			/**
			 * Remove all titles/alts (Breaks the zoomer)
			 */
			$(this).removeAttr("title").removeAttr("alt");
			$("img", this).removeAttr("title").removeAttr("alt").removeAttr("longdesc");
			var mask_size_width = $(this).parent("div").children(".zoom_mask_div").css("width");
			var mask_size_height = $(this).parent("div").children(".zoom_mask_div").css("height");
			mask_size_width = mask_size_width.replace("px", "");
			mask_size_height= mask_size_height.replace("px", "");
			
			$(this).css("display", "block");
			$(this).parent("div").mousemove(function(e){
				
				var thumb_img_height = $(this).children("a").children("img").height();
				var thumb_img_width = $(this).children("a").children("img").width();
				
				
				
				var mouseX = e.pageX;
				var mouseY = e.pageY;

				offset = $(this).offset();
				  var x = (mouseX - offset.left);
				  var y = (mouseY - offset.top);
				  	
			        var x_mask = x - (mask_size_width/2);
			        var y_mask = y - (mask_size_height/2);

				//Top/Left Mask
				if(x_mask<0) { x_mask = 0; } 
				if(y_mask<0) { y_mask = 0; }

				//Bottom/Right Mask
				if(x_mask>(thumb_img_width-mask_size_width)) { x_mask = thumb_img_width-mask_size_width; } 
				if(y_mask>(thumb_img_height-mask_size_height)) { y_mask = thumb_img_height-mask_size_height; }
			  
				  $(this).children("div.zoom_mask_div").css("display", "");
				  $(this).children("div.zoom_mask_div").css("position", "absolute");
				  
				  $(this).children("div.zoom_div").css("display", "");
				  
				  
				  
				  $(this).children("div.zoom_mask_div").css("left", x_mask);
				  $(this).children("div.zoom_mask_div").css("top", y_mask);
				  
				  
				  
			        //Big Image Time.... GOOD LUCK!
					var img = new Image();
					img.src = $(this).children("div.zoom_div").children("img").attr("src");

			        var big_image_size_width = $(this).children("div.zoom_div").children("img").width();
			        var big_image_size_height = img.height/(img.width/big_image_size_width);

			        var img_zoom_div_width = $(this).children("div.zoom_div").width();
			        var img_zoom_div_height = $(this).children("div.zoom_div").height();
			        
					var image_scale_x = (big_image_size_width / thumb_img_width);					
					var image_scale_y = (big_image_size_height / thumb_img_height);
					debug(image_scale_x+'/'+image_scale_y);
					var x_big_image   = (x_mask * image_scale_x);
					var y_big_image   = (y_mask * image_scale_y);

					var x_symbol = "";
					var y_symbol = "";
				    if(x_big_image > 0)x_symbol = "-";
				    if(y_big_image > 0)y_symbol = "-";

   				    $(this).children("div.zoom_div").children("img").css("left", x_symbol + x_big_image + 'px');
					$(this).children("div.zoom_div").children("img").css("top", y_symbol + y_big_image + 'px');
	
				  
			}).mouseout(function(){
				$(this).children("div.zoom_mask_div").css("display", "none");
				$(this).children("div.zoom_div").css("display", "none");
				//debug("Left obj");
		    });
			


			});
	};
	//
	// private function for debugging
	//
	function debug($obj) {
		return false;
		if (window.console && window.console.log)
			window.console.log($obj);
	}
	;
	//
	// define and expose our format function
	//
	$.fn.zoom.format = function(txt) {
		return '<strong>' + txt + '</strong>';
	};
	//
	// plugin defaults
	//
	$.fn.zoom.defaults = {
		foreground :'red',
		background :'yellow'
	};
	//
	// end of closure
	//
})(jQuery);

function change_image(new_src){

    var main_img = $("#main_pimg");
    var zoom_img = main_img.children().children().parent(".zoom_div").children("img");
    $(".zoom_div").children("img").attr("src", "/hr_images/" + new_src);
    main_img.parent("a").attr("href","/hr_images/" + new_src);
    
     
    main_img.attr("src", "/lg_images/" + new_src);
    zoom_img.attr("src", "/hr_images/" + new_src);
    }

