
var pv = new viewPicture();
function zoom_picture(pic, w, h){
	pv.open({
		picture_width : w,
		picture_height : h,
		picture_onclick : 'pv.close();',
		picture : pic
	});
}

function viewPicture(){
	var Setting;

	this.open = function(set){
		Setting = set;
		Setting_original = set;
		o = this;
		
		Number.prototype.NaN0 = function(){ return isNaN(this) ? 0 : this; }
		var objBody = document.getElementsByTagName("body").item(0);
		var zIndex = 500;
		var pageSize = o.getPageSize();
		var objOverlay = document.createElement("div");

		objOverlay.setAttribute('id','viewpicture_layer' );
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = zIndex;
		objOverlay.style.width = (pageSize.pw + 'px');
		objOverlay.style.minWidth = '100%';
		objOverlay.style.height = (pageSize.ph + 'px');
		objOverlay.style.minHeight = '100%';
		objOverlay.style.backgroundColor = '#000000';
		objOverlay.style.filter = 'alpha(opacity=50)';
		objOverlay.style.opacity = 0.50;
		objBody.appendChild(objOverlay);

		var objContent = document.createElement("div");
		objContent.setAttribute('id','viewpicture_content');
		objContent.style.visibility	= 'hidden';
		objContent.style.position = 'absolute';
		objContent.style.top = '0';
		objContent.style.left = '0';
		objContent.style.zIndex = zIndex + 1;
		objBody.appendChild(objContent);
		
		var pageSize = o.getPageSize();
		var objContent = document.getElementById('viewpicture_content');
		
		if(Setting.picture_width>pageSize.pw || Setting.picture_height>pageSize.wh){
			if(Setting.picture_width>Setting.picture_height){
				if(Setting.picture_width>pageSize.pw){
					Setting.picture_height = (pageSize.pw/Setting.picture_width*Setting.picture_height);
					Setting.picture_width = pageSize.pw;
				}else if(Setting.picture_height>pageSize.wh){
					Setting.picture_width = (pageSize.wh/Setting.picture_height*Setting.picture_width);
					Setting.picture_height = pageSize.wh;
				}
			}else if(Setting.picture_width<Setting.picture_height){
				if(Setting.picture_height>pageSize.wh){
					Setting.picture_width = (pageSize.wh/Setting.picture_height*Setting.picture_width);
					Setting.picture_height = pageSize.wh;
				}else if(Setting.picture_width>pageSize.pw){
					Setting.picture_height = (pageSize.pw/Setting.picture_width*Setting.picture_height);
					Setting.picture_width = pageSize.pw;
				}
			}
			Setting.picture_height = Setting.picture_height - 50;
			Setting.picture_width = Setting.picture_width - 50;
		}
		
		var scroll = o.getPageScroll(0);
		toppos   = scroll.y + (pageSize.wh - Setting.picture_height) / 2;
		leftpos	= scroll.x + (pageSize.pw - Setting.picture_width) / 2;

		var top  = (toppos < 0) ? '0px' : toppos + 'px';
		var left = (leftpos < 0) ? '0px' : leftpos + 'px';
		
		objContent.innerHTML = '<div class="pic" style="cursor:pointer; position:absolute; top:'+ top +'; left:'+ left +'; width:'+ Setting.picture_width +'px; height:'+ Setting.picture_height +'px;">'+
				'<img onclick="' + Setting.picture_onclick + '" src="' + Setting.picture + '" alt="" width="'+ Setting.picture_width +'" height="' + Setting.picture_height + '" />'+
			'</div>';
			
		objOverlay.style.display = '';
		objContent.style.visibility	= '';
	};
	
	this.close = function(){
		layerID = 'viewpicture_content';
		objElement = document.getElementById(layerID);
		if(objElement && objElement.parentNode && objElement.parentNode.removeChild){
			objElement.parentNode.removeChild(objElement);
		}

		layerID = 'viewpicture_layer';
		objElement = document.getElementById(layerID);
		if(objElement && objElement.parentNode && objElement.parentNode.removeChild){
			objElement.parentNode.removeChild(objElement);
		}
	};

	this.getPageSize = function(){
		var scroll = o.getPageScroll(1);

		var windowWidth, windowHeight;
		if(self.innerHeight) {	// all except
			windowWidth = self.innerWidth; windowHeight = self.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict
			windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight;
		}else if(document.body) { // other
			windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight;
		}

		pageHeight = (scroll.y < windowHeight) ? windowHeight : scroll.y ;
		pageWidth = (scroll.x < windowWidth) ? windowWidth : scroll.x ;

		return {pw:pageWidth, ph:pageHeight, ww:windowWidth, wh:windowHeight};
	};

	this.getPageScroll = function(mode){
		var xScroll, yScroll;
		if(mode==1){
			if(window.innerHeight && window.scrollMaxY) {
				xScroll = document.body.scrollWidth; yScroll = window.innerHeight + window.scrollMaxY;
			}else if(document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight;
			}else{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight;
			}
		}else{
			if(self.pageYOffset){
				xScroll = self.pageXOffset; yScroll = self.pageYOffset;
			}else if(document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
				xScroll = document.documentElement.scrollLeft; yScroll = document.documentElement.scrollTop;
			}else if(document.body){ // other
				xScroll = document.body.scrollLeft; yScroll = document.body.scrollTop;
			}
		}
		return {x:xScroll,y:yScroll};
	};
}