// Von:  http://nettuts.com/videos/screencasts/learn-how-to-create-a-jquery-plugin/


(function($){

$.fn.center = function(options){
	
	 var options = $.extend({
            parentElement : window,
            center_x: true,
            center_y: true
        }, options);
	
        
	$(this).each(function(){
		var elem = $(this);
		
		changeCss(elem, options, false);
		
		$(window).bind("resize", function(){
		    changeCss(elem, options, true);
		});
		
		function changeCss(element, options, do_animation){
			var parent = options.parentElement;
			
		    var imageHeight = $(element).height();
		    var imageWidth = $(element).width();
		    var windowWidth = $(parent).width();
		    var windowHeight = $(parent).height();
		    
			var left = windowWidth / 2 - imageWidth / 2;
			var top = windowHeight /2 - imageHeight / 2;
			
			
			if (options.center_y===false) {
				top = $(element).css("top");
			}
			if (options.center_x===false) {
				left = $(element).css("left");
			}
			if (do_animation===false) {
			    $(element).css({
			        "position" : "absolute",
			        "left" : left,
			        "top" : top
			    });
			}
			else {
				$(element).animate({
			        left: left,
			        top: top
			    }, "normal", "swing");
			}
		};
		
	});
	
};

})(jQuery);
