﻿var accessingNmlSiteFlag = true;

var POPUPNAME_PLAYMUSIC = "playMusicWindow";    // 楽曲再生ポップアップウィンドウ名
var POPUPNAME_ONTOMO = "ontomoWindow"; // 音楽中辞典ポップアップウィンドウ名
var POPUPNAME_ZGAKU = "AboutUs";    // シリーズ雑学ポップアップウィンドウ名
var POPUPNAME_PLAYLISTTRACKDETAIL = "PlaylistTrackDetailWindow"; // プレイリスト演奏家詳細ポップアップウインドウ名

var LIMIT_POPUPTOMAINACCESS_COUNT= 60;
var popupToMainAccessCount = 0;

var intervalTimerID = null;
var intervalTimerDelay = 1000;

var resetTimerFlagArray = new Array();
resetTimerFlagArray[POPUPNAME_PLAYMUSIC] = false;
resetTimerFlagArray[POPUPNAME_ONTOMO] = false;
resetTimerFlagArray[POPUPNAME_ZGAKU] = false;
resetTimerFlagArray[POPUPNAME_PLAYLISTTRACKDETAIL] = false;

//
// 音楽中辞典専用処理
//

function PopupOntomo(url)
{
    window.open(url, POPUPNAME_ONTOMO, "");
    SetPopupOpenInfo(POPUPNAME_ONTOMO, true, window.document.location.href);
}

//
// 全ポップアップウィンドウ共通処理
//

// 全ポップアップウィンドウに対するタイマーリセット要求のためのフラグをオンにする
// タイムアウト前確認ポップアップ時に呼ばれる
function ResetAllTimerFlags()
{
    if (GetPopupOpenFlag(POPUPNAME_PLAYMUSIC) == true)
    {
        resetTimerFlagArray[POPUPNAME_PLAYMUSIC] = true;
    }
    if (GetPopupOpenFlag(POPUPNAME_ONTOMO) == true)
    {
        resetTimerFlagArray[POPUPNAME_ONTOMO] = true;
    }
    if (GetPopupOpenFlag(POPUPNAME_ZGAKU) == true)
    {
        resetTimerFlagArray[POPUPNAME_ZGAKU] = true;
    }
    if (GetPopupOpenFlag(POPUPNAME_PLAYLISTTRACKDETAIL) == true)
    {
        resetTimerFlagArray[POPUPNAME_PLAYLISTTRACKDETAIL] = true;
    }
}

// すべてのポップアップウィンドウを閉じる
function CloseAllPopupWindow()
{
    ClosePopupWindow(POPUPNAME_PLAYMUSIC);
    ClosePopupWindow(POPUPNAME_ONTOMO);
    ClosePopupWindow(POPUPNAME_ZGAKU);
    ClosePopupWindow(POPUPNAME_PLAYLISTTRACKDETAIL);
}

// ポップアップ自身のタイマーリセット
function ResetPopupTimer(popupName)
{
    var openFlag = GetPopupOpenFlag(popupName);
    if (openFlag == false)
    {
        return;
    }
    
    var openerObj = GetOpenerObject(popupName);
    if (openerObj == null || openerObj.closed == true)
    {
        ClosePopupWindow(popupName);
        return;
    }
    else if (openerObj.accessingNmlSiteFlag == null)
    {
        popupToMainAccessCount++;
        if (popupToMainAccessCount >= LIMIT_POPUPTOMAINACCESS_COUNT)
        {
            ClosePopupWindow(popupName);
        }
        return;
    }
    else
    {
        popupToMainAccessCount = 0;
    }
    
    if (openFlag == true && (openerObj.document.location.href != GetPopupOpenerURL(popupName) ||
                             openerObj.resetTimerFlagArray[popupName] == true))
    {
        self.window.initTimer();
        
        if (openerObj.document.location.href != GetPopupOpenerURL(popupName))
        {
            SetPopupOpenerURL(popupName, openerObj.document.location.href);
        }
        else if (openerObj.resetTimerFlagArray[popupName] == true)
        {
            openerObj.resetTimerFlagArray[popupName] = false;
        }
    }
}

