// 2004.05.19
// 이미지 사이즈 제한 스크립트
// 사용법 onload=Rsize(this, 400, 400, 1); Rsize(this, 가로제한크기, 세로제한크기, 원본링크);
// 이미지경로, 가로크기제한, 세로크기제한, 원본새창링크

function imgRsize(img, rW, rH){
	var iW = img.width;
	var iH = img.height;
	var g = new Array;
	if(iW < rW && iH < rH) { // 가로세로가 축소할 값보다 작을 경우
		g[0] = iW; 
		g[1] = iH; 
	} else {
		if(img.width > img.height) { // 원크기 가로가 세로보다 크면
			g[0] = rW;
			g[1] = Math.ceil(img.height * rW / img.width);
			} else if(img.width < img.height) { //원크기의 세로가 가로보다 크면
			g[0] = Math.ceil(img.width * rH / img.height);
			g[1] = rH;
			} else {
			g[0] = rW;
			g[1] = rH;
			}
			if(g[0] > rW) { // 구해진 가로값이 축소 가로보다 크면
			g[0] = rW;
			g[1] = Math.ceil(img.height * rW / img.width);
			}
			if(g[1] > rH) { // 구해진 세로값이 축소 세로값가로보다 크면
			g[0] = Math.ceil(img.width * rH / img.height);
			g[1] = rH;
			}
		}
		g[2] = img.width; // 원사이즈 가로
		g[3] = img.height; // 원사이즈 세로
		return g;
	}

	function Rsize(img, ww, hh, aL){
	var tt = imgRsize(img, ww, hh);
					if(img.width > ww || img.height > hh){ // 가로나 세로크기가 제한크기보다 크면
							img.width = tt[0]; // 크기조정
							img.height = tt[1];
							img.alt = 'クリックすると原本イメージを見ることができます。';
							if(aL){ // 자동링크 on
											img.onclick = function(){
													wT = Math.ceil((screen.width - tt[2])/2.6); // 클라이언트 중앙에 이미지위치.
													wL = Math.ceil((screen.height - tt[3])/2.6);
													mm = window.open("", 'viewOrig', 'width='+tt[2]+',height='+tt[3]+',top='+wT+',left='+wL);
													var doc = mm.document;
													doc.body.style.margin = 0; // 마진제거
													doc.body.style.cursor = "hand";
													var previewimg = doc.createElement("img");
													previewimg.src = img.src;
													doc.body.appendChild(previewimg);
													doc.body.onmousedown = function(){ mm.close();}
													doc.title = 'NUX';
											}
											img.style.cursor = "hand";
							}
					} else {
							img.onclick = function(){ alert("現在のイメージが原本イメージです。"); }
					}
	}

	function image_window(img)
    {
        var w = img.tmp_width; 
        var h = img.tmp_height; 
        var winl = (screen.width-w)/2; 
        var wint = (screen.height-h)/3; 

        if (w >= screen.width) { 
            winl = 0; 
            h = (parseInt)(w * (h / w)); 
        } 

        if (h >= screen.height) { 
            wint = 0; 
            w = (parseInt)(h * (w / h)); 
        } 

        var js_url = "<script language='JavaScript1.2'> \n"; 
            js_url += "<!-- \n"; 
            js_url += "var ie=document.all; \n"; 
            js_url += "var nn6=document.getElementById&&!document.all; \n"; 
            js_url += "var isdrag=false; \n"; 
            js_url += "var x,y; \n"; 
            js_url += "var dobj; \n"; 
            js_url += "function movemouse(e) \n"; 
            js_url += "{ \n"; 
            js_url += "  if (isdrag) \n"; 
            js_url += "  { \n"; 
            js_url += "    dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x; \n"; 
            js_url += "    dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y; \n"; 
            js_url += "    return false; \n"; 
            js_url += "  } \n"; 
            js_url += "} \n"; 
            js_url += "function selectmouse(e) \n"; 
            js_url += "{ \n"; 
            js_url += "  var fobj      = nn6 ? e.target : event.srcElement; \n"; 
            js_url += "  var topelement = nn6 ? 'HTML' : 'BODY'; \n"; 
            js_url += "  while (fobj.tagName != topelement && fobj.className != 'dragme') \n"; 
            js_url += "  { \n"; 
            js_url += "    fobj = nn6 ? fobj.parentNode : fobj.parentElement; \n"; 
            js_url += "  } \n"; 
            js_url += "  if (fobj.className=='dragme') \n"; 
            js_url += "  { \n"; 
            js_url += "    isdrag = true; \n"; 
            js_url += "    dobj = fobj; \n"; 
            js_url += "    tx = parseInt(dobj.style.left+0); \n"; 
            js_url += "    ty = parseInt(dobj.style.top+0); \n"; 
            js_url += "    x = nn6 ? e.clientX : event.clientX; \n"; 
            js_url += "    y = nn6 ? e.clientY : event.clientY; \n"; 
            js_url += "    document.onmousemove=movemouse; \n"; 
            js_url += "    return false; \n"; 
            js_url += "  } \n"; 
            js_url += "} \n"; 
            js_url += "document.onmousedown=selectmouse; \n"; 
            js_url += "document.onmouseup=new Function('isdrag=false'); \n"; 
            js_url += "//--> \n"; 
            js_url += "</"+"script> \n"; 

        var settings;

       
       
            settings  ='width='+w+','; 
            settings +='height='+h+','; 
       
        settings +='top='+wint+','; 
        settings +='left='+winl+','; 
        settings +='scrollbars=no,'; 
        settings +='resizable=yes,'; 
        settings +='status=no'; 


        win=window.open("","image_window",settings); 
        win.document.open(); 
        win.document.write ("<html><head> \n<meta http-equiv='imagetoolbar' CONTENT='no'> \n<meta http-equiv='content-type' content='text/html; charset=UTF-8'>\n"); 
        var size = "イメージサイズ : "+w+" x "+h;
        win.document.write ("<title>"+size+"</title> \n"); 
        if(w >= screen.width || h >= screen.height) { 
            win.document.write (js_url); 
            var click = "ondblclick='window.close();' style='cursor:move' title=' "+size+" \n\n イメージサイズが画面より大きいです。 \n 左側ボタンをクリックした後マウスを動かして見てください。 \n\n ダブルクリックすれば閉まります。'"; 
        } 
        else 
            var click = "onclick='window.close();' style='cursor:pointer' title=' "+size+" \n\n クリックすれば閉まります。 '"; 
        win.document.write ("<style>.dragme{position:relative;}</style> \n"); 
        win.document.write ("</head> \n\n"); 
        win.document.write ("<body leftmargin=0 topmargin=0 bgcolor=#dddddd style='cursor:arrow;'> \n"); 
        win.document.write ("<table width=100% height=100% cellpadding=0 cellspacing=0><tr><td align=center valign=middle><img src='"+img.src+"' width='"+w+"' height='"+h+"' border=0 class='dragme' "+click+"></td></tr></table>");
        win.document.write ("</body></html>"); 
        win.document.close(); 

        if(parseInt(navigator.appVersion) >= 4){win.window.focus();} 
    }