function lightbox_view_detail(productNo, pos, aid, w, h)
{
    settings = "width=" + w + ",height=" + h + ",scrollbars=yes,top=10,left=10";
    url = "../common/view_detail.php?ProductNo=" + productNo + "&pos=" + pos + "&work_type=lightbox_search&alamy_id="+aid;
    
    win = window.open(url,'_detail_view', settings);

    if (win.focus) win.focus();
}

function MessageSianServiceUnavailable()
{
    alert("죄송합니다. 일반회원은 시안요청을 하실 수 없습니다.\r\n[정보변경]의 [회원등급 변경요청]에서 정회원 신청을 해주시기 바랍니다.");
    agree = confirm('정회원 신청 페이지로 이동하시겠습니까?');
    
    if (agree)
    {
        location.href = "../member/uplevel_req.php";
    }
}

function MessageOrderServiceUnavailable()
{
    alert("죄송합니다. 일반회원은 구매요청을 하실 수 없습니다.\r\n[정보변경]의 [회원등급 변경요청]에서 정회원 신청을 해주시기 바랍니다.");
    agree = confirm('정회원 신청 페이지로 이동하시겠습니까?');
    
    if (agree)
    {
        location.href = "../member/uplevel_req.php";
    }
}

function MessageSianServiceRestricted()
{
    alert("죄송합니다. 고객님은 무료시안 서비스 대상 고객이 아니므로 시안서비스 제공과\r\n\r\n관련해서는 당사 고객센터로 직접 문의바랍니다. 이용에 불편을 드려 죄송합니다.\r\n\r\n"+gjv_service_phone_no+" ("+gjv_service_hours+")");
}

function MessageOrderServiceRestricted()
{
    alert("죄송합니다. 구매를 원하실 경우 당사 고객센터로 직접 문의바랍니다. 이용에 불편을 드려 죄송합니다.\r\n\r\n"+gjv_service_phone_no+" ("+gjv_service_hours+")");
}

function order_images()
{
    var selectedImages = $("input:checkbox[name=chkImageSelect[]]:checked").length;
    var alamySelectedImages = $("input:checkbox[name=chkAlamyImageSelect[]]:checked").length;
	
    if (selectedImages <= 0 && alamySelectedImages <= 0)
    {
        alert("이미지를 한개 이상 선택해 주세요!");
        return false;
    }

    exception1 = validate_order_images('chkImageSelect');
    exception2 = validate_order_images('chkAlamyImageSelect');
    
    if (exception1 && exception2)
    {
    	
    	if (gjv_text_book_company == "YES")
    		if (!confirm("현재 교과서 프로젝트 건으로 10M 무료시안서비스(RM한함)를 제공중입니다.\n\n그래도 구매로 진행하시겠습니까?")) return;
		
	    document.frmImageSelect.action = "../down/shopbag_insert.php?url=../down/shopbag.php?lightbox=1";
	    document.frmImageSelect.submit();
    }
}

function sian_images()
{
    var selectedImages = $("input:checkbox[name=chkImageSelect[]]:checked").length;
    var alamySelectedImages = $("input:checkbox[name=chkAlamyImageSelect[]]:checked").length;
    
    if (selectedImages <= 0 && alamySelectedImages <= 0)
    {
        alert("이미지를 한개 이상 선택해 주세요!");
        return;
    }
    
    if (selectedImages > gjv_max_sian_request_cart)
    {
        alert("한 건당 " + gjv_max_sian_request_cart + "개 이하로 요청해 주세요!");
        return;
    }

    // 남은 시안요청 이미지 갯수
    if (selectedImages > (gjv_max_sian - gjv_today_sian_count))
    {
        if ((gjv_max_sian - gjv_gjv_today_sian_count) > 0)
            alert("하루 최대 시안요청 이미지수 "+gjv_max_sian+"개를 초과하셨습니다.\r\n" + (gjv_max_sian - gjv_gjv_today_sian_count) + " 개의 이미지만 요청하실 수 있습니다.");
        else
            alert("하루 최대 시안요청 이미지수 "+gjv_max_sian+"개를 모두 사용하셨습니다.");
        
        return;
    }
    
    exception1 = validate_sian_images('chkImageSelect');
    exception2 = validate_sian_images('chkAlamyImageSelect');
    
    if (exception1 && exception2)
    {
    	document.frmImageSelect.action = "../lightbox/sian_request.php?cmd=add";
    	document.frmImageSelect.submit();
    }
}

function validate_sian_images(name)
{
	var result = true;
	
    $("input:checkbox[name="+name+"[]]:checked").each( function () {
    	if ($(this).attr("SaleStatus") != "S")
        {
            alert($(this).attr("ProductId") + " 이미지는 판매종료된 이미지입니다. 시안을 제공해 드릴 수 없습니다.");
            result = false;
            return;
        }

    	if ($(this).attr("LicenseType") == "CD")
        {
            alert($(this).attr("ProductId") + " 는 시안을 제공하지 않습니다.");
            result = false;
            return;
        }

    	if ($(this).attr("LicenseType") == "RF" && $(this).attr("SaleType").indexOf("RF-sale") == -1)
        {
            if ($(this).attr("ProductType") == "Footage") alert($(this).attr("ProductId") + " 동영상은 시안을 제공하지 않습니다.");
            else alert($(this).attr("ProductId") + " 이미지는 CD 로만 구매 가능하며 시안을 제공하지 않습니다.");
            
            result = false;
            return;
        }

        if ($(this).attr("CompService") != "Y")
        {
            alert($(this).attr("ProductId") + " 이미지는 시안을 제공해 드릴 수 없습니다.");

            result = false;
            return;
        }
    });
    
    return result;
}

