$(document).ready(function() {

	// Toggle default text in login and search fields
	$('#keywords')
	  .focus(function() {
		if( this.value == this.defaultValue ) this.value = "";
	}).blur(function() {
		if( !this.value.length ) this.value = this.defaultValue;
	});
	
	// Home page slideshow
	if($('#viewer').length) {
	    $('#viewer').cycle({ 
	        fx:     'fade', 
	        speed:   800, 
	        timeout: 10000, 
	        pager:  '#viewer-nav' 
	    });
	}
	
	// Main Navigation dropdowns
    $('ul#nav').superfish({ 
        hoverClass:  'current',
        delay:       700,
        animation:   {'opacity':'show','height':'show'},
        speed:       'fast', 
        autoArrows:  false,
        dropShadows: false
    }); 
	
	// Smooth-scroll internal links
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
			var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
			var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($target) {
				var targetOffset = $target.offset().top;
				$(this).click(function() {
					$('html, body').animate({scrollTop: targetOffset}, 1000);
					return false;
				});
			}
		}
	});
	
	// Home Page - Make callout columns behave like valigned table cells
    $('#callouts > div').each(function(){
        // Check dimensions
        var ph = $(this).parent().height();
        var ch = $(this).outerHeight();
        var $kids = $(this).children();
        var diff = ph - ch;
        var adj = diff / $kids.length;
        // Adjust only the short column
        if(diff > 0) {
            $kids.each(function(){
                // Distribute height
                var h = $(this).height();
                $(this).height(h+adj);
                // Vertical align within new height
                $wrap = $(this).children().wrapAll('<div />').parent();
                var oh = $(this).outerHeight();
                var ih = $wrap.height();
                $wrap.css('padding-top', (oh-ih)/2 + 'px');
            });
        }
        // Ensure the class of "last" is added to the last callout div
        $(this).find(":last-child").addClass('last');
    });
});

$(window).load(function() {
	
	// Automatic photo captions
	$("img.photo-right, img.photo-left, img.photo-center, img.photo").each(function() {	
		var title = $(this).attr('title');
		if (title) {
			var imgClass = 'photo';
			if($(this).hasClass('photo-right')) imgClass = 'photo-right';
			if($(this).hasClass('photo-left')) imgClass = 'photo-left';
			if($(this).hasClass('photo-center')) imgClass = 'photo-center';
			$(this).removeClass(imgClass).addClass('photo');
			// Create wrapper and caption
			$wrapper = $('<div>').addClass(imgClass).width($(this).width()+10);
			$('<div>').addClass('caption').html(title).appendTo($wrapper);
			// Check if parent is link and replace
			var parent = $(this).parent().get(0);
			if(parent.tagName.toUpperCase() == 'A') {
				$(parent).clone().prependTo($wrapper);
				$(parent).replaceWith($wrapper);
			} else {
				$(this).clone().prependTo($wrapper);
				$(this).replaceWith($wrapper);
			}
		}
	});
	
});
