/*
±èÀ±È£ 2011.02.24
º»¹®³» ÀÌ¹ÌÁöÀÇ ½ÇÁ¦ ³ÐÀÌ°¡ Á¦ÇÑµÈ ³ÐÀÌº¸´Ù Å¬°æ¿ì ÁÙ¿©º¸¿©ÁÖ°í, 
·¹ÀÌ¾î ¶ç¿ö ¿øº» º¸ÀÌ°Ô
*/

var resizePhoto = {
	vars : {"width":650, "element":"articleBody"},//ÃÊ±â°ª.. ¹¹ÇÏ¸é º»¹® Å×ÀÌºí ³ÐÀÌ¸¦ Àç¾î ¿Íµµ µÊ, ÇÑÁ¤ÁöÀ» elementById , "imgRst":{}, "cnt":0

	addEvt:function(obj, evt, fun){
		if(obj.addEventListener)	obj.addEventListener(evt, fun, false);
		else obj.attachEvent("on"+evt, fun);
	},

	init:function(){
		var  imgs = document.getElementById(resizePhoto.vars.element);///ÇÑÁ¤ÇÒ ºÎºÐ .. ¿©±â¼± ±â»ç º»¹® ÀÌ´Ï °Å±âºÎÅÍ ÀÌ¹ÌÁö Å½»ö

		if(imgs && imgs!="undefined"){
			resizePhoto.vars.imgs=imgs;
			resizePhoto.findImgTags();
		}		
	},

	findImgTags:function(){//ÀÌ¹ÌÁö Ã£±â
		var imgs = this.vars.imgs.getElementsByTagName('img');
		if(imgs.length>0){
			this.vars.imgObj=imgs;
			this.sizeOf();
		}
	},
	
	chkImg:function(img, src){//¿øº»ÆÄÀÏÀÇ È®ÀåÀÚ°¡ ¹ºÁö ¸ð¸£´Ï, ÀÏ´Ü ÀÌ°Í¸¸ Ã¼Å©
		//var arr = ["_src.jpg","_src.gif","_src.png","_src.JPG","_src.GIF","_src.PNG"];
		//var _file = src.replace(".",arr[resizePhoto.vars.cnt]);
		//var _file = src.replace(/\.(jpg|JPG|png|PNG|gif|GIF)$/,"_src.$1"); //ÀÌ°Ç _src¶ó´Â ¿øº»ÀÌ¹ÌÁö¸¦ ¶Ç´Ù½Ã ³²°Ü³õÀ»¶§..
		var _file = src;
		var bigImg = document.createElement("img");
	
		try{
			bigImg.setAttribute("src", _file); //Ãß°¡ ½ÃÅ²ÈÄ ½ÇÁ¦ ³ÐÀÌ¸¸ Àç¾î ¿È
			this.addEvt(bigImg, "error", function(){//¿¡·¯ÀÏ¶© ¾Æ¹«ÀÏ¾ÈÇÔ
				/*
				if(resizePhoto.vars.cnt<5){
					resizePhoto.vars.cnt++;
					resizePhoto.chkImg(bigImg, _file);
				}
				*/
			});

			if(window.navigator.userAgent.indexOf("MSIE")>0){ //ie ´Â Â¯³ª°Ô ÇÏ³×;
				var realWidth = resizePhoto.realSize(bigImg);//½ÇÁ¦ ³ÐÀÌ
				
				if(realWidth!=false && realWidth.w>resizePhoto.vars.width && img.width!="undefined"){
					var w = img.width;//ÅÂ±× ³ÐÀÌ
					var h = img.height;
					var src = realWidth.file;
	
					img.setAttribute("width", w<resizePhoto.vars.width?w:resizePhoto.vars.width);
					img.setAttribute("title", "Å¬¸¯ÇÏ¸é Å« ÀÌ¹ÌÁö·Î º¼¼ö ÀÖ½À´Ï´Ù.");
					img.style.border = "2px solid #f4f4f4";
					resizePhoto.clickPhotoEvt(img, _file, realWidth.w, realWidth.h);
				}
			}else{			
				this.addEvt(bigImg, "load", function(){//ÆÄÀÏÀÌ ÀÖÀ»¶§¸¸ Ã³¸®
					var realWidth = resizePhoto.realSize(bigImg);//½ÇÁ¦ ³ÐÀÌ
					
					if(realWidth!=false && realWidth.w>resizePhoto.vars.width && img.width!="undefined"){
						var w = img.width;//ÅÂ±× ³ÐÀÌ
						var h = img.height;
						var src = realWidth.file;
		
						img.setAttribute("width", w<resizePhoto.vars.width?w:resizePhoto.vars.width);
						img.setAttribute("title", "Å¬¸¯ÇÏ¸é Å« ÀÌ¹ÌÁö·Î º¼¼ö ÀÖ½À´Ï´Ù.");
						img.style.border = "2px solid #f4f4f4";
						resizePhoto.clickPhotoEvt(img, _file, realWidth.w, realWidth.h);
					}
				});
			}
		}catch(e){}
		
		return _file;
	},
	
	realSize:function(img){		
		//this.chkImg(img, src)
		
		if(img != "undefined"){
			var _w = img.width;
			var _h = img.height;
			var _file = img.src;
			//document.body.appendChild(img);	
			//document.body.removeChild(img);//²Ü·° °Å¸®´Â°Ô ½È¾î¼­ ÁÖ¼®, ¹«½¼ÀÏ ÀÖÀ¸·Á³ª ¤Ñ¤Ña
			//return {w:_w, file:_file};
		}else return false;
		
		//this.vars.imgRst = {}; //ÃÊ±âÈ­
		return {w:_w, h:_h, file:_file};
	},

	sizeOf:function(){//»çÀÌÁî Á¶Àý
		var obj = this.vars.imgObj;

		for(var i=0; i<obj.length; i++){
			if(obj[i].src.indexOf("icon_p.gif")<0){
				this.chkImg(obj[i], obj[i].src);
			}
		}
	},
	
	//IE Àý´ë ÁÂÇ¥ ±¸ÇÏ±â, daum.net ¼Ò½ºÀÓ (body ºÎÅÍ Â÷·Ê·Î °è»êÇØ¼­ ¿È)
	getAbsoluteTop:function(oNode) { // Àý´ë ³ôÀÌ °è»ê 
	    var oCurrentNode=oNode; 
	    var iTop=0; 
	    while(oCurrentNode.tagName!="BODY") { 
	        iTop+=oCurrentNode.offsetTop; 
	        oCurrentNode=oCurrentNode.offsetParent; 
	    } 
	    return iTop; 
	},

	getAbsoluteLeft:function(oNode) { // Àý´ë ÁÂÃø °è»ê 
	    var oCurrentNode=oNode; 
	    var iLeft=0; 
	    while(oCurrentNode.tagName!="BODY"){ 
	        iLeft+=oCurrentNode.offsetLeft; 
	        oCurrentNode=oCurrentNode.offsetParent; 
	    } 
	    return (iLeft>600?390:iLeft); 
	},
	
	addMoreImg:function(obj, src, w, h){
		if(document.getElementById("bigSizePhoto")) resizePhoto.removeMoreImg();
	
		var img = document.createElement("img");
		img.setAttribute("src","/image2006/icon_p.gif");
		img.setAttribute("title","Å« ÀÌ¹ÌÁö º¸±â");
		//img.setAttribute("id", "bigSizePhoto");
		img.style.position = "absolute";
		img.style.cursor = "pointer";
		img.style.opacity = "0.7"; 
		img.style.filter = "alpha(opacity:70)";
		
		//this.addEvt(img, "mouseover", function(){resizePhoto.addMoreImg(obj, src, w, h);});
		this.addEvt(img, "mousedown", function(){resizePhoto.clickPhoto(src, w, h);});
		
		if(obj.x){
			img.style.left = (obj.x>600?390:obj.x)+(obj.width-24)+"px";
			img.style.top = obj.y+(obj.height-22)+"px";
		}else{
			img.style.left = this.getAbsoluteLeft(obj)+(obj.width-24)+"px";
			img.style.top = this.getAbsoluteTop(obj)+(obj.height-22)+"px";
		}
		
		obj.parentNode.appendChild(img);
	},
	
	removeMoreImg:function(){
		if(document.getElementById("bigSizePhoto"))
		document.getElementById("bigSizePhoto").parentNode.removeChild(document.getElementById("bigSizePhoto"));
	},

	clickPhotoEvt:function(obj, src, w, h){//ÀÌº¥Æ® ÁÜ
		obj.style.cursor = "pointer";
		resizePhoto.addMoreImg(obj, src, w, h);
		
		this.addEvt(obj, "mouseover", function(){
									obj.style.border = "2px solid #696969";
									//resizePhoto.addMoreImg(obj, src, w, h);
								});

		this.addEvt(obj, "mouseout", function(){
									obj.style.border = "2px solid #f4f4f4";
									//resizePhoto.removeMoreImg();
								});
		
		this.addEvt(obj, "mousedown", function(){									
									resizePhoto.clickPhoto(src, w, h);
								});

	},
	
	clickPhoto:function(src, w, h){//Å¬¸¯
		this.positionLayer(src, w, h);
		this.addEvt(window, "resize", function(){  
													if(document.getElementById("imgResizeView") || document.getElementById("imgResizeBox")){
														resizePhoto.closeLayer();//¸®»çÀÌÁî ÇÏ±âÀü Á¦°Å
														resizePhoto.positionLayer(src, w, h);
													}else{//remove event
														if(window.removeEventListener) window.removeEventListener("resize");
														else window.detachEvent("resize");
													}
												 });
	}, 

	positionLayer:function(src, w, h){//ÀÌ¹ÌÁö ¶ç¿ì±â ½ÇÇà
		var cw = document.body.clientWidth >  document.body.scrollWidth ? document.body.clientWidth : document.body.scrollWidth;
		var ch = document.body.clientHeight > document.body.scrollHeight ? document.body.clientHeight : document.body.scrollHeight;
		var st = document.body.scrollTop || document.documentElement.scrollTop;
	
		var xc = cw>w ? (cw/2)-(w/2) : 0; //°¡¿îµ¥ Ã£±â
		var tp = st + 10; //À§·Î ºÎÅÍ ¶ç¿ö¼­

		var tw = cw>w ? cw : w; //·¹ÀÌ¾îÅ©±â , ÀÌ¹ÌÁö°¡ ´õ Å©´Ù¸é ÀÌ¹ÌÁö Å©±â·Î
		var th = (ch>h ? ch : h)+tp; //·¹ÀÌ¾îÅ©±â , ÀÌÁö°¡ ´õ Å©´Ù¸é ÀÌ¹ÌÁö Å©±â·Î + ½ºÅ©·ÑÅ¾
		
		xc = parseInt(xc);
		tp = parseInt(tp);
		tw = parseInt(tw);
		th = parseInt(th);

		//background div
		var div = document.createElement("div");
		div.setAttribute("width", tw);
		div.setAttribute("height", th);
		div.setAttribute("id", "imgResizeView");
		div.setAttribute("title", "Å¬¸¯ÇÏ¸é ÀÌ¹ÌÁö°¡ »ç¶óÁý´Ï´Ù.");
		div.style.position = "absolute";
		div.style.width = tw + "px";
		div.style.height = th + "px";
		div.style.top = "0px";
		div.style.left = "0px";
		div.style.backgroundColor = "#000";
		div.style.opacity = "0.0"; //0.7
		div.style.filter = "alpha(opacity:0)";//70
		div.style.cursor = "pointer";
		div.style.zIndex = "10000000";
		this.addEvt(div, "click", resizePhoto.closeLayer);


		//ÀÌ¹ÌÁö div	
		var imgDiv = document.createElement("div");
		imgDiv.setAttribute("width", w);
		imgDiv.setAttribute("height", h);
		imgDiv.setAttribute("id", "imgResizeBox");
		imgDiv.setAttribute("title", "Å¬¸¯ÇÏ¸é ÀÌ¹ÌÁö°¡ »ç¶óÁý´Ï´Ù.");
		imgDiv.style.position = "absolute";
		imgDiv.style.top = tp + "px";
		imgDiv.style.left = xc + "px";
		imgDiv.style.width = w + "px";
		imgDiv.style.height = h + "px";
		imgDiv.style.opacity = "0.0"; //0.7
		imgDiv.style.filter = "alpha(opacity:0)";//70
		imgDiv.style.backgroundImage = "url("+src+")";
		imgDiv.style.cursor = "pointer";
		imgDiv.style.border = "10px solid #e6e6e6";
		imgDiv.style.zIndex = "10000000";
		this.addEvt(imgDiv, "click", resizePhoto.closeLayer);
		
		
		document.body.appendChild(div); /////·¹ÀÌ¾î ¶ä.
		document.body.appendChild(imgDiv);//±×À§¿¡ ÀÌ¹ÌÁö(img´Â absolute ;;;
		
		this.fadeIn(document.getElementById("imgResizeView"), document.getElementById("imgResizeBox"), 10, 60);
	},
	
	fadeIn:function(obj1, obj2, duration, speed){//°´Ã¼1, °´Ã¼2, °£°Ý, ¼Óµµ 
		var untilAlpha = 70;
		var alpha = 0;
		
		var st1 = setInterval(function(){
									if(alpha < untilAlpha){
										alpha += speed;
										obj1.style.opacity = (alpha/100);
										obj1.style.filter = "alpha(opacity:"+alpha+")";
									}else{
										alpha = 0;
										clearInterval(st1);
										var st2 = setInterval(function(){
																	if(alpha <= 100){
																		alpha += speed;
																		obj2.style.opacity = (alpha/100);
																		obj2.style.filter = "alpha(opacity:"+alpha+")";
																	}else clearInterval(st2);
																}, duration);
									}
								}, duration);
	},

	closeLayer:function(){
		var div = document.getElementById("imgResizeView");
		var imgbox = document.getElementById("imgResizeBox");

		if(imgbox) document.body.removeChild(imgbox);
		if(div) document.body.removeChild(div);
	}
}

/*Àû¿ë
resizePhoto.addEvt(window, 'load', resizePhoto.init);
*/
