if (Core == undefined) {
	var Core = {
		version: "1.0minimal",
		author: "Jan-Erik Pedersen"
	}
}

Core.WS = Class.create({
    initialize: function(service, methods, onSuccess, onFailure, params){
        for (key in methods) {
            var body = 
				"var m = this.methods." + key + ", vargs = $A(arguments), onSuccess, onFailure;" + 
				"typeof vargs.last() == 'function' && (onSuccess = vargs.pop());" +
				"typeof vargs.last() == 'function' && (onFailure = onSuccess) && (onSuccess = vargs.pop());" +
				"this.invoke(m.method, m.args, vargs, onSuccess, onFailure)";
            this[key] = Function(body);
        }
		for (key in params) {
			switch(key) {
				case "cid" : this.setCid(params[key]); break;
				case "sid" : this.setSid(params[key]); break;
				case "page" : this.setPage(params[key]); break;
				case "lang" : this.setLang(params[key]); break;
				case "test" : this.setTest(params[key]); break;
			}
		}
		this.async = true;
		this.methods = methods;
        this.service = service;
        this.onSuccess = onSuccess ? onSuccess : Prototype.emptyFunction();
        this.onFailure = onFailure ? onFailure : Prototype.emptyFunction();
		this.baseUrl = location.protocol + "//" + location.host + "/eGlue/";
    },
    setTest: function(mode){
        this.test = mode;
    },
    setCid: function(cid){
        this.cid = cid;
    },
	clearCid: function() {
		delete this.cid;
	},
    setContext: function(ctx){
        this.ctx = ctx;
    },
    setLang: function(lang){
        this.lang = lang;
    },
    setPage: function(page){
        this.page = page;
    },
    setVersion: function(version){
        this.version ;
    },
	setSid: function(list) {
		function getHex(str, n) {
			n || (n = 0);
			return Number("0x" + str.substr(n*2, 2).toLowerCase());
		}
		console.info(getHex(list));
		var wsPart = list.substr((4*getHex(list) + 1)*2);
		var n = getHex(wsPart), hosts = [];
		wsPart = wsPart.substr(2);
		for (var i = 0; i < n; i++) {
			hosts.push(getHex(wsPart) + "." + getHex(wsPart, 1) + "." + getHex(wsPart, 2) + "." + getHex(wsPart, 3));
		}
		this.hosts = hosts.join(",");
	},
	block: function(value) {
		this.async = value ? true : false;
	},
    invoke: function(method, args, vargs, onSuccess, onFailure){
        var success = onSuccess ? onSuccess : this.onSuccess;
        var failure = onFailure ? onFailure : this.onFailure;
        var url = this.baseUrl;
		this.ctx && (url += this.ctx + "/");
        url += this.service + ".xws?method=" + method;
		// arguments
		var obj = args.length ? {} : vargs.length ? vargs[0] :  null;
		for (var i = 0, n = args.length; i < n; i++) {
			if (vargs.length) {
				obj[args[i]] = vargs[i];
			} else {
				throw new Error("Not enough arguments for WS call");
			}
		}
		var contentType = (obj ? (obj.documentElement ? "application/xml" : "application/json") : "text/plain");
		// special headers
		var ecgXHeaders = {};
        this.cid && (ecgXHeaders["x-ecoit-ecg-cid"] = this.cid);
		this.hosts && (ecgXHeaders["x-ecoit-ecg-hosts"] = this.hosts);
		this.lang && (ecgXHeaders["x-ecoit-ecg-lang"] = this.lang);
		this.page && (ecgXHeaders["x-ecoit-ecg-page"] = this.page);
		this.version && (ecgXHeaders["x-ecoit-ecg-version"] = this.version);
		this.test && (ecgXHeaders["x-ecoit-ecg-test"] = this.test);
		// execute Ajax call
        new Ajax.Request(url, {
            method: "post",
            contentType: contentType,
			requestHeaders: ecgXHeaders,
            postBody: (obj ? (obj.documentElement ? obj : Object.toJSON(obj)) : ""),
            onSuccess: success,
            onFailure: failure,
			asynchronous: this.async
        });
    }
});
console.info("core_ws loaded");
