//Run jQuery in no conflict mode
jQuery.noConflict();


//Run when the document is loaded
jQuery(document).ready(function()
{
	//Activate sIFR
	var animatedGothic = { src: 'http://www.avantipress.com/templates/avanti/js/animated-gothic.swf' };
	sIFR.activate(animatedGothic);
	sIFR.replace(animatedGothic, 
	{
	  selector: 'h1',
	  css: '.sIFR-root {color: #FFFFFF;}',
	  wmode: 'transparent'
	});
	
	
	//Run jQuery Cycle
	jQuery('.slider').cycle(
	{
		fx: 'fade',
		cssBefore: {
			zIndex: 0
		},
		cssAfter: {
			zIndex: -1
		},
		pause: 1
	});
	
	
	//Add a listener for the drop downs
	jQuery('#primaryNav ul.menu li.item53, #primaryNav ul.menu li.item54, #primaryNav ul.menu li.item55, #primaryNav ul.menu li.item56').mouseover(function()
	{
		jQuery(this).setDropDown();
	});
	jQuery('#primaryNav .drop').hover(
		function()
		{
			
		},
		function()
		{
			jQuery(this).clearDropDown();
		}
	);
	jQuery('#primaryNav ul.menu li.item1').mouseover(function()
	{
		jQuery(this).clearDropDown();
	});
	
	
	//Remove the underline from the last item in the secondary nav
	jQuery('#secondaryNav ul.menu li:last').each(function()
	{
		if (jQuery(this).children('ul').length)
		{
			jQuery(this).children('ul li:last').children('a').css('border-bottom', '0px');
		}
		else
		{
			jQuery(this).children('a').css('border-bottom', '0px');
		}
	});
	
	
	//Dynamically position the character
	jQuery(window).scroll(function()
	{
		jQuery(this).posCharacter();
	});
	
	jQuery(window).resize(function()
	{
		jQuery(this).posCharacter();
	});
	
	jQuery(window).load(function()
	{
		jQuery(this).posCharacter();
	});

	
	//Set the min-height of the article
	jQuery('#article').css('min-height', (jQuery('#leftColumn').height() - jQuery('#mainContent .buttons').height())+ 'px');


	//Toggle the text in the search area
	jQuery('#mainSearch .text').focus(function()
	{
		if (jQuery(this).val() == 'search Avanti')
		{
			jQuery(this).val('');
			jQuery(this).addClass('active');
		}
	});
	jQuery('#mainSearch .text').blur(function()
	{
		if (jQuery(this).val() == '')
		{
			jQuery(this).val('search Avanti');
			jQuery(this).removeClass('active');
		}
	});
	
	
	//Add listeners to catalogs
	jQuery('body #article a.lbCatalog').live('click', function()
	{
		var path = '<a class="close"></a><iframe id="iframeMyCatalog" name="iframeMyCatalog" align="middle" src="' + jQuery(this).attr('href') + '&Window=Medium&Embedded=true" style="border:solid 1px #999999; width:800px; height:604px; overflow:hidden; background-color:#ffffff" frameBorder="no">Your browser does not support inline frames or is currently configured not to display inline frames.</iframe>';
		jQuery(this).lightBox(path);
		return false;
	});
});




//Position the character
(function($)
{
	$.fn.posCharacter = function()
	{
		return this.each(function()
		{
			//Get the size of the content window and viewport
			var content = $('#mainContent').height();
			var wrapper = $('#wrapper').height();
			var offset = wrapper - content + $('#footer').height();
			var viewport = $(window).height();
			var charHeight = $('#character').height();
			var character = $('#character').css('bottom').replace('px', '');
			var top = $(window).scrollTop();
			var article = $('#article').height();
			
			
			//If the height of the content is greater than the viewport
			if (wrapper > viewport && character >= 0)
			{
				var bot = content - top - viewport + charHeight - offset + 80;
				
				//Make sure we're not taking the image out of the article
				if (charHeight + bot < article)
				{
					$('#character').css('bottom', bot);
				
					if ($('#character').css('bottom').replace('px', '') < 0)
						$('#character').css('bottom', '0px');
				}
				else
				{
					var bottom = article - charHeight;
					$('#character').css('bottom', bottom);
				}
			}
		});
	};
})(jQuery);




