(function ($) {
  // HTML 5 placeholder element fallback
  $(document).ready(function () {
    if (!("placeholder" in document.createElement("input"))) {
      $('*[placeholder]').each(function () {
        $this = $(this);
        var placeholder = $(this).attr('placeholder');
        if ($(this).val() === '') {
          $this.val(placeholder);
          $this.css("color", "#555");
        }
        $this.bind('focus', function () {
          if ($(this).val() === placeholder) {
            this.plchldr = placeholder;
            $(this).val('');
            $(this).css("color", "#222");
          }
        });
        $this.bind('blur', function () {
          if ($(this).val() === '' && $(this).val() !== this.plchldr) {
            $(this).val(this.plchldr);
            $(this).css("color", "#555");
          }
        });
      });
      $('form#edit-search-block-form--2').bind('submit', function () {
        $(this).find('*[placeholder]').each(function () {
          if ($(this).val() === $(this).attr('placeholder')) {
            $(this).val('');
          }
        });
      });
    }

    //Adds a class to the menu when it's the deepest level in the tree or the section below is hidden..
    $('.menu-block-wrapper li.active:not(:has(ul))').parent().addClass("bottom-ul");


    // Popular Categories sidebar "view more button implemention"
    if (($(".view-popular-categories .view-content > .views-row").length) > 3) {
      $('.view-popular-categories .view-content').after('<div class="view-footer"><a class="view-more" href="#">View more</a>    </div>');
      $('.view-popular-categories .view-content').after($("<div />").addClass('after').append($(".view-id-popular_categories .views-row:eq(2)").nextAll()));
    }
    $(".view-popular-categories .after").hide();
    $(".view-more").click(function () {
      $(".after").slideToggle(300);
      return false
    });

  });

  //Header height checker - if header content/image is smaller than the ul add correct margin to center title
  $(window).load(function () {
    var siteTitleHeight = $('#header #site-title').outerHeight();
    var secMenuHeight = $('#header #secondary-menu').outerHeight(true);
    if (siteTitleHeight < secMenuHeight) {
      var siteTitleMargin = (secMenuHeight - siteTitleHeight) / 2;
      $('#header #site-title').css({
        marginTop: siteTitleMargin,
        marginBottom: siteTitleMargin
      });
    }
  });
})(jQuery);;
/**
 * Footer service links popup
 */
// image preload
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }

  $(document).ready(function() {
    $('.region-content-footer ul.tools').prepend('<li class="share"><a href="#">Share</a></li>');
    $('.share a').click(function() {
      //if close link exists remove - prevent multiple close links being created
      $('.service-popup-close').remove();
      //add close link
      $('.block-service-links').prepend('<a class="service-popup-close"></a>');
      //toggle share block
      $('.block-service-links').toggle();
      //activate close button
      $('.service-popup-close').click(function() {
        $('.block-service-links').hide();
      });
      return false;
    });
  });
})(jQuery)
;
/**
 * Stylesheet switcher.
 *
 * Code stolen from http://newzealand.govt.nz/
 * and also http://www.alistapart.com/articles/alternate/
 */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

(function($) {
  $(document).ready(function() {
    var cookie = $.cookie('style');
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
  });

  // bind to the user leaving the page and set a cookie
  window.onunload = function(e) {
    var title = getActiveStyleSheet();
    $.cookie('style', title, { path: '/', domain: document.location.hostname });
  }
})(jQuery)

;

