$(document).ready(function() {
    var obj = null;
	// Add spans into h3s to create tab shapes
    obj = $('.content #content_main h3');
	if( obj && obj !== undefined )
    {
        obj = obj.not('#category_spotlight h3');
        if( obj && obj !== undefined )
        {
            obj = obj.not('#description h3');
            if( obj && obj !== undefined )
            {
                obj.wrapInner('<span></span>');
            }
        }
    }

	obj = $('#info h5');
	if( obj && obj !== undefined )
    {
    	obj = obj.not('h5#highlight');
    	if (obj && obj !== undefined)
    	{
        	obj.wrapInner('<span></span>');
        }
    }
    
    obj = $('#info h5#highlight');
    if( obj && obj !== undefined ){
    	obj.wrapInner('<span style="background:rgb(255, 0, 0);"></span>');
    }
	
	// Add span inside content page header h2 to allow for gloss background
    obj = $('.content #content_main h2');
	if( obj && obj !== undefined )
    {
        obj.wrapInner('<span></span>');
    }
	
	// Add an inner div to the category spotlight to create the double-border effect
    obj = $('#category_spotlight');
	if( obj && obj !== undefined )
    {
        obj.wrapInner('<div class="inner"></div>');
    }


	// If the rounded.js file has been included, then round all the corners.
	if(typeof(rounded) != 'undefined' && rounded == true) {
        obj = $('#info h5 span');
        if( obj && obj !== undefined ) obj.corner('#fff 5px TR TL');
        obj = $('#content_sub > ul > li');
        if( obj && obj !== undefined )
        {
            obj = obj.not('.advertisement');
            if( obj && obj !== undefined ) 
			{
				obj = obj.not('#calendarsearch-container');
				if( obj && obj !== undefined ) 
				{
					obj.corner('10px');
				}
			}
        }
        obj = $('#main_nav');
        if( obj && obj !== undefined ) obj.corner('10px TR TL');
        obj = $('#home_featured_events');
        if( obj && obj !== undefined ) obj.corner('10px');
        obj = $('#home_featured_events > ul > li');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('#content #maincolumnlistA > li');
        if( obj && obj !== undefined ) obj.corner('10px');
        obj = $('#content #maincolumnlistB > li');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('#site_info > ul > li');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('.content #content_main');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('#meta_links');
        if( obj && obj !== undefined )
        {
            obj.wrap('<div class="meta_wrap"></div>');
            obj.corner('10px TR TL');
        }
		obj = $('#category_spotlight');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('#category_spotlight .inner');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('.content #content_main h3 span');
        if( obj && obj !== undefined ) obj.corner('5px TR TL');
		obj = $('#info > li');
        if( obj && obj !== undefined ) obj.corner('10px');
		obj = $('#main_nav li.selected > a');
        if( obj && obj !== undefined ) obj.corner('10px TR TL');
		obj = $('#home_spotlight');
        if( obj && obj !== undefined ) obj.corner('8px');
	}

// Add 'selected' class to top-level LI when it is clicked. This will make the 2nd & 3rd level menus for
	// that item appear and will make the tab appear selected.
    obj = $('#main_nav > li > a');
    if( obj && obj !== undefined ) 
    {
        obj.click(function() {
            var obj = null;
     		if(typeof(rounded) != 'undefined' && rounded == true) {
                obj = $('#main_nav .selected a');
    			if( obj && obj !== undefined ) obj.corner('destroy'); /* Get rid of the rounded corner */
    		}
            obj = $('#main_nav li');
            if( obj && obj !== undefined ) obj.removeClass('selected');
            obj = $(this).parent();
    		if( obj && obj !== undefined ) obj.addClass('selected');
     		if(typeof(rounded) != 'undefined' && rounded == true) {
                obj = $('#main_nav .selected > a');
    			if( obj && obj !== undefined ) obj.corner('10px TR TL'); /* Add the rounded corner */
    		}
    		return false;
    	});
     }

	// Create tabbed navigation on detail pages info section
    obj = $('#info');
    if( obj && obj !== undefined ) 
    {
        obj.semantictabs({
            panel:'#info > li',         //-- Selector of individual panel body
            head:'h4',           //-- Selector of element containing panel header
            active:':first',            //-- Which panel to activate by default
            activate:false             //-- Argument used to activate panel programmatically
        });
    }
	
	// If the rounded.js file has been included, then round all the corners.
	if(typeof(rounded) != 'undefined' && rounded == true) {
        obj = $('.semtabs');
        if( obj && obj !== undefined ) obj.corner('10px TR TL');
		obj = $('.semtabs li.selected');
        if( obj && obj !== undefined ) obj.corner('#fff 10px TR TL');
	}

	// Hide full content span and add an ellipsis and a link to expand the full content
	obj = $('.full_content');
    if( obj && obj !== undefined )
    {
        obj = obj.after('<span class="ellipsis">...</span> <a href="javascript:void(0)" class="expand">Expand</a>')
        if( obj && obj !== undefined ) obj.hide();
    }

    obj = $('a.expand');
    if( obj && obj !== undefined )
    {
        obj.toggle( function() {
            var obj = $(this).parent();
    		if( obj && obj !== undefined )
            {
                obj = obj.children('.ellipsis');
                if( obj && obj !== undefined ) obj.hide();
            }
            obj = $(this).parent();
    		if( obj && obj !== undefined )
            {
                obj = obj.children('.full_content');
                if( obj && obj !== undefined ) obj.show();
            }
            $(this).removeClass('expand').addClass('collapse').text('Collapse');
        }, function() {
            var obj = $(this).parent();
    		if( obj && obj !== undefined )
            {
                obj = obj.children('.ellipsis');
                if( obj && obj !== undefined ) obj.show();
            }
            obj = $(this).parent();
    		if( obj && obj !== undefined )
            {
                obj = obj.children('.full_content');
                if( obj && obj !== undefined ) obj.hide();
            }
    		$(this).removeClass('collapse').addClass('expand').text('Expand');
        });
    }
	
	// Set the when input field to use the jquery UI datepicker.
    obj = $('#when');
    if( obj && obj !== undefined )
	{
        obj.datepicker({ 
    		showOn: 'both', 
    		buttonImage: '/images/icon-calendar.gif', 
    		buttonImageOnly: true, 
    		onSelect: function(dateText, inst) {
                var obj = $(this).parents('form');
                if( obj && obj !== undefined ) obj.submit();
            } 
    	});
    }

	// #dateBegin and #dateEnd are fields from the events advanced search form ( /advanced_search )
    obj = $('#dateBegin');
    if( obj && obj !== undefined )
    {
        obj.datepicker({ 
    		showOn: 'both', 
    		buttonImage: '/images/icon-calendar.gif', 
    		buttonImageOnly: true
    	});
    }
    obj = $('#dateEnd');
    if( obj && obj !== undefined )
    {
    	obj.datepicker({ 
    		showOn: 'both', 
    		buttonImage: '/images/icon-calendar.gif', 
    		buttonImageOnly: true
    	});
    }

	// Add popup preview balloons to line listings on category pages
    obj = $('.line_listing td a');
    if( obj && obj !== undefined )
    {
    	obj.each(function() {
            if( $(this) )
            {
                $(this).bt({
                    contentSelector: "$('#' + $(this).attr('rel')).html()",
        			fill: '#FFF',
        			cornerRadius: 10,
        			strokeWidth: 1,
        			strokeStyle: '#999',
        			positions:['top','bottom','right','left'],
        			width: '360px',
        			centerPointX:.6,
        			centerPointY:.5,
        			killTitle: true,
        			shadow: true,
        			shadowOffsetX: 2,
        			shadowOffsetY: 2,
        			shadowBlur: 3,
        			shadowColor: "#000",
        			shadowOverlap: false,
                    closeWhenOthersOpen: true,
        			noShadowOpts: {strokeStyle: '#000'},
        			windowMargin: 0
                });
            }
    	});
    }

	// Add popup preview balloons to search event results on search/results pages
    obj = $('table#search_event_results td a');
    if( obj && obj !== undefined )
    {
        obj.each(function() {
            if( $(this) )
            {
                $(this).bt({
                    contentSelector: "$('#' + $(this).attr('rel')).html()",
        			fill: '#FFF',
        			cornerRadius: 10,
        			strokeWidth: 1,
        			strokeStyle: '#999',
        			positions:['top','bottom','right','left'],
        			width: '360px',
        			centerPointX:.6,
        			centerPointY:.5,
        			killTitle: true,
        			shadow: true,
        			shadowOffsetX: 2,
        			shadowOffsetY: 2,
        			shadowBlur: 3,
        			shadowColor: "#000",
        			shadowOverlap: false,
                    closeWhenOthersOpen: true,
        			noShadowOpts: {strokeStyle: '#000'},
        			windowMargin: 0
                });
            }
        });
    }
	
	// Add class="odd" to every other tr in line listing tables
    obj = $('.line_listing tr:even');
    if( obj && obj !== undefined ) obj.addClass('even');

    if( $.browser.msie )
    {
        obj = $('.image_listing tr:last-child td');
        if( obj && obj !== undefined ) obj.css({'padding-bottom':'0'});
    }

	// Check the width of the 2nd level nav. If it is too wide, add line breads and the "two-line" class to menu items until it is narrow enough.
	obj = $('#main_nav > li > ul');
	if( obj && obj !== undefined )
		obj.each(function() 
		{
			var currentNav = $(this);
			var currentNavWidth = getNavWidth($(this));
			if (currentNavWidth>990) 
			{
				// Get the overage in width
				var widthDifference = currentNavWidth - 990;

				// Find all 2nd level nav items that have ampersands in them.
				var navItemsWithAmps = $(this).children('li:contains("&")').length;
				var amountToSubtract = widthDifference/navItemsWithAmps;

				// Insert a break after the ampersand and add a class of "two_line".
				$(this).children('li:contains("&")').each(function() 
				{
					$(this).children('a').each(function()
					{
						if (getNavWidth(currentNav)<970){
							return;
						}
						$(this).html($(this).html().replace('&amp;','&amp;<br />')).addClass('two_line');
					});
				});
			}
		
			// If the nav is still too wide, insert breaks and add "two_line" class into any menu items with spaces.
			if (getNavWidth($(this))>990)
			{
				$(this).children('li:contains(" ")').not('li:contains("&")').each(function()
				{
					$(this).children('a').each(function()
					{
						if (getNavWidth(currentNav)<990){
							return;
						}
						$(this).html($(this).html().replace(' ','<br />')).addClass('two_line');
					});
				});
			}

			// If it's still too wide, pack the last few LIs into a "more" menu.
			if (getNavWidth($(this))>990)
			{
				// Create "more" LI.
				$(this).append('<li class="more_tabs"><a href="javascript:void(0);">More</a><ul class="more_tabs"></ul></li>');

				// Find all the LIs and put them in a variable.
       	     		var allListItems = $(this).children('li');

				// For each list item, take the last one in the list and append it to the "more" dropdown.
				var i=0;
				for(i=allListItems.length-2; i>0; i--) 
				{
					if (getNavWidth(currentNav)<990){
						return;
					}
					allListItems.eq(i).children('a').removeClass('two_line').children('br').replaceWith(' ');
			   		currentNav.find('ul.more_tabs').append(allListItems.eq(i));
				}
			}
		});

	obj = $('#main_nav > li > ul');
	if( obj && obj !== undefined )
	{
		obj = obj.children('li:has(br)');
		if( obj && obj !== undefined )
		{
			obj = obj.children('a');
			if( obj && obj !== undefined ) obj.css({'line-height':'14px','padding-top':'5px','padding-bottom':'5px'});
		}
	}

	function getNavWidth(nav)
	{
		var navWidth = 0;
		nav.children('li').each(function() 
		{
			navWidth += $(this).width();
		});
		return navWidth;
	}

	// Add a class of 'dropdown' to any top nav items that have subcategories
	obj = $('#main_nav li li ul');
	if( obj && obj !== undefined )
	{
		obj = obj.parent();
		if( obj && obj !== undefined )
		{
			obj = obj.addClass('dropdown');
			if( obj && obj !== undefined )
			{
				obj = obj.children('a');
				if( obj && obj !== undefined ) obj.wrapInner('<span></span>');
			}
		}
	}
});

function submitForm ( frm, btn )
{
	if (btn && document.forms[frm].which_button) document.forms[frm].which_button.value = btn;
	document.forms[frm].submit();
}