// ****************************************
// The George Washington University MBA Association Base JavaScript (with JQuery Help)
// Created by Brian Talbot on 2009-07-19
// (c) The George Washington University MBA Association.
// ****************************************

// Since we might be playing with MooTools a bit - No Conflict time!
//jQuery.noConflict();

// ========================================
// Utility functions (Not Loaded with DOM)
// ========================================

// FadeToggle
jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

// SlideToggle
jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

// Get the Index of an Element
jQuery.fn.getIndex = function(){
  var parentSet=jQuery(this).parent().children();
  return jQuery(parentSet).index(this);
};

// Display/Hide Loading
jQuery.fn.toggleLoading = function(){
  jQuery("#loading").fadeToggle();
};

// Form Label Overlaying
jQuery.fn.overLabel = function() {
  this.each(function(index) {
    var label = jQuery(this); var field;
    var id = this.htmlFor || label.attr('for');
    
    if (id && (field = document.getElementById(id))) {
      var control = jQuery(field);
      label.addClass("overlabel_apply");
      if (field.value !== '') {
      label.css("text-indent", "-1000px");
    }
    
    control.focus(function () {label.css("text-indent", "-1000px");}).blur(function () {
      if (this.value === '') {
        label.css("text-indent", "0px");
      }
    });
    
    label.click(function() {
      var label = jQuery(this); var field;
      var id = this.htmlFor || label.attr('for');
      
      if (id && (field = document.getElementById(id))) {
        field.focus();
      }
    });
    }
  });
};

jQuery.fn.summarize = function(length){
  var str = jQuery(this).text().slice(0,length);
  // alert( "Let's trim: "       + jQuery(this).text());
  // alert( "Sliced: "           + str + "...");
  // alert( "Last space is at: " + (str.lastIndexOf(' ')));
  return str.slice(0,(str.lastIndexOf(' '))) + "&hellip;" + '<span class="more">More</span>';
};

// Fading
jQuery.fn.fadeDown = function(speed) {  
   return jQuery(this).animate({	opacity: 0.01	}, speed);
};
jQuery.fn.fadeUp = function(speed) {  
   return jQuery(this).animate({	opacity: 1 }, speed);
};

// Delaying Elements
jQuery.fn.delay = function(time, callback){
  // Empty function:
  jQuery.fx.step.delay = function(){};
  // Return meaningless animation, (will be added to queue)
  return this.animate({delay:1}, time, callback);
};


jQuery(document).ready(function(){

// ========================================
// Utility functions (Loaded with DOM)
// ========================================
	
  // Prepping JS-conditional styles
  jQuery("body").addClass("enhanced");
	
	// Input focus and blur
	jQuery("input, textarea", jQuery("form")).focus(function(){
      jQuery(this).addClass("focus");
      jQuery(this).parents(".form-field").addClass("cur");
  });
  jQuery("input, textarea", jQuery("form")).blur(function(){
      jQuery(this).removeClass("focus");
      jQuery(this).parents(".form-field").removeClass("cur");
  });
	
	// Add utility first/last/alt classes to LIs.
	jQuery("ul li:first-child, ol li:first-child").addClass("first");
	jQuery("ul li:last-child, ol li:last-child").addClass("last");
	jQuery("ul li:nth-child(even), ol li:nth-child(even), table tr:nth-child(even)").addClass("alt");
	jQuery(".listing_photos ul li:nth-child(5n)").addClass("cinco");


	// Wrap Your HRs For Easy Styling
	jQuery("hr").each(function() {
	  var hrClass = jQuery(this).attr("class");
	  jQuery(this).wrap("<div class='hr " + hrClass +"'></div>");
	});
  
  
	// Wrap the Inside of Button Elements
	jQuery("button, .button").each(function() {
	  var hasSpan = jQuery(this).find("span")[0];
	  if (hasSpan = "undefined") {
	    jQuery(this).wrapInner("<span></span>");
	  }
	});
  
  // Summarizing
  // jQuery(".something").each(function(){
  //   var summarized_content = jQuery(this).summarize(40);
  //   jQuery(this).html(summarized_content);
  // });

  // Making whole areas clickable, accessibly
  jQuery("ul.clickify li, .promos ul li").each(function() {

    var list_item = jQuery(this);
    var link = jQuery(this).find('a[class="call"]').attr("href");

    // covering the hover state of the interaction
    list_item.hover(
      function(){ jQuery(this).addClass("hover"); }
      , 
      function(){ jQuery(this).removeClass("hover"); }  
     );

    // covering the click state of the interaction
    list_item.click(function(){
      window.location = link;
    });
  });
  
  	// Hacking Nasty iCal Plugin Output (This is not good to do :o\)
	/*
        jQuery("div[id^='ics-event-list']").each(function(){
		var list = jQuery(this).find('ul').addClass("events");
		var items = list.find('li');

		items.each(function(){
			var timeframe = jQuery(this).find('strong').text();
			var name = jQuery(this).find('i').text();
			var place = jQuery(this).html();
			place = place.slice((place.lastIndexOf('(') + 1),place.lastIndexOf(')'));
          
			var content = '<h2>' + name  + '</h2><div class="meta"><span class="date">' + timeframe + '</span><span class="location">' + place + '</span></div>';

			jQuery(this).html(content);
		});
	});
*/

	
  // Overlabeling
  // jQuery("form label").overLabel();
  //   jQuery(".wpcf7-form label").overLabel();
});