// temp

var Event = {
    DATA_LOADING: "dataLoading",
    DATA_LOADED:  "dataLoaded",
    CLIPS_LOADED: "clipsLoaded",
    CLIP_PLAYING: "clipPlaying",
    CLIP_ENDED:   "clipEnded",
    CLIP_PROGRESS: "clipProgress" // tmp
};

function listen(evt, callback) {
    $.log(">>>> listen: "+ evt);
    $("html").bind(evt, callback);
}
function dispatch(evt, obj) {
    $.log("<<<< dispatch: "+ evt + (obj ? " / "+ obj[0] : ""));
    if (obj) $.log(obj);
    $("html").trigger(evt, obj);
}

jQuery.VXOneWS = function(options) {
    var config = {
        //cache_buster: "123DFG744", // FIXME: auto generate this
        //webservice_url: "http://dev.api.vxone.kitd.com/VXOneBase/VXOneContent21BaseREST.svc",
        //webservice_url: "http://qa.api.vxone.kitd.com/VXOneBase/VXOneContent21BaseREST.svc",
        //webservice_url: "http://qa.api.vxone.kitd.com/VXOneBase/30/VXOneContent30BaseREST.svc",
        webservice_url: "http://dev.api.vxone.kitd.com/VXOneBase/30/VXOneContent30BaseREST.svc",
        //webservice_url: "http://dev.api.vxone.kitd.com/VXOneBase/31/VXOneContent31BaseREST.svc",
        customer: "",
        site_id:  "",
        language: 50,
        _cb_list: [],
        //technology_id: "vxone",
        get_channels_response_data: null
    };
    $.extend(config, options);
   
    return {
        cache_buster: function () {
            return "VI" + ((new Date()).getTime()).toString().substring(0,8);
        },
        customer: function () {return config.customer},
        get_channels: function(cb) {
            this.call_ws("GetChannels", [config.site_id, true, config.language], cb);
            /*
            if (this.get_channels_response_data) {
                cb(this.get_channels_response_data);
            } else {
                var owner = this;
                this.call_ws("GetChannels", [config.site_id, true, config.language], function(data) {
                    owner.get_channels_response_data = data;
                    cb(data);
                });
            }
            */
        },
        // WARN: untested and unused.
        get_categories: function(channel_uid, category_id, cb) {
            this.call_ws("GetChannelCategories", [channel_uid, category_id, config.language], cb);
        },
        get_content: function(channel_uid, category_id, cb) {
            this.call_ws("GetChannelContentList", [channel_uid, category_id, config.language], cb);
        },
        get_content_with_paging: function(channel_uid, category_id, page, page_size, cb) {
            this.call_ws("GetChannelContentListWithPaging", [channel_uid, category_id, config.language, page, page_size], cb);
        },
        get_inventory: function(inventory_uid, technology_id, cb) {
            this.call_ws("GetProgramInventory", [inventory_uid, technology_id], cb);
        },
        get_program_info: function(program_uid, technology_id, cb) {
            this.call_ws("GetProgramInfo", [program_uid, config.language, technology_id], cb);
        },
        get_payment_options: function(inventory_uid, cb) {
            this.call_ws("GetPayGateDetails", [inventory_uid, config.language], cb);
        },
        search_programs: function(query, cb) {
            var _page      = 0; // TMP
            var _page_size = 10;
            this.call_ws("ProgramSearchEx", [config.site_id, query, config.language, _page, _page_size], cb);
        },
        get_epg: function(search_string, cb) {
            var _page = 0;
            var _page_size = 10;
            this.call_ws("GetSiteEpg", [config.site_id, search_string, config.language, _page, _page_size], cb);
        },
        rateProgram: function(channel_uid, program_uid, rating, cb) {
            this.call_ws("RateProgram", [SESSION_ID, channel_uid, program_uid, MEMBER_UID, rating, true]);
        },
        call_ws: function(fn, params, callback) {
            dispatch(Event.DATA_LOADING, [fn]);
            var that = this;
            if (callback) {
                var cb_name = this.get_unique_cb();
                var cb_fn = eval("window."+ cb_name +" = function(data) {callback(data, cb_name);dispatch(Event.DATA_LOADED, [fn, data]);}");
                params.push(cb_name);
            }
            
            var ws_url = this.ws_call_url(fn, params)
            $.log([fn, ws_url]);
            $.getScript(ws_url);
        }, /*
        check_response_before_callback: function(ws_function, response, callback, cb_name) {
            if (response.errorcode && response.message && response.details) {
                $.log("!!! "+ ws_function + " error");
                $.log("!!! code:    "+ response.code);
                $.log("!!! message: "+ response.message);
                $.log("!!! details: "+ response.details);
            } else {
                callback(response, cb_name);
            }
        },*/
        get_unique_cb: function() {
            var cb;
            //do {
                cb = "wsrequest"+Math.round(Math.random()*100000);
            //} while (this.config._cb_list.indexOf(cb) > -1)
            //$.log(this.config._cb_list);
            //this.config._cb_list.push(cb);
            return cb;
        },
        ws_call_url: function(fn, params) {
            params.unshift(this.cache_buster(), config.customer);
            $.log(params);
            return [config.webservice_url, fn, b64.encode(params.join("/"))].join("/");
        }
    }
};
