// JavaScript Document
var timerison=false;

var tUnit = 0;
var lUnit = 0;
var counter = 1;
function resizeDiv(obj, x, y, dW, dH) {


	var speed=10;
	var pos = $('#mainPicCont').position();
	
	var posX = Math.round((dW-x)/2);
	var posY = Math.round((dH-y)/2);
	if (tUnit == 0)
		{
			lUnit = (pos.left-$(document).scrollLeft()-posX)/10;
			tUnit = (pos.top-$(document).scrollTop()-posY)/10;
			startW = $(obj).width();
			startH = $(obj).height();
		}
	//alert(counter+' '+startH+' '+(startH-((startH-y)/10)*counter)+' '+((startH-y)/10));
		
				

	//var distX = $(obj).width() - x;
	//var newWidth = Math.round($(obj).width() - distX/divider ) ;
	//$(obj).css('width', newWidth);
	$(obj).css('width', (startW-((startW-x)/10)*counter) );

	//var distY = $(obj).height() - y;
	//var newHeight = Math.round($(obj).height() - distY/divider ) ;
	//$(obj).css('height', newHeight);
	$(obj).css('height', (startH-((startH-y)/10)*counter) );

	//position the container div
	//end position of the div

	
	//var distL = (pos.left-posX)/divider;
	//var distT = (pos.top-posY)/divider;
	
	$('#mainPicCont').css('left', pos.left-$(document).scrollLeft()-lUnit);
	$('#mainPicCont').css('top', pos.top-$(document).scrollTop()-tUnit);

	//$('#mainPicCont').css('left', posX);
	//$('#mainPicCont').css('top', posY);

	//$('#darkLayer').append(document.createTextNode(posX+' '+posY));
	//$('#darkLayer').append(document.createElement('br'));
	
	if (counter<10)
		{
			setTimeout(function(){ resizeDiv(obj,x,y, dW, dH) }, speed);
			counter++;
		}
		else
		{
			//$('#darkLayer').fadeTo(1000, 0.8);
			counter=1;
			tUnit = 0;
		}
		
}

function setPosition(source, target){
	var pos = $(target).position();
	$(source).css('top', pos.top-$(document).scrollTop()-10);
	$(source).css('left', pos.left-$(document).scrollLeft()-10);
	$(source).css('height', $(target).height());
	$(source).css('width', $(target).width());
}

$('document').ready(function() {
		$('.thumbnail').click(function(){
				$('#mainPicCont').hide()
				$('#mainPicCont').html('');
				$('#darkLayer').hide();
				setPosition($('#mainPicCont'), $(this));
				$('<img />').attr('src', $(this).attr('longdesc')+'_big.jpg?random=' + (new Date()).getTime()).load(function(){
	        			$(this).attr('id', 'actualImg');
						$('#mainPicCont').html('');
						$('#mainPicCont').append(this);
						$('#mainPicCont').show();
						var imgW = $(this).width();
						var imgH = $(this).height();
						$(this).css('height', $('#mainPicCont').height());
						$(this).css('width', $('#mainPicCont').width());
						var dW = $(window).width();
						var dH = $(window).height();
						timerison=1;
						resizeDiv($(this), imgW, imgH,dW,dH);
						$('#mainPicCont').click(function(){
								timerison=0;
								$(this).hide()
								$(this).html('');
								$('#darkLayer').hide();
						})
						$('#darkLayer').click(function(){
								timerison=0;
								$('#mainPicCont').hide()
								$('#mainPicCont').html('');
								$('#darkLayer').hide();
						})
				});
		
		})
	
});
