balaurWidget = new function() {
  baseUrl = 'http://balaur.ro';

  var Url = {
    // public method for url encoding
    encode: function (string) {
      return escape(this._utf8_encode(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function (string) {
      string = string.replace(/\r\n/g,"\n");
      var utftext = "";
      for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        if (c < 128) {
          utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
          utftext += String.fromCharCode((c >> 6) | 192);
          utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
          utftext += String.fromCharCode((c >> 12) | 224);
          utftext += String.fromCharCode(((c >> 6) & 63) | 128);
          utftext += String.fromCharCode((c & 63) | 128);
        }
      }
      return utftext;
    }
  }

  // Taken from http://www.theurer.cc/blog/2005/12/15/web-services-json-dump-your-proxy/
  function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
  }

  // Static script ID counter
  JSONscriptRequest.scriptCounter = 1;

  // buildScriptTag method
  JSONscriptRequest.prototype.buildScriptTag = function () {
    // Create the script tag
    this.scriptObj = document.createElement("script");
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
  }

  // removeScriptTag method
  JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);
  }

  // addScriptTag method
  JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
  }

  this.callback = function(responseText) {
    var link = document.getElementById('balaur-widget-link').firstChild;
    if (!link || !link.href || !link.href.match('^' + baseUrl) ||
        (link.rel && link.rel.search('nofollow') != -1)) {
      document.getElementById('balaur-job-results').innerHTML = "Widget-ul a fost modificat!";
    } else {
      document.getElementById('balaur-job-results').innerHTML = responseText;
    }
  }

  this.run = function(options) {
    var jobResultsDiv = document.createElement('div');
    jobResultsDiv.setAttribute('id', 'balaur-job-results');
    document.getElementById('balaur-job-widget').insertBefore(jobResultsDiv,
        document.getElementById('balaur-widget-link'));
    var remoteUrl = baseUrl + '/cauta?' +
        "q=" + Url.encode(options['keywords']) +
        "&l=" + Url.encode(options['location']) +
        "&i=" + Url.encode(options['industry']) +
        "&e=" + Url.encode(options['employer']) +
        "&t=" + Url.encode(options['type']) +
        "&count=" + Url.encode(options['count']) +
        "&sortby=" + Url.encode(options['sortby']) +
        "&format=widget";
    var jsr = new JSONscriptRequest(remoteUrl);
    jsr.buildScriptTag();
    jsr.addScriptTag();
  }
}

balaurWidget.run(balaurWidgetOptions);

