jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function switchStylestyle(styleName) {
  $('link[@rel*=style][title]').each(function(i) {
    this.disabled = true;
    if (this.getAttribute('title') == styleName) this.disabled = false;
  });
  $.cookie('style', styleName, { expires: 365, path: '/'});
}

// CLIENT CODE AFTER DOM READY
jQuery(document).ready(function($){

      $('.styleswitch').click(function()
      {
         switchStylestyle(this.getAttribute("rel"));
         return false;
      });
      var c = $.cookie('style');
      if (c !== null) {
        switchStylestyle(c);
      }

   //TEXTSIZE
   var getTextSize = function() {
     var textsize = $.cookie('textsize');
     $("body")
.removeClass("default")
.removeClass("small")
.removeClass("medium")
.removeClass("large").addClass(textsize);
   };
   getTextSize();
   $("#sizeSelector").click(function(event){
       $target = $(event.target);
       if ($target.is("span")) {
         $.cookie('textsize', $target.attr("class"), { expires: 300, path: '/'});
       }
       getTextSize();
       return false;
   });
  // Validate e-mail
  var emailpattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  
  // Signup newsletter form
  jQuery("#newsletterForm").submit(function(e){
    e.preventDefault();
    var $this = jQuery(this); //cache
    var $email = $this.find("input.text"); //cache
    if(!$this.find(".response").get()[0]) {
      jQuery("<div class='response' />").prependTo($this);
    }
    var $response = $this.find(".response"); // Cache
    if (emailpattern.test($email.val())) {
      $response.text("Sender...");
      $.ajax({
        url: "?template=newsletter;ajax=true;",
        data: $this.serialize(),
        error: function() {
          $response.text("Feil / Error");
        },
        success: function(data) {
          $response.text("Din e-postadresse er registrert");
          $email.removeClass("error").val("");
        }
      });
    } else {
      $email.addClass("error");
      $response.text("Det har skjedd en feil ved registrering");
    }
  });
})
