// thanks to quirksmode.org for the cookie functions

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else
    var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(";");
  for(var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == " ")
      c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0)
      return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

function restoreHide(aId, aClickShow, aClickHide, aDefault) {
  var val = readCookie(aId);
  if (val == null) {
    val = (aDefault) ? "0" : "1";
    createCookie(aId, val, 365);
  }
  var style = (val != "1") ? "none" : "";
  var nstyle = (style == "none") ? "" : "none";
  if (aId && document.getElementById(aId))
    document.getElementById(aId).style.display = style;
  if (aClickShow && document.getElementById(aClickShow))
    document.getElementById(aClickShow).style.display = nstyle;
  if (aClickHide && document.getElementById(aClickHide))
    document.getElementById(aClickHide).style.display = style;
}

function toggleHide(aId, aClickShow, aClickHide) {
  var val = readCookie(aId);
  if (val == null || val != "1")
    createCookie(aId, "1", 365);
  else
    createCookie(aId, "0", 365);
  restoreHide(aId, aClickShow, aClickHide);
}

/************************************************************/

function scx_count(aReferer, aUrl, aTitle) {
  var scx_width=screen.width;
  var scx_referer = ""+aReferer;
  // var scx_referer = parent.document.referrer;
  var scx_title = "";
  var scx_url = "";
  var scx_unique = 0;
  var scx_returning = 0;
  var scx_returns = 0;
  var scx_agent = navigator.appName+' '+navigator.appVersion;
  var scx_base_dir;
  var scx_error=0;
  var scx_remove=0;
  var scx_http_url="http";
  var scx_link_back_start = "";
  var scx_link_back_end = "";
  var scx_security_code = "";

  if(window.sc_https) {
    if(scx_https==1) {
      scx_doc_loc = ''+document.location;
      myRE = new RegExp("^https", "i")
      if(scx_doc_loc.match(myRE)) {
        scx_http_url = "https";
      }
      else {
        scx_http_url = "http";
      }
    }
    else
      scx_http_url = "http";
  }

  if (window.sc_partition) {
    var scx_counter = sc_partition+1;
    scx_base_dir = scx_http_url+"://c"+scx_counter+".statcounter.com/";
  }
  else {
    scx_base_dir = scx_http_url+"://c1.statcounter.com/";
    var scx_partition=0;
  }

  if(window.sc_text)
    scx_base_dir += "text.php?";
  else
    scx_base_dir += "t.php?";

  if(window.sc_project) {
    scx_base_dir += "sc_project="+sc_project;
    if(sc_project=="610695")
      scx_remove=1;
  }
  else if(window.usr) {
    scx_base_dir += "usr="+usr;
  }
  else {
    scx_error = 1;
  }

  if(window.sc_remove_link) {
    scx_link_back_start = "";
    scx_link_back_end = "";
  }
  else {
    scx_link_back_start = "<a href=\"http://www.StatCounter.com\" target=\"_blank\">";
    scx_link_back_end = "<\/a>";
  }

  scx_date = new Date();
  scx_time = scx_date.getTime();
  scx_agent = scx_agent.toUpperCase();

  scx_time_difference = 60*60*1000;

  scx_title = ""+aTitle;
  scx_url = ""+aUrl;
  scx_referer = scx_referer.substring(0, 150);
  scx_title = scx_title.substring(0, 150);
  scx_url = scx_url.substring(0, 150);
  scx_referer = escape(scx_referer);
  scx_title = escape(scx_title);
  scx_url = escape(scx_url);

  if (window.sc_security) {
    scx_security_code = sc_security;
  }

  var scx_tracking_url = scx_base_dir+"&resolution="+scx_width+"&camefrom="+scx_referer+"&u="+scx_url+"&t="+scx_title+"&java=1&security="+scx_security_code+"&sc_random="+Math.random();

  // if no usr or project set then display visibile

  // if usr=="someuser" then display visibile

  if(scx_error==1) {
    document.writeln("Code corrupted. Insert fresh copy.");
  }
  else if(scx_remove==1) {
    document.writeln("<b>StatCounter cannot track a high volume website like yours for free. This was stated several times during the sign up process. Please remove the code ASAP.</b>");
  }
  //else if(scx_partition==1) {
  // down at the moment
  //}
  else {
    var scx_img = new Image();
//    dump(scx_tracking_url+"\n");
    scx_img.src = scx_tracking_url;
  }
}


var cntparam;

