// Common variabiles
COOKIE_DOMAIN = "ubuntu-it.org";
COOKIE_PREFIX = "ubuntu-it_custom_";
COOKIE_ACCESIBILITY_NAME = "accessibility";
COOKIE_POLICY_NAME = "policy"


// Start Cookie in browser functions
function set_cookie ( name, value, expires, path, domain, secure )
{
  name = COOKIE_PREFIX + name;
  var cookie_string = name + "=" + escape ( value );

  if ( expires ) {
    cookie_string += "; expires=" + expires.toGMTString();
  }
  else {
    var expires = new Date ( 2100, 1, 1); // never expires
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );
  else
        cookie_string += "; path=" + escape ("/")

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  else
//        cookie_string += "; domain=" + escape (location.host)
        cookie_string += "; domain=" + escape (COOKIE_DOMAIN);

  if ( secure )
        cookie_string += "; secure";
  document.cookie = cookie_string;
}

function delete_cookie ( cookie_name ) {
  cookie_name = COOKIE_PREFIX + cookie_name;
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie ( cookie_name ) {
  cookie_name = COOKIE_PREFIX + cookie_name;
  if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(cookie_name + "=");
    if (c_start!=-1)
          {
          c_start = c_start + cookie_name.length+1;
          c_end = document.cookie.indexOf(";", c_start);
          if (c_end == -1) c_end = document.cookie.length;
          return unescape(document.cookie.substring(c_start,c_end));
          }
    }
    return null
}
// End Cookie in browser functions


// Cookie policy
function get_cookie_policy() {
    cookie_name = COOKIE_POLICY_NAME;
    return get_cookie(cookie_name);
}

function set_cookie_policy() {
    cookie_name = COOKIE_POLICY_NAME;
    set_cookie(cookie_name, "accepted");

    var bar = document.getElementById('_ubuntu-it_cookie_policy');
    if ( bar != null ) {
        bar.remove();
    }
}

function init_cookie_policy_bar() {
    value = get_cookie_policy();
    if ( value == null ) {
        display_cookie_policy_bar()
    }
}

function display_cookie_policy_bar() {
    var content = '<div style="position: fixed; z-index: 4814; bottom: 0px;' +
                  '            background: #ffffff; width: 100%;' +
                  '            border-top: 2px solid rgb(221, 72, 20);"' +
                  '     id="_ubuntu-it_cookie_policy">' +
                  '    <div style="float: left; padding: 5px 20px;">' +
                  '         Ubuntu-it utilizza dei cookie per fornirti una' +
                  '         migliore esperienza di navigazione. L\'uso dei' +
                  '         servizi offerti implica l\'accettazione di ' +
                  '         ci&ograve;.' +
                  '    </div>' +
                  '    <div style="float: right;">' +
                  '        <a style="display: inline-block; padding: 5px' +
                  '                  10px 5px 0px;"' +
                  '           href="http://www.ubuntu-it.org/cookies">' +
                  '            Maggiori informazioni' +
                  '        </a>' +
                  '        <a style="display: inline-block; background:' +
                  '                  #dd4814; color: #ffffff; padding:' +
                  '                  5px 20px;" href="#cookies-ok"' +
                  '           onclick="set_cookie_policy(); return false;">' +
                  '            Va bene' +
                  '        </a>' +
                  '    </div>' +
                  '</div>';
    var wrapper = document.createElement("div");
    wrapper.innerHTML = content;
    document.body.appendChild(wrapper);
}

window.onload = function() {
    init_cookie_policy_bar();
}


// Accessibility
function set_cookie_accessibility (cookie_value) {
  cookie_name = COOKIE_ACCESIBILITY_NAME;
  set_cookie (cookie_name, cookie_value);
}

function get_cookie_accessibility () {
  cookie_name = COOKIE_ACCESIBILITY_NAME;
  return get_cookie (cookie_name);
}

