(function ($) {

    $(window).load(function () {
        master.init();
    });

    var master = function () {

        var setupScroller = function () {
            var scrollable = $(".scrollable");

            if (!scrollable.length) return;

            var scroller = scrollable.scrollable({ circular: true, easing: "easeInOutExpo", speed: 600 }).navigator(".nav").autoscroll({ autoplay: true, interval: 5000 });
            scroller.eq(0).data("scrollable").focus();
        };

        var setupHomeNews = function () {
            if ($(".home-news ul").cycle) {
                $(".home-news ul").cycle({
                    fx: 'fade',
                    speed: 1000,
                    timeout: 7000
                });
            }
        };

        var applyOffsetTiles = function () {

            var intro = $(".offset-tiles .intro-copy");

            if (!intro.length) return;

            var offset = intro.outerHeight();
            $(".offset-tiles aside").css('padding-top', offset + 8);

        };

        init = function () {
            setupScroller();
            setupHomeNews();
            applyOffsetTiles();
            $(".header .search input[type=text]").defaultValueInput();
        };

        return { init: init };

    } ();
})(jQuery);


// Utils
(function ($) {
  jQuery.fn.defaultValueInput = function () {
    for (var i = 0; i < $(this).length; i++) {
      $(this[i]).data('defaultValue', $(this[i]).attr('value'));
      $(this[i]).addClass('default-value');
    };
    $(this).focus(function () {
      if ($(this).attr('value') == $(this).data('defaultValue')) {
        $(this).attr('value', '');
        $(this).removeClass('default-value');
      }
    });
    $(this).blur(function () {
      if ($(this).attr('value') == '' || $(this).attr('value') == $(this).data('defaultValue')) {
        $(this).attr('value', $(this).data('defaultValue'));
        $(this).addClass('default-value');
      }
    });
  }
})(jQuery);
