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";

    var arrayPageScroll = getPageScrollTop();
    features += ",top=" + arrayPageScroll[0];
    features += ",left=" + arrayPageScroll[1];
    
    if (width) {
        features += ",width=" + width;
    }
    if (height) {
        features += ",height=" + height;
    }
    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) {
    // actを設定
    $("input[name='act']").val(act);

	var formData = null;
	
	if (openerFlg) {
		formData = opener.$("form[name='" + formName + "']").serialize() + '&ajax=1';
	}
	else {
		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();
            }
        }
    }); 
}



