/**
 * Resizes floated image containers to the size of the image
 */
$.fn.imageWidth = function(threshold) {
	/**
	 * Function takes a jquery object and a css property (called dimension) and determines how many pixels the item is
	 * @param {jQuery Object} $item The jQuery object we're looking at
	 * @param {String} dimension The CSS property name to look for
	 * @returns The width or height of the CSS property with 'px' removed
	 * @type Number
	 */
	var	determineDimension = function($item, dimension) {
		$item = $($item);
		if ($item.css(dimension)) {
			return parseInt($item.css(dimension).replace('px', ''), 10);
		} else {
			return 0;
		};
		return false;
	};
	
	var	resizeImage = function($image, $parent, $container) {
		// Determine the width of the image along with borders and padding
		var imageWidth = $image.width();
		var paddingLeft = determineDimension($image, 'padding-left');
		var paddingRight = determineDimension($image, 'padding-right');
		var borderLeft = determineDimension($image, 'border-left-width');
		var borderRight = determineDimension($image, 'border-right-width');

		// Calculate total edge (padding and border) width and the total width (edge and image)
		var edgeWidth = paddingLeft + paddingRight + borderLeft + borderRight;
		var totalWidth = imageWidth + edgeWidth;

		// Determine parent width
		var parentWidth = $parent.width();

		// If the image is greater then the threshold times the parent width resize the image and the container width
		// Otherwise set the image left's div to the size of the image plus the edge
		if ((threshold * parentWidth) <= totalWidth) {
			var revisedWidth = parentWidth * threshold;
			var revisedImageWidth = revisedWidth - edgeWidth;

			$image.width(revisedImageWidth);
			$container.width(parseInt(revisedWidth, 10));
		} else {
			$container.width(totalWidth);
		};
	};

	return this.each(function() {
		// Threshold is the maximum width an image plus its border and padding can be 
		// in relation to its parent container
		var threshold = (threshold) ? threshold : 2/3;

		// Find image within div
		var $image = $('img', $(this));
		var $parent = $(this).parent();
		var $container = $(this);

		$image.each(function(index) {
			resizeImage($image, $parent, $container);
			$(this).load(function() {			
				resizeImage($image, $parent, $container);
			});
		});
	});		
};

/**
 * Builds pull quote divs assuming you've wrappted your content with a span with the class: pullquote-left or pullquote-right
 */
$.fn.pullQuote = function() {
	return this.each(function() {
		var contents = $.trim($(this).html());
		var firstCharacterCode = contents.charCodeAt(0);
		if (firstCharacterCode < 65 || firstCharacterCode > 96) {
			contents = '… ' + contents;
		};
		
		var lastCharacter = contents.charAt(contents.length - 1);
		if ("?!.".search(lastCharacter) < 0) {
			contents = contents + ' …';
		};
		var $parent = $(this).parent();
		var $pullquote = $('<div>').attr('class', $(this).attr('class')).html(contents);
		$parent.before($pullquote);
	});		
};

/**
 * Makes the placeholder attribute on input items useful by emulating the behavior
 */ 
var placeholder = function() {
	if ('placeholder' in document.createElement('input')) return this;
	
	$('input[placeholder]').each(function(index) {
		$(this).val($(this).attr('placeholder')).addClass('placeholder')
		.focus(function() {
			if ($(this).val() === $(this).attr('placeholder')) {
		        $(this).val('').removeClass('placeholder');
			}
		})
		.blur(function() {
			if ($(this).val() === ''){
				$(this).val($(this).attr('placeholder')).addClass('placeholder');
			}
		});
	});
};

/**
 * Adds the class 'last' to the last list items, and the last table item in a row. 
 * Also adds alt class to odd table rows.
 */
var markupPrep = function() {
	var listPrep = function() {
		$('> li:last', 'ul, ol').addClass('last');
	};
	
	var	tablePrep = function() {
		$('table tr:odd').addClass('alt');
		$('table td:last, table th:last').addClass('last');
		$('table tbody tr').hover(rowIn,rowOut);
	};
	
	listPrep();
	tablePrep();
};

/* Highlight the detail row and the main row if either are hovered over. */
var rowIn = function(event) {
	$(this).addClass("active");
	if($(this).is(".detail")) {
		$(this).prev().addClass("active");
	} else { 
		$(this).next(".detail").addClass("active");
	}
}

/* Clear highlight states on rollout. */
var rowOut = function(event) {
	$(this).siblings().andSelf().removeClass("active");
}

/**
 * Given an item and a selector for it's siblings, this will make them all the same height
 * @param {String} previous_item Previous item jQuery selector
 */
$.fn.same_height_as = function(previous_item) {
	return this.each(function() {
		$right = $(this);
		$left = $(this).siblings(previous_item);
		
		if ($left.height() > $right.height()) {
			$right.height($left.height());
		} else {
			$left.height($right.height());
		};
	});		
};

