if (window.addEventListener) {
    window.addEventListener('load', function() {
        if (navigator.userAgent.match(/(iPod|iPhone); U; CPU iPhone OS/) && location.search.match(/pc=1/)) {
            var div = document.createElement('div');
            div.className = 'toiphone';
            var a = document.createElement('a');
            a.href = location.href.replace(/(http:\/\/[^\/]+)/, "$1" + '/i').replace(/\?pc=1/, '');
            a.appendChild(document.createTextNode('サントリートピックスiPhone版を表示'));
            div.appendChild(a);
            var body = document.getElementsByTagName('body');
            body[0].appendChild(div);

        }
    }, false);
}

if (navigator.userAgent.match(/(iPod|iPhone); U; CPU iPhone OS/) && location.search.match(/pc=1/)) {
    document.getElementsByTagName('html')[0].addEventListener('click', function(event) {
        event = event || window.event;
        var target = event.target || event.srcElement;
        while (target.parentNode && target.nodeName.toLowerCase() != 'a') {
            target = target.parentNode;
        }
        if (target.nodeName.toLowerCase() == 'a') {
            if (!target.href.match(new RegExp('^http:\/\/' + location.host + '\/'))) {
                return true;
            }
            if (target.href.match(new RegExp('^http:\/\/' + location.host + '\/i\/'))) {
                return true;
            }
            var hash = '';
            if (target.href.match(/(#.+)$/)) {
                var hash = RegExp.$1;
            }

            if (target.href.match(/\?/)) {
                target.href = target.href + '&pc=1';
            } else {
                target.href = target.href.replace(/#.+$/, '') + '?pc=1' + hash;
            }
        }
    }, false);
}

