$(document).ready(function() {

	$(window)
	.resize(function() {
		positionEnlarge();
	});

	$("a.enlarge")
	.each(function(e){
		var href = $(this).attr('href');
		MM_preloadImages(href);
	})
	.click(function(e) {

		e.preventDefault();

		var href = $(this).attr('href');
		var position = "fixed";
		var height = "100%";

		if( $.browser.msie ) {
			if ($.browser.version < 7.0) {
				position = "absolute";
				height = document.documentElement.clientHeight;
				$('html').css({"overflow":"hidden"});
				$(window).resize(function(){
					if ($.browser.version < 7.0){
						$("#overlay").height(document.documentElement.clientHeight);
					}
				});
			}
		}

		$('<div id="overlay"></div>')
		.appendTo('body')
		.css({
			"background": "#f45c19"
			,"position": position
			,"height": height
			,"top": "0px"
			,"left": "0px"
			,"width": "100%"
			,"opacity": "0.0"
			,"z-index": "200"
		})
		.click(function() {
			removeEnlarge();
		})
		.animate({
			"opacity": "0.9"
		}, "slow", function() {

			var o = $('<div/>')
			.attr("id", "enlarge")
			.hide()
			.css({
				"position": position
				,"background": "#fff"
				,"z-index": "300"

			})
			.appendTo('body')
			;

			$('<img />')
			.attr('src', "/libs/image-enlarge/close-button.png")
			.appendTo(o)
			.css({
				"margin": "20px"
				,"width": "47px"
				,"height": "47px"
				,"position": "absolute"
				,"top": "-60px"
				,"right": "-60px"
				,"cursor": "pointer"
			})
			.click(function() {
				removeEnlarge();
			})
			;

			var i = $('<img />')
			.attr('src', href)
			.css({
				"margin": "20px"
				,"width": "auto"
				,"height": "auto"
			})
			.click(function() {
				removeEnlarge();
			})
			;

			var d = $("<div/>")
			.attr("id","ImageContainer")
			.css({
				"overflow-y" : "auto"
				,"overflow-x" : "auto"
			})
			.appendTo(o)
			.append(i)
			.ready(function() {
				positionEnlarge(); //fix for ie
			})
			.load(function() {
				positionEnlarge();
			})
			;

			o.fadeIn();

		})
		;

		OmnitureCustom("Enlarge:" + href);

		return false;
	});

});

function positionEnlarge() {

	var i = $("#ImageContainer img");
	if( !(i.height() > 0) ){
		setTimeout("positionEnlarge();",100);
		return;
	};

	var top = ( $(window).height() - i.height() ) / 2;
	var left = ( $(window).width() - i.width() - 60 ) / 2;

	if ( top < 40 ) top = 40;
	if ( left < 20 ) left = 20;

	if ( top + i.height() + 60 > $(window).height() ) {
		$("#ImageContainer").height( $(window).height() - 60 );
	} else {
		$("#ImageContainer").height( i.height() + 40 );
	}

	if ( left + i.width() + 80 > $(window).width() ) {
		$("#ImageContainer").width( $(window).width() - 60 );
	} else {
		$("#ImageContainer").width( i.width() + 40 );
	}

	var lb = $('#enlarge')
	.css({
		'top': top,
		'left': left
	});

	if( $.browser.msie ) {
		if( $.browser.version < 7.0 ) {
			var docScrollTop = document.documentElement.scrollTop;
			$('#overlay').css({
				"top": docScrollTop + "px"
				,"height": document.documentElement.clientHeight
			})
			$('#enlarge').css({
				"top": docScrollTop + 100 + "px"
			})
			$('#enlarge img').css({
				"behavior": "url(css/iepngfix.htc)"
			});
			$("#ImageContainer").css({
				"display" : "block"
				,"overfloaw": "auto"
			});
		}
		$(lb).show();
	} else {
		$(lb).fadeIn();
	}
}

function removeEnlarge() {
	if( $.browser.msie ) {
		$("html").css({"overflow-y":"auto"});
		$("#overlay, #enlarge").remove();
	} else {
		$('#overlay, #enlarge')
		.fadeOut('slow',function() {
			$(this).remove();
		});
	}
}
