/**
 * Javascript for Default Theme
 */
jQuery(function($) {
	$.fn.tallest = function()       { return this._extremities({ 'aspect' : 'height', 'max' : true  })[0] };
	$.fn.tallestSize = function()   { return this._extremities({ 'aspect' : 'height', 'max' : true  })[1] };
	$.fn.shortest = function()      { return this._extremities({ 'aspect' : 'height', 'max' : false })[0] };
	$.fn.shortestSize = function()  { return this._extremities({ 'aspect' : 'height', 'max' : false })[1] };
	$.fn.widest = function()        { return this._extremities({ 'aspect' : 'width',  'max' : true  })[0] };
	$.fn.widestSize = function()    { return this._extremities({ 'aspect' : 'width',  'max' : true  })[1] };
	$.fn.thinnest = function()      { return this._extremities({ 'aspect' : 'width',  'max' : false })[0] };
	$.fn.thinnestSize = function()  { return this._extremities({ 'aspect' : 'width',  'max' : false })[1] };

	/**
	 *	Returns an array: the first item is the matched element, and the second item is the dimension
	 */
	$.fn._extremities = function(options) {
		var defaults = {
			aspect : 'height', // or 'width'
			max : true	// or false to find the min
		};
		options = $.extend(defaults, options);

		if (this.length < 2) {
			return [this, this[options.aspect]()];
		}

		var bestIndex = 0,
			bestSize = this.eq(0)[options.aspect](),
			thisSize
		;
		for (var i = 1; i < this.length; ++i) {
			thisSize = this.eq(i)[options.aspect]();
			if ((options.max && thisSize > bestSize) || (!options.max && thisSize < bestSize)) {
				bestSize = thisSize;
				bestIndex = i;
			}
		}

		return [ this.eq(bestIndex), bestSize ];
	};
});

$(document).ready(function() {
	$("ul.sf-menu").supersubs({
		minWidth:    12,                                // minimum width of sub-menus in em units
		maxWidth:    20,                                // maximum width of sub-menus in em units
		extraWidth:  1                                  // extra width can ensure lines don't sometimes turn over
	}).superfish({
		delay:       400,                               // delay on mouseout
		animation:   {opacity:'show',height:'show'},    // fade-in and slide-down animation
		speed:       'fast',                            // faster animation speed
		autoArrows:  false,                             // disable generation of arrow mark-up
		dropShadows: false                              // disable drop shadows
	});
	
	$('body').append('<div id="galleryCache" style="display: none;"></div>');
	
	$('a[class=modal], a[rel=lightbox]').colorbox();
	
	$('a[class=gallery]').bind('click', function(e) {
		var galleryLink = $(this);
		
		$.ajax({
			url: galleryLink.attr('href'),
			type: 'GET',
			dataType: 'html',
			success: function(data) {
				$('#galleryCache').html(data);
				
				$('#galleryCache a').colorbox();
				$('#galleryCache').find('a:first').trigger('click');
			}
		});
		
		e.preventDefault();
	});
	
	$('.flourish').each(function(i) {
		this.innerHTML = '<span>'+this.innerHTML+'</span>'
	});
});

$(document).bind('cbox_complete', function(e) {
	var el = $.colorbox.element();
	
	if (el.attr('title') == 'undefined' || el.attr('title') == '') {
		$('#cboxTitle').hide();
	}
});
