// Niconico Video Player for OpenPNE (URL CMD)
// Coded by Ikuchang 2008.02.28
// Last Update 2008.09.02
function nico_url2cmd(url) {
    if ( !url.match(/http:\/\/www\.nicovideo\.jp\/(watch|thumb|mylist)\/(\w+)$/) ) {
        pne_url2a(url);
        return false;
    }
    
    var dir = RegExp.$1, id = RegExp.$2, html;
    if ( dir == "watch" && id.match(/^sm/) ) {
        // Player
        var obj = NVP_getCurrentScript(), w, h;
        while(obj.parentNode) {    // Search element width
            var style = obj.currentStyle || document.defaultView.getComputedStyle(obj, '');
            if ( obj.offsetWidth > 0 ) {
                w = obj.offsetWidth;
                w = w - parseInt(style.paddingLeft) - parseInt(style.paddingRight);
                break;
            } else if ( obj.style.width ) {
                w = parseInt(obj.style.width);
                w = w - parseInt(style.paddingLeft) - parseInt(style.paddingRight);
                break;
            }
            obj = obj.parentNode;
        }
        w = (w > 485 ? 485 : (w < 320 ? 320 : w));
        h = parseInt(w / 485 * 385);
//        html = '<script type="text/javascript" '
//            + 'src="http://ext.nicovideo.jp/thumb_watch/' + id + '?w=' + w + '&h=' + h + '">'
//            + '</script>';
        html = '<iframe width="312" height="176" '
            + 'src="http://ext.nicovideo.jp/thumb/' + id + '" scrolling="no" '
            + 'style="border:solid 1px #CCC;" frameborder="0">'
            + '<a href="http://www.nicovideo.jp/watch/' + id + '">'
            + '【ニコニコ動画】' + id + '</a></iframe>';
    } else if ( dir.match(/watch|thumb/) ) {
        // Thumbnail
        html = '<iframe width="312" height="176" '
            + 'src="http://www.nicovideo.jp/thumb/' + id + '" scrolling="no" '
            + 'style="border:solid 1px #CCC;" frameborder="0">'
            + '<a href="http://www.nicovideo.jp/watch/' + id + '">'
            + '【ニコニコ動画】' + id + '</a></iframe>';
    } else if ( dir == "mylist" ) {
        // My list
        html = '<iframe width="312" height="176" '
            + 'src="http://www.nicovideo.jp/thumb_mylist/' + id + '" scrolling="no" '
            + 'style="border:solid 1px #CCC;" frameborder="0">'
            + '<a href="http://www.nicovideo.jp/mylist/' + id + '">'
            + '【ニコニコ動画】マイリスト</a></iframe>';
    }
    
    document.write(html);
}

// Where am I ?
function NVP_getCurrentScript() {
    return (function (e) {
        if ( e.nodeName.toLowerCase() == 'script' ) return e;
        return arguments.callee(e.lastChild);
    })(document);
}
