function attach_file_set(){

	var attach_image = document.getElementsByName("attach_image");
	var attach_image_length = attach_image.length;

	var imageMenu = document.createElement('DIV');
	document.body.appendChild(imageMenu);
	imageMenu.id = "imageMenu";
	imageMenu.style.display = "block";
	imageMenu.style.position = "absolute";
	imageMenu.onmouseover = function() {imageMenu.style.display='block'};
	imageMenu.onmouseout = function() {imageMenu.style.display='none'};

	for(i=0;i<attach_image_length;i++){
		resize_image(attach_image[i], 570, 0);
		attach_image[i].onmouseover = image_menu_show;
		attach_image[i].onmouseout = image_menu_hide;
	}

	//-----------------------

	var attach_object = document.getElementsByName("attach_object");
	var attach_object_length = attach_object.length;

	for(i=0;i<attach_object_length;i++){

		var file_url = attach_object[i].href;

		var canCreateContextualFragment = (
			typeof document.body.ownerDocument != 'undefined' && 
			document.body.ownerDocument.createRange &&
			document.body.ownerDocument.createRange().createContextualFragment
		);

		if (document.getElementById("attach_object_" + i) == null) {
			var sHTML = "<br><embed id='attach_object_" + i + "' src='"+file_url+"' autostart=0 showstatusbar=1 showdisplay=0 type='application/x-mplayer2'><br>";
			insertHTML (attach_object[i], sHTML)
		}
	}

}


function attach_image_popup(imageSrc) {
	imageSrc = imageSrc.replace("/thumbnail/", "/")
	openLargeImgWindow("/_Cmn/JS/popupImage/jsPopupImage.asp?url="+escape(imageSrc), "popup_image", 350, 250, "center", "center", "scrollbars,status")
}


function image_menu_show() {

	var imageMenu = document.getElementById("imageMenu");

	imageMenu.innerHTML = '<table border="0" cellspacing="0" cellpadding="2" bgcolor="#FFFFFF" style="border:1px solid #C0C0C0">' + 
		'<tr><td style="font-size:9pt;white-space:nowrap">'+
		'<a href="#imagePopup" onclick="attach_image_popup(\''+this.src+'\');"><img src="/_Cmn/JS/popupImage/zoom.gif"></a>'+
		'</td></tr></table>';

	imageMenu.style.display = "block";

	var x = posX(this) + this.offsetWidth - imageMenu.offsetWidth;
	var y = posY(this) + this.offsetHeight - imageMenu.offsetHeight;

	imageMenu.style.left = x + "px";
	imageMenu.style.top =  y + "px";
}

function image_menu_hide() {
	var imageMenu = document.getElementById("imageMenu");
	imageMenu.style.display = "none";
}


function openLargeImgWindow(url, name, width, height, align, valign, option) {
	var x,y;
	var window_option = "width="+width+",height="+height;
	
	if (option!=null) window_option+=","+option;
	if (align==null) align="center";
	if (valign==null) valign="center";
	
	if (align=="left") x=0;
	else if (align=="right") x=(screen.width-width);
	else if (align=="center") x=(screen.width-width)/2
		
	if (valign=="top") y=0;
	else if (valign=="bottom") y=(screen.height-height);
	else if (valign=="center") y=(screen.height-height)/2
	
	window_option+=",left="+x+",top="+y;

	var win_obj = window.open(url,name,window_option);

	win_obj.focus();
	return win_obj;
}


function resize_image(img, re_width, re_height) {
	var orig_width = img.width;
	var orig_height = img.height;

	if (re_width == 0) re_width = orig_width;
	if (re_height == 0) re_height = orig_height;
 
	var re_image = new Array;
	if(orig_width < re_width && orig_height < re_height) {
		re_image[0] =  orig_width; 
		re_image[1] =  orig_height; 
	} else {
		if(img.width > img.height) {
			re_image[0] = re_width;
			re_image[1] = Math.ceil(img.height * re_width / img.width);
		} else if(img.width < img.height) {
			re_image[0] = Math.ceil(img.width * re_height / img.height);
			re_image[1] = re_height;
		} else {
			re_image[0] = re_width;
			re_image[1] = re_height;
		}
		if(re_image[0] > re_width) {
			re_image[0] = re_width;
			re_image[1] = Math.ceil(img.height * re_width / img.width);
		}
		if(re_image[1] > re_height) {
			re_image[0] = Math.ceil(img.width * re_height / img.height);
			re_image[1] = re_height;
		}
	}

	if(img.width > re_width || img.height > re_height){
		img.width = re_image[0];
		img.height = re_image[1];
	}
}


function posX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	return curleft;
}

function posY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	return curtop;
}


function insertHTML (obj, sHTML) {
	if (document.all) {
		// ÀÎÅÍ³Ý ÀÍ½ºÇÃ·Î·¯ 4 ÀÌ»ó
		obj.insertAdjacentHTML('beforeEnd', sHTML);
	} else if(document.createRange) {
		// ³Ý½ºÄÉÀÌÇÁ 6 ÀÌ»ó
		var objRange = document.createRange();
		objRange.setStartAfter(obj.lastChild);
		var objDocFrag = objRange.createContextualFragment(sHTML);
		obj.appendChild(objDocFrag);
	} else if (document.layers) {
		// ³Ý½ºÄÉÀÌÇÁ 4
		var objLayer = new Layer(window.innerWidth); 
		objLayer.document.open(); 
		objLayer.document.write(sHTML);
		objLayer.document.close();
		objLayer.top = document.height;
		document.height += objLayer.document.height;
		objLayer.visibility = 'show';
	}
}

if (typeof window.addEventListener != "undefined") {
	window.addEventListener("load", attach_file_set, true);
} else if (typeof window.attachEvent != "undefined") {
	window.attachEvent("onload", attach_file_set);
} else {
	window.onload = attach_file_set;
}
