var viewBackdrop, viewContainer, viewImage, viewClose, remove=false, fullSized, viewLoaded, viewYOffset, sourceHeight,loadIndicator;

function view(obj) {
	createWindow(obj);
	return false;
}

function createWindow(obj) {
	viewBackdrop = document.createElement('div');
	viewBackdrop.className		= 'viewScreen';
	viewBackdrop.style.position	= 'fixed';
	viewBackdrop.style.display	= 'block';
	viewBackdrop.style.top		= viewBackdrop.style.left = viewBackdrop.style.opacity = '0';
	viewBackdrop.currentAlpha	= 0;
	viewBackdrop.targetAlpha	= 0.7;
	viewBackdrop.innerHTML		= '&nbsp;';
	viewBackdrop.onclick		= function () { destroyWindow(); };
	document.body.appendChild(viewBackdrop);

	viewContainer = document.createElement('div');
	viewContainer.className		 = 'viewContainer';
	viewContainer.style.position = 'absolute';
	viewContainer.style.display	 = 'block';
	viewContainer.style.top		 = window.pageYOffset;
	viewContainer.style.height	 = '0';
	viewContainer.style.width	 = viewContainer.style.opacity = '0';
	viewContainer.currentHeight	 = 0;
	viewContainer.currentRate	 = viewContainer.currentAlpha = 1;
	document.body.appendChild(viewContainer);
	
	viewImage = document.createElement('img');
	viewImage.src	 = obj.href;
	viewImage.onload = function () { showImage(); };
	viewImage.style.display			= 'block';
	viewImage.style.top				= viewImage.style.left = viewImage.style.opacity = '0';
	viewImage.style.width = viewImage.style.height = '0';
	viewImage.currentAlpha			= 0;
	viewContainer.appendChild(viewImage);
	viewLoaded=false;
	sourceHeight = obj.height;
	
	loadIndicator = document.createElement('div');
	loadIndicator.style.position	= 'fixed';
	loadIndicator.innerHTML		= 'Loading...';
	document.body.appendChild(loadIndicator);
	
	var viewDiv = document.createElement('div');
	viewDiv.style.marginTop = '4px';
	viewContainer.appendChild(viewDiv);
	
	viewClose = document.createElement('span');
	viewClose.className		= 'viewClose';
	viewClose.innerHTML		= 'Close';
	viewClose.title			= 'Close window';
	viewClose.style.opacity	= '0';
	viewClose.style.display	= 'inline-block';
	viewClose.onclick = function () { destroyWindow(); };
	viewDiv.appendChild(viewClose);
	
	if (window.addEventListener){  
		window.addEventListener('resize',adjustFrame, false);   
	} else if (window.attachEvent){  
		window.attachEvent('onresize',adjustFrame);  
	}
	adjustFrame();
	fadeIn();
}

function fadeIn() {
	if (remove) { return; }
	if (viewBackdrop.currentAlpha < viewBackdrop.targetAlpha) {
		viewBackdrop.currentAlpha += (viewBackdrop.targetAlpha-viewBackdrop.currentAlpha)/16+0.01;
		viewBackdrop.style.opacity = viewBackdrop.currentAlpha.toString();
		setTimeout('fadeIn()',10);
	}
}

function showImage() {
	if (remove) { return; }
	if (!viewLoaded) {
		document.body.removeChild(loadIndicator);
		if (typeof(viewImage.naturalWidth) != 'number') {
			viewImage.naturalHeight	= viewImage.height;
			viewImage.naturalWidth	= viewImage.width;
		}
		var screenWidth = 0, screenHeight = 0;
		viewImage.viewHeight = viewImage.naturalHeight;
		viewImage.viewWidth	 = viewImage.naturalWidth;
		if (typeof(window.innerWidth) == 'number') {
			screenWidth = window.innerWidth;
			screenHeight = window.innerHeight;
		} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			screenWidth = document.documentElement.clientWidth;
			screenHeight = document.documentElement.clientHeight;
		} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			screenWidth = document.body.clientWidth;
			screenHeight = document.body.clientHeight;
		}
		fullSized = true;
		if (viewImage.viewHeight > screenHeight*0.95 && sourceHeight < screenHeight*0.95) {
			viewImage.viewHeight = screenHeight*0.95;
			viewImage.viewWidth *= (screenHeight/viewImage.naturalHeight)*0.95;
			fullSized = false;
		} else if (viewImage.viewWidth > screenWidth*0.95) {
			viewImage.viewWidth	  = screenWidth*0.95;
			viewImage.viewHeight *= (screenWidth/viewImage.naturalWidth)*0.95;
			fullSized = false;
		}
			//viewContainer.style.width	= (viewImage.width = viewImage.viewWidth)+'px';
			//viewContainer.style.height	= (viewImage.height = viewImage.viewHeight)+'px';
		viewYOffset = window.pageYOffset;
		viewLoaded = true;
	}
	if (viewBackdrop.currentAlpha < viewBackdrop.targetAlpha) {
		setTimeout('showImage()',500);
	} else if (viewContainer.currentHeight < viewImage.viewHeight+viewClose.offsetHeight+4) {
			viewContainer.style.width	= viewImage.style.width = (viewImage.width = viewImage.viewWidth)+'px';
			viewContainer.style.height	= viewImage.style.height = (viewImage.height = viewImage.viewHeight)+'px';
		viewContainer.style.opacity	= '1';
		viewContainer.currentHeight	= Math.sin((viewContainer.currentRate/60)*Math.PI)*(viewImage.viewHeight+viewClose.offsetHeight+4);
		viewContainer.style.height	= viewContainer.currentHeight+'px';
		viewContainer.currentRate++;
		adjustFrame();
		setTimeout('showImage()',10);
	} else if (viewImage.currentAlpha < 1) {
		viewImage.currentAlpha += (1-viewImage.currentAlpha)/16+0.01;
		viewImage.style.opacity = viewClose.style.opacity = viewImage.currentAlpha.toString();
		setTimeout('showImage()',10);
	} else {
		viewContainer.currentRate = 1;		
		if (fullSized) {
			viewImage.onclick = function () { destroyWindow(); };
			viewImage.style.cursor = 'url(\'./includes/zoom-out.png\'), -moz-zoom-out';
			viewImage.title = 'Return';
		} else {
			viewImage.onclick = function () { fullSize(); };
			viewImage.style.cursor = 'url(\'./includes/zoom-in.png\'), -moz-zoom-in';
			viewImage.title = 'View Full Size';
		}
	}	
}

