"use strict";
$(function () {
    $.Radiko.EventEmitter.on('removeview', function () {
        if (!_.isEmpty($.Radiko.views)) {
            _.each($.Radiko.views, function (view) {
                if (view.onClose) {
                    view.onClose();
                }
                view.remove();
            });
        }
        if (document.getElementById('facebook-jssdk')) {
            if (window.FB !== undefined) {
                window.FB = undefined;
            }
            $('#facebook-jssdk').remove();
        }
        if (document.getElementById('twitter-wjs')) {
            $('#twitter-wjs').remove();
        }
        if (!_.isUndefined($.Radiko.scrollPosition[location.href]) && !$.Radiko.isPageTransition) {
            var scrollPosi_1 = $.Radiko.scrollPosition[location.href];
            clearInterval(window.doScroll);
            window.doScroll = setInterval(function () {
                var documentHeight = $(document).height();
                if ((scrollPosi_1 + $(window).height()) <= documentHeight) {
                    $(window).scrollTop(scrollPosi_1);
                    $.Radiko.scrollPosition[location.href] = undefined;
                    clearInterval(window.doScroll);
                }
            }, 500);
        }
        if ($.Radiko.isPageTransition) {
            $.Radiko.isPageTransition = false;
        }
        $.Radiko.views = [];
    });
    var initializeRecommendStatue = function () {
        if (Radiko.Logger.Recommend.toURL
            && !(new RegExp(location.hash).test(Radiko.Logger.Recommend.toURL))) {
            Radiko.Logger.Recommend.referrerType = undefined;
            Radiko.Logger.Recommend.toURL = undefined;
        }
    };
    var initializeDataLayer = function () {
        dataLayer.push({
            'eAreaId': undefined,
            'eEventRegionName': undefined,
            'eStationId': undefined,
            'eProgramName': undefined,
            'eStartTime': undefined,
            'eSearchWord': undefined,
            'eSearchDate': undefined,
            'eStreamType': undefined,
            'eCategory': undefined,
            'eAction': undefined,
            'eLabel': undefined
        });
    };
    var render = function (main, pageType, context) {
        $.Radiko.EventEmitter.trigger('removeview');
        initializeRecommendStatue();
        initializeDataLayer();
        main(context);
        if (pageType === Radiko.CONST.PAGE_TYPE.LV) {
            $('body').attr('class', 'page-default');
        }
        else if (pageType === Radiko.CONST.PAGE_TYPE.TF) {
            $('body').attr('class', 'page-timeshift');
        }
        else {
            $('body').attr('class', 'page-static');
        }
    };
    var onPcOrMobile = function (onPc, onMobile) {
        if (Radiko.Device.isMobile()) {
            return function () {
                onMobile(this);
            };
        }
        else {
            return function () {
                onPc(this);
            };
        }
    };
    Path.map('#!/top').to(onPcOrMobile(function () { render(radikoTop, Radiko.CONST.PAGE_TYPE.LV); }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/out').to(onPcOrMobile(function () {
        render(function () {
            radikoTop();
            $.colorbox({
                width: '640px',
                inline: true,
                href: '#colorbox--oversea',
                speed: 0,
                open: true,
                opacity: 0.5,
                onClosed: function () {
                    $.get('/rg/policy/?_=' + +moment())
                        .done(function (e) {
                        var el = $.grep($(e), function (el) {
                            return $(el).attr('name') === 'date';
                        });
                        var date = undefined;
                        if (el.length) {
                            date = $(el).attr('content');
                        }
                        var key = 'radiko-policy-accept';
                        var lastAcceptVersion = $.cookie(key) || store.get(key);
                        if (!lastAcceptVersion || lastAcceptVersion !== date) {
                            var defaultParams = {
                                html: '<div class="colorbox__title"><h1>radikoに関する利用規約等</h1></div><iframe src="/rg/policy/"' + +moment() + '></iframe><div class="text-center btn__area btn__area___agree"><button class="btn btn--primary btn--medium js-policy-accept">承諾してradikoを利用する</button></div>',
                                className: 'colorbox policy',
                                closeButton: false,
                                overlayClose: false,
                                opacity: 0.3,
                                onOpen: function (e) {
                                    $('.policy').on('click', '.js-policy-accept', function () {
                                        if (date) {
                                            $.cookie(key, date, {
                                                path: '/',
                                                expires: 365 * 10
                                            });
                                            store.set(key, date);
                                        }
                                        td.trackEvent('events_radiko', {
                                            action: 'click',
                                            category: '規約ダイアログ',
                                            label: '承諾してradikoを利用する',
                                            a_exp: $.Radiko.Mixins.Utility.uid3()
                                        });
                                        $.colorbox.close();
                                    });
                                },
                                onComplete: function () {
                                    if ($('#cboxContent').height() > $(window).height()) {
                                        $.colorbox.resize({
                                            width: '80%',
                                            height: '80%'
                                        });
                                    }
                                }
                            };
                            var params = $.extend(defaultParams, {});
                            if (window.screen.width > 768) {
                                params = $.extend(defaultParams, {
                                    width: '840px'
                                });
                            }
                            $.colorbox(params);
                        }
                        else {
                            $.cookie(key, lastAcceptVersion, {
                                path: '/',
                                expires: 365 * 10
                            });
                            store.set(key, lastAcceptVersion);
                        }
                    })
                        .fail(function (e) {
                        console.log(e);
                    });
                }
            });
        }, Radiko.CONST.PAGE_TYPE.LV);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/live/:id').to(onPcOrMobile(function (context) {
        render(function (context) {
            if (!_.isUndefined(window.jumpAnimation)) {
                clearInterval(window.jumpAnimation);
            }
            showLive(context.params.id);
        }, Radiko.CONST.PAGE_TYPE.LV, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/info/').to(onPcOrMobile(function (context) {
        render(function (context) { radikoInfo(context.params.month, context.params.pageNumber); }, Radiko.CONST.PAGE_TYPE.LV, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/info/:id').to(onPcOrMobile(function (context) {
        render(function (context) { radikoInfoDetail(context.params.id); }, Radiko.CONST.PAGE_TYPE.LV, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/info/archive/:month/:page').to(onPcOrMobile(function (context) {
        render(function (context) { radikoInfo(context.params.month, context.params.page); }, Radiko.CONST.PAGE_TYPE.LV, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/timeshift').to(onPcOrMobile(function () { render(radikoTs, Radiko.CONST.PAGE_TYPE.TF); }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/timeshift/s').to(onPcOrMobile(function () { render(radikoTss, Radiko.CONST.PAGE_TYPE.TF); }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/timetable').to(onPcOrMobile(function () { render(radikoTt, Radiko.CONST.PAGE_TYPE.LV); }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/timetable/:id/live').to(onPcOrMobile(function (context) {
        render(function (context) {
            $.cookie('station_id', context.params.id, { path: '/' });
            $('html, body').animate({ scrollTop: 0 }, 10, 'linear');
            radikoTw(context.params.id);
        }, Radiko.CONST.PAGE_TYPE.LV, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/search/live?:req').to(onPcOrMobile(function () {
        render(function () {
            radikoSl(Radiko.CONST.SEARCH_TYPE.LV);
        }, Radiko.CONST.PAGE_TYPE.LV);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/search/timeshift?:req').to(onPcOrMobile(function () {
        render(function () {
            radikoSl(Radiko.CONST.SEARCH_TYPE.TF);
        }, Radiko.CONST.PAGE_TYPE.TF);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/search/history?:req').to(onPcOrMobile(function () {
        render(function () {
            radikoSl(Radiko.CONST.SEARCH_TYPE.HISTORY);
            $('html, body').animate({ scrollTop: 0 }, 10, 'linear');
        }, Radiko.CONST.PAGE_TYPE.TF);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/areafree').to(onPcOrMobile(function () { render(areaFree, Radiko.CONST.PAGE_TYPE.LV); }, function () { window.location.href = window.location.origin + "/premium"; }));
    Path.map('#!/ts/:id/:datetime').to(onPcOrMobile(function (context) {
        render(function (context) {
            tsDetail(context.params.id, context.params.datetime);
        }, Radiko.CONST.PAGE_TYPE.TF, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/fun').to(function () {
        window.location.href = window.location.origin + "/about";
    });
    Path.map('#!/fun/areafree').to(onPcOrMobile(function () {
        render(function () {
            radikoStatic('/fun/areafree.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/fun/timeshift').to(function () {
        window.location.href = window.location.origin + "/about";
    });
    Path.map('#!/fun/guide/pc').to(onPcOrMobile(function () {
        render(function () {
            radikoStatic('/fun/guide/pc.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    }, function () { window.location.href = window.location.origin + "/about"; }));
    Path.map('#!/fun/guide/app').to(onPcOrMobile(function () {
        render(function () {
            radikoStatic('/fun/guide/app.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    }, function () { window.location.href = window.location.origin + "/about"; }));
    Path.map('#!/distribution_area').to(function () {
        render(function () {
            radikoStatic('/area/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/transactions').to(function () {
        render(function () {
            radikoStatic('/transactions/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/cancellation').to(function () {
        render(function () {
            radikoStatic('/cancellation/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/linkfree').to(onPcOrMobile(function () {
        render(function () {
            radikoStatic('/linkfree/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/privacypolicy').to(function () {
        render(function () {
            radikoStatic('/privacypolicy/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/informative_data').to(function () {
        render(function () {
            radikoStatic('/informative_data/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/guidelines').to(function () {
        render(function () {
            radikoStatic('/guidelines/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/about').to(function () {
        render(function () {
            radikoStatic('/about/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/news_release').to(function () {
        render(function () {
            radikoStatic('/news_release/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/howto').to(function () {
        window.location.href = window.location.origin + "/about";
    });
    Path.map('#!/listening').to(function () {
        render(function () {
            radikoStatic('/listening/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/areafree_area').to(function () {
        render(function () {
            radikoStatic('/areafree_area/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/restriction').to(function () {
        render(function () {
            radikoStatic('/restriction/index.html');
        }, Radiko.CONST.PAGE_TYPE.STATIC);
    });
    Path.map('#!/history').to(onPcOrMobile(function () {
        render(function () {
            Radiko.History.Main();
            $('html, body').animate({ scrollTop: 0 }, 10, 'linear');
        }, Radiko.CONST.PAGE_TYPE.TF);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/history/page/:pageNumber').to(onPcOrMobile(function (context) {
        render(function (context) {
            Radiko.History.Main(+context.params.pageNumber);
            $('html, body').animate({ scrollTop: 0 }, 10, 'linear');
        }, Radiko.CONST.PAGE_TYPE.TF, context);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/popular').to(onPcOrMobile(function () {
        render(function () {
            Radiko.Popular.Main();
            $('html, body').animate({ scrollTop: 0 }, 10, 'linear');
        }, Radiko.CONST.PAGE_TYPE.TF);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('#!/recommend').to(onPcOrMobile(function () {
        render(function () {
            Radiko.Recommend.Main();
            $('html, body').animate({ scrollTop: 0 }, 10, 'linear');
        }, Radiko.CONST.PAGE_TYPE.TF);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.map('').to(onPcOrMobile(function () {
        render(function () {
            radikoTop();
        }, Radiko.CONST.PAGE_TYPE.LV);
    }, function () { window.location.href = window.location.origin + "/mobile"; }));
    Path.root('');
    Path.rescue(function () {
        if (location.hash) {
            if (location.hash.match(/^#([-\w]+)$/)) {
                var station_id = RegExp.$1;
                location.hash = '#!/live/' + station_id;
                return;
            }
            if (Radiko.Device.isMobile()) {
                window.location.href = window.location.origin + '/mobile';
            }
            else {
                notFound();
                $('body').attr('class', 'page-default');
            }
        }
    });
    Path.listen();
    if ('onhashchange' in window && (!document.documentMode || document.documentMode >= 8)) {
        if (location.hash === '') {
            if (Radiko.Device.isMobile()) {
                window.location.href = window.location.origin + '/mobile';
            }
            else {
                radikoTop();
                $('body').attr('class', 'page-default');
            }
        }
    }
    else {
        if (location.hash === '') {
            if (Radiko.Device.isMobile()) {
                window.location.href = window.location.origin + '/mobile';
            }
            else {
                radikoTop();
                $('body').attr('class', 'page-default');
            }
        }
    }
    $(window).on('statechange', function (ev) {
        $.Radiko.Suggest.action_id = 0;
    });
});
