
(function() {

	if (!window.console) window.console = { log: function() { } };

	var OVERLAY_IMAGE_URL = "/media/css/images/image_corners.png";
	var OVERLAY_IMAGE_URL_IE6 = "/media/css/images/image_corners.gif";
	var OVERLAY_SECTION_WIDTH = 32;
	var OVERLAY_SECTION_HEIGHT = 32;

	function processOverlay() {

		var img = $(this);

		img.removeClass("overlay");

		this.parentNode.style.position = "relative";

		var overlay = document.createElement("img");

		var isIE6 = jQuery.browser.msie && jQuery.browser.version === "6.0";
		var isIE7 = jQuery.browser.msie && jQuery.browser.version === "7.0";

		overlay.src = isIE6 ? OVERLAY_IMAGE_URL_IE6 : OVERLAY_IMAGE_URL;
		overlay.width = OVERLAY_SECTION_WIDTH*2;
		overlay.height = OVERLAY_SECTION_HEIGHT*2;

		var x = this.offsetLeft;
		var y = this.offsetTop;

		var clipX = 0;
		var clipY = 0;

		if (img.hasClass("right")) {
			x += (this.offsetWidth - OVERLAY_SECTION_WIDTH);
		} else {
			x -= OVERLAY_SECTION_WIDTH;
			clipX += OVERLAY_SECTION_WIDTH;
		}
		
		if (img.hasClass("right") && img.attr("id") == "recipe-slider") {
			x += 30;
		}
		
		if (img.hasClass("bottom")) {
			y += (this.offsetHeight - OVERLAY_SECTION_HEIGHT);
		} else {
			y -= OVERLAY_SECTION_HEIGHT;
			clipY += OVERLAY_SECTION_HEIGHT;
		}

		if (isIE6) x -= 25;
		if (isIE7) x -= 25;

	//	var shim = document.createElement("iframe");

		overlay.style.clip = "rect(" + clipY + "px," + (clipX + OVERLAY_SECTION_WIDTH) + "px," + (clipY+OVERLAY_SECTION_HEIGHT) + "px," + clipX + "px)";
		overlay.style.position = "absolute";
		overlay.style.zIndex = 100;
		overlay.style.top = y + "px";
		overlay.style.left = x + "px";
	/*
		shim.frameBorder = 0;
		shim.style.position = "absolute";
		shim.style.filter = "alpha(opacity=0)";
		shim.style.opacity = "0";
		shim.style.zIndex = 99;
		shim.style.width = (OVERLAY_SECTION_WIDTH*2) + "px";
		shim.style.height = (OVERLAY_SECTION_HEIGHT*2) + "px";
		shim.style.overflow = "hidden";
		shim.style.top = y + "px";
		shim.style.left = x + "px";
	*/
	//	shim.innerHTML = "<iframe frameborder='0' width='100%' height='100%' style='background:transparent'></iframe>";

		$(overlay).addClass("overlay-image");

		this.parentNode.insertBefore(overlay, this);

	//	this.parentNode.insertBefore(shim, this.nextSibling);

	}

	function processOverlays() {


		$("img.overlay").each(function(i) {

			var img = $(this);
			processOverlay.call(this);
		});

    	$("div.overlay").each(function(i) {
			var img = $(this);
    		processOverlay.call(this);
		});

	}

	window.processOverlays = processOverlays;

	$(processOverlays);

})();

