var gPopupEvent = null;
var gPopupWidth = 250;
var gPopupHeight = 250;
var gPopupId = "pop";
var gCenterFlg = false;



/**
 * window openのポップアップを開く
 */
function openBasicPopup(event, act, url, formName, width, height, centerFlg) {
    gPopupEvent = event;
    gPopupId = "basic_pop_div";

    // actを設定
    $("input[name='act']").val(act);

    
    var param = $("form[name='" + formName + "']").serialize();

    if (param) {
        url += "?" + param;
    }

	if (centerFlg) {
		gCenterFlg = centerFlg;
	}
	
    var features = "location=no,scrollbars=yes,resizable=yes";

    if (width) {
        features += ",width=" + width;
    }
    if (height) {
        features += ",height=" + height;
    }

	if (gCenterFlg) {
	    //var pagesize = getPageSize();
	    //var arrayPageScroll = getPageScrollTop();
	    //var left = (arrayPageScroll[0] + (pagesize[0] - width)/2);
	    //var top  = (arrayPageScroll[1] + (pagesize[1] - height)/2);
	    var left = (screen.width / 2) - (width / 2);
	    var top  = (screen.height / 2) -(height / 2) ;

	    features += ",left=" + left;
	    features += ",top="  + top;
    }
	else {
	    var rect = getClietnRect(event);
	    style = {width: width, height: height, left: rect[0], top: rect[1]};
    }
    
    window.open(url,gPopupId,features);
}

/**
 * popupを閉じる
 */
function closeBasicPopup() {
    window.close();
    return false;
}


/**
 * AJAXポップアップを開く(ポップアップ上で検索とかすると利用できない)
 */
function openAjaxPopup(event, act, url, formName, width, height, centerFlg) {
    gPopupEvent = event;
    gPopupId = "basic_pop_div";

    if (!width) {
        width = 250;
    }

    if (!height) {
        height = 250;
    }
    
    gPopupWidth = width;
    gPopupHeight = height;
    
    
	if (centerFlg) {
		gCenterFlg = centerFlg;
	}

    // actを設定
    $("input[name='act']").val(act);

    // 既に一度呼び出されている場合
    if ( !$("#" + gPopupId).length ) {
        $("body").append('<div id="' + gPopupId  + '" class="jqDnR jqDrag"></div>');
        $("#" + gPopupId ).bgiframe();

        var style = { backgroundColor: "#ffffff",
                     position:"absolute", 
                     "z-index": 101,
                     width:gPopupWidth, 
                     height:gPopupHeight
                     };
        $("#" + gPopupId ).css(style);

        setAjaxPopupPosition();

    }

    $.ajax({
        type: "POST",
        url: url,
        data: $("form[name='" + formName + "']").serialize() + '&ajax=1',
        async: false,
        cache:false,
        // 成功
        success: function(data, dataType){
            $("#" + gPopupId).empty();
            $("#" + gPopupId).html(data);
        },
        error: function() {
            closeBasicPopup();
        }
    });


    $("#" + gPopupId ).show();
    
    //$(window).scroll(setPalletPosition);
    //$(window).resize(setPalletPosition);

    //var rect = getClietnRect(event);

    
    return false;
}

/**
 * popupを閉じる
 */
function closeAjaxPopup() {
    $("#" + gPopupId).slideUp("normal", function() {
	    $("#" + gPopupId).empty();
	});
    //$("#" + gPopupId).remove();
    return false;
}

/**
 * ポップアップのポジションを取得する
 *
 */
function setAjaxPopupPosition() {
    var wWidth = gPopupWidth;
    var wHeight = gPopupHeight;
    
    var style = null;
    
    // センター表示するかどうか
    if (gCenterFlg) {
	    var pagesize = getPageSize();
	    var arrayPageScroll = getPageScrollTop();
	    style = {width: wWidth, left: (arrayPageScroll[0] + (pagesize[0] - wWidth)/2), top: (arrayPageScroll[1] + (pagesize[1]-wHeight)/2)};
	}
	else {
	    var rect = getClietnRect(gPopupEvent);
	    style = {width: wWidth, height: wHeight, left: rect[0], top: rect[1]};
    }
    
    $("#" + gPopupId).css(style);
    
    // ドラッグできるようにする
    $("#" + gPopupId).jqDrag();
}



function setReflectData(event, act, url, formName, refId, callbackFunc, openerFlg) {

	var formData = null;
	
	if (openerFlg) {
	    // actを設定
	    opener.$("input[name='act']").val(act);
		formData = opener.$("form[name='" + formName + "']").serialize() + '&ajax=1';
	}
	else {
	    // actを設定
	    $("input[name='act']").val(act);
		formData = $("form[name='" + formName + "']").serialize() + '&ajax=1';
	}

    $.ajax({
        type: "POST",
        url: url,
        data: formData,
        async: false,
        cache:false,
        // 成功
        success: function(data, dataType){
            if (openerFlg) {
                opener.$("#" + refId).empty();
                opener.$("#" + refId).html(data);
            }
            else {
                $("#" + refId).empty();
                $("#" + refId).html(data);
            }
        },
        complete: function(XMLHttpRequest, textStatus) {
            if (callbackFunc && (typeof callbackFunc == 'function')) {
                // コールバック関数が定義されていた場合
                callbackFunc();
            }
        }
    }); 
}


/**
 * divやspanなどに書かれているエリアをポップアップを開く
 */
function openAreaPopup(event, popId, width, height, centerFlg) {
    gPopupEvent = event;
    gPopupId = popId;

    if (!width) {
        width = 250;
    }

    if (!height) {
        height = 250;
    }
    
    gPopupWidth = width;
    gPopupHeight = height;
    
    
	if (centerFlg) {
		gCenterFlg = centerFlg;
	}

    //$("#" + gPopupId ).bgiframe();
    var style = { backgroundColor: "#ffffff",
                 position:"absolute", 
                 "z-index": 101,
                 width:gPopupWidth, 
                 height:gPopupHeight
                 };
    $("#" + gPopupId ).css(style);

    setAjaxPopupPosition();

    $("#" + gPopupId ).show();
    
    //$(window).scroll(setPalletPosition);
    //$(window).resize(setPalletPosition);

    //var rect = getClietnRect(event);

    return false;
}

/**
 * divやspanなどに書かれているエリアをpopupを閉じる
 */
function closeAreaPopup(popId) {
    $("#" + popId).slideUp("normal", function() {
	    $("#" + popId).hide();
	});
    return false;
}


