$.fn.enlarge = function() {

var viewport = {
o: function() {
if (self.innerHeight) {
this.pageYOffset = self.pageYOffset;
this.pageXOffset = self.pageXOffset;
this.innerHeight = self.innerHeight;
this.innerWidth = self.innerWidth;
} else if (document.documentElement && document.documentElement.clientHeight) {
this.pageYOffset = document.documentElement.scrollTop;
this.pageXOffset = document.documentElement.scrollLeft;
this.innerHeight = document.documentElement.clientHeight;
this.innerWidth = document.documentElement.clientWidth;
} else if (document.body) {
this.pageYOffset = document.body.scrollTop;
this.pageXOffset = document.body.scrollLeft;
this.innerHeight = document.body.clientHeight;
this.innerWidth = document.body.clientWidth;
}
return this;
},

init: function(el) {
$(el).css({visibility: "visible"});
$(el).css("width",($(".enlarge_large_image").width() + 60));
$(el).css("left",Math.round(viewport.o().innerWidth/2) + viewport.o().pageXOffset - Math.round($(el).width()/2));
$(el).css("top",Math.round(viewport.o().innerHeight/2) + viewport.o().pageYOffset - Math.round($(el).height()/2));
$(el).hide().fadeIn("medium",function(){$('#enlarge_loading').remove();});
}
};

$("a[@rel='enlarge']").click(function(){

$("body").append("<div id='enlarge_loading'></div>");

$(".enlarge_window").remove();

// Checks if the image has a title, otherwise gets text in the HTML element (since it'll probably be an anchor
if($(this).find("img").attr("title")) {
var srtTitle = $(this).find("img").attr("title");
} else if($(this).attr("title")) {
var srtTitle = $(this).attr("title");
} else {
var srtTitle = $(this).text();
}

var strEnlarge = "<div class='enlarge_window'><div>";
strEnlarge += "<a href='#' class='enlarge_close'>[x] Close</a>";
strEnlarge += "<img src='" + $(this).attr("href") + "' class='enlarge_large_image' />";
strEnlarge += "<p>" + srtTitle + "</p>";
strEnlarge += "</div></div>";

$("body").append(strEnlarge);

//Check if image is loaded
$(".enlarge_large_image").load( function() { viewport.init(".enlarge_window"); });

$("#enlarge_overlay,.enlarge_window").click(function(){
$("#enlarge_overlay,.enlarge_window").fadeOut("medium",function(){$('#enlarge_overlay,#enlarge_loading,#enlarge_hideSelect,.enlarge_window').remove();});

if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
$("body","html").css({height: "auto", width: "auto"});
$("html").css("overflow","");
}

return false;
});
return false;
});

};

$(function() {
$("a[@rel='enlarge']").enlarge();
});