function validate_order_images(name)
{
	var result = true;
	
    $("input:checkbox[name="+name+"[]]:checked").each( function () {
    	if ($(this).attr("PossibleOrder") != "Y")
        {
            alert($(this).attr("ProductId") + " 이미지는 구입하실 수 없는 이미지입니다.");

            result = false;
            return;
        }

        if ($(this).attr("SaleStatus") != "S")
        {
            alert($(this).attr("ProductId") + " 이미지는 판매종료된 이미지입니다. 구입하실 수 없습니다.");

            result = false;
            return;
        }

        if ($(this).attr("LicenseType") == "RF" && $(this).attr("SaleType").indexOf("RF-sale") == -1 && $(this).attr("ProductType") != "Footage")
        {
            alert($(this).attr("ProductId") + " 이미지는 CD 로만 구매 가능합니다.");

            result = false;
            return;
        }
    });

    return result;
}

function sendmail_images()
{
    var count = 0;

    if (document.frmImageSelect != null)
    {
        for (i = 0; i < document.frmImageSelect.elements.length; i++)
        {
            if (document.frmImageSelect.elements[i].checked) count++;
        }
    }

    if (count == 0)
    {
        alert("이미지를 한개 이상 선택하여 주시기 바랍니다.");
        return false;
    }

    url = "";
    window.open(url, "sendmail", "left=0,top=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=470");

    document.frmImageSelect.action = "lightbox_sendmail.php";
    document.frmImageSelect.target = "sendmail";
    document.frmImageSelect.submit();
}

function reloading_mini_lightbox(foldertype)
{
	onBlock();
	
	loading_mini_lightbox(foldertype);
}

function change_folder_mini_lightbox(folder, foldertype)
{
	$.ajax({
	 	   type: "GET",
	 	   url: "../lightbox/lightbox.v2.ajax.php",
	 	   data: "foldertype="+foldertype+"&folderno="+folder,
	 	   success: responseMiniLightbox
	 	});
}

function loading_mini_lightbox(foldertype)
{
	$.ajax({
	 	   type: "GET",
	 	   url: "../lightbox/lightbox.v2.ajax.php",
	 	   data: "foldertype="+foldertype,
	 	   success: responseMiniLightbox
	 	});
}

/* 검색결과에서 일반 라이트박스 담기 */
function lightbox_item_handler(pno)
{
	if ($("#im_"+pno).attr("lightbox") == "yes") md = "off";
	else md = "on";

	$("#im_"+pno).attr("src", "../imgs/mini-loader.gif");
	
	$.ajax({
	 	   type: "GET",
	 	   url: "../common/toolbar_lightbox_insert.php",
	 	   data: "pno="+pno+"&md="+md,
	 	   success: responseLightboxItemHandler
	 	});
}

/* 검색결과에서 회원제용 라이트박스 담기 */
function sc_lightbox_item_handler(pno)
{
	if ($("#im_"+pno).attr("lightbox") == "yes") md = "off";
	else md = "on";

	$("#im_"+pno).attr("src", "../imgs/mini-loader.gif");
	
	$.ajax({
	 	   type: "GET",
	 	   url: "../common/toolbar_lightbox_insert.php",
	 	   data: "pno="+pno+"&md="+md+"&subscription=on",
	 	   success: responseLightboxItemHandler
	 	});
}

function lightbox_item_handler_alamy(pno, aguid, alic)
{
	if ($("#im_"+pno).attr("lightbox") == "yes") md = "off";
	else md = "on";

	$("#im_"+pno).attr("src", "../imgs/mini-loader.gif");
	
	$.ajax({
	 	   type: "GET",
	 	   url: "../common/toolbar_lightbox_insert.php",
	 	   data: "pno="+pno+"&md="+md+"&aguid="+aguid+"&alic="+alic,
	 	   success: responseLightboxItemHandler
	});
}

function responseLightboxItemHandler(response)
{
	var resultData = response.split('|');
	
	if (resultData[0] == "insert")
	{
		$("#im_"+resultData[1]).attr("lightbox", "yes");
		$("#im_"+resultData[1]).attr("src", "../imgs/lightbox_on.gif");
		
		$.growlUI('라이트박스에 담았습니다.', '라이트박스에서 확인하실 수 있습니다.');
	}
	
	if (resultData[0] == "remove")
	{
		$("#im_"+resultData[1]).attr("lightbox", "no");
		$("#im_"+resultData[1]).attr("src", "../imgs/lightbox_off.gif");
		
		$.growlUI('라이트박스에서 제거되었습니다.', '라이트박스에서 확인하실 수 있습니다.');
	}
	
	loading_mini_lightbox(resultData[2]);
}

function responseMiniLightbox(response)
{
	$.unblockUI();

	$("#mini_lightbox_container").html(response);
	$("div.scrollable").scrollable();
}

