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 prvObj;

function view_detail(productNo, pos, pw, ph)
{
        var win, settings;

        pw = pw + 375;
        ph = ph + 160;

        if (pw < 650) pw = 650;
        if (ph < 600) ph = 600;

        settings = "width=" + pw + ",height=" + ph +",scrollbars=yes,left=10,top=10,resizable=yes";
        url = "/common/view_detail.php?ProductNo=" + productNo + "&pos=" + pos + "&resize=yes";

        win = window.open(url, productNo, settings);

        if (win.focus) win.focus();
}

function set_cookie (name, value, expires)
{
    document.cookie = name + "=" + escape (value) + "; path=/; expires=" + expires.toGMTString();
}

function get_cookie(name)
{
    var search = name + "=";
    
    if(document.cookie.length > 0)
    {
        offset = document.cookie.indexOf(search);
        
        if(offset != -1)
        {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            
            if(end == -1) end = document.cookie.length;
            
            return unescape(document.cookie.substring(offset, end));
        }
    }

    return "";
}

function showRequestMessage(message)
{
	$("#request_bar").html(message);
	$("#request_bar").fadeIn();
}

function hideRequestMessage()
{
	$("#request_bar").fadeOut();
}

function showNoticeBarWarningInit(message)
{
	$("#notice_bar").attr("class", "init_warning");
	$("#notice_bar .message").html(message);
	$("#notice_bar").slideDown();
}

function showNoticeBarWarning(message)
{
	$("#notice_bar").attr("class", "warning");
	$("#notice_bar .message").html(message);
	$("#notice_bar").slideDown();
	
	// var hideNoticeBarTimer = window.setInterval("hideNoticeBar()", 3000);
}

function showNoticeBarInfo(message)
{
	$("#notice_bar").attr("class", "info");
	$("#notice_bar .message").html(message);
	$("#notice_bar").slideDown();
	
	// var hideNoticeBarTimer = window.setInterval("hideNoticeBar()", 3000);
}

function hideNoticeBar()
{
	$("#notice_bar").slideUp();
}

function tooglepreview(img)
{
    var expdate = new Date();
    expdate.setTime(expdate.getTime() + 1000 * 3600 * 24 * 365);

    if (get_cookie("user_preview") == "yes")
    {
        img.src = "/imgs/icon_preview0a.gif";

        set_cookie("user_preview", "no", expdate);
    }
    else
    {
        img.src = "/imgs/icon_preview1a.gif";
        set_cookie("user_preview", "yes", expdate);
    }
}

function loginIdHandler()
{
	var expdate = new Date();
    expdate.setTime(expdate.getTime() + 1000 * 3600 * 24 * 365);
    
	if ($("#autoLogin:checked").val() == "on")
	{
		if ($("#login_id").val() == "")
		{
			alert("아이디를 입력해주십시오.");
			return;
		}
		
		set_cookie("save_user_id", $("#login_id").val(), expdate);
	}
	else
	{
		set_cookie("save_user_id", "", expdate);
	}
}

if (document.getElementById || document.all)
{
	document.write('<div id="trailimageid" style="width:100px; height:100px;">');
	document.write('</div>');
}

