/*
jQUERY
--------------------------------------------------*/
jQuery(document).ready(function() {
	
	jQuery('body.home div#slideshow').cycle({
		fx: 'fade',
		speed: 2000, 
		timeout: 5000,
		pause: 1,
		before: onBefore,
		after: onAfter
	});
	
	// NAVIGATION HOVER/DROP-DOWN
	jQuery('div#navigation ul > li').hover(
		function() {
			$this = jQuery(this);
			
			$this.css('z-index',400);
			
			jQuery('ul',this).append('<div class="arrow"></div>');
			jQuery('ul',this).css({
				top: -4,
				left: $this.width() + 13
			});
		},
		function() {
			$this = jQuery(this);
			
			$this.css('z-index',100);
			
			jQuery('div.arrow',this).remove();
			jQuery('ul',this).css({
				top: -9999,
				left: -9999
			});
		}
	);
	
	// HEADER CAPTION LINK
	jQuery('a#secondary-caption-link').toggle(
		function() {
			jQuery('div#slideshow').append('<div id="caption">' + jQuery(this).html() + '</div>');
			
			return false;
		},
		function() {
			jQuery('div#caption').remove();
			
			return false;
		}
	);
	
	// FIX CAPTION CONTAINER WIDTH
	jQuery('div.wp-caption').each(function() {
		jQuery(this).css('width', jQuery('img',this).width());
	});
	jQuery('p.wp-caption-text').toggle(
		function() {
			$thisParent = jQuery(this).closest('div');
			
			$thisParent.append('<div class="caption">' + jQuery(this).html() + '</div>');
			jQuery('div.caption',$thisParent).css('width', jQuery('img',$thisParent).width() - 55);
		},
		function() {
			$thisParent = jQuery(this).closest('div');
			
			jQuery('div.caption',$thisParent).remove();
		}
	);
		
});

function onBefore() {
	jQuery('div#slideshow div#caption').remove();
}
function onAfter() {
	if (this.alt != '') {
		jQuery('div#slideshow a#caption-link').remove();
		jQuery('div#slideshow').append('<a href="#" id="caption-link">' + this.alt + '</a>');
		
		// CAPTION LINK
		jQuery('a#caption-link').toggle(
			function() {
				jQuery('div#slideshow').append('<div id="caption">' + jQuery(this).html() + '</div>');
				
				return false;
			},
			function() {
				jQuery('div#caption').remove();
				
				return false;
			}
		);
	} else {
		jQuery('div#slideshow a#caption-link').remove();
	}
}
