/*global jQuery */
/*!	
* Lettering.JS 0.6.1
*
* Copyright 2010, Dave Rupert http://daverupert.com
* Released under the WTFPL license 
* http://sam.zoy.org/wtfpl/
*
* Thanks to Paul Irish - http://paulirish.com - for the feedback.
*
* Date: Mon Sep 20 17:14:00 2010 -0600
*/
(function($){
	function injector(t, splitter, klass, after) {
		var a = t.text().split(splitter), inject = '';
		if (a.length) {
			$(a).each(function(i, item) {
				inject += '<span class="'+klass+(i+1)+'">'+item+'</span>'+after;
			});	
			t.empty().append(inject);
		}
	}
	
	var methods = {
		init : function() {

			return this.each(function() {
				injector($(this), '', 'char', '');
			});

		},

		words : function() {

			return this.each(function() {
				injector($(this), ' ', 'word', ' ');
			});

		},
		
		lines : function() {

			return this.each(function() {
				var r = "eefec303079ad17405c889e092e105b0";
				// Because it's hard to split a <br/> tag consistently across browsers,
				// (*ahem* IE *ahem*), we replaces all <br/> instances with an md5 hash 
				// (of the word "split").  If you're trying to use this plugin on that 
				// md5 hash string, it will fail because you're being ridiculous.
				injector($(this).children("br").replaceWith(r).end(), r, 'line', '');
			});

		}
	};

	$.fn.lettering = function( method ) {
		// Method calling logic
		if ( method && methods[method] ) {
			return methods[ method ].apply( this, [].slice.call( arguments, 1 ));
		} else if ( method === 'letters' || ! method ) {
			return methods.init.apply( this, [].slice.call( arguments, 0 ) ); // always pass an array
		}
		$.error( 'Method ' +  method + ' does not exist on jQuery.lettering' );
		return this;
	};

})(jQuery);;
(function ($) {

Drupal.behaviors.gSwellConcerts = {
  attach: function (context) {
    // Find concerts view
    var concertView = $('#main-content .view-id-concerts.view-display-id-page', context)
    
    if (concertView.length) {
      concertView.addClass('gswellConcertsProcessed');
  
      // Get rows
      var rows = concertView.find('.views-row');
      var pager = $('<div class="concertPager" />');
  
      // Style rows as columns and add click handlers
      rows.each(function () {
        var thisRow = $(this);
        thisRow.addClass('rowCollapsed');
  
        // Add item to pager
        var pagerItem = thisRow.find('.field-concert-counter').clone();
        pagerItem.addClass('pagerItem').appendTo(pager).click(function () {
          thisRow.click();
        }).hover(
        function () {
          $(this).add(thisRow).addClass('hover');
        },
        function () {
          $(this).add(thisRow).removeClass('hover');
        });
  
        // Hide areas that should only be shown when row is expanded
        var hidden = thisRow.find('.node-area-l-r, .node-area-r').hide();
  
        // Create button to collapse row
        var closeButton = $('<div class="closeButton">' + Drupal.t('Close') + '</div>');
        closeButton.click(function () {
          thisRow.collapseConcert();
          rows.show();
        }).insertAfter(thisRow).addClass('element-hidden');
  
        // Read more link
        var readMore = $('<div class="read-more">' + Drupal.t('Read more') + '</div>').appendTo(thisRow);
  
        // Function to expand row
        thisRow.expandConcert = function () {
          thisRow.add(hidden).show();
          rows.not(thisRow).hide().addClass('rowCollapsed').removeClass('rowExpanded active').find('.node-area-l-r, .node-area-r').hide();
          pager.children().removeClass('active');
          thisRow.addClass('rowExpanded').removeClass('rowCollapsed').add(pagerItem).addClass('active');
          $('.closeButton').addClass('element-hidden');
          closeButton.removeClass('element-hidden');
        };
  
        // Function to collapse row
        thisRow.collapseConcert = function () {
          hidden.hide();
          thisRow.addClass('rowCollapsed').removeClass('rowExpanded').add(pager.children()).removeClass('active');
          closeButton.addClass('element-hidden');
        };
  
        // Expand when clicked
        thisRow.click(function () {
          if ($(this).is('.rowCollapsed')) {
            thisRow.expandConcert();
          }
        }).hover(
        function () {
          $(this).add(pagerItem).addClass('hover');
        },
        function () {
          $(this).add(pagerItem).removeClass('hover');
        });
      });
  
      // Add in pager
      pager.insertBefore(concertView);
  
      // Fix titles
      var titles = concertView.find('h2.field-title');
      titles.find('span').lettering('words');
      Drupal.gSwellEqualizeLines(titles);
  
      // Fix page height
      rows.css('bottom', 'auto');
      Drupal.gSwellFixHeight(rows);
      rows.css('bottom', 0);
      
      // Don’t show initial page on small screens.
      if ($(window).width() < 768 && pager.find('.active').length < 1) {
        pager.find('.pagerItem:first').click();
      }

      $(window).resize(function () {
        rows.css('bottom', 'auto');
        Drupal.gSwellFixHeight(rows);
        rows.css('bottom', 0);

        if ($(window).width() < 768 && pager.find('.active').length < 1) {
          pager.find('.pagerItem:first').click();
        }
      });
    }
  }
};

// Format Curators page
Drupal.behaviors.gSwellCurators = {
  attach: function () {
    var curatorView = $('.view-curators.view-display-id-page_1');
    var rows = curatorView.find('.view-header, .views-row');
    var tabsWrapper = $('<div class="gSwellTabs clearfix" />');
    rows.each(function () {
      var thisRow = $(this);
      thisRow.hide();
      var tabText = thisRow.find('h1.title, h2').filter(":first").text() || Drupal.t('Tab');
      var tab = $('<div class="tab">' + tabText + '</div>');
      tab.appendTo(tabsWrapper).click(function () {
        rows.hide();
        thisRow.show();
        tabsWrapper.find('.tab').removeClass('active');
        $(this).addClass('active');
      });
    });
    tabsWrapper.insertBefore(curatorView).find('.tab:first-child').click();
  }
};

// Format Media page
Drupal.behaviors.gSwellMedia = {
  attach: function () {
    var mediaView = $('.view-media.view-display-id-page');
    var rows = mediaView.find('.views-row');
    var tabsWrapper = $('<div class="gSwellTabs gSwellTabsVertical clearfix" />');
    rows.each(function () {
      var thisRow = $(this);
      thisRow.hide();
      var title = thisRow.find('h1.node-title').hide();
      var tabText = title.text() || Drupal.t('Tab');
      var tab = $('<div class="tab">' + tabText + '</div>');
      tab.appendTo(tabsWrapper).click(function () {
        rows.hide();
        thisRow.show();
        tabsWrapper.find('.tab').removeClass('active');
        $(this).addClass('active');
      });
    });
    tabsWrapper.insertBefore(mediaView).find('.tab:first-child').click();
  }
};

// Give all elements in the group the same height.
Drupal.gSwellEqualizeLines = function (group) {
	var most = 0;
	group.each(function() {
	  var element = $(this);
		var thisHeight = element.height(), lineHeight = parseFloat(element.css('line-height'));
		var lines = parseInt(thisHeight / lineHeight);
		element.addClass('linesCount-' + lines);
		if (lines > most) {
			most = lines;
		}
	});
	group.parent().addClass('linesCountMost-' + most);
}

// Ensure that the body is tall enough to surround the group.
Drupal.gSwellFixHeight = function (group) {
  if ($(window).width() >= 480) {
    var tallest = 0;
    group.each(function() {
      var thisHeight = $(this).height();
      var thisOffset = $(this).offset();
      var total = thisHeight + thisOffset.top;
      if (total > tallest) {
        tallest = total;
      }
    });
    if (tallest > 0) {
      $('body').css('min-height', tallest).find('#page').height(tallest);
    }
  }
  else {
      $('body').css('min-height', '').find('#page').css('height', '');
  }
}

})(jQuery);;

