﻿/*
	Class:    	dwIMageProtector
	Author:   	David Walsh
	Website:    http://davidwalsh.name
	Version:  	1.0.0
	Date:     	08/09/2008
	Built For:  jQuery 1.2.6
*/

jQuery.fn.protectImage = function(settings) {
	settings = jQuery.extend({
		image: '/images/blank.gif',
		zIndex: 10
	}, settings);
	return this.each(function() {
    var position = $(this).offset();
    var height = $(this).height();
    var width = $(this).width();
    var srcimg = this;

    var protect = $('<img id=\'protector\' />').attr({
    width: width+10,
    height: height+10,
    src: settings.image
    }).css({
   /* border: '1px solid #f00', */
    top: position.top,
    left: position.left,
    position: 'absolute',
    zIndex: settings.zIndex
    }).bind(
    'mouseenter', function() {
    if($(srcimg).hasClass('imagecache-product_list')) {
    $(srcimg).toggleClass('hovering');
    }
    $(srcimg).trigger("mouseenter");
    }).bind(
    'mouseleave', function() {
    if($(srcimg).hasClass('imagecache-product_list')) {
    $(srcimg).toggleClass('hovering');
    }
    $(srcimg).trigger("mouseleave");
    });

    if($(srcimg).parent().get(0).tagName == "A") {
    link = $(srcimg).parent().clone();
    link.html(protect);
    link.appendTo('body');
    }
    else {
        var oP = document.getElementById("protector");
        if(oP){document.body.removeChild(oP);}
        $(protect).appendTo('body');
    }

    });
    };

    $(window).bind('load', function() {
	    $('img.protect').protectImage();
    });
    
    function doSomething() {
    $('img.protect').protectImage();
    };

    var resizeTimer = null;
    $(window).bind('resize', function() {
        if (resizeTimer) clearTimeout(resizeTimer);
        resizeTimer = setTimeout(doSomething, 100);
    });