function gettrailobj()
{
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

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 showtrailvcd(imagename, title, imagecount, showpreview, width, height, linecount, rowcount)
{
	if (width > 0) {currentimagewidth = width + (linecount * 11);}
	if (height > 0) {currentimageheight = height + (rowcount * 5);}

	document.onmousemove=followmouse2;

	var prevImage = imagename.split(",");

	newHTML = '<div style="padding: 0px; background-color: #FFF;">';
	newHTML = newHTML + '<table width="'+currentimagewidth+'" cellspacing="0" cellpadding="0" border="0"><tr><td bgcolor="#f0f0f0" style="padding: 5px;">';
	newHTML = newHTML + '<strong><font color=#333333>' + title + '</strong></font> <br/>';
	newHTML = newHTML + ' ※<font color=#777777 class=d11>전체 <strong>' + imagecount + '</strong>개의 이미지 중 무작위로 선정한 ' + (prevImage.length - 1) + '개의 이미지입니다.<br/>';
    hewHTML = newHTML + '</td></tr></table>';
	
	for (i = 0; i < prevImage.length - 1; i++)
	{
		newHTML = newHTML + '<div align="center" style=" float: left; padding-left: 4px; padding-bottom: 5px;">';
		newHTML = newHTML + '<img src="' + prevImage[i] + '" border="0"></div>';
	}

	newHTML = newHTML + '</div>';

	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().visibility="visible";
}

function showtrail(imagename, thumb, title, description, showpreview, width, height, ck)
{
    if (ck == "yes")
    {
        if (showpreview <= 0 || get_cookie("user_preview") != "yes") return;
    }
    else
    {
        if (showpreview <= 0) return;
    }

	if (width == 0 && height == 0)
	{
		var pv = new Image();

		pv.src = imagename;

		currentimagewidth = pv.width;
		currentimageheight = pv.height;
	}
	else
	{
		if (width > 0) {currentimagewidth = width;}
		if (height > 0) {currentimageheight = height;}
	}

	document.onmousemove=followmouse;

	newHTML = '<div style="padding: 0px;">';
	newHTML = newHTML + '<table cellspacing="0" cellpadding="0" border="0" style="border: 1px solid #333333;"><tr><td bgcolor="#444444" style="padding: 5px;color:white;font-weight: bold; font-size: 11pt;">';
	newHTML = newHTML + title;
    hewHTML = newHTML + '</td></tr></table>';

    newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;">';
	newHTML = newHTML + '<img name="realpreview" id="realpreview" src="' + imagename + '" border="0" style="display:none;">';
	newHTML = newHTML + '<img name="loadingpreview" id="loadingpreview" src="' + thumb + '" border="0" width="'+width+'" height="'+height+'" style="display:block;">';
	newHTML = newHTML + '<div name="loader" id="loader" style="border-width:0px;margin:0px;padding:0px;z-index:100;position:absolute;width:43px;left:'+((width/2)-15)+'px;top:'+((height/2)-10)+'px;"><img src="/imgs/loader.gif"></div>';
	newHTML = newHTML + '</div>';
	
	newHTML = newHTML + '</div>';
	
	prvObj = setTimeout("checkPreviewLoading()",100);
	
	gettrailobjnostyle().innerHTML = newHTML;
//	gettrailobj().visibility="visible";
        gettrailobj().display="block";
}

function checkPreviewLoading()
{
	var real;
	var loading, loader;
	
	if (document.getElementById) 
	{
		real = document.getElementById('realpreview');
		loading = document.getElementById('loadingpreview');
		loader = document.getElementById('loader');
	}
	else if (document.all) 
	{
		imgreal = document.all.realpreview;
		loading = document.all.loadingpreview;
		loader = document.all.loader;
	}
	
	if (real.complete)
	{
		loading.style.display = 'none';
		loader.style.display = 'none';
		real.style.display = 'block';
		
		clearTimeout(prvObj);
	}
	else prvObj = setTimeout("checkPreviewLoading()",100);
}

function hidetrail()
{
	if (prvObj && prvObj > 0) clearTimeout(prvObj);
	
//	gettrailobj().visibility="hidden";
	gettrailobj().display="none";
	document.onmousemove="";
//	gettrailobj().left="-1000px";
}

function followmouse(e)
{
	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) : pageYOffset+Math.min(window.innerHeight)

	if (typeof e != "undefined")
	{
		if (docwidth - e.pageX < (currentimagewidth + 40))
			xcoord = e.pageX - xcoord - (currentimagewidth + 20); // Move to the left side of the cursor
		else 
			xcoord += e.pageX;

		if (docheight - e.pageY < (currentimageheight + 50))
			ycoord += e.pageY - Math.max(0,(50 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		else 
			ycoord += e.pageY;

	} 
	else if (typeof window.event != "undefined")
	{
		if (docwidth - event.clientX < (currentimagewidth + 40))
			xcoord = event.clientX + truebody().scrollLeft - xcoord - (currentimagewidth + 20); // Move to the left side of the cursor
		else 
			xcoord += truebody().scrollLeft+event.clientX
		
		if (docheight - event.clientY < (currentimageheight + 50))
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(50 + currentimageheight + event.clientY - docheight));
		else 
			ycoord += truebody().scrollTop + event.clientY;
	}

	if(ycoord < 0) {ycoord = ycoord*-1;}
	
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}

function insert_lightbox_into_MyLightBox(folderno, pno)
{
	$("#im"+pno).attr("src", "/imgs/mini-loader.gif");

	var pars = "action=insertMyLightBox&productno="+pno;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			$("#im"+pno).attr("src", "/imgs/icon_lightbox_on.gif");
			$("#im"+pno).attr("alt", "라이트박스 제거");
			$("#im"+pno).attr("title", "라이트박스 제거");
			$("#im"+pno).attr("lightbox", "yes");
                        $("#im"+pno).removeAttr("onclick");
                        $("#im"+pno).unbind('click');
			$("#im"+pno).click(function(){
                            remove_lightbox(folderno, pno);
                        });
		} 
	 });
}

