// remap jQuery to $
(function($){
  $("body.not-front #main .node h2, #block-views-e1c117b8c536172b57608774f144a732 h2.apply").each(function(i) {
       var current = $(this);
      current.attr("id", "title" + i);
       $("#dynamic-page-sections").append("<li><a id='link" + i + "' href='#title" +
           i + "'>" + 
           current.html() + "</a></li>");
   });
   $("#dynamic-page-sections li:last").addClass('last');
   $("#dynamic-page-sections li a").click(function() {
     var ps = $(this).attr("href");
      $("body.not-front #main .node h2").removeClass('active');
      $(ps).addClass('active');
   });
   $("#block-views-black_box-block_1 tbody tr:last").addClass("last");
    $('ul#page-actions').prepend('<li class="print"><a href="javascript:window.print()">Print</a></li>');
   // $('ul#page-actions li.print a').click(function() {
   //   window.print();
   //   return false;
   //  });

//BX Slider
$('.slider1').bxSlider({
	displaySlideQty: 3,
	moveSlideQty: 1
});

// Funding table styling
$('.funding-table table table tr:odd, body.subsection-financial-statements #main table tr:odd, #rss-table tr:odd, table.grants tr:odd, body.node-type-news #main #updates table tr:odd').addClass('oddTr');
// $('.view-Apply-for-Funding-Program:contains("</table><p>&nbsp;</p>")').addClass('nothing');


// Add icon after external links
$("a[href$='.pdf']:not('#footer a, #header a'), a[href$='.doc'], a[href$='.docx'], a[href$='.xls'], a[href$='.xlsx'], a[href$='.pptx'], a[href$='.ppt']").each(function(){
  $(this).addClass("external").attr('target', '_blank');
  });


$("a[href^=http]:not('#header a, body.front .leaf a, #block-menu_block-7 .menu-block-7 .menu li.leaf a, #block-block-6 a, body.page-contact-us #content-content a, table#soc-med-table a, #block-menu_block-26 a')").each(function(){
 if(this.href.indexOf(location.hostname) == -1) {
   $(this).addClass("external").attr('target', '_blank');
   }
 });
// Removes external icon from social media links, while keeping the target_blank
   $('#block-menu_block-10 a, #block-menu_block-13 a').removeClass("external");

   $('.menu-mlid-837 a, .menu-mlid-1186 a, .menu-mlid-1136 a').attr('target', '');

// Search bar default value & blur effect
$('input, textarea').each(function(){
    $(this).data('initial', this.value);
}).focus(function(){
    var $this = $(this);
    if ( (this.value) == $this.data('initial') )
      this.value = '';
}).blur(function(){
    var $this = $(this);
    if ( (this.value) == '' )
        this.value = $this.data('initial');
})


// jCaption -- Gives inline images a caption & photo credit
$('.section-news #main .node .content img, .page-about-us #content-content img, .page-about-us-board-trustees #main img, .section-kresge-blog #main .node .content img').jcaption();

//jQuery Validate (for email form)
$("#webform-client-form-396-1").validate({
  rules: {
    "submitted[first_name]": "required",
    "submitted[message]": {
      required: true,
      maxlength: 300
    }
    //'edit-submitted-last-name': "required"
  },
  messages: {
     "submitted[first_name]": "This field is required",
     "submitted[message]": "Maximum of 300 characters"
     // 'edit-submitted-last-name': "This field is required"
  }
  
});

// $("#signupForm").validate({
//    rules: {
//      firstname: "required",
//      lastname: "required",
//      username: {
//        required: true,
//        minlength: 2
//      },
//      password: {
//        required: true,
//        minlength: 5
//      },
//      confirm_password: {
//        required: true,
//        minlength: 5,
//        equalTo: "#password"
//      },
//      email: {
//        required: true,
//        email: true
//      },
//      topic: {
//        required: "#newsletter:checked",
//        minlength: 2
//      },
//      agree: "required"
//    },
//    messages: {
//      firstname: "Please enter your firstname",
//      lastname: "Please enter your lastname",
//      username: {
//        required: "Please enter a username",
//        minlength: "Your username must consist of at least 2 characters"
//      },
//      password: {
//        required: "Please provide a password",
//        minlength: "Your password must be at least 5 characters long"
//      },
//      confirm_password: {
//        required: "Please provide a password",
//        minlength: "Your password must be at least 5 characters long",
//        equalTo: "Please enter the same password as above"
//      },
//      email: "Please enter a valid email address",
//      agree: "Please accept our policy"
//    }
//  });

//Smooth scrolling to anchor links [page section links]
$(function() {
        
                function filterPath(string) {
                        return string
                        .replace(/^\//,'')
                        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                        .replace(/\/$/,'');
                }
        
                var locationPath = filterPath(location.pathname);
                var scrollElem = scrollableElement('html', 'body');
        
                // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
                $('a[href*=#]').each(function() {
        
                        // Ensure it's a same-page link
                        var thisPath = filterPath(this.pathname) || locationPath;
                        if (  locationPath == thisPath
                                && (location.hostname == this.hostname || !this.hostname)
                                && this.hash.replace(/#/,'') ) {
        
                                        // Ensure target exists
                                        var $target = $(this.hash), target = this.hash;
                                        if (target) {
        
                                                // Find location of target
                                                var targetOffset = $target.offset().top;
                                                $(this).click(function(event) {
        
                                                        // Prevent jump-down
                                                        event.preventDefault();
        
                                                        // Animate to target
                                                        $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
        
                                                                // Set hash in URL after animation successful
                                                                location.hash = target;
        
                                                        });
                                                });
                                        }
                        }
        
                });
        
                // Use the first element that is "scrollable"  (cross-browser fix?)
                function scrollableElement(els) {
                        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
                                var el = arguments[i],
                                $scrollElement = $(el);
                                if ($scrollElement.scrollTop()> 0) {
                                        return el;
                                } else {
                                        $scrollElement.scrollTop(1);
                                        var isScrollable = $scrollElement.scrollTop()> 0;
                                        $scrollElement.scrollTop(0);
                                        if (isScrollable) {
                                                return el;
                                        }
                                }
                        }
                        return [];
                }
        
        });


// Opacichange
var imgfade = false;
var views = ".col";
var rownum = 0;
// console.log(rownum);
var img = " img.imagecache-Banner-Image";
// console.log(rownum);
randomfade();
function randomfade() {
	for ( var i = 0; i < 15; i++) {
		var position = views + rownum + img;
		var randnumb = Math.floor(Math.random()*2)
		if (randnumb === 0) {
			imgfade = false;
			$(position).css({ opacity: 0 });
			$(position).hover(
			function() {
				$(this).stop().animate({"opacity": "1"}, 400);
			},
			function() {
				$(this).stop().animate({"opacity": "0"}, 400);
			});
		} else {
			imgfade = true;
			$(position).css({ opacity: 1 });
			$(position).hover(
			function() {
				$(this).stop().animate({"opacity": "0"}, 400);
			},
			function() {
				$(this).stop().animate({"opacity": "1"}, 400);
			});
		}
		rownum++;
	}
}
			// $(".gallery a").fancybox({
			// 	'overlayColor'  : '#000',
			// 	'enableEscapeButton' : true,
			// 	'transitionIn'  : 'elastic', 
   //              'transitionOut' : 'elastic',
   //              'type' : 'ajax',
   //              'autoDimensions' : true
			// });


 // Lightbox
$('.gallery a, #block-block-13 a').lightBox();


// Fancy Box Youtube
$("#rip-vid-light").click(function() {
  $.fancybox({
      'padding'   : 0,
      'autoScale'   : false,
      'transitionIn'  : 'none',
      'transitionOut' : 'none',
      'title'     : this.title,
      'width'   : 680,
      'height'    : 495,
      'href'      : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
      'type'      : 'swf',
      'swf'     : {
           'wmode'    : 'transparent',
        'allowfullscreen' : 'true'
      }
    });

  return false;
});


$('.google-appliance-keymatches').prepend('<li><h4>RECOMMENDED RESULTS</h4></li>');


})(jQuery);

