// aOverrides is called from aUI()
// This helps for things like Cufon that need to be setup again after an AJAX call
function aOverrides()
{
	Cufon.refresh();
}

function aSlideshowAsBackgroundImages(slideshowItems, params, callback)
{
	if (typeof slideshowItems == 'string') {
		slideshowItems = $(slideshowItems); // If a string is passed in, try to use it as a selector
	};
	if (typeof slideshowItems == 'object') {
		// if we have an object, let's do this!
		slideshowItems.each(function(){
			var item = $(this);
			var imageContainer = item.find('li.a-slideshow-image');
			var image = imageContainer.children('img');
			var imgsrc = image.attr('src');
			var imgh, imgw;
			if (!params['height']) { imgh = image.attr('height'); } else { imgh = params['height'] };
			if (!params['width']) { imgw = image.attr('width'); } else { imgw = params['width'] };
			image.css('visibility','hidden');
			imageContainer.css({
				'height': imgh+'px',
				'width': imgw+'px',
				'background': 'url(' + imgsrc + ') top left no-repeat'
			});

			if (params['corner']) {
				imageContainer.corner(params['corner']);
			};

			if (params['dropshadow']) {
				imageContainer.addClass('dropshadow');
			};
		});

		if (typeof callback == 'string') {
			callback = eval(callback);
			callback(slideshowItems);
	 	};

	}
	else
	{
		return false;
	};
}

function homeBanner(slideshowItems)
{
	slideshowItems.each(function(){
		var item = $(this);
		var imageDescription = item.find('li.a-slideshow-description');
		var imageTitle = item.find('li.a-slideshow-title');
		imageTitle.wrapInner(document.createElement("h4")).append(imageDescription);
	});
}

function aInsetSlots()
{
	// Builds a slug for RichText slots that follow an inset slot or callout slots in a Callout Template
	// The slug is sized to match the slot preceding the RichText slot, giving the richtext an element to wrap around
	var insetSlots = $('.a-slot.inset_left, .a-slot.inset_right, .a-focus .a-slot.focus');
	$('.inset_slug, .callout_slug').remove();
	insetSlots.each(function(){
		thisSlot = $(this);
		nextSlot = thisSlot.next();
		if (nextSlot.hasClass('aRichText')) {
			insetClass = (thisSlot.hasClass('inset_left'))? 'inset_slug left':'inset_slug right';
			(thisSlot.hasClass('focus'))? insetClass = 'callout_slug':'';
			insetSlug = $('<div>');
			insetSlug.css({
				width: thisSlot.width(),
				height: thisSlot.height()
			}).addClass(insetClass).html('&nbsp;');
			nextSlot.addClass('has-inset-content').find('.a-slot-content').prepend(insetSlug);
			nextSlot.css('z-index',1); /* Because the inset slot precedes the RichText slot, z-index must be set */
			thisSlot.css('z-index',2);
		};
	});
}

function fmQuickLinks()
{
	ql = $('#quicklinks');
	ql.mouseenter(function(){
		ql.find('ul').css('display','block');
	}).mouseleave(function(){
		ql.find('ul').css('display','none');
	});
}

function fmLoginLink()
{
	$('#fandm-login-button').unbind('click.fm').bind('click.fm',function(e){
		e.preventDefault();
		$('#a-login-form-container').fadeIn();
		$('#signin_username').focus();
		$('.a-login-cancel-button').unbind('click.fm').bind('click.fm',function(e){
			e.preventDefault();
			$('#a-login-form-container').fadeOut();
		})
	});
}

function fmFocusSlots()
{
	// the Focus template slideshows need to adjust the inset slug when they advance
	$('.a-slot.aSlideshow.focus .a-arrow-left, .a-slot.aSlideshow.focus .a-arrow-right, .a-slot.aSlideshow.focus .a-slideshow-image').unbind('click.fm').bind('click.fm', function(){
		var slideshow = $(this).parents('.aSlideshow');
		var slug = slideshow.next().find('div.callout_slug');
		slug.css('height',slideshow.height());
	});
}
