/**
 * went_clip
 *
 * Copyright: Alike Co.,Ltd.
 */

/**
 * 対象URLから取得したコンテンツをiframeを使ってダイアログで開きます
 * 単なるXHRで読み込むので、対象は同一ドメインである必要があります。
 * @param url_ {string} 開くURL
 * @param height {int} 高さ(px)
 * @param width {int} 幅(px)
 */
function openWentClipDialog(url_,
                            height_,
                            width_)
{
    var target = self.parent || self;
    var doc = target.document;
    var shadowDiv = jQuery("#shadow___", doc);
    shadowDiv = (shadowDiv.length > 0)
                ? shadowDiv
                : jQuery('<div id="shadow___"></div>');
    var body = jQuery('body', doc);
    var isIE6 = (typeof document.body.style.maxHeight === "undefined") ? true : false;
    if (isIE6)
    {
        jQuery("html, body", doc).css("height", "100%");
		//jQuery("html", doc).css("overflow", "hidden"); // for erase scrollbar
        jQuery('<iframe src="javascript:false;" id="fix_select___"></iframe>', doc)
            .css({
                "z-index": 99,
                top: 0,
                left: 0,
                "background-color": "#fff",
                border: "none",
                opacity: 0,
                width: "100%",
                position: "absolute",
                height: (document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + 'px')
            })
            .appendTo(body);
    }
    
    shadowDiv
        .css({
            height: '100%',
            left: 0,
            position: 'fixed',
            top: 0,
            width: '100%',
            'z-index': 100,
            'background-color': '#000000',
            opacity:0.75
        });

    if (isIE6)
    {
        shadowDiv.css(
            {
                height: body.height(),
                position: 'absolute'
            }
        );
    }
    
    shadowDiv.appendTo(body);

    var dialog = jQuery("#dialog___", doc);
    if (dialog.length > 0)
    {
        var oldDialog = dialog.attr("id", "dialog___old");
        var oldFixSelect = jQuery("#fix_select___", doc).attr("id", "fix_select___old");
        setTimeout(function(){
            oldDialog.remove();
            oldFixSelect.remove();
        }, 10);
    }
    var iframeId = "if_" + new Date().getTime(); // for WebKit
    dialog = jQuery('<div id="dialog___"><iframe id="' + iframeId + '" src="' + url_ + '" frameborder="0"></iframe></div>', doc);

    var clientHeight = (target.innerHeight
                        || doc.documentElement.clientHeight
                        || doc.body.clientHeight
                        || 0);
    var clientOffsetTop = (clientHeight - height_) / 2;
    var scrollTop = (target.scrollY
                     || target.pageYOffset
                     || doc.documentElement.scrollTop
                     || doc.body.scrollTop
                     || 0);
    var top = clientOffsetTop + scrollTop;
    var left = (shadowDiv.width() - width_) / 2;
    
    top = Math.max(top, scrollTop);
    left = Math.max(left, 0);
    
    dialog.css({
        position: 'absolute',
        left: left,
        top: top,
        height: height_,
        width: width_,
        'z-index': 101,
        overflow: 'hidden'
    });
    dialog.children().css({
        border: 'none',
        height: height_,
        width: width_
    });
    dialog.appendTo(body);
    return false;
}
