﻿jQuery(document).ready(function() {
    /* Enter Key search */
    jQuery('#search-button').keypress(function(e) {
        if (e.which == 13) {
            if(jQuery('#search-button').val() != '') {
                jQuery('form#search').submit();
            }
            else {
                if (e.preventDefault) { e.preventDefault(); }
            }
        }
    });

    jQuery('#search-button').click(function(e) {
        if (jQuery('#search-button').val() == 'Search') {
            jQuery('#search-button').val('');
        }
    });

    /* displaying/hiding main navigation drop downs on hover and focus */
    jQuery("#nav > li").hoverIntent(
			function() {
			    jQuery(this).addClass("hover");
			    jQuery(this).css("z-index", "300").find(".drop-down-shadow").slideDown();
			},
			function() {
			    jQuery(this).removeClass("hover");
			    jQuery(this).css("z-index", "200").find(".drop-down-shadow").slideUp();
			}
		);
    jQuery("#nav > li > .drop-down-shadow").hoverIntent(
			function() { jQuery(this).closest("li").addClass("hover"); },
			function() { jQuery(this).closest("li").removeClass("hover"); }
		);
    /* focus - for keyboard only users */
    jQuery("#nav > li > a").focus(
				function() {
				    jQuery("#nav > li .drop-down-shadow").hide();
				    jQuery("#nav > li").removeClass("hover");
				    jQuery(this).closest("li").addClass("hover");
				    jQuery(this).closest("li").find(".drop-down-shadow").slideDown();
				});
    jQuery("#nav > li > ul li a").focus(
				function() { jQuery(this).closest("li").addClass("hover"); },
				function() { jQuery(this).closest("li").removeClass("hover"); }
			);


    /* adding alt classes to alt columns,rows,elements and adding classes to input elements to style them, yo */
    jQuery("#content .primary:nth-child(odd)").addClass("alt");
    jQuery("#content table tr:nth-child(odd)").addClass("alt");
    jQuery("#content fieldset li input[type='text']").addClass("shadow");
    jQuery("#content fieldset textarea").addClass("shadow");
    jQuery("#content fieldset li input[type='file']").addClass("shadow file-upload");

    /* adding first and last classes to relevant elements...and stuff */
    jQuery("#content .gallery .primary:last-child").addClass("last");
    jQuery("#content .sidebar .promo-list dd:first").addClass("first");
    jQuery("body.body-key-people .key-people li:first").addClass("first");
    jQuery("#content .primary:first").addClass("first");
    jQuery("#content .primary:last-child").addClass("last");
    jQuery("#content .wesley-connections li .all-people-container:last").addClass("wesley-connections-last-element");
    jQuery("#campus-tours .tours-term:last").addClass("last");
    jQuery("#campus-tours .tours-term ul li.column:last").addClass("last");


    /* adjusting position in single image gallery */
    jQuery("body.body-gallery-single .gallery .primary .image-container .image-content a img").each(function(e) {
        var boxWidth = jQuery(this).closest("li").width();
        var boxHeight = jQuery(this).closest("li").height();
        var imgWidth = jQuery(this).attr("width");
        var imgHeight = jQuery(this).attr("height");
        var topMarginNeeded = ((boxHeight - imgHeight) / 2) - 5;
        var leftMarginNeeded = ((boxWidth - imgWidth) / 2) - 5;
        jQuery(this).closest("li").find("div.image-container").css("margin-left", leftMarginNeeded).css("margin-top", topMarginNeeded);
    });

    /* removing bottom border...where it's not wanted...can't sit here ya dirty ugly border */
    jQuery("#content .sidebar .image-promo:last-child").css("border-bottom", "none");

    /* enrolment yes|no radio buttons drop down functionality */
    jQuery("#content .trigger-down").click(function(e) {
        jQuery(this).closest("li").find(".all-people-container").slideDown();
    });

    jQuery("#content .trigger-up").click(function(f) {
        jQuery(this).closest("li").find(".all-people-container").slideUp();
    });



    /* book campus tour slidey magic */
    jQuery("#campus-tours ul .tours-term").hide(); /* hide all terms */
    jQuery("#campus-tours ul .term-active").show(); /* find the one that is initially labeled as being "active" and show it */
    jQuery("#campus-tours .tour-details-container").equalHeights(); /* setting equal height columns on active term tour dates only */

    jQuery("#campus-tours .view-toggles li.prev a").click(function(e) {
        jQuery("#campus-tours ul .term-active").removeClass("term-active").fadeOut('fast', function() {
            jQuery(this).closest("li.tours-term").prev("li.tours-term").addClass("term-active").fadeIn("fast");
            jQuery(this).closest("li.tours-term").prev("li.tours-term").find(".tour-details-container").equalHeights();
        });
        if (e.preventDefault) { e.preventDefault(); }
    });

    jQuery("#campus-tours .view-toggles li.next a").click(function(f) {
        jQuery("#campus-tours ul .term-active").removeClass("term-active").fadeOut('fast', function() {
            jQuery(this).closest("li.tours-term").next(".tours-term").fadeIn().addClass("term-active");
            jQuery(this).closest("li.tours-term").next("li.tours-term").find(".tour-details-container").equalHeights();
        });
        if (f.preventDefault) { f.preventDefault(); }
    });

    /* blog author comment border magic */
    jQuery("#content .post-comments dl dd.author-comment").next("dd.primary").addClass("comment-after-author-comment");

    /* teacher scroller thingo that scrolls....teachers. */
    var globalImageWidth = 280;
    var scrollContainer = jQuery("#image-scroller ul.scroll-container");
    var totalScrollElements = jQuery("#image-scroller li.scroll-unit").size();
    var scrollElementCount = jQuery("#image-scroller li.scroll-unit").size() - 1;

    if (totalScrollElements < 2) {
        jQuery("#image-scroller").addClass("single-unit");
    }
    else {
        jQuery("#image-scroller li.scroll-unit .image-container a").click(function(b) {
            if (scrollElementCount > 0) { /* check to see if the current element is the last in the list (of scrollable elements) */
                var position = scrollContainer.position();
                var scrollOffset = position.left;
                var newScrollOffset = scrollOffset - globalImageWidth;
                jQuery(scrollContainer).fadeOut('fast').animate({ 'left': newScrollOffset }, 0).fadeIn('fast'); /* if it's not the last element, scroll it left by the globalImageWidth */
                scrollElementCount = scrollElementCount - 1;
            }
            else {
                var lastElementScrollPosition = (scrollElementCount * globalImageWidth);
                jQuery(scrollContainer).fadeOut('fast').animate({ 'left': lastElementScrollPosition }, 0).fadeIn('fast');
                var position = scrollContainer.position();
                var scrollOffset = position.left;
                var newScrollOffset = scrollOffset - globalImageWidth;
                scrollElementCount = scrollElementCount + totalScrollElements - 1;
            }
            if (b.preventDefault) { b.preventDefault(); }
        });

        jQuery("#image-scroller li.scroll-prev a").click(function(a) {
            if (scrollElementCount < (totalScrollElements - 1)) { /* check to see if the current element is the first in the list (of scrollable elements) */
                var position = scrollContainer.position();
                var scrollOffset = position.left;
                var newScrollOffset = scrollOffset + globalImageWidth;
                jQuery(scrollContainer).fadeOut('fast').animate({ 'left': newScrollOffset }, 0).fadeIn('fast'); /* if it's not the first element, scroll it (the containg ul scrolling container) right by the globalImageWidth */
                scrollElementCount = scrollElementCount + 1;
            }
            else {
                var lastElementScrollPosition = ((totalScrollElements * globalImageWidth) - globalImageWidth);
                jQuery(scrollContainer).fadeOut('fast').animate({ 'left': -lastElementScrollPosition }, 0).fadeIn('fast');
                scrollElementCount = 0;
            }
            if (a.preventDefault) { a.preventDefault(); }
        });

        jQuery("#image-scroller li.scroll-next a").click(function(z) { /* assigning click function to right facing arrow */
            if (scrollElementCount > 0) { /* check to see if the current element is the last in the list (of scrollable elements) */
                var position = scrollContainer.position();
                var scrollOffset = position.left;
                var newScrollOffset = scrollOffset - globalImageWidth;
                jQuery(scrollContainer).fadeOut('fast').animate({ 'left': newScrollOffset }, 0).fadeIn('fast'); /* if it's not the last element, scroll it left by the globalImageWidth */
                scrollElementCount = scrollElementCount - 1;
            }
            else {
                var lastElementScrollPosition = (scrollElementCount * globalImageWidth);
                jQuery(scrollContainer).fadeOut('fast').animate({ 'left': lastElementScrollPosition }, 0).fadeIn('fast');
                var position = scrollContainer.position();
                var scrollOffset = position.left;
                var newScrollOffset = scrollOffset - globalImageWidth;
                scrollElementCount = scrollElementCount + totalScrollElements - 1;
            }
            if (z.preventDefault) { z.preventDefault(); }
        });
    }
});
	

function setOrderClass(a,b){
	
	if ($(b).val() > 0 ){
	
		$('#class').val( a );
		
		$('.class-select').each (
			 function(){
				if ( $(this).hasClass( 'class-'+a ) ){
					$(this).attr('disabled','');	
				}
				else {
					$(this).attr('disabled','disabled');
				}
			}
		);
	}
	else {
		$('#class').val( '' );
		$('.class-select').each (
			 function(){
				$(this).attr('disabled','');	
			 }
		);
	}
}


