/*******************************************************************************************************
* 프리뷰 이미지 레이어
* 사용법 (#표시는 필수)
	보이기 : ShowPreView(#이미지코드, 회사코드, 타이틀, 설명, #사용값, #가로사이즈, #세로사이즈, 쿠키사용여부, #이미지주소);
	숨기기 : HidePreView();

	/common/default.css 의 [#trailimageid] 부분 필수 참조.
	쿠키 사용 시 common.js에 추가한 set_cookie, get_cookie 사용 필수 참조.
*******************************************************************************************************/

var offsetfrommouse=[15, -15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var currentimagewidth = 600;	// maximum image width.
var currentimageheight = 400;	// maximum image height.
var nowPreviewPath = "";
var preImageCode = "";			// 이전 이미지코드를 기억후 다시 로딩하여 부하를 줄인다.

if (document.getElementById || document.all) {
	document.write('<div id="trailimageid" style="width:1; height:1; z-index:100; display:none;">');
	document.write('</div>');
}

if (document.getElementById || document.all) {
	document.write('<iframe id="DivShim" src="about:blank" scrolling="no" frameborder="0" style="position:absolute; top:100px; left:100px;" width="0" height="0">');
	document.write('</iframe>');
}

function getshimobj() {
	if (document.getElementById) return document.getElementById("DivShim").style
	else if (document.all) return document.all.DivShim.style
}

function gettrailobj() {
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

function getshimobjnostyle() {
	if (document.getElementById) return document.getElementById("DivShim")
	else if (document.all) return document.all.DivShim
}

function gettrailobjnostyle() {
	if (document.getElementById) return document.getElementById("trailimageid")
	else if (document.all) return document.all.trailimagid
}

function truebody() {
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

function ShowPreView(imageCd, CompanyId, title, description, showpreview, width, height, ck, PreviewUrl) {

	if (ck == "yes") {
		if (showpreview <= 0 || get_cookie("user_preview") != "yes") return;
	} else {
		if (showpreview <= 0) return;
	}
	nowPreviewPath = PreviewUrl;

	if (width == 0 && height == 0) {
		var pv = new Image();

		pv.src = nowPreviewPath;

		currentimagewidth = pv.width;
		currentimageheight = pv.height;										
	} else {
		if (width > 0) { currentimagewidth = width; }
		if (height > 0) { currentimageheight = height; }
	}

	document.getElementById("trailimageid").style.display = "";
	document.onmousemove=followmouse;

	newHTML = '<div style="padding:0px; background-color:#FFFFFF; z-index:100;">';
	newHTML = newHTML + '<table cellpadding="10" cellspacing="1" border="0" bgcolor="#000000"><tr><td bgcolor="#FFFFFF" align="center">';
	newHTML = newHTML + '<div align="center" style="padding: 10px 10px 10px 10px;">';
	newHTML = newHTML + '<img src="' + nowPreviewPath + '" border="0"></div>';
	newHTML = newHTML + '</div>';
	newHTML = newHTML + '</td></tr></table>';

	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().visibility="visible";
}

function HidePreView() {
	gettrailobj().visibility = "hidden";
    getshimobj().visibility = "hidden";

	document.onmousemove="";
	gettrailobj().left="-1000px";
}

var getNowScroll = function() {
	var de = document.documentElement;
	var b = document.body;
	var now = {};

	now.X = document.all ? (!de.scrollLeft ? b.scrollLeft : de.scrollLeft) : (window.pageXOffset ? window.pageXOffset : window.scrollX);
	now.Y = document.all ? (!de.scrollTop ? b.scrollTop : de.scrollTop) : (window.pageYOffset ? window.pageYOffset : window.scrollY);

	return now;
}

function followmouse(e) {
	// 이미지 사이즈 한번더 체크
	if (currentimagewidth == 0 || currentimageheight == 0) {
		var pv = new Image();

		pv.src = nowPreviewPath;

		currentimagewidth = pv.width;
		currentimageheight = pv.height;
	}

	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15;
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);

	obj = document.getElementById("listPhoto_Box");

	if (typeof e != "undefined") {
		var inBodytop = getObjectTop(obj) - getNowScroll().Y;
		var inBodyleft = getObjectLeft(obj) - getNowScroll().X;

		// width
		if (e.pageX > currentimagewidth)
		{ // to left
			xcoord = e.pageX - inBodyleft - currentimagewidth - 55;
		}
		else
		{
			xcoord = e.pageX - inBodyleft + 20;
		}

		// height
		if ((e.pageY - getNowScroll().Y) > currentimageheight + 50)
		{ // to Top
			ycoord = (e.pageY - getNowScroll().Y) - inBodytop - currentimageheight - 55;
		}
		else if((docheight - (e.pageY - getNowScroll().Y)) > currentimageheight + 100)
		{ // to Down
			ycoord = (e.pageY - getNowScroll().Y) - inBodytop + 20;
		}
		else
		{ // to Middle
			if (inBodytop >= 0)
			{
				ycoord = 50 - inBodytop;
			}
			else
			{
				ycoord = Math.abs(inBodytop) + 50;
			}
		}

	} else if (typeof window.event != "undefined") {
		var inBodytop = getObjectTop(obj) - document.body.scrollTop;
		var inBodyleft = getObjectLeft(obj) - document.body.scrollLeft;

		// width
		if (event.clientX > currentimagewidth)
		{ // to left
			xcoord = event.clientX - inBodyleft - currentimagewidth - 55;
		}
		else
		{
			xcoord = event.clientX - inBodyleft + 20;
		}

		// height
		if (event.clientY > currentimageheight + 50)
		{ // to Top
			ycoord = event.clientY - inBodytop - currentimageheight - 55;
		}
		else if((docheight - event.clientY) > currentimageheight + 100)
		{ // to Down
			ycoord = event.clientY - inBodytop + 20;
		}
		else
		{ // to Middle
			if (inBodytop >= 0)
			{
				ycoord = 50 - inBodytop;
			}
			else
			{
				ycoord = Math.abs(inBodytop) + 50;
			}
		}
	}

	gettrailobj().left=xcoord+"px";
	gettrailobj().top=ycoord+"px";

    getshimobj().top = gettrailobj().top;
    getshimobj().left = gettrailobj().left;
    getshimobjnostyle().width = gettrailobjnostyle().offsetWidth;
    getshimobjnostyle().height = gettrailobjnostyle().offsetHeight;
    getshimobj().zIndex = gettrailobj().zIndex - 1;
    getshimobj().visibility = "visible";

	/*
	//Value test
	alr_str = "현재화면 w : "+docwidth+"\n";
	alr_str += "현재화면 h : "+docheight+"\n";
	alr_str += "이미지 w : "+currentimagewidth+"\n";
	alr_str += "이미지 h : "+currentimageheight+"\n";
	alr_str += "마우스위치 x : "+e.pageX+"\n";
	alr_str += "마우스위치 y : "+e.pageY+"\n";
	alr_str += "스크롤 x : "+getNowScroll().X+"\n";
	alr_str += "스크롤 y : "+getNowScroll().Y+"\n";
	alr_str += "안쪽레이어 x : "+getObjectLeft(obj)+"\n";
	alr_str += "안쪽레이어 y : "+getObjectTop(obj)+"\n";
	alr_str += "inBody x : "+inBodyleft+"\n";
	alr_str += "inBody y : "+inBodytop+"\n";	
	alr_str += "뿌려지는 위치 x : "+xcoord+"\n";
	alr_str += "뿌려지는 위치 y : "+ycoord+"\n";
	alert(alr_str);
	*/
	xcoord = 0;
	ycoord = 0;
	inBodytop = 0;
	inBodyleft = 0;

}

function getObjectTop(obj)
{
    if(obj.offsetParent == document.body)
        return obj.offsetTop;
    else
        return obj.offsetTop + getObjectTop(obj.offsetParent);
}

function getObjectLeft(obj)
{
    if(obj.offsetParent == document.body)
       return obj.offsetLeft;
   else
       return obj.offsetLeft + getObjectLeft(obj.offsetParent);
}

function getPreviewPath(originalRequest)
{
	//put returned XML in the textarea
	nowPreviewPath = originalRequest.responseText;
}