function xpi_install(aObj) {
  var url = aObj.href;  
  var params = {
    "Checkyesss": { URL: url,
                    IconURL: aObj.getAttribute("iconURL"),
                    toString: function () { return this.URL; }
    }
  }
  tit = aObj.getAttribute("title");
  if (InstallTrigger) {
    InstallTrigger.install(params, doneFn);
    cntparam = [ document.location, url, tit ];
    if (!InstallTrigger.updateEnabled())
      scx_count(document.location, url + "-INSTALL_DISABLED", tit);
  }
  else {
    scx_count(document.location, url + "-NO_INSTALLER", tit);
    return true;
  }
  return false;
}


function doneFn ( name , result ){
  if (result) {
    var res = "ERROR"+result;
    var errors = {
      "-202": "ACCESS_DENIED",
      "-210": "USER_CANCELLED",
      "-215": "READ_ONLY",
      "-219": "INVALID_PATH_ERR",
      "-223": "UNINSTALL_FAILED",
      "-227": "INSTALL_CANCELLED",
      "-228": "DOWNLOAD_ERROR",
      "-230": "ALREADY_EXISTS",
      "-235": "INSUFFICIENT_DISK_SPACE",
      "-239": "CHROME_REGISTRY_ERROR",
      "-299": "OUT_OF_MEMORY",
      "999":  "REBOOT_NEEDED" };
    if ((""+result) in errors)
      res = errors[""+result];
    scx_count(cntparam[0], cntparam[1] + "-"+res, cntparam[2]);
  }
  else
    scx_count(cntparam[0], cntparam[1], cntparam[2]);
}
// Error codes: see http://lxr.mozilla.org/seamonkey/source/xpinstall/src/nsInstall.h#165

/************************************************************/

var langSensitive = [
  "index.html",
  "installation.html",
  "screenshots.html",
  "faq.html" ];


function checkLangPage(aUrl) {
  var url = aUrl.replace(/_(de|en)(\.html?)/, "$2");
  for (var i = 0; i < langSensitive.length; i++)
    if (url.indexOf(langSensitive[i]) >= 0)
      return true;
  return false;
}


function switchPage() {
  var url = document.location.href.replace(/\/$/, "/index.html");
  var lang;
  
  var getparam = url.match(/\?.*\blang=(en|de)\b/);
  if (getparam) {
    lang = getparam[1];
    createCookie("language", lang, 365);
  }
  else {
    lang = readCookie("language");
    if (!lang) {
      if (navigator.language.indexOf("de") > -1)
        lang = "de";
      else
        lang = "en";
      createCookie("language", lang, 365);
    }
  }
  if (checkLangPage(url)) {
    if ((lang == "de") && (url.match(/_en\.html?/)))
      document.location.href = url.replace(/_en(\.html?)/, "$1");
    else if ((lang == "en") && (!url.match(/_en\.html?/)))
      document.location.href = url.replace(/(\.html?)/, "_en$1");
  }
}


function switchLang(aLang) {
  createCookie("language", aLang, 365);
  var url = document.location.href.replace(/\/$/, "/index.html");
  url = url.replace(/\blang=\w+&?/, "");
  if (checkLangPage(url)) {
    if ((aLang == "de") && (url.match(/_en\.html?/)))
      document.location.href = url.replace(/_en(\.html?)/, "$1");
    else if ((aLang == "en") && (!url.match(/_en\.html?/)))
      document.location.href = url.replace(/(\.html?)/, "_en$1");
  }
  else {
    // at least reload so PHP can reevaluate the 'language' cookie
    window.location.reload(true);
  }
  return false;
}


function selectDisplayLang(aId) {
  var url = document.location.href.replace(/\/$/, "/index.html");
  
  if (checkLangPage(url)) {
    var lang = readCookie("language");
    if (!lang)
      if (navigator.language.indexOf("de") > -1)
        lang = "de";
      else
        lang = "en";
    
    if (document.getElementById(aId+"_en"))
      document.getElementById(aId+"_en").style.display = (lang == "en") ? "" : "none";
    if (document.getElementById(aId+"_de"))
      document.getElementById(aId+"_de").style.display = (lang == "de") ? "" : "none";
  }
  else {
    if (document.getElementById(aId+"_en"))
      document.getElementById(aId+"_en").style.display = "none";
    if (document.getElementById(aId+"_de"))
      document.getElementById(aId+"_de").style.display = "none";
  }
}

/************************************************/

switchPage();
