/*********************************************************************
 * ÀÌ¹ÌÁö »õÃ¢À¸·Î ¿­¾î¼­ º¸±â
 * ÆÄÀÏ¸í : showImage.htm
 * ÀÛ¼ºÀÚ : ¼­ºñÅ¥¶ó(subicura@nate.com)
 * ÀÛ¼ºÀÏ : 06/01/27
 * »ó¾÷ÀûÀÎ ½ÎÀÌÆ®¿¡¼­´Â ¹«´ÜÀ¸·Î »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.
 *********************************************************************/
/*********************************************************************
 * ÀÌ¹ÌÁö¸¦ º¼¶§ ÇÏ³ªÀÇ À©µµ¿ì¸¸ »ç¿ëÇÒ °ÍÀÎ°¡?
 * isUsePerText : % Ç¥½Ã À¯¹«
 * newWndMargin : À©µµ¿ì ÇØ»óµµ¿¡¼­ ÀÌ¹ÌÁö Ã¢ÀÇ ¿©À¯(margin)°ª
 * newWndMinSize : Ã¢ÀÇ ÃÖ¼Ò Å©±â
 *********************************************************************/
var isUsePerText = true;
var newWndMargin = 160;
var newWndMinSize = 250;

/*********************************************************************
 * °¢Á¾º¯¼ö
 *********************************************************************/
var ImgWnd, isUseZoom, isZoom ;
var img_original_width, img_original_height, img_modify_width, img_modify_height;
var pop_mouse_x, pop_mouse_y, pop_scroll_x, pop_scroll_y, pop_mouseStatus;
var zoombox_x, zoombox_y;

/*********************************************************************
 * ½ÃÀÛÇÔ¼ö
 *********************************************************************/
function showImage(filename)
{
	image_copy = new Image();
	image_copy.src = filename;

	if(image_copy.width == 0 || image_copy.height == 0)
		setTimeout("showImage('"+filename+"');",50);
	else
		popupImage(image_copy);
}

/*********************************************************************
 * ÀÌ¹ÌÁö ÆË¾÷Ã¢ »ý¼º
 *********************************************************************/
