/*
 * OpenCart 1.x jQuery Image Hover Lite script by Qphoria
 * 
 * Based on Image Preview by Alen Grakalic 
 * Enhanced for dynamic use, alternative source, corrected quoting, and internal css by Qphoria
 *
 */
 
// starting the script on page load
$(document).ready(function(){
	var $hoverSize = "-250x250"; // Default "-250x250" (note the dash) This should match what you have set for "Product Image Thumb Size" or "Product Image Popup Size" in the System->Settings->Image

	$('.middle img').parent().addClass('preview');
	$('#image').parent().removeClass('preview');
	$('.middle img').each(function() {
		var $imgsrc = $(this).attr('src');
		if (!$hoverSize) { $imgsrc = $imgsrc.replace(/cache/, ""); } 
		
		// Getting image sizes with javascript requires the image to be loaded once in the local cache
		// In the case where an image hasn't loaded, try to scan for other usual sizes and replace.
		var $thumbSize = '-' + $(this).width() + 'x' + $(this).height();
		if ($thumbSize.indexOf('0x0') != -1){
			var $thumbSize = "-38x38";
			$imgsrc = $imgsrc.replace($thumbSize, $hoverSize);
			//var $thumbSize = "-75x75";
			//$imgsrc = $imgsrc.replace($thumbSize, $hoverSize);
			var $thumbSize = "-120x120";
			$imgsrc = $imgsrc.replace($thumbSize, $hoverSize);
			var $thumbSize = "-125x125";
			$imgsrc = $imgsrc.replace($thumbSize, $hoverSize);
			var $thumbSize = "-150x150";
			$imgsrc = $imgsrc.replace($thumbSize, $hoverSize);
		} else {
			$imgsrc = $imgsrc.replace($thumbSize, $hoverSize);  
		}
		$(this).parent('a').attr('hover', $imgsrc);
	});
	imagePreview(); 
});

this.imagePreview = function(){	
	/* START CONFIG */
	// these 2 variable determine popup's distance from the cursor you might want to adjust to get the right result
	xOffset = 10;
	yOffset = 30;
	
	/* END CONFIG */
	
	$("a.preview").hover(function(e){
		
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append('<p id="preview"><img src="'+ $(this).attr('hover') +'" alt="'+this.title+'" />'+ c +'</p>');								 
		$("#preview")
			.css("position", "absolute")
			.css("border", "1px solid #ccc")
			.css("background", "#FFF")
			.css("padding", "1px")
			.css("display", "none")
			.css("color", "#fff")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");			
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function getImgSize(imgSrc) {
	var newImg = new Image();
	newImg.src = imgSrc;
	var height = newImg.height;
	var width = newImg.width;
	alert ('The image size is '+width+'*'+height);
	return width + 'x' + height;
}