function insert_lightbox(folderno, pno)
{
	$("#im"+pno).attr("src", "/imgs/mini-loader.gif");

	var pars = "action=insert&productno="+pno+"&folderno="+folderno;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			$("#im"+pno).attr("src", "/imgs/icon_lightbox_on.gif");
			$("#im"+pno).attr("alt", "라이트박스 제거");
			$("#im"+pno).attr("title", "라이트박스 제거");
			$("#im"+pno).attr("lightbox", "yes");
                        $("#im"+pno).removeAttr("onclick");
                        $("#im"+pno).unbind('click');
			$("#im"+pno).click(function(){
                            remove_lightbox(folderno, pno);
                        });
		}
	 });
}

function insert_lightbox_just_into_MyLightBox(folderno, pno, link)
{
	var pars = "action=insertMyLightBox&productno="+pno;
	
	link.innerText = "담는중...";
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			alert("라이트박스에 담았습니다.");
			link.style.display = "none";
		}
	 });
}

function insert_lightbox_just(folderno, pno, link)
{
	var pars = "action=insert&productno="+pno+"&folderno="+folderno;
	
	link.innerText = "담는중...";
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			alert("라이트박스에 담았습니다.");
			link.style.display = "none";
		}
	 });
}

function remove_lightbox(folderno, pno)
{
//    $("#im"+pno).unbind('click');
    move_to_trash(folderno, pno);
    //alert("sads");
}

function move_to_trash(folderno, pno)
{
	$("#im"+pno).attr("src", "/imgs/mini-loader.gif");
	$('#lightbox_item_'+pno).css("background-color", "#ffd7d7");
	
	var pars = "action=remove&productno="+pno+"&folderno="+folderno;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			$('#lightbox_item_'+pno).remove();
//			$("#trashItems").text(parseInt($("#trashItems").text()) + parseInt(data));
			$("#im"+pno).attr("src", "/imgs/icon_lightbox.gif");
			$("#im"+pno).attr("alt", "라이트박스 담기");
			$("#im"+pno).attr("title", "라이트박스 담기");
                        $("#im"+pno).removeAttr("onclick");
                        $("#im"+pno).unbind('click');
			$("#im"+pno).click(function(){
                            insert_lightbox(folderno, pno);
                        });
		}
	 });
}

function delete_lightbox_image(folderno, pno)
{
	$('#lightbox_item_'+pno).css("background-color", "#ffd7d7");
	
	var pars = "action=delete&productno="+pno+"&folderno="+folderno;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			$('#lightbox_item_'+pno).remove();
//			$("#trashItems").text(parseInt($("#trashItems").text()) + parseInt(data));
//			$("#im"+pno).attr("src", "/imgs/icon_lightbox.gif");
//			$("#im"+pno).attr("alt", "라이트박스 담기");
//			$("#im"+pno).attr("title", "라이트박스 담기");
//                        $("#im"+pno).removeAttr("onclick");
//                        $("#im"+pno).unbind('click');
//			$("#im"+pno).click(function(){
//                            insert_lightbox(folderno, pno);
//                        });
		}
	 });
}

function recovery_to_lightbox(folderno, pno)
{
	$('#lightbox_item_'+pno).css("background-color", "#d7ebff");
	
	var pars = "action=recovery&productno="+pno+"&folderno="+folderno;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {$('#lightbox_item_'+pno).remove();}
	 });
}

function recovery_all_to_lightbox(folderno)
{
	var pars = "action=recoveryall&folderno="+folderno;

	if (!confirm("모든 이미지를 라이트박스로 복원하시겠습니까?")) return;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {location.href = "/lightbox/";}
	 });
}

function empty_trash(folderno)
{
	var pars = "action=empty&folderno="+folderno;
	
	if (!confirm("휴지통을 모두 비우시겠습니까?")) return;
	
	$.ajax({
		type: "GET",
		url: "/lightbox/lightbox.tools.php",
		data: pars,
		dataType: "html",
		success: function (data) {
			location.href = "/lightbox/";
		}
	 });
}

function mumberComma(str) 
{ 
	var sp=""; 
	var mc=""; 

	str = str + '';

	for(var i=str.length-1; i>=0; i--) 
	{ 
		sp=str.charAt(i); 
		mc=sp+mc; 
		if(i%3==str.length%3 && i!=0) mc=','+mc;
	}

	return mc; 
} 

