/*
 * version : 1
 * var img = new ImageResizer("img", "/img/temp.gif", "viewImg", 200, 200);
 * img.imgChecker();
 *
 * var [ÀÎ½ºÅÏ½º¸í] = new StaticMenu("[ÀÎ½ºÅÏ½º¸í]", [¿øº» image Url], [Html Tag·Î ¼±¾ðµÈ º¸¿©Áú IMGÀÇ ID], [ÃÖ´ë °¡·Î±æÀÌ], [ÃÖ´ë ¼¼·Î±æÀÌ]);
 * 
 * ¸ðµçÇ×¸ñ ÇÊ¼ö
 * instanceName         : ¼±¾ðµÈ instanceÀÇ ÀÌ¸§ : timer ÁöÁ¤½Ã ´ë»ó object ¼±º°¿¡ »ç¿ë
 * srcUrl				: ¿øº» imageÀÇ URL
 * displySrc			: º¸¿©Áú Image TagÀÇ ID
 * maxWidth				: Á¦ÇÑµÇ¾îÁú ÃÖ´ë °¡·Î±æÀÌ
 * maxHeight			: Á¦ÇÑµÇ¾îÁú ÃÖ´ë ¼¼·Î±æÀÌ
 *
 *
 * var imgList = new Array();
 * imgList[imgList.length] = new ImageResizer("imgList["+imgList.length+"]", "/img/temp.gif", "viewImg"+imgList.length, 200, 200);
 * imgList[imgList.length-1].imgChecker();
 * imgList[imgList.length] = new ImageResizer("imgList["+imgList.length+"]", "/img/temp.gif", "viewImg"+imgList.length, 200, 200);
 * imgList[imgList.length-1].imgChecker();
 * À§¿Í °°ÀÌ ¹è¿­¿¡ ´ã°ÜÁú °æ¿ì¿¡ instanceNameÀ» À§¿Í °°ÀÌ ¼±¾ðÇÑ´Ù.
 */
    function ImageResizer(instanceName, srcUrl, displySrc, maxWidth, maxHeight){
        this.thisInstanceName = instanceName;
        this.img = new Image();
        this.img.src = srcUrl;
        this.displySrc = displySrc;
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
        this.useOnclickAction = true;
        
        this.imgChecker = function (){
            if(this.img!=null && this.img.complete == true && this.displySrc!=null){
                var originalImg = this.img;
                var targetImg = document.getElementById(this.displySrc);
                if(originalImg.width > originalImg.height){
                    if(eval(originalImg.width>this.maxWidth)){
                        originalImg.width = this.maxWidth;
                    }
                    targetImg.width = originalImg.width;
                }else{
                    if(eval(originalImg.height>this.maxHeight)){
                        originalImg.height = this.maxHeight;
                    }
                    targetImg.height = originalImg.height;
                }
                targetImg.src = originalImg.src;
                targetImg.style.cursor = "hand";
                if(this.useOnclickAction){
                    targetImg.onclick = openImageTemplerOriginImg;
                }
                targetImg.alt = targetImg.src.substr(targetImg.src.lastIndexOf("/")+1) + " ±×¸²";
            }else{
                this.startSleep();
            }
        }
        
        this.startSleep = function (){
            timer_id = setTimeout(this.thisInstanceName+".imgChecker()", 100);
        }
    }
	
    function openImageTemplerOriginImg(){
        if(!endsWith(window.event.srcElement.src, 'temp.gif')){
            var url = window.event.srcElement.src;
            if(url.indexOf("thumbnail") > -1){
                s = url.substr(0,url.indexOf("thumbnail"));
                n = url.substr(url.indexOf("thumbnail"));
                n = n.substr(n.indexOf("/")+1);
                url = s+n;
            }
            
            var wopt = "scrollbars=yes,status=no,resizable=yes";
                wopt += ",left=0,top=0";
            wopt += ",height=100,width=100";
/*
            var wbody = "<head><title>¢Æ ¿øº»±×¸² º¸±â</title>\n";
                wbody+= "</head>\n";
                wbody+= "<script>\n";
                wbody+= "var timer_id = null;\n";
                wbody+= "function imageLoadChecker(){\n";
                wbody+= "    alert('aa');\n";
                wbody+= "    var imgObj = document.getElementById('imgSrc')\n";
                wbody+= "    if(imgObj!=null && imgObj.complete == true){\n";
                wbody+= "        var i = 0;\n";
                wbody+= "        var width=100, height=100;\n";
                wbody+= "        if(imgObj.width >= screen.width){\n";
                wbody+= "            width = screen.width;\n";
                wbody+= "        }else{\n";
                wbody+= "            width = imgObj.width + 30;\n";
                wbody+= "        }\n";
                wbody+= "        if(imgObj.height >= screen.height){\n";
                wbody+= "            height = screen.height;\n";
                wbody+= "        }else{\n";
                wbody+= "            height = imgObj.height + 55;\n";
                wbody+= "        }\n";
                wbody+= "        window.resizeTo(width, height);\n";
                wbody+= "    }else{\n";
                wbody+= "        timer_id = setTimeout('imageLoadChecker()', 100);\n";
                wbody+= "    }\n";
                wbody+= "}\n";
                wbody+= "</script>\n";
                wbody+= "<body style='margin:0px;padding:0px;' valign='middle' align='center'>\n";
                wbody+= "<a href='javascript:window.close()'><img id='imgSrc' src='" + url + "' border=0 onload='imageLoadChecker()'></a>\n";
                wbody+= "</body></html>\n";*/
            winResult = window.open(contextPath+"/include/realImgViewer.jsp?imgPath="+url,"",wopt);
            //winResult = window.open("about:blank","",wopt);
            //winResult.document.open("text/html", "replace");
            //winResult.document.write(wbody);
            //winResult.document.close();
        }
        return;
    }