var CrossSite_url = 'http://www.tube4vn.tv';

//
// get video list; need to implement callback_getVideos
// sSort: by_date, by_popularity, by_title
// sFilterType:
// - null: get all video
// - tags: get video list by tag - sFilterValue = tag name
// - search: get video list by search - sFilterValue = keyword
//
function CrossSite_getVideos(iPageNum, sSortBy, sFilterType, sFilterValue) {
    var aObj = null;
    if( sFilterType=='tags' ) {
        aObj = new JSONscriptRequest(CrossSite_url + '/videos/' + sFilterValue + '/' + sSortBy + '/' + iPageNum + '.json');
    }
    else if( sFilterType=='search' )
        aObj = new JSONscriptRequest(CrossSite_url + '/search?q=' + escape(sFilterValue) + '&page=' + iPageNum + '&sort=' + sSortBy + '&format=json&x=0&y=0');
    else
        aObj = new JSONscriptRequest(CrossSite_url + '/videos/all/' + sSortBy + '/' + iPageNum + '.json');

    aObj.buildScriptTag();
    aObj.addScriptTag();
}

//
// get info of a video; need to implement callback_getVideo
//
function CrossSite_getVideo(iVideoId) {
    var aObj = new JSONscriptRequest(CrossSite_url + '/videos/' + iVideoId + '.json');
    aObj.buildScriptTag();
    aObj.addScriptTag();
}

//
// get clip list of a video; need to implement callback_getClips
//
function CrossSite_getClips(iVideoId) {
    var aObj = new JSONscriptRequest(CrossSite_url + '/videos/' + iVideoId + '/clips.json');
    aObj.buildScriptTag();
    aObj.addScriptTag();
}

//
// callback after cross-site ajax request return
//
function jsonLoaded(jsonObj) {

    //
    // callback for getVideos()
    //
    if( jsonObj['videos']!=null && typeof callback_getVideos != 'undefined' ) {
        callback_getVideos(jsonObj);
    }
    
    //
    // callback for getVideo()
    //
    else if( jsonObj['clips_count']!=null && typeof callback_getVideo != 'undefined' ) {
        callback_getVideo(jsonObj);
    }
    
    //
    // callback for getClips()
    //
    else if( jsonObj.length>0 && jsonObj[0]['src_link']!=null && typeof callback_getClips != 'undefined' ) {
        callback_getClips(jsonObj);
    }
}

