/*------------------------------------------------------------------------------

	Copyright 2009 Anton Johansson (anton (at) kernelpanic.nu)

	This file is part of WP-ThumbMe.

	WP-ThumbMe is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	WP-ThumbMe is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

------------------------------------------------------------------------------*/

function zoomIn(image, width, height) {

	document.getElementById("popup").style.visibility = 'visible';

	var popup		= document.getElementById("popup");
	var IpopTop		= (document.body.clientHeight - document.getElementById("popup").offsetHeight) / 2;
	var IpopLeft		= (document.body.clientWidth - document.getElementById("popup").offsetWidth) / 2;

	popup.style.left	= IpopLeft + document.body.scrollLeft + (parseInt(popup.style.width)/2)+"px";
	popup.style.top		= IpopTop + document.body.scrollTop + (parseInt(popup.style.height)/2)+"px";


	var maxWidth		= 800;
	var maxHeight		= 600;
	popup.style.visibility	= 'visible';
	var step		= 30;

	var currWidth		= parseInt(popup.style.width);
	var currHeight		= parseInt(popup.style.height);
	var currLeft		= parseInt(popup.style.marginLeft);
	var currTop		= parseInt(popup.style.marginTop);

	//Size of the thumbnail to be displayed.
	if( width > height){
		if( width > 700 ){
			height	= 700/(width/height);
			width	= 700;
		}
	}
	else{
		if( height > 525 ){
			width	= 525*(width/height);
			height	= 525;
		}
	}

	if(currWidth < maxWidth) {
		popup.style.width	= (parseInt(currWidth) + step) + "px";
		popup.style.height	= (Math.round(parseInt(currWidth) * (maxHeight/maxWidth))) + "px";

		popup.style.marginLeft	= (parseInt(currLeft) - (step/2)) + "px";
		popup.style.marginTop	= (parseInt(currTop) - (step/2.6)) + "px";

		setTimeout("zoomIn(\'" + image + "\', \'" + width + "\', \'" + height + "\')", 10);
	}
	else if(currWidth >= maxWidth) {
		popup.innerHTML = '<img class=\"popup\" src=\"' + 'wp-content/plugins/wp-thumbme/php/thumbnail.php?im=' + image + '&maxsize=' + width + '\" width=\"' + width + '\" height=\"' + height + '\" id=\"popImg\"><div style=\"width:700px;margin-left:auto;margin-right:auto;text-align:right;font-size:12px;color:#9E9E9E;\"><a href=\"wp-content/plugins/wp-thumbme/php/watermark.php?src=' + image + '\">View image in full size</a></div>';
		//popup.innerHTML = '<img class=\"popup\" src=\"' + 'wp-content/plugins/wp-thumbme/php/thumbnail.php?im=' + image + '&maxsize=' + width + '\" width=\"' + width + '\" height=\"' + height + '\" id=\"popImg\">';
		//popup.innerHTML = width + ' - ' + height + ' - ' + image;
		document.getElementById("popImg").style.marginLeft = -1*(width/2) + "px";
		document.getElementById("popImg").style.marginTop = -1*(height/2) + "px";
		document.getElementById("popImg").style.visibility = 'visible';
	}

}

function zoomOut() {

	var popup			= document.getElementById("popup");
	popup.innerHTML			= '';
	var maxWidth			= 800;
	var maxHeight			= 600;
	var step			= 30;

	currWidth			= parseInt(popup.style.width);
	currHeight			= parseInt(popup.style.height);
	currLeft			= parseInt(popup.style.marginLeft);
	currTop				= parseInt(popup.style.marginTop);

	if(currWidth > 0) {
		popup.style.width	= (parseInt(currWidth) - step) + "px";
		popup.style.height	= (Math.round(parseInt(currWidth) * (maxHeight/maxWidth))) + "px";

		popup.style.marginLeft	= (parseInt(currLeft) + (step/2)) + "px";
		popup.style.marginTop	= (parseInt(currTop) + (step/2.6)) + "px";

		setTimeout("zoomOut()", 10);
	}
	else if(currWidth == 0) {
		popup.style.left	= "50%";
		popup.style.top		= "50%";
		popup.style.marginLeft	= "0";
		popup.style.marginTop	= "0";
		popup.style.visibility	= 'hidden';
	}

}

