/*
 * Overlay - jQuery plugin
 * Version: 1.0 (8/08/2009)
 * Copyright (c) 2009 Jacaranda
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.3+
*/
;(function($) {

	$.fn.fixPNG = function() {
		return this.each(function () {
			var image = $(this).css('backgroundImage');
			if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
				image = RegExp.$1;
				$(this).css({
					'backgroundImage': 'none',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
				}).each(function () {
					var position = $(this).css('position');
					if (position != 'absolute' && position != 'relative')
						$(this).css('position', 'relative');
				});
			}
		});
	};

	var opts;
	var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);

	$.fn.imageoverlay = function(settings) {
	    settings = $.extend({}, $.fn.imageoverlay.defaults, settings);
	    opts = settings;
		var ret = this.each(function () {
			var html = '<div class="overlay-image"/>';
			var ov = $(this).parent().append(html).find(":last");
			var op = ov.offsetParent();
			
			var pos = $(this).offset();
			
			var ol = (ov == null)? pos.left : pos.left - op.offset().left;
			var ot = (ov == null)? pos.too : pos.top - op.offset().top;
			
			if (opts.position == 'bottomright') {
			    var l = Math.round(ol + ($(this).width() - ov.width())) - 5;
			    var t = Math.round(ot + ($(this).height() - ov.height())) - 5;
			} else {
			    var l = Math.round(ol + ($(this).width()/2 - ov.width()/2));
			    var t = Math.round(ot + ($(this).height()/2 - ov.height()/2));
			}
			ov.css({'position':'absolute', 'top': t + 'px', 'left': l  + 'px'});
		});
		if (isIE) {
			$('.overlay-image').fixPNG();
		}
		return ret;
	};
	
	$.fn.imageoverlay.defaults = {
		position			:	'bottomright'
	};

})(jQuery);