/**
 * PopupTitle.js
 * Powered by hisato http://chibinowa.net/
 */

function FlatMedia()
{
    this.UA = navigator.userAgent;
    this.IE = this.UA.indexOf('Opera',0)<0 && this.UA.indexOf('MSIE',0)>-1;
    
    this.image_loading = RESBASEURL + 'images/nowloading.png';

    // コンテンツのWidth
    this.getContentWidth = function()
    {
        if (this.IE)
            return parseInt(document.body.scrollWidth);
        else
            return parseInt(document.documentElement.offsetWidth);
    }
    // コンテンツのHeight
    this.getContentHeight = function()
    {
        if (this.IE)
            return parseInt(document.body.scrollHeight);
        else
            return parseInt(document.documentElement.offsetHeight);
    }
    // ウィンドウのWidth
    this.getWindowWidth = function()
    {
        if (this.IE) {
            if (document.compatMode == 'BackCompat')
                return parseInt(document.body.clientWidth);
            else
                return parseInt(document.documentElement.clientWidth);
        }
        if (document.getElementById)
            return parseInt(document.body.clientWidth);
        else
            return 1;
    }
    // ウィンドウのHeight
    this.getWindowHeight = function()
    {
        if (this.IE) {
            if (document.compatMode == 'BackCompat')
                return parseInt(document.body.clientHeight);
            else
                return parseInt(document.documentElement.clientHeight);
        }
        if (document.getElementById)
            return parseInt(window.innerHeight);
        else
            return 1;
    }

    // 縦スクロール取得
    this.getScrollTop = function()
    {
        if(window.pageYOffset)
            return window.pageYOffset;
        if(document.compatMode == 'BackCompat')
            return document.body.scrollTop;
        return 0;
    },

    // 横スクロール取得
    this.getScrollLeft = function()
    {
        if(window.pageXOffset)
            return window.pageXOffset;
        if(document.compatMode == 'BackCompat')
            return document.body.scrollLeft;
        return 0;
    }

    this.test = function(){}

    this.close = function()
    {
        document.getElementById('FlatMediaFilterLayer').style.visibility = 'hidden';
        document.getElementById('FlatMediaLayer').style.visibility = 'hidden';
        document.getElementById('FlatMediaImgLayer').src = this.image_loading;
    }

    // 初期化
    this.init = function()
    {
        // フィルタレイヤー
        this.o_filter = document.createElement('div');
        this.o_filter.setAttribute('id','FlatMediaFilterLayer');
        with (this.o_filter.style) {
            filter = 'alpha(opacity=90)';
            visibility = 'hidden';
        }
        this.o_filter.onclick = this.close;
        document.body.appendChild(this.o_filter);

        // Base
        this.oFlatMediaLayer = document.createElement('div');
        this.oFlatMediaLayer.setAttribute('id', 'FlatMediaLayer');
        this.oFlatMediaLayer.style.visibility = 'hidden';
        this.oFlatMediaLayer.style.top = '0px';
        this.oFlatMediaLayer.style.left = '0px';
        document.body.appendChild(this.oFlatMediaLayer);
        
        // 何も入っていない時のベースレイヤのサイズ
        this.defaultSizeWidth = this.oFlatMediaLayer.offsetWidth;
        this.defaultSizeHeight = this.oFlatMediaLayer.offsetHeight;

        // Base>Media
        this.oFlatMediaMediaLayer = document.createElement('div');
        this.oFlatMediaMediaLayer.setAttribute('id', 'FlatMediaMediaLayer');
        this.oFlatMediaLayer.appendChild(this.oFlatMediaMediaLayer);

        // Base>MediaImage
        this.oFlatMediaImgLayer = document.createElement('img');
        this.oFlatMediaImgLayer.setAttribute('id', 'FlatMediaImgLayer');
        this.oFlatMediaImgLayer.setAttribute('src', this.image_loading);
        this.oFlatMediaImgLayer.onclick = this.close;
        this.oFlatMediaMediaLayer.appendChild(this.oFlatMediaImgLayer);

        // Base>Title
        this.oFlatMediaTitleLayer = document.createElement('a');
        this.oFlatMediaTitleLayer.setAttribute('id', 'FlatMediaTitleLayer');
        this.oFlatMediaTitleLayer.setAttribute('href', 'javascript:void(0)');
        this.oFlatMediaTitleLayer.setAttribute('target', 'FlatMedia');
        this.oFlatMediaTitleLayer.appendChild(document.createTextNode('title'));
        this.oFlatMediaLayer.appendChild(this.oFlatMediaTitleLayer);

        // Base>Close
        this.oFlatMediaCloseLayer = document.createElement('a');
        this.oFlatMediaCloseLayer.setAttribute('id', 'FlatMediaCloseLayer');
        this.oFlatMediaCloseLayer.setAttribute('href', 'javascript:void(0)');
        this.oFlatMediaCloseLayer.appendChild(document.createTextNode('Click to close'));
        this.oFlatMediaCloseLayer.onclick = this.close;
        this.oFlatMediaLayer.appendChild(this.oFlatMediaCloseLayer);

        this.resizeFilter();
    }
    // ポップアップ
    this.open = function(obj, owidth, oheight)
    {        
        var media_src = obj.href;
        var media_title = obj.title;

        var oFlatMediaFilterLayer = document.getElementById('FlatMediaFilterLayer');
        var oFlatMediaLayer       = document.getElementById('FlatMediaLayer');
        var oFlatMediaMediaLayer  = document.getElementById('FlatMediaMediaLayer');
        var oFlatMediaTitleLayer  = document.getElementById('FlatMediaTitleLayer');
        var oFlatMediaCloseLayer  = document.getElementById('FlatMediaCloseLayer');
        var oFlatMediaImgLayer    = document.getElementById('FlatMediaImgLayer');
        var oFlatMediaFlashLayer  = document.getElementById('FlatMediaFlashLayer');

        oFlatMediaTitleLayer.href = media_src;
        oFlatMediaTitleLayer.removeChild(oFlatMediaTitleLayer.childNodes[0]);
        oFlatMediaTitleLayer.appendChild(document.createTextNode(media_title));


        switch (media_src.slice(-3).toLowerCase())
        {
            case 'png':
            case 'gif':
            case 'jpg':
                // Image
                oFlatMediaImgLayer.style.display = 'block';
                if (oFlatMediaFlashLayer)
                    oFlatMediaFlashLayer.style.display = 'none';

                    var tmpimg = new Image();
                    tmpimg.src = media_src;
                if (!owidth || !oheight) {
                    var tmpimg = new Image();
                    tmpimg.src = media_src;
                    owidth  = tmpimg.width;
                    oheight = tmpimg.height;
                }
                with (oFlatMediaImgLayer) {
                    src    = media_src;
                    title  = media_src;
                    width  = owidth;
                    height = oheight;
                }
                break;
            case 'swf':
                // Flash
                oFlatMediaImgLayer.style.display = 'none';
                if (oFlatMediaFlashLayer)
                    oFlatMediaMediaLayer.removeChild(document.getElementById('FlatMediaFlashLayer'));

                oFlatMediaFlashLayer = document.createElement('object');
                oFlatMediaFlashLayer.setAttribute('id', 'FlatMediaFlashLayer');
                oFlatMediaMediaLayer.appendChild(oFlatMediaFlashLayer);

                var params = new Array();
                for (var i=0; i<4; i++) {
                    params[i] = document.createElement('param');
                }
                params[0].setAttribute('name',    'movie');
                params[0].setAttribute('value',    media_src);
                params[0].setAttribute('id',    'FlashParamMovie');
                params[1].setAttribute('name',    'allowScriptAccess');
                params[1].setAttribute('value',    'sameDomain');
                params[2].setAttribute('name',    'quality');
                params[2].setAttribute('value',    'high');
                params[3].setAttribute('name',    'bgcolor');
                params[3].setAttribute('value',    '#ffffff');
                for (var i=0; i<4; i++) {
                    oFlatMediaFlashLayer.appendChild(params[i]);
                }

                with (oFlatMediaFlashLayer) {
                    setAttribute('type',   'application/x-shockwave-flash');
                    setAttribute('data',   media_src);
                    setAttribute('width',  owidth);
                    setAttribute('height', oheight);
                }
                break;
        }

        oFlatMediaTitleLayer.style.width = owidth+'px';

        // 画像が入った後の位置を調整
        this.resizePictField(
            this.defaultSizeWidth + owidth,
            this.defaultSizeHeight + oheight
        );

        // 可視にする
        oFlatMediaFilterLayer.style.visibility = 'visible';
        oFlatMediaLayer.style.visibility = 'visible';

        return false;
    }
    // リサイズ（フィルタレイヤ＆メインレイヤ）
    this.resize = function()
    {
        this.resizeFilter();
        //this.resizePictField();
    }
    // リサイズ（フィルタ）
    this.resizeFilter = function()
    {
        if (document.getElementById('FlatMediaFilterLayer')) {
            var ww = this.getWindowWidth();
            var wh = this.getWindowHeight();
            var cw = this.getContentWidth();
            var ch = this.getContentHeight();
            with (this.o_filter.style) {
                position = 'absolute';
                top = 0;
                left = 0;
                if (this.IE) {
                    width = ww + 'px';
                }
                else {
                    width = '100%';
                }
                height = (wh >= ch? wh: ch) + 'px';
            }
        }
    }
    // リサイズ（メインレイヤ）
    this.resizePictField = function(owidth, oheight)
    {
        if (document.getElementById('FlatMediaLayer')) {
            if (!owidth) owidth = this.oFlatMediaLayer.offsetWidth;
            if (!oheight) oheight = this.oFlatMediaLayer.offsetHeight;
            var ww = this.getWindowWidth();
            var wh = this.getWindowHeight();
            with (this.oFlatMediaLayer.style) {
                left = (parseInt(ww - owidth)/2)+this.getScrollLeft() + 'px';
                if (oheight > wh) {
                    top = '0px';
                }
                else {
                    top = (parseInt(wh - oheight)/2)+this.getScrollTop() + 'px';
                }
            }
        }
    }
}

var flatmedia = new FlatMedia();
EventListener.add(window, 'load', function(){flatmedia.init()}, false);
EventListener.add(window, 'resize', function(){flatmedia.resize()}, false);
