(function ($) {

  Drupal.behaviors.ottar = {
    attach: function (context) {

      var random = Math.floor(Math.random()*4+1);

      if (random == 1) {
        random = "blue";
      }
      else if (random == 2) {
        random = "pink";
      }
      else if (random == 3) {
        random = "grey";
      }
      else if (random == 4) {
        random = "original";
      }

      $("body").addClass(random);

      // Add striped div on views-row hover

      var hoverDiv = $("<div class=\"views-row-hover\"></div>");

      $(".view-id-articles.view-articles-list .views-row").hover(
        function () {
         $(this).append(hoverDiv);
          hoverDiv.click(function () {
            window.location = $(this).parents(".views-row").find("div .readmore a").attr("href");
          });
        },
        function () {
          $(this).find(".views-row-hover").remove();
        }
      );

      // Make front page slider views-row clickable
      $("#views_slideshow_cycle_main_articles-page_articles_front .views-row").click(function(){
        window.location = $(this).find("a").attr("href");
      });

      // Make whole views-rows clickable
      var views = [
        "#block-views-issue-block-1",
        ".sidebar-block-articles",
        "#block-views-blog-block-blog",
        ".view-id-articles_front_page",
        ".view-id-articles.view-articles-list",
        "#content .view-id-issue"
      ];
      $(views.join(", "))
        .find(".views-row").each(function() {
          $(this).click(function(e) {
            window.location = $(this).find("a").eq(0).attr("href");
            e.stopPropagation();
          });
          $(this).find("a").click(function(e) {
            e.stopPropagation();
          });
        });

      // Open share links in a popup window.
      $(".ottar-share-links a").click(function(e) {
        window.open($(this).attr("href"), 'ottar-share-box', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=400');
        e.preventDefault();
      });

      // Use Overlabel for certain form fields.
      $(".form-item-mailchimp-lists-75fccfa4df-EMAIL label", context).overlabel();
      $("#content .contact-form label", context).overlabel();
      $("#user-login label, #user-pass label", context).overlabel();
      $("#edit-keys-wrapper label", context).overlabel();

      // Remove empty article type spans.
      $("#content .submitted .article-type", context).each(function () {
        if ($(this).text() == "") {
          $(this).remove();
        }
      });

    }
  };
})(jQuery);
;
( function( $ ) {
 
    // plugin definition
    $.fn.overlabel = function( options ) {
 
        // build main options before element iteration
        var opts = $.extend( {}, $.fn.overlabel.defaults, options );
 
        var selection = this.filter( 'label[for]' ).map( function() {
 
            var label = $( this );
            var id = label.attr( 'for' );
            var field = document.getElementById( id );
 
            if ( !field ) return;
 
            // build element specific options
            var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
 
            label.addClass( o.label_class );
 
            var hide_label = function() { label.css( o.hide_css ) };
            var show_label = function() { this.value || label.css( o.show_css ) };
 
            $( field )
                 .parent().addClass( o.wrapper_class ).end()
                 .focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
 
            return this;
 
        } );
 
        return opts.filter ? selection : selection.end();
    };
 
    // publicly accessible defaults
    $.fn.overlabel.defaults = {
 
        label_class:   'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css:      { 'text-indent': '-10000px' },
        show_css:      { 'text-indent': '0px', 'cursor': 'text' },
        filter:        false
 
    };
 
} )( jQuery );;

