var isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1);

(function() {
	var opening = null;
	var closing = null;
	var queue = null;
	var tester = null;
	var curid = null;
	var cururl = null;
	var width = null;
	var height = null;
	var newid = null;
	var newurl = null;
	var newwidth = null;
	var newheight = null;
	var ratio = 1;
	var step = 8;

	bigImage = function(url, swidth, sheight, id) {
		if(id == curid) return;
		if(opening != null || closing != null) {
			newid = id;
			newurl = url;
			newwidth = swidth;
			newheight = sheight;
			if(tester != null) clearInterval(tester);
			tester = setInterval("testFunc()", 100);

		} else {
			if(curid != null) {
				var img = document.getElementById("img"+curid+"_big");
				img.parentNode.removeChild(img);
			}
			curid = id;
			cururl = url;
			width = swidth;
			height = sheight;
			start();
			/*
			var element = document.getElementById("img"+id);
			var img = document.getElementById(element.id+"_big");
			if(img == null) img = document.createElement('img');
			img.width = element.width;
			img.height = element.height;
			img.style.top = getPosition(element).y + "px";
			img.style.left = getPosition(element).x + "px";
			img.style.position = "absolute";
			img.src = url;
			img.id = "img"+id+"_big";
			img.rel = id;
			img.towidth = swidth;
			img.toheight = sheight;
			img.onload = function() {start();};
			img.onmouseover = element.onmouseover;
			img.onmouseout = function() {smallImage();};
			element.parentNode.appendChild(img);
			//idlist[element.id]
			*/
		}

	};
	var start = function() {


		var element = document.getElementById("img"+curid);
		var img = document.getElementById(element.id+"_big");
		if(img == null) img = document.createElement('img');
		img.src = cururl;
		img.width = element.width;
		img.height = element.height;
		img.setAttribute("style", "z-index:2000;");
		img.style.top = getPosition(element).y + "px";
		img.style.left = getPosition(element).x + "px";
		img.style.position = "absolute";
		img.id = "img"+curid+"_big";
		if(element.width == width) return;
		//img.onload = function() {showImg();};
		img.onmouseover = element.onmouseover;
		img.onmouseout = function() {smallImage();};
		element.parentNode.appendChild(img);

		showImg();


	};
	smallImage = function() {

		if(opening != null) {
			clearInterval(opening);
			ratio = 1;
			opening = null;
		}
		if(curid == null) return;
		closing = setInterval("resizeDown()", 5);

	};
	var showImg = function() {
		if(closing != null) {
			clearInterval(closing);
			ratio = 1;
			closing = null;
		}
		if(curid == null) return;
		opening = setInterval("resizeUp()", 5);
	};

	resizeDown = function() {

		var img2resize = document.getElementById("img"+curid+"_big");
		if(img2resize == null) {
			clearInterval(closing);
			closing = null;
			curid = null;
			ratio = 1;
			return;
		}
		var img = document.getElementById("img"+curid);

		if(img2resize.width > img2resize.height) w = true;
		else w = false;

		stepw = (w == true) ? Math.floor((img2resize.width/img2resize.height*step)*ratio) : step*ratio;
		steph = (w == true) ? step*ratio : Math.floor((img2resize.height/img2resize.width*step)*ratio);

		if((w == true && (img2resize.width-stepw) > img.width) || (w == false && (img2resize.height-stepw) > img.height)){
			var widthfn = img2resize.width-stepw;
			var heightfn = img2resize.height-steph;
			img2resize.width = widthfn;
			img2resize.height = heightfn;

			img2resize.style.left = parseInt(img2resize.style.left) + Math.ceil(stepw/2) + "px";
			img2resize.style.top = parseInt(img2resize.style.top) + Math.ceil(steph/2) + "px";
			ratio = ratio*2;
		} else {

			img2resize.width = img.width;
			img2resize.height = img.height;

			img2resize.style.left = Math.ceil((img2resize.width-img.width)/2) + "px";
			img2resize.style.top = Math.ceil((img2resize.height-img.height)/2) + "px";

			clearInterval(closing);

			img2resize.parentNode.removeChild(img2resize);
			closing = null;
			curid = null;
			ratio = 1;
			img2resize.onload = null;
		}

	};
	resizeUp = function() {

		var img2resize = document.getElementById("img"+curid+"_big");
		var element = document.getElementById("img"+curid);
		if(img2resize == null) {
			clearInterval(opening);
			ratio = 1;
			opening = null;
			return;
		}
		var img = document.getElementById("img"+curid);

		if(img2resize.width > img2resize.height) w = true;
		else w = false;

		stepw = (w == true) ? Math.ceil((img2resize.width/img2resize.height*step)*ratio) : step*ratio;
		steph = (w == true) ? step*ratio : Math.ceil((img2resize.height/img2resize.width*step)*ratio);
		if((w == true && (img2resize.width+stepw) < width) || (w == false && (img2resize.height+stepw) < height)){

			var widthfn = img2resize.width+stepw;
			var heightfn = img2resize.height+steph;
			img2resize.width = widthfn;
			img2resize.height = heightfn;

			img2resize.style.left = parseInt(img2resize.style.left) - Math.floor(stepw/2) + "px";
			img2resize.style.top = parseInt(img2resize.style.top) - Math.floor(steph/2) + "px";
			ratio = ratio*2;
		} else {/*
			img2resize.width = Math.ceil(img2resize.width+stepw);
			img2resize.height = Math.ceil(img2resize.height+steph);

			img2resize.style.left = Math.floor(parseInt(img2resize.style.left) - stepw/2) + "px";
			img2resize.style.top = Math.floor(parseInt(img2resize.style.top) - steph/2) + "px";*/
			/*
			img2resize.width = width;
			img2resize.height = height;

			img2resize.style.left = Math.floor(parseInt(img2resize.style.left) - (width-img2resize.width)/2) + "px";
			img2resize.style.top = Math.floor(parseInt(img2resize.style.top) - (height-img2resize.height)/2) + "px";
			*/
			clearInterval(opening);

			ratio = 1;
			opening = null;
		}

	};
	testFunc = function() {
		if(opening == null && closing == null){
			curid = newid;
			cururl = newurl;
			width = newwidth;
			height = newheight;
			start();
			clearInterval(tester);
			tester = null;

		}
	};
	var getPosition = function(e){
		var left = 0
		var top  = 0
		while (e.offsetParent){
			left += e.offsetLeft
			top  += e.offsetTop
			e     = e.offsetParent
		}
		left += e.offsetLeft
		top  += e.offsetTop + getBodyScrollTop();
		return {x:left, y:top}
	};
})();

function getBodyScrollTop() {
  return (isIE) ? (self.pageYOffset || (document.body && document.body.scrollTop)) : 0;
}
