// ==SiteScript==
// @siteName    @niftyビデオ共有
// @siteUrl     http://video.nifty.com
// @author      DarkKnight
// @authorUrl   http://darkknightlabs.com/
// @scriptUrl   http://darkknightlabs.com/site-script/
// @description 
// @date        2008/11/03
// @version     0.1
// ==/SiteScript==


function CravingSiteScript() {
    this._initialize();
}


CravingSiteScript.prototype = {
    _xhr: null,
    
    _initialize: function() {},
    
    _getXmlHttpRequest: function() {
        if ( this._xhr != null ) {
            return this._xhr;
        }
        
        var xhr = null;
        var these = [
              function() { return new XMLHttpRequest(); }
            , function() { return new ActiveXObject( "Msxml2.XMLHTTP" ); }
            , function() { return new ActiveXObject( "Microsoft.XMLHTTP" ); }
            , function() { return new ActiveXObject( "Msxml2.XMLHTTP.4.0" ); }
        ];
        
        for ( var i = 0, length = these.length; i < length; i++ ) {
            var func = these[ i ];
            try {
                xhr = func();
                break;
            }
            catch( e ) {}
        }
        this._xhr = xhr;
        
        return this._xhr;
    },
    
    _load: function( url, data, method ) {
        var req = this._getXmlHttpRequest();
        
        var mtd = ( method == null ) ? "GET" : "POST";
        
        req.open( mtd, url, false );
        
        if ( mtd == "POST" ) {
            req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
        }
        
        req.send( data );
        
        return req.responseText;
    },
    
    getResponseText: function( url, data, method ) {
        return this._load( url, data, method );
    },
    
    getResponseJSON: function( url, data, method ) {
        var text = this._load( url, data, method );
        
        return eval( "("+text+")" );
    },
    
    /// Math
    random: function( limit ) {
        return Math.floor( Math.random() * limit );
    },
    
    /// String
    decodeHtml: function( str ) {
        return str.replace( /&(quot|#34);/ig,    "\"" )
                  .replace( /&(amp|#38);/ig,     "&"  )
                  .replace( /&(apos|#39);/ig,    "'"  )
                  .replace( /&(lt|#60);/ig,      "<"  )
                  .replace( /&(gt|#62);/ig,      ">"  )
                  .replace( /&(nbsp|#160);/ig,   " "  )
                  .replace( /&(frasl|#8260);/ig, "/"  );
    },
    
    encodeHtml: function( str ) {
        return str.replace( /_/ig,  "%5F" )
                  .replace( /-/ig,  "%2D" );
    }
}


function isSiteUrl( url ) {
    if ( url.match( /http:\/\/video\.nifty\.com\/cs\/catalog\/video_metadata\/catalog.*\.htm/ ) ) {
        return true;
    }
}


function getVideoDetail( url ) {
    url.match( /http:\/\/video\.nifty\.com\/cs\/catalog\/video_metadata\/catalog_(\d+)_1\.htm/ );
    var id = RegExp.$1;
    
    var xmlUrl = "http://api.video.nifty.com/cs/catalog/video_metadata/detailsapi-v1/catalog_";
    xmlUrl += id + "_1.xml";
    
    var craving = new CravingSiteScript();
    var text = craving.getResponseText( xmlUrl );
    
    if ( text == null ) {
        return null;
    }
    
    text.match( /<title><!\[CDATA\[(.*?)\]\]><\/title>/ );
    var title = RegExp.$1;
    
    text.match( /<category_id>(\d+)<\/category_id>/ );
    var category_id = RegExp.$1;
    
    text.match( /<user_id>(\d+)<\/user_id>/ );
    var user_id = RegExp.$1;
    
    var data = "spinfo_id=0&category_id=" + category_id;
    data += "&catalog_id=" + id + "&user_id=" + user_id + "&onData=[type Function]";
    
    data = encodeURI( data );
    data = craving.encodeHtml( data );
    
    var configUrl = "http://dl.video.nifty.com/player_conf2.pl";
    
    text = craving.getResponseText( configUrl, data, "POST" );
    
    if ( text == null ) {
        return null;
    }
    
    text.match( /flvName=(.*?);/ );
    var realUrl = RegExp.$1;
    
    return { videoTitle0: title, videoUrl0: realUrl };
}
