/* Zoomit version 0.5 - Erlend Oftedal */

var zoomIt = {};

zoomIt.active = null;


zoomIt.zoomIn = function(link) {
	var img = new Image();
	var thumb = $(link).find("img")[0];
	jQuery(link).css("text-decoration", "none");
	img.onload = function() {
		zoomIt.zoomImage(thumb, img);
	};
	img.src = link.href;
	if (thumb.loader == null) {
		zoomIt.createLoader(thumb);
	}
	jQuery(thumb.loader).show();
	zoomIt.active = thumb;
	return false;
}
zoomIt.createFull = function(thumb, preloadedImg) {
	var img = document.createElement("img");
	jQuery(img).attr("src", preloadedImg.src);
	jQuery(img).css("position", "absolute").css("z-index", "20")
	img.className = "zoomItFull";
	thumb.full = img;
	img.thumb = thumb;
	img.onclick = function() {
		return zoomIt.zoomOut(thumb);			
	};
}
zoomIt.createLoader = function(thumb) {
	var loader = document.createElement("div");
	jQuery(loader).css("position", "absolute").css("z-index", "10")
		.css("top", jQuery(thumb).offset().top).css("left", jQuery(thumb).offset().left);
	jQuery(loader).width(jQuery(thumb).width());
	thumb.loader = loader;
	jQuery(loader).css("color", "#ffffff")
		.css("opacity", "0.5").css("background-color", "#000000")
		.css("font-weight", "bold");
	jQuery(loader).css("text-align", "center").html("Loading...");
	thumb.parentNode.appendChild(loader);
}


zoomIt.zoomImage = function(thumb, preloadedImg) {
	jQuery(thumb.loader).hide();
	var img;
	if (thumb.full == null) {
		zoomIt.createFull(thumb, preloadedImg);
	}
	img = thumb.full;
	jQuery(img).css("top", jQuery(thumb).offset().top).css("left", jQuery(thumb).offset().left);
	jQuery(img).width(jQuery(thumb).width());
	jQuery(img).height(jQuery(thumb).height());
	if (img.parentNode == null) {
		document.body.appendChild(img);
	}
	var limit = 5;
	var dimension = jQuery.clientCoords();
	var topDest = jQuery(thumb).offset().top - preloadedImg.height/2;
	topDest = zoomIt.adjust(topDest - document.body.scrollTop, dimension.height, preloadedImg.height, limit) + document.body.scrollTop;
	var leftDest = jQuery(thumb).offset().left - preloadedImg.width/2;
	leftDest = zoomIt.adjust(leftDest - document.body.scrollLeft, dimension.width, preloadedImg.width, limit) + document.body.scrollLeft;
	$(img).animate(
		{ top: topDest, left: leftDest, width: preloadedImg.width, height: preloadedImg.height},
		500, null,
		function() {
			if (zoomIt.active != thumb) {
				zoomIt.zoomOut(thumb);
			} else {
				zoomIt.decorate(thumb);
			}
		}
	);
}

zoomIt.decorate = function(thumb) {
	if (thumb.decoration == null) {
		var div = document.createElement("div");
		var divInner = document.createElement("div");
		div.appendChild(divInner);
		divInner.className = "zoomitTitle";
		jQuery(div).css("position", "absolute").css("zIndex","19");
		div.inner = divInner;
		thumb.decoration = div;
		thumb.parentNode.appendChild(div);
	}
	var deco = thumb.decoration;
	jQuery(deco).show();
	jQuery(deco).width(jQuery(thumb.full).width());
	jQuery(deco.inner).html(thumb.parentNode.title);
	var endTop = (jQuery(thumb.full).offset().top + jQuery(thumb.full).height());
	jQuery(deco).css("top", endTop  - jQuery(deco).height())
		.css("left", jQuery(thumb.full).offset().left);
	jQuery(deco).animate(
		{top: endTop},
		500
	);
}


zoomIt.adjust = function(value, maxValue, objSize, margin) {
	if (value < margin) return margin;
	if ((maxValue - value - objSize) < margin) return maxValue - margin - objSize;
	return value;
}

zoomIt.zoomOut = function(thumb) {
	if (thumb.decoration != null) jQuery(thumb.decoration).hide();
	var img = thumb.full;
	if (zoomIt.active == thumb) {
		zoomIt.active = null;
	}
	if (img.parentNode != null) {
	   $(img).animate(
		{width: jQuery(thumb).width(), height: jQuery(thumb).height(), 
			top: jQuery(thumb).offset().top, left: jQuery(thumb).offset().left},
		500,
		null,
		function() {
			if (img.parentNode != null) {
				img.parentNode.removeChild(img);
			}
		}
	   );
	}
	return false;	
}

zoomIt.keydown = function(e) {
	var active = zoomIt.active;
	if (active != null && (e.which == 37 || e.which == 39)) {
		var all = jQuery("a.zoomIt");
		var index = all.index(active.parentNode);
		if (active.full != null) {
			jQuery(active.full).stop();
			zoomIt.zoomOut(active);
		}
		index = index + (e.which == 37 ? -1 : 1);
		index = (index + all.length) % all.length;
		var newActive = all.get(index);
		if (newActive.img != null) {
			jQuery(newActive.img).stop();
		}
		zoomIt.zoomIn(newActive);
				

	}
}



jQuery.clientCoords = function() {
     var dimensions = {width: 0, height: 0};
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    dimensions.width = window.innerWidth;
    dimensions.height = window.innerHeight;
  } else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    dimensions.width = document.documentElement.clientWidth;
    dimensions.height= document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    dimensions.width = document.body.clientWidth;
    dimensions.height= document.body.clientHeight;
  }
  return dimensions;
}

jQuery(document.body).keydown(zoomIt.keydown);