// ポップアップ側から自分と親ウィンドウのタイマーリセット処理
function ResetAllTimer(popupName)
{
    self.window.initTimer();
    
    var openerObj = GetOpenerObject(popupName);
    
    if (openerObj == null || openerObj.closed == true)
    {
        ClosePopupWindow(popupName);
        return;
    }
    else if (openerObj.accessingNmlSiteFlag == null)
    {
        popupToMainAccessCount++;
        if (popupToMainAccessCount >= LIMIT_POPUPTOMAINACCESS_COUNT)
        {
            ClosePopupWindow(popupName);
        }
        return;
    }
    
    popupToMainAccessCount = 0;
    
    if (openerObj.initTimer != null)
    {
        openerObj.initTimer();
        
        // 自分以外のポップアップウィンドウにタイマーリセットさせるためのフラグをオンにする
        if (POPUPNAME_PLAYMUSIC != popupName && GetPopupOpenFlag(POPUPNAME_PLAYMUSIC) == true)
        {
            openerObj.resetTimerFlagArray[POPUPNAME_PLAYMUSIC] = true;
        }
        if (POPUPNAME_ONTOMO != popupName && GetPopupOpenFlag(POPUPNAME_ONTOMO) == true)
        {
            openerObj.resetTimerFlagArray[POPUPNAME_ONTOMO] = true;
        }
        if (POPUPNAME_ZGAKU != popupName && GetPopupOpenFlag(POPUPNAME_ZGAKU) == true)
        {
            openerObj.resetTimerFlagArray[POPUPNAME_ZGAKU] = true;
        }
        if (POPUPNAME_PLAYLISTTRACKDETAIL != popupName && GetPopupOpenFlag(POPUPNAME_PLAYLISTTRACKDETAIL) == true)
        {
            openerObj.resetTimerFlagArray[POPUPNAME_PLAYLISTTRACKDETAIL] = true;
        }
    }
}

// メインウィンドウを監視して閉じられていたらポップアップウィンドウを閉じる処理
function WatchOpenerWindow(popupName)
{
    var openerObj = GetOpenerObject(popupName);
    
    if (openerObj == null || openerObj.closed == true)
    {
        ClosePopupWindow(popupName);
    }
    else if (openerObj.accessingNmlSiteFlag == null)
    {
        popupToMainAccessCount++;
        if (popupToMainAccessCount >= LIMIT_POPUPTOMAINACCESS_COUNT)
        {
            ClosePopupWindow(popupName);
        }
    }
    else
    {
        popupToMainAccessCount = 0;
    }
}

// メインウィンドウ側から自分と表示中のポップアップウィンドウのタイマーリセット処理
// メインウィンドウのプレイリスト再生中のトラック切り替わり時に呼ばれる
function ResetAllTimerByMainWindow()
{
    self.window.initTimer();
    
    ResetAllTimerFlags();
}

// ポップアップウィンドウをオープンしたウィンドウオブジェクトを取得
function GetOpenerObject(popupName)
{
    var opener = null;
    
    if (popupName == POPUPNAME_ONTOMO)
    {
        opener = top.window.opener;
    }
    else 
    {
        opener = window.opener;
    }
    
    return opener;
}

function SetIntervalTimerCallResetPopupTimer(popupName)
{
    ClearIntervalTimer();
    intervalTimerID = setInterval('ResetPopupTimer(\'' + popupName + '\')', intervalTimerDelay);
}

function SetIntervalTimerCallWatchOpenerWindow(popupName)
{
    ClearIntervalTimer();
    intervalTimerID = setInterval('WatchOpenerWindow(\''+ popupName + '\')', intervalTimerDelay);
}

function ClearIntervalTimer()
{
    if (intervalTimerID != null)
    {
        clearInterval(intervalTimerID);
    }
}

function ClosePopupWindow(popupName)
{
    if (GetPopupOpenFlag(popupName) == true)
    {
        var win = window.open("", popupName, "");
        if (win != null)
        {
            win.close();
            DeletePopupOpenInfo(popupName);
        }
    }
}

function SetPopupOpenInfo(popupName, flag, url)
{
    SetPopupOpenFlag(popupName, flag);
    SetPopupOpenerURL(popupName, url);
}

function SetPopupOpenFlag(popupName, flag)
{
    SetCookie(popupName + "Flag", flag);
}

function GetPopupOpenFlag(popupName)
{
    return new Boolean(GetCookie(popupName + "Flag"));
}

function SetPopupOpenerURL(popupName, url)
{
    SetCookie(popupName + "URL", url);
}

function GetPopupOpenerURL(popupName)
{
    return GetCookie(popupName + "URL");
}

function DeletePopupOpenInfo(popupName)
{
    DeleteCookie(popupName + "Flag");
    DeleteCookie(popupName + "URL");
}