function fullSize() {
	if (remove) { return; }
	if (viewImage.height < viewImage.naturalHeight) {
		viewImage.onclick		= function () {};
		viewImage.style.cursor	= 'default';
		viewImage.title			= 'Zoom Out';
		viewContainer.currentHeight	= viewImage.viewHeight+Math.sin((viewContainer.currentRate/30)*Math.PI)*(viewImage.naturalHeight-viewImage.viewHeight);
		viewImage.style.height		= viewContainer.currentHeight+'px';
		viewImage.style.width		= viewContainer.style.width = viewImage.viewWidth+Math.sin((viewContainer.currentRate/30)*Math.PI)*(viewImage.naturalWidth-viewImage.viewWidth)+'px';
		viewContainer.style.height	= viewContainer.currentHeight+viewClose.offsetHeight+4+'px';
		viewContainer.currentRate++;
		adjustFrame();
		setTimeout('fullSize()',10);
	} else {
		viewContainer.currentRate = 1;
		viewImage.onclick = function () { reverseSize(); };
		viewImage.style.cursor	= 'url(\'./includes/zoom-out.png\'), -moz-zoom-out';
	}
}

function reverseSize() {
	if (remove) { return; }
	if (viewImage.height > viewImage.viewHeight) {
		viewImage.onclick		= function () {};
		viewImage.style.cursor	= 'default';
		viewImage.title			= 'View Full Size';
		viewContainer.currentHeight	= viewImage.naturalHeight-Math.sin((viewContainer.currentRate/30)*Math.PI)*(viewImage.naturalHeight-viewImage.viewHeight);
		viewImage.style.height		= parseInt(viewContainer.currentHeight)+'px';
		viewImage.style.width		= viewContainer.style.width = viewImage.naturalWidth-Math.sin((viewContainer.currentRate/30)*Math.PI)*(viewImage.naturalWidth-viewImage.viewWidth)+'px';
		viewContainer.style.height	= viewContainer.currentHeight+viewClose.offsetHeight+4+'px';
		viewContainer.currentRate++;
		adjustFrame();
		setTimeout('reverseSize()',10);
	} else {
		viewContainer.currentRate = 1;
		viewImage.onclick = function () { fullSize(); };
		viewImage.style.cursor	= 'url(\'./includes/zoom-in.png\'), -moz-zoom-in';
	}
}

function adjustFrame() {
	viewBackdrop.style.width	= viewBackdrop.style.height = '0';
	var screenWidth = 0, screenHeight = 0;
	if (typeof(window.innerWidth) == 'number') {
			screenWidth = window.innerWidth;
			screenHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			screenWidth = document.documentElement.clientWidth;
			screenHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			screenWidth = document.body.clientWidth;
			screenHeight = document.body.clientHeight;
	}
	viewBackdrop.style.width	= screenWidth+'px';
	viewBackdrop.style.height	= screenHeight+'px';
	var left = (screenWidth-viewContainer.scrollWidth)/2;
	var top	 = ((viewImage.viewHeight < screenHeight?screenHeight:viewContainer.scrollHeight)-viewContainer.currentHeight)/2;
	viewContainer.style.left = (left<0?0:left)+'px';
	viewContainer.style.top	 = viewYOffset+top+'px';
}

function destroyWindow() {
	if (remove) { return; }
	remove = true;
	//document.body.removeChild(loadIndicator);
	viewImage.style.cursor	= 'default';
	fadeOut();
}

function fadeOut() {
	if (viewBackdrop.currentAlpha > 0 || viewContainer.currentAlpha > 0) {
		viewBackdrop.currentAlpha  -= (viewBackdrop.currentAlpha)/10+0.01;
		viewBackdrop.style.opacity	= viewBackdrop.currentAlpha.toString();
		viewContainer.currentAlpha -= (viewContainer.currentAlpha)/10+0.01;
		viewContainer.style.opacity	= viewContainer.currentAlpha.toString();
		setTimeout('fadeOut()',10);
	} else {
		remove = false;
		document.body.removeChild(viewBackdrop);
		document.body.removeChild(viewContainer);
	}
}

