var version;

var allowed_versions = new Array("male", "female");

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 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 getPrefVersion() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("meta")[i]); i++) {
    if(a.getAttribute("name") == 'version'
       && a.getAttribute("content")
       ) return a.getAttribute("content");
  }
  return null;
}

function setActiveVersion(v) {
  var i, a;

  version = v;
  for(i=0; (a = document.getElementsByTagName("span")[i]); i++) {
    if(a.getAttribute("class")) {
      if (a.getAttribute("class") == version) {
       a.style.display = "inline";
      } else {
       a.style.display = "none";
      } 
    }
  }
  return true;
}


window.onload = function(e) {
  var cookie = readCookie("version");
  version = cookie ? cookie : getPrefVersion();
  found = false;
  for (var i in allowed_versions) {
	if (version == allowed_versions[i]) {
		found = true;
		break;
	}
  }
  if (!found) {
  	version = getPrefVersion();
  }
  setActiveVersion(version);
}

window.onunload = function(e) {
  createCookie("version", version, 365);
}