function popupImage(img_obj)
{
	// º¯¼ö ÃÊ±âÈ­
	initValue();

	// ÀÌ¹ÌÁö width, height ÀúÀå
	img_original_width = img_obj.width;
	img_original_height = img_obj.height;

	// »ý¼ºµÉ Ã¢ÀÇ Å©±â Á¶Á¤
	if(img_original_width + newWndMargin > screen.width)
	{
		img_modify_width = screen.width - newWndMargin;
		img_modify_height = (img_original_height*img_modify_width/img_original_width);
		if(img_modify_height + newWndMargin > screen.height)
		{
			img_modify_height = screen.height - newWndMargin;
			img_modify_width = Math.ceil(img_original_width*img_modify_height/img_original_height);
		}
	}
	else if(img_original_height + newWndMargin > screen.height)
	{
			img_modify_height = screen.height - newWndMargin;
			img_modify_width = Math.ceil(img_original_width*img_modify_height/img_original_height);
	}
	else
	{
		img_modify_width = img_original_width;
		img_modify_height = img_original_height;
	}
	
	// À§Ä¡ Á¶Á¤
	var i_left = Math.ceil((screen.width - img_modify_width)/2);
	var i_top = Math.ceil((screen.height - img_modify_height)/2);
	if(i_top > newWndMargin)
		i_top = newWndMargin;

	// È®´ë Ãà¼Ò »ç¿ë¿©ºÎ
	if(img_original_width != img_modify_width && img_original_height != img_modify_height)
		isUseZoom = true;
	else
		isUseZoom = false;

	// »õÃ¢ ¸¸µé±â
	if(ImgWnd) ImgWnd.close(); // ÃÊ±âÈ­
	if(img_original_width > newWndMinSize || img_original_height > newWndMinSize)
	{
		ImgWnd = window.open('', 'viewImage', 'width='+img_modify_width+', height='+img_modify_height+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
		if(!ImgWnd.document.body)
			ImgWnd = window.open('', 'viewImage', 'width='+img_modify_width+', height='+img_modify_height+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
	}
	else
	{
		ImgWnd = window.open('', 'viewImage', 'width='+newWndMinSize+', height='+newWndMinSize+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
		if(!ImgWnd.document.body)
			ImgWnd = window.open('', 'viewImage', 'width='+newWndMinSize+', height='+newWndMinSize+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
	}

	if(ImgWnd.document.body.innerHTML)
	{
		ImgWnd.close();
		if(img_original_width > newWndMinSize || img_original_height > newWndMinSize)
		{
			ImgWnd = window.open('', 'viewImage', 'width='+img_modify_width+', height='+img_modify_height+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
			if(!ImgWnd.document.body)
				ImgWnd = window.open('', 'viewImage', 'width='+img_modify_width+', height='+img_modify_height+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
		}
		else
		{
			ImgWnd = window.open('', 'viewImage', 'width='+newWndMinSize+', height='+newWndMinSize+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
			if(!ImgWnd.document.body)
				ImgWnd = window.open('', 'viewImage', 'width='+newWndMinSize+', height='+newWndMinSize+', top='+i_top+', left='+i_left+', scrollbars=0,resizable=no');
		}
	}

	// »õÃ¢ ÃÊ±âÈ­
	initImgWnd(ImgWnd);

	// ÀÌ¹ÌÁö »ý¼º
	var doc = ImgWnd.document;
	var doc_style = doc.body.style;
	var resize_width, resize_height;
	
	if(img_original_width > newWndMinSize || img_original_height > newWndMinSize)
	{
			var new_img = doc.createElement('IMG');
			new_img.id = 'showImage';
			new_img.src = img_obj.src;
			new_img.width = img_modify_width;
			new_img.height = img_modify_height;
			doc.body.appendChild(new_img);
	}
	else
	{
		doc_style.background='url('+img_obj.src+')';
		doc_style.backgroundRepeat = 'no-repeat';
		doc_style.backgroundPosition = 'center';
		doc_style.backgroundColor = "#000000";
		doc.body.innerHTML = "<table width=100% height=100%><tr><td></td></tr></table>";
	}

	// »õÃ¢¿¡ °¢Á¾°´Ã¼ »ðÀÔ
	if(isUseZoom == true)
	{
		var old_zoom_box = ImgWnd.document.getElementById('zoom_box');

		// zoom °ü·Ã °´Ã¼¸¦ ¹­À½
		var zoom_box = doc.createElement('DIV');
		zoom_box.id = 'zoom_box';
		zoom_box.style.position = 'absolute';
		zoom_box.style.top = img_modify_height - 60;
		zoom_box.style.left = img_modify_width - 64;
		zoom_box.style.width = 38;
		zoom_box.style.height = 43;
		doc.body.appendChild(zoom_box);

		// zoom ¹öÆ° »ý¼º
		var zoom_button = doc.createElement('IMG');
		zoom_button.id = 'zoom_button';
		zoom_button.src = 'http://comedu.korea.ac.kr/~subicura/java/100_icon.gif';
		zoom_button.style.position = 'absolute';
		zoom_button.style.width = 38;
		zoom_button.style.height = 43;
		zoom_button.style.top = '0px';
		zoom_button.style.left = '0px';
		zoom_box.appendChild(zoom_button);

		if(isUsePerText == true)
		{
			// ºñÀ² ±ÛÀÚ »ý¼º
			var zoom_percent = doc.createElement('DIV');
			zoom_percent.id = 'zoom_percent';
			zoom_percent.style.position = 'absolute';
			zoom_percent.style.top = '29px';
			zoom_percent.style.left = '3px';
			zoom_percent.style.width = '30px';
			zoom_percent.style.textAlign = 'center';
			zoom_percent.style.fontSize = '8pt';
			zoom_percent.style.fontFamily = 'µ¸¿ò';
			zoom_percent.innerHTML = Math.round(img_modify_width/img_original_width*100)+'%';
			zoom_box.appendChild(zoom_percent);
		}
		zoombox_x = eval(zoom_box.style.left.substring(0,zoom_box.style.left.length-2));
		zoombox_y = eval(zoom_box.style.top.substring(0,zoom_box.style.top.length-2));

		setButtonAlpha(70);
	}

	// ¸¶¿ì½º ÀÌº¥Æ®
	doc.onmousedown=ImageMouseDown;
	doc.onmousemove=ImageMouseDrag;
	doc.onmouseup=ImageMouseUp;
	doc.onmousewheel=ImageMouseWheel; // IE Àü¿ë FF´Â?

	// Æ÷Ä¿½º
	ImgWnd.focus();
}

/*********************************************************************
 * Ã¢ ÃÊ±âÈ­
 *********************************************************************/
function initImgWnd()
{
	var doc = ImgWnd.document;
	var doc_style = doc.body.style;

	doc_style.margin = 0;
	doc_style.background='url("http://comedu.korea.ac.kr/~subicura/java/loading.jpg")';
	doc_style.backgroundRepeat = 'no-repeat';
	doc_style.backgroundPosition = 'center';
	doc_style.backgroundColor = "#000000";
	doc_style.cursor = "pointer";
	doc.title = 'ÀÌ¹ÌÁö';
}

/*********************************************************************
 * Ã¢ »èÁ¦
 *********************************************************************/
function destroyImgWnd()
{
	initValue();
	ImgWnd.document.body.style.cursor = '';
	setTimeout("ImgWnd.close(); ImgWnd='';",10);
	ImgWnd.document.onmousedown=null;
	ImgWnd.document.onmousemove=null;
	ImgWnd.document.onmouseup=null;
	ImgWnd.document.onmousewheel=null;
}

// °¢Á¾º¯¼ö ÃÊ±âÈ­
function initValue()
{
	isZoom = false;
	wheelCount = 0;
	pop_scroll_x = pop_scroll_y = 0;
	pop_mouseStatus = '';
}

/*********************************************************************
 * ¸¶¿ì½º ÀÌº¥Æ®
 *********************************************************************/
function ImageMouseDown(e)
{
	if (ImgWnd.window.event) // IE
	{
		pop_mouse_x = ImgWnd.window.event.clientX;
		pop_mouse_y = ImgWnd.window.event.clientY;
		if(ImgWnd.window.event.srcElement.id == 'zoom_button' || ImgWnd.window.event.srcElement.id == 'zoom_percent')
		{
			pop_mouseStatus = '';
			zoomImg();
		}
		else if(ImgWnd.window.event.button != 2 && ImgWnd.window.event.button != 4)
			pop_mouseStatus = 'drag';
	}
	else
	{
		pop_mouse_x = e.clientX;
		pop_mouse_y = e.clientY;
		if(e.target.getAttribute('id') == 'zoom_button' || e.target.getAttribute('id') == 'zoom_percent')
		{
			pop_mouseStatus = '';
			zoomImg();
		}
		else if(e.button != 2 && e.button != 1)
			pop_mouseStatus = 'drag';
	}

	// Å¬¸¯¼ø°£ÀÇ ÁÂÇ¥ ±â¾ï
	pop_scroll_x = ImgWnd.document.body.scrollLeft;
	pop_scroll_y = ImgWnd.document.body.scrollTop;

	// Ä¿¼­ ¸ð¾ç ¹Ù²Þ
	if(isZoom == true)
		ImgWnd.document.body.style.cursor = "move";

	return false;
}

function ImageMouseUp(e)
{
	ImgWnd.document.body.style.cursor = "pointer";
	// Á¾·á
	if (ImgWnd.window.event) // IE
	{
		if(pop_mouse_x == ImgWnd.window.event.clientX && pop_mouse_y == ImgWnd.window.event.clientY && pop_mouseStatus == 'drag')
			destroyImgWnd();
	}
	else
	{
		if(pop_mouse_x == e.clientX && pop_mouse_y == e.clientY && pop_mouseStatus == 'drag')
			destroyImgWnd();
	}

	// ¾ÆÀÌÄÜ À§Ä¡ ÀÌµ¿
	if(isUseZoom == true)
	{
		if(pop_mouseStatus == 'drag' && isZoom == true)
		{
			var zoom_box = ImgWnd.document.getElementById('zoom_box');
			zoom_box.style.left = (ImgWnd.document.body.scrollLeft + zoombox_x)+'px';
			zoom_box.style.top = (ImgWnd.document.body.scrollTop + zoombox_y)+'px';
			zoom_box.style.display = '';
		}
	}
	
	// ½ºÅ©·Ñ À§Ä¡ ±â¾ï/ÈÙ À§Ä¡ ÃÊ±âÈ­
	pop_scroll_y = ImgWnd.document.body.scrollTop;
	wheelCount = 0;

	pop_mouseStatus = '';
	return false;
}

function ImageMouseDrag(e)
{
	// µå·¡±×ÇÒ¶§ ½ºÅ©·Ñ¸µ
	if(pop_mouseStatus == 'drag' && isZoom == true)
	{
		// ÁÜ¹öÆ° ¼û±è
		ImgWnd.document.getElementById('zoom_box').style.display = 'none';

		if(ImgWnd.window.event) // IE
			ImgWnd.scroll(pop_scroll_x + (pop_mouse_x - ImgWnd.window.event.clientX)*2, pop_scroll_y+(pop_mouse_y - ImgWnd.window.event.clientY)*2);
		else
			ImgWnd.scroll(pop_scroll_x + (pop_mouse_x - e.clientX)*2, pop_scroll_y+(pop_mouse_y - e.clientY)*2);
		
	}
	else if(pop_mouseStatus == 'drag' && isZoom == false)
	;// ¹º°¡ µé¾î°¡¾ß µÇ´Âµ¥ ÂÁ;
	else
		ImgWnd.document.body.style.cursor = "pointer";

	// zoom ·Ñ¿À¹ö Ã¼Å©
	if(isUseZoom == true)
	{
		if (ImgWnd.window.event) // IE
		{
			if(ImgWnd.window.event.srcElement.id == 'zoom_button' || ImgWnd.window.event.srcElement.id == 'zoom_percent')
				setButtonAlpha(95);
			else
				setButtonAlpha(70);
		}
		else
		{
			if(e.target.getAttribute('id') == 'zoom_button' || e.target.getAttribute('id') == 'zoom_percent')
				setButtonAlpha(95);
			else
				setButtonAlpha(70);
		}
	}

	return false;
}

function ImageMouseWheel(e)
{
	if(isZoom == true)
	{
		var zoom_box = ImgWnd.document.getElementById('zoom_box');
		var temp = ImgWnd.document.body.scrollTop;

		wheelCount += (ImgWnd.window.event.wheelDelta > 0) ? 1 : -1;
		zoom_box.style.display = 'none';
		ImgWnd.scroll(pop_scroll_x, (pop_scroll_y-wheelCount*120));

		if(temp != ImgWnd.document.body.scrollTop)
		{
			zoom_box.style.left = (ImgWnd.document.body.scrollLeft + zoombox_x)+'px';
			zoom_box.style.top = (ImgWnd.document.body.scrollTop + zoombox_y)+'px';
		}
		else
		{
			wheelCount -= (ImgWnd.window.event.wheelDelta > 0) ? 1 : -1;
		}
		zoom_box.style.display = '';
	}
}

/*********************************************************************
 * ¹öÆ° Åõ¸íµµ Á¶Á¤
 *********************************************************************/
function setButtonAlpha(opacity)
{
	var zoom_box;
	zoom_box = ImgWnd.document.getElementById('zoom_box');
	zoom_box.style.filter = "alpha(opacity=" + opacity + ")";
	zoom_box.style.KhtmlOpacity = (opacity / 100);
	zoom_box.style.MozOpacity = (opacity / 100);
}

/*********************************************************************
 * ÀÌ¹ÌÁö ÁÜÀÎ/ÁÜ¾Æ¿ô
 *********************************************************************/
function zoomImg()
{
	// ÀÌ¹ÌÁö °ü·Ã
	var zoom_img = ImgWnd.document.getElementById('showImage');
	var per_text = ImgWnd.document.getElementById('zoom_percent');
	per_text.innerHTML = Math.ceil(zoom_img.style.width/img_original_width*100)+'%';

	// À©µµ¿ì Ã¢ Å©±â Á¶Á¤ °ü·Ã
	var resize_width=0, resize_height=0;

	if(zoom_img.clientWidth == img_original_width)
	{
		zoom_img.style.width = img_modify_width;
		zoom_img.style.height = img_modify_height;
		per_text.innerHTML = Math.ceil(img_modify_width/img_original_width*100)+'%';
		isZoom = false;

		// Ã¢ Å©±â Á¶Á¤
		if(img_modify_width == screen.width - newWndMargin)
		{
			if(!ImgWnd.window.innerHeight) // IE
				resize_height = img_modify_height - ImgWnd.window.document.body.offsetHeight;
			else
				resize_height = img_modify_height - ImgWnd.window.innerHeight;
		}
		else
		{
			if(!ImgWnd.window.innerWidth) // IE
				resize_width = img_modify_width - ImgWnd.window.document.body.offsetWidth;
			else
				resize_width = img_modify_width - ImgWnd.window.innerWidth;
		}

		// ¹öÆ° À§Ä¡ ÃÊ±âÈ­
		var zoom_box = ImgWnd.document.getElementById('zoom_box');
		zoom_box.style.left = zoombox_x + 'px';
		zoom_box.style.top = zoombox_y + 'px';

		// ¹öÆ° ÀÌ¹ÌÁö º¯°æ
		var zoombutton = ImgWnd.document.getElementById('zoom_button');
		zoombutton.src = '/js/images/100_icon.gif';

	}
	else
	{
		zoom_img.style.width = img_original_width;
		zoom_img.style.height = img_original_height;
		per_text.innerHTML = '100%';
		isZoom = true;

		var zoombutton = ImgWnd.document.getElementById('zoom_button');
		zoombutton.src = '/js/images/00_icon.gif';

		// Ã¢ Å©±â Á¶Á¤
		if(img_modify_width+newWndMargin == screen.width)
		{
			if(img_original_height+newWndMargin > screen.height)
				resize_height = screen.height-newWndMargin-img_modify_height;
			else
				resize_height = img_original_height-img_modify_height;
		}
		else
		{
			if(img_original_width+newWndMargin > screen.width)
				resize_width = screen.width-newWndMargin-img_modify_width;
			else
				resize_width = img_original_width-img_modify_width;
		}
	}

	//  Ã¢ Å©±â Á¶Á¤
	if(Math.abs(resize_width) > 70 || Math.abs(resize_height) > 70)
	{
		if(navigator.appName.indexOf("Microsoft") != -1) // IE Bug
			ImgWnd.moveTo(0,0);
		ImgWnd.resizeBy(resize_width,resize_height);

		// À©µµ¿ì Ã¢ À§Ä¡ Á¶Á¤
		if(!ImgWnd.window.innerHeight) // ie
		{
			ImgWnd.moveTo(Math.ceil((screen.width - ImgWnd.window.document.body.offsetWidth)/2), Math.ceil((screen.height - ImgWnd.window.document.body.offsetHeight)/2));
		}
		else
		{
			ImgWnd.moveTo(Math.ceil((screen.width - ImgWnd.window.innerWidth)/2), Math.ceil((screen.height - ImgWnd.window.innerHeight)/2));
		}

		// ÁÜ ¹öÆ° À§Ä¡ ÀÌµ¿
		var zoom_box = ImgWnd.document.getElementById('zoom_box');
		zoombox_x += resize_width;
		zoombox_y += resize_height;
		zoom_box.style.left = zoombox_x+'px';
		zoom_box.style.top = zoombox_y+'px';
	}
}

