Coasterpedia
Log in

MediaWiki:Common.js

MediaWiki:Common.js

Note: After saving, you may have to bypass your browser's cache to see the changes.

var x = document.getElementById("bodyContent");
var lengthString = x.getElementsByClassName("convertable-length");
var lengthInitial = Array.prototype.map.call(
    lengthString,
    function(element) {
        return element.innerHTML;
    }
);
jQuery( document ).ready( function( $ ) {
    switchInitial();
} );

//source: w3schools
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    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);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
// end
function switchInitial() {
  var prefCookie = getCookie("unitsPref");
  if (prefCookie == "") {
    return;
  }
  document.getElementById("unitSelect").value = prefCookie;
  switchUnits();
}

function setPrefCookie() {
  var unitPref = document.getElementById("unitSelect").value;
  setCookie("unitsPref", unitPref, 1000);
  switchUnits();
}

function switchUnits() {
  var i;
  var prefCookie = getCookie("unitsPref");
  for (i = 0; i < lengthString.length; i++) {
    var lStripped = lengthInitial[i].replace(/[^0-9.]/g, "");
    if (prefCookie == "default") {
      lengthString[i].innerHTML = lengthInitial[i];
    }
    if (prefCookie == "metric") {
      if (lengthInitial[i].match(/(\sm\s|km|meter|cm)/)) {
        lengthString[i].innerHTML = lengthInitial[i];
      } else if (lengthString[i].innerHTML.match(/feet|foot/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 0.3048) + " meters";
      } else if (lengthString[i].innerHTML.match(/mile/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 1.609344) + " km";
      } else if (lengthString[i].innerHTML.match(/mph/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 1.609344) + " km/h";
      } else if (lengthString[i].innerHTML.match(/inch/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 2.54) + " cm";
      }
    }
    if (prefCookie == "imperial") {
      if (lengthInitial[i].match(/(feet|foot|inch|mile|mph)/)) {
        lengthString[i].innerHTML = lengthInitial[i];
      } else if (lengthString[i].innerHTML.match(/meter|metre|\sm\s/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 3.28084) + " feet";
      }  else if (lengthString[i].innerHTML.match(/km$/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 0.621371) + " miles";
      } else if (lengthString[i].innerHTML.match(/km\/h/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 0.621371) + " mph";
      } else if (lengthString[i].innerHTML.match(/cm/)) {
        lengthString[i].innerHTML = Math.round(lStripped * 0.393701) + " inches";
      }
    }
  }
}