var ScriptQuery = function(scriptPath) { this.scriptPath = scriptPath; }

ScriptQuery.prototype = {  get: function() {
		var srcRegex = new RegExp(this.scriptPath.toLowerCase().replace('.', '\\.') + '(\\?.*)?$');
		var scripts = document.getElementsByTagName("script");
		var lastMatch = -1;
		for (var i = 0; i < scripts.length; i++) {
			if (scripts[i].src && scripts[i].src.toLowerCase().match(srcRegex)) {
				lastMatch = i;
			}
		}
		if(lastMatch >= 0){
			var query = scripts[lastMatch].src.match(/\?([^#]*)(#.*)?/);
			return !query ? '' : query[1];
		}
		
		return '';
	},  
	
	parse: function() {    
		var result = {};    
		var query = this.get();    
		var components = query.split('&');     
		for (var i = 0; i < components.length; i++) {      
			var pair = components[i].split('=');      
			var name = pair[0], value = pair[1];       
			if (!result[name]) result[name] = [];      // decode    
				if (!value) {        
					value = 'true';      
				} else {
					try {          
						value = decodeURIComponent(value); 
					} catch (e) {          
						value = unescape(value);
					}      
				}       // MacIE way     
				var values = result[name];      
				values[values.length] = value;    
			}    
			return result;  
	},  
	
	flatten: function() {    
		var queries = this.parse();    
		for (var name in queries) {      
			queries[name] = queries[name][0];    
		}    
		return queries;  
	},  
	
	toString: function() {    return 'ScriptQuery [path=' + this.scriptPath + ']';  }
}

whenDone = function(result) {
	var div = document.getElementById("cpp-" + result.Id);
	if (div != null) {
		div.innerHTML = result.Advertisement;
	}
}

var conf = {
    addParams : ";sguid;ct;",
    ssrc: "http://ads.ceneo.pl/AdvertisementServer/AdvertisementService.svc/GetAdvertisement",
    opt : "opt",
    cid : "cid",
    call : "whenDone"
}

show = function(queries) {
    if (queries[conf.cid] != null) {
        if (typeof ado === "object") { ado.beginExternal(); }

        document.write("<div id='cpp-" + queries[conf.cid] + "'></div>");
        var params = "?call=" + conf.call + "&c=" + queries[conf.cid];
        var providedAdd = 0;

        if (queries[conf.opt]) {
            params += "&opt=" + queries[conf.opt];
            for (var name in queries) {
                if (conf.addParams.match(";" + name + ";")) {
                    providedAdd++;
                    params += "&param" + providedAdd + "=" + name + "&value" + providedAdd + "=" + queries[name];
                }
            }
        }
        
        var script = document.createElement("script");
        script.setAttribute("src", conf.ssrc + (providedAdd > 0 ? providedAdd + "AddParams" : "") + params);
        script.setAttribute("type", "text/javascript");
        document.getElementsByTagName("head")[0].appendChild(script);

        if (typeof ado === "object") { ado.endExternal(); }
    }
}

show(new ScriptQuery('CeneoPPv2.js').parse());
