var pages_urls =
{
  club: 'index.php?confirm=1',
  shop: 'http://www.barpremium.com/php/homepage.php'
};

var cookieName = 'is_major';
function writeCookie (name, value, days)
{
  var expireDate = new Date();
  expireDate.setTime(expireDate.getTime() + (days * 24 * 3600 * 1000));

  document.cookie = name
    + '=' + escape(value)
    + ';expires='
    + expireDate.toGMTString()
    + ';path=/';
}

function readCookie (name)
{
  start = document.cookie.indexOf(name + "=");
  if (start >= 0)
  {
    start += name.length + 1;
    end = document.cookie.indexOf(";", start);
    if (end < 0)
    {
      end = document.cookie.length;
    }

    return unescape(document.cookie.substring(start, end));
  }

  return '';
}

function deleteCookie (name)
{
  writeCookie(name, '', -1);
}

window.onload = function()
{
  document.getElementById('major').checked = false;
  document.getElementById('not_major').checked = false;
  for (var i = 0; i < document.getElementById('actions').getElementsByTagName('a').length; i++)
  {
    document.getElementById('actions').getElementsByTagName('a')[i].onclick = function(){this.blur();return false;}
  }
  document.getElementById('not_major').onclick = function()
  {
    if (this.checked == true)
    {
      this.blur();
      this.parentNode.getElementsByTagName('label')[0].className = 'is_selected';
    }
  };
  document.getElementById('major').onclick = function()
  {
    if (this.checked == true)
    {
      // write cookie
      writeCookie(cookieName, true, 365);

      this.blur();
      var active_buttons=
        '<a href="' + pages_urls.club + '" class="club png_fix">\
            <span>\
              Bar Premium, Le club\
            </span>\
          </a>\
          <a href="' + pages_urls.shop + '" class="shop png_fix">\
            <span>\
              Bar Premium, La boutique\
            </span>\
          </a>';
      document.getElementById('actions').innerHTML = active_buttons;

      this.parentNode.parentNode.removeChild(this.parentNode.parentNode.getElementsByTagName('div')[0]);
      var actions_explanation = document.createElement('p');
      actions_explanation.appendChild(document.createTextNode("Cliquez sur le site de votre choix :"));
      actions_explanation.className = "actions_explanation";
      this.parentNode.parentNode.insertBefore(actions_explanation, document.getElementById('actions'));
      document.getElementById('actions').className = document.getElementById('actions').className + " active_buttons";

      document.getElementById('actions').getElementsByTagName('a')[0].style.backgroundPosition = "left -92px";
      document.getElementById('actions').getElementsByTagName('a')[1].style.backgroundPosition = "left -184px";

      document.getElementById('actions').getElementsByTagName('a')[0].onmouseover = function()
      {
        this.style.backgroundPosition = "left -184px";
        this.parentNode.getElementsByTagName('a')[1].style.backgroundPosition = "left -184px";
      };
      document.getElementById('actions').getElementsByTagName('a')[0].onmouseout = function()
      {
        this.style.backgroundPosition = "";
        this.parentNode.getElementsByTagName('a')[1].style.backgroundPosition = "";
      };
      document.getElementById('actions').getElementsByTagName('a')[1].onmouseover = function()
      {
        this.style.backgroundPosition = "left -92px";
        this.parentNode.getElementsByTagName('a')[0].style.backgroundPosition = "left -92px";
      };
      document.getElementById('actions').getElementsByTagName('a')[1].onmouseout = function()
      {
        this.style.backgroundPosition = "";
        this.parentNode.getElementsByTagName('a')[0].style.backgroundPosition = "";
      };
      this.onclick = function(){};
      for (var i = 0; i < document.getElementById('actions').getElementsByTagName('a').length; i++)
      {
        document.getElementById('actions').getElementsByTagName('a')[i].onclick = function(){}
      }
    }
  };
  
  // read existing cookie
  if(value = readCookie(cookieName))
  {
    // or just redirect me...
    var e = document.getElementById('major');
    e.checked = true;
    e.click();
  }
};