$(document).ready(function() {
    

	// ----------------------------------------------------------------------	
	//  Main navigation drop-down menus
	// ----------------------------------------------------------------------
	
	$('ul.sf-menu').supersubs({
		minWidth: 	12,
		maxWidth: 	27,
		extraWidth: 1
	}).superfish({
		delay:       400,
		animation:   {opacity:'show', height:'show'},
		speed:       'fast',
		autoArrows:  false,
		dropShadows: true
	});


	// ----------------------------------------------------------------------	
	//  Toggle default text in search fields
	// ----------------------------------------------------------------------
	
	$('#keywords')
	.focus(function() {
		if( this.value == this.defaultValue ) this.value = "";
	})
	.blur(function() {
		if( !this.value.length ) this.value = this.defaultValue;
	});
	
	
	// ----------------------------------------------------------------------	
	//  Auto captions
	// ----------------------------------------------------------------------
	
	$('#content .article img').jcaption({ 
		wrapperClass : 'photo'
	});
	
	
	// ----------------------------------------------------------------------	
	//  Open external links in new window
	// ----------------------------------------------------------------------
	
	$("a[href^=http]").each(function() {
		if(this.href.indexOf(location.hostname.replace(/www./gi, "")) == -1) {
			$(this).click(function() { window.open(this.href, "_blank"); return false; });
		}
	})
	
	
	// ----------------------------------------------------------------------	
	//  Auto-height main column
	// ----------------------------------------------------------------------
	
	var sidebarHeight = 0;
	var contentHeight = $('#content').outerHeight();
	
	$('.sidebar').each(function(){
		if($(this).outerHeight() > sidebarHeight) sidebarHeight = $(this).outerHeight();
	});
	
	if(contentHeight < sidebarHeight) {
	
		var diff = sidebarHeight - contentHeight;
		
		// add height to last inner content div
		var $elem = $('#content .module:last');
		//$elem.height($elem.height()+diff);
	}


	// ----------------------------------------------------------------------	
	//  Slideshows - requires the Cycle plugin
	// ----------------------------------------------------------------------
	
	// add slides to slideshow dynamically 
    $('#slideshow-thumbs ul li > a').each(function(i) {
		$(this).click(function() { return false; });
		if(i>0) {
			$('#slideshow-img').append('<img src="'+$(this).attr("href")+'" title="'+$(this).children('img:first').attr("alt")+'" style="display: none" />');
		}
		
    });
    
    // set up navigation
    if($('#slideshow-thumbs > ul li').length > 5) {
		
		// break up thumbs into pages
		$('#slideshow-thumbs > ul li').wrapInChunks('<ul />', 5).appendTo($('#slideshow-thumbs ul'));
		$('#slideshow-thumbs > ul ul').unwrap(); // un-nest
		
		// add prev/next
		$('<div id="slideshow-nav" />')
		.append($('<a id="slideshow-prev" href="#">Previous</a>').click(function(){return false;}))
		.append($('<a id="slideshow-next" href="#">Next</a>').click(function(){return false;}))
		.appendTo('#slideshow');
		
		// page the pager
		$('#slideshow-thumbs').cycle({
			fx: 'scrollHorz',
			timeout: 0,
			next: '#slideshow-next',
			prev: '#slideshow-prev'
		});
    }

    //initialize
    $('#slideshow-img').cycle({
        fx : 'fade',
        timeout : 0,
        pager: '#slideshow-thumbs',
        pagerAnchorBuilder: function(idx, slide) { return '#slideshow-thumbs a:eq(' + idx + ')'; },
        after: function() { $('#slideshow-caption').html($(this).attr("title")); },
        before: function() {
        	$(this).css({ left: '50%', marginLeft: -$(this).width()/2}); // center horizontally
        }
    });

});


// ----------------------------------------------------------------------	
//  Utility
// ----------------------------------------------------------------------

// Wrap in chunks - http://bit.ly/aX6kBt
jQuery.fn.wrapInChunks = function(html, chunkSize) {

    chunkSize = chunkSize || 1;

    var items = this.get(),
        rows = [],
        cur = rows[0] = $(html);

    while (items[0]) {
        if (rows[rows.length - 1].children().length === chunkSize) {
            cur = rows[rows.length] = $(html);
        }
        cur.append( items.shift() );
    }

    return this.pushStack(rows);

};