/**
 * Handles the same height problem on the home page since all three blocks shouldn't be the same height
 */
var same_height = function() {
	if ($('body.home').size() > 0) {
		$right = $('.news .inner');
		$left = $('.calendar .inner');
		
		if ($left.height() > $right.height()) {
			$right.height($left.height());
		} else {
			$left.height($right.height());
		};
	} else {
		$('.block.left, .block.right').same_height_as('.block');
	}
	$('.full #content-section').same_height_as('.full #content-slideshow');
};

/**
 * Launches slideshows for ALL slideshows
 */

var	slideshow = function() {
	if ($('#content-slideshow .slides').children().size()>1)  {
		$('#content-slideshow .slides').innerFade({
			timeout: 6000,
			indexContainer: '.slide-navigation'
		});
	};
};

/**
 * Moves foreground images if you're using IE6
 */
var move_images = function() {
	if ($.browser.msie == true && $.browser.version <= 6) {
		$('img.foreground').css({right: '-1px', bottom: '-1px'});
	};
};

/**
 * Hides steps 2-3 on the flow diagram on financial aid
 */
var flow_fader = function() {
	$('ul.toggle li a').click(function(event) {
		event.preventDefault();
		
		$('ul.toggle li').removeClass('active');
		$(this).parent().addClass('active');
		
		
		if ($(this).hasClass('second')) {
			$('li.omit').slideUp();
		} else {
			$('li.omit').slideDown();
		}
	
	});
};

/**
 * Sets up the carousel
 */
var scrollable = function() {
	$('.carousel-previous, .carousel-next').click(function(event) {
		event.preventDefault();
	});
	
	$('.carousel-inner').scrollable({
		next: ".carousel-next",
		prev: ".carousel-previous"
	});
};

/**
 * Slide the section navigation
 */



var section_navigation = function() {
        var speed = 600;
	var easing = 'easeInOutCubic';
	
	// T4: Remove link on first level elements that have children
	$("#section-nav > ul > li:has(ul) > a, #section-nav > ul > li:has(ul) > span > a").attr('href','javascript:;');
	
	// T4: Add right arrow using position'd span
	$('#section-nav > ul > li > a, #section-nav > ul > li > span a').before('<span class="caret-right"> </span>');

	// Get the active index from the meta tag, then get the corresponding list item.
	$defaultBranch = $("#section-nav span.currentbranch0").parent();
	
	// Simulate a click on the first li a tag
	

	// T4: If there is an open branch set it to active. If there is none hide children to initilize accordian
	if($defaultBranch.length > 0) {
		$defaultBranch.addClass("active").siblings().children('ul').hide();
	} else {
		$('#section-nav > ul > li:has(ul)').children('ul').hide();
	}
	
	// T4: Bind click to first level (a) or selected items (span a)
	$('#section-nav > ul > li:has(ul) > a, #section-nav > ul > li:has(ul) > span > a').live('click', function(event) {
                $(this).closest("li").siblings('.active').removeClass('active').children('ul').slideUp(speed,easing);
		$(this).closest("li").addClass('active').children('ul').slideDown(speed,easing);
                event.preventDefault();
	});
};

/**
 * Hide the emergency bar when the close button is clicked
 */
var emergency = function() {
	if ($.cookie('show_emergency') == "no") {
		$('#emergency').hide();
	} else {
		$.cookie('show_emergency', 'yes');
		
		$('#emergency .close a').click(function(event) {
			event.preventDefault();
			var height = $('#emergency').innerHeight() + 2;
			$('#emergency').animate({top: -height}, 200, function() {
				$(this).hide();
				// Set a cookie to not show the emergency an hour from now
				$.cookie('show_emergency', 'no', {expires: _hour_from_now()}); 
			});
		});
	};
};

/**
 * Creates a date object for an hour from the current browser's time
 * @returns Date object for an hour from now
 * @type Object
 * @see #emergency
 */
var _hour_from_now = function() {
	var now = new Date();
	return now.setHours(now.getHours() + 1);
};

$(document).ready(function() {
/*$("div.aside div.content ul:not(:has('li'))").parent().parent().parent().hide();
$("div.content ul:not(:has('li'))").prev().hide();*/
$('#section-nav > ul > li:first-child > a').trigger('click');
	placeholder();
	$('div.image-left, div.image-right').imageWidth();
	$('span.pullquote-left, span.pullquote-right').pullQuote();
	
	markupPrep();
	slideshow();
	flow_fader();
	scrollable();
	section_navigation();
	emergency();
	
	setTimeout(function() {same_height();}, 25);
	setTimeout(function() {move_images();}, 30);

});

var lft = $('.offices-block').children(".left").length;
var rgt = $('.offices-block').children(".right").length;
var cnt = $('.offices-block').children(".center").length;
if (lft==0&&rgt==0&&cnt==0){
$('.offices-block').detach();
}