//Take the given element and make it a drop down menu
(function($)
{
	$.fn.setDropDown = function()
	{
		return this.each(function()
		{
			var top = $('#primaryNav .drop .top').height();
			var width = '';
			var innerWidth = '';
			
			
			//Set the width based on the class
			if ($(this).hasClass('item55'))
			{
				width = 150;
				innerWidth = 120;
			}
			else if ($(this).hasClass('item56'))
			{
				width = 115;
				innerWidth = 85;
			}
			else
			{
				width = Number($(this).width()) + (Number($('#primaryNav .drop .top .left').width() * 2));
				innerWidth = Number($(this).width());
			}
			
			//Get how far to the left the dropdown should be
			var left = Number($(this).position().left) + (Number($(this).css('margin-left').replace('px', '')) + Number($('#primaryNav .drop .top .left').width())) - ((width - $(this).width()) / 2);
			//Set the styles on
			$('#primaryNav .drop').css({
											'display': 'block',
											'visibility': 'hidden',
											'left': left,
											'top': -top,
											'width': width
									   });
			
			$('#primaryNav .drop .center, #primaryNav .drop .content').width(innerWidth);
			
			//Replace the html with this content
			$('#primaryNav .drop .content').html("<a href='" + $(this).children('a:first').attr('href') + "' class='" + $(this).attr('class') + "'></a><ul>" + $(this).children('ul').html() + "</ul>");
			$('#primaryNav .drop .middle').css('height', $('#primaryNav .drop .middle .content ul').height() + Number($('#primaryNav .drop .middle .content ul').css('margin-top').replace('px', '')));
			$('#primaryNav .drop .middle .content ul li a:last').css('background-image', 'none');
			
			$('#primaryNav ul li a').removeClass('active');
			$(this).children('a').addClass('active');
			
			$('#primaryNav .drop').css({
											'display': 'none',
											'visibility': 'visible'
									   });
			
			$('#primaryNav .drop').show();
		});
	};
})(jQuery);




//Clear the drop downs
(function($)
{
	$.fn.clearDropDown = function()
	{
		return this.each(function()
		{
			$('#primaryNav .drop').hide();
			$('#primaryNav ul li a').removeClass('active');
		});
	};
})(jQuery);







//Set the lightbox
(function($)
{
	$.fn.lightBox = function(data)
	{
		return this.each(function()
		{
			//Set the lb content
			$('#lightBox .lbContent .body .center').html(data);
			
			
			//Make the lbContent visible
			$('#lightBox .lbContent').show();
			
			
			//Set the width of the middle of the box
			jQuery('#lightBox .lbContent').width(jQuery('#lightBox .body .center').width() + 48);
			jQuery('#lightBox').width(jQuery('#lightBox .body .center').width() + 48);
			jQuery('#lightBox .top .center, #lightBox .bot .center').width(jQuery('#lightBox .body .center').width() + 15);
						
									
			//Reposition the lightbox
			var left = ((jQuery('#container').width() - jQuery('#lightBox').width()) / 2) + 14;
			var top = jQuery('#mainContent').offset().top + ((jQuery('#mainContent').height() - jQuery('#lightBox').height()) / 2);
			jQuery('#lightBox').css('top', top);
			jQuery('#lightBox').css('left', left);
						
						
			//Show the lightbox and the background
			jQuery('#lightBox').css('visibility', 'visible');
			jQuery('#grayOut').show();
			
			
			//Add a listener to the close buttons
			jQuery('#lightBox .close, #grayOut').click(function()
			{
				//Hide the lightbox and the grayOut
				jQuery('#lightBox').css('visibility', 'hidden');
				jQuery('#grayOut').hide();
				
				//Remove this event listeners
				jQuery('#lightBox .close, #grayOut').unbind('click');
								
				return false;
			});	

		});
	};
})(jQuery);
