﻿this.imagePreview = function() {
    /* CONFIG */
    xOffset = 10;
    yOffset = 30;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result
    /* END CONFIG */
    $("a.preview").hover(function(e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? this.t + "<br/>" : "";
        $("body").append("<p id='preview'>" + c + "<img src='" + this.href + "' alt='Image preview' class='prodimg' /></p>");
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.show();
    },
	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");
    });
    $("a.preview").click(function(e) {
        var id = parseInt(this.id.replace('i', ''));
        document.getElementById(id).checked = true;
        document.getElementById(id).click();
        $("#preview").remove();
        $("#l" + id).animate({ backgroundColor: "#FFFF99" }, 600).animate({ backgroundColor: "#EEEEEE" }, 600);

        //$("#l" + id).toggle("fast").css("background-color", "#EEEEEE");
        return false;
    });
    $("img.preview").mouseover(function(e) {
        this.t = this.alt;
        var c = (this.alt != "") ? this.alt + "<br/>" : "";
        this.alt = '';
        $("body").append("<p id='preview'>" + c + "<img src='" + this.src + "' alt='Image preview' /></p>");
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.show();
    },
	function() {
	    //this.alt = this.t;
	    //$("#preview").remove();
	});
    $("img.preview").mouseout(function(e) { this.alt = this.t; $("#preview").remove(); });
    $("img.preview").mousemove(function(e) {
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });
    $("img.preview").click(function(e) {
        var parentid = '#' + $(this).parent("div").attr("id");
        var href = $(parentid).find('.add-to-basket').attr("href");
        location = href;
        return false;
    });
};
// starting the script on page load
$(document).ready(function() {
    imagePreview();
});