var TOKSTA = function(cfg) {
  // set defaults for width and height
  var width = cfg.width || 640;
  var height = cfg.height || 480;

  var known_params = ["app_id", "user_id", "user_password", "user_name", "user_data", "css_url"];
  known_params.has = function(el) {
    for (var i=0; i<this.length; i++)
      if (this[i] == el)
        return true;

    return false;
  };

  var tk_str = "";

  for (var i in cfg)
    if (cfg.hasOwnProperty(i) && known_params.has(i))
      tk_str += escape(i)+"="+escape(cfg[i])+"&";

  this.init = function() {
    if (document.location.pathname.substring(document.location.pathname.lastIndexOf('/')) == '/login.html')
      document.location.href = 'muckl.html?'+tk_str;
    else
      document.write(this.getInitHTML());
  };

  this.getInitHTML = function() {
    var target = cfg.base_href?cfg.base_href:'';
    if (cfg.user_id)
      target += 'muckl.html?'+tk_str;
    else
      target += 'login.html?app_id='+cfg.app_id+"&width="+encodeURIComponent(cfg.width)+"&height="+encodeURIComponent(cfg.height)+"&css_url="+encodeURIComponent(cfg.css_url);
	return "<iframe width='"+width+"' height='"+height+"' src='"+target+"' style='border:0;'></iframe>";
  }
};

if (typeof TOKSTA_CONFIG != 'undefined')
  (new TOKSTA(TOKSTA_CONFIG)).init();

