$(document).ready(function () {
	
	// Add default text to search boxes
	$('#search_section input[type="text"]').each(function(){
 
    this.value = $(this).attr('title');
    $(this).addClass('text-label');
 
    $(this).focus(function(){
        if(this.value == $(this).attr('title')) {
            this.value = '';
            $(this).removeClass('text-label');
        }
    });
 
    $(this).blur(function(){
        if(this.value == '') {
            this.value = $(this).attr('title');
            $(this).addClass('text-label');
        }
    });
});
	
    // Add spans into h3s to create tab shapes
    $('.content #content_main h3').not('#category_spotlight h3').not('#description h3').wrapInner('<span></span>');

    $('#info h5').not('h5#highlight').wrapInner('<span></span>');

    $('#info h5#highlight').wrapInner('<span style="background:rgb(255, 0, 0);"></span>');

    // Add span inside content page header h1 to allow for gloss background
    $('.content #content_main h1').wrapInner('<span></span>');

    // Add an inner div to the category spotlight to create the double-border effect
    $('#category_spotlight').wrapInner('<div class="inner"></div>');


    // If the rounded.js file has been included, then round all the corners.
    if (typeof(rounded) != 'undefined' && rounded == true) {
        $('#info h5 span').corner('#fff 5px TR TL');
        $('#content_sub > ul > li').not('.advertisement').not('#calendarsearch-container').corner('10px');
        $('#content_sub > ul > li li').corner('destroy');
        $('#info_bar').corner('10px BR BL');
        $('#main_nav').corner('10px TR TL');
        $('#home_featured_events').corner('10px');
        $('#home_featured_events > ul > li').corner('10px');
        $('#home_featured_events > ul > li ul').corner('destroy');
        $('#content #maincolumnlistA > li').corner('10px');
        $('#content #maincolumnlistB > li').corner('10px');
        $('#site_info > ul > li').corner('10px');
        $('.content #content_main').corner('10px');
        $('#meta_links').wrap('<div class="meta_wrap"></div>').corner('10px TR TL');
        $('#category_spotlight').corner('10px');
        $('#category_spotlight .inner').corner('10px');
        $('.content #content_main h3 span').corner('5px TR TL');
        $('#info > li').corner('10px');
        $('#main_nav li.selected > a').corner('10px TR TL');
        $('#home_spotlight').corner('8px');

        if($("#maincolumnlistC").length) {
            $('#content #maincolumnlistC > li').corner('10px');
        }
    }
	

// 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.
    $('#main_nav > li > a').click(function() {
     		if(typeof(rounded) != 'undefined' && rounded == true) {
                $('#main_nav .selected a').corner('destroy'); /* Get rid of the rounded corner */
    		}
            $('#main_nav li').removeClass('selected');
            $(this).parent().addClass('selected');
     		if(typeof(rounded) != 'undefined' && rounded == true) {
                $('#main_nav .selected > a').corner('10px TR TL'); /* Add the rounded corner */
    		}
    		return false;
    	});


    // Create tabbed navigation on detail pages info section
    $('#info').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) {
        $('.semtabs').corner('10px TR TL');
        $('.semtabs li.selected').corner('#fff 10px TR TL');
    }

    // Hide full content span and add an ellipsis and a link to expand the full content
    $('.full_content').after('<span class="ellipsis">...</span> <a href="javascript:void(0)" class="expand">Expand</a>').hide();

    $('a.expand').toggle(function () {
        $(this).parent().children('.ellipsis').hide();
        $(this).parent().children('.full_content').show();
        $(this).removeClass('expand').addClass('collapse').text('Collapse');
    }, function () {
        $(this).parent().children('.ellipsis').show();
        $(this).parent().children('.full_content').hide();
        $(this).removeClass('collapse').addClass('expand').text('Expand');
    });

    // #dateBegin and #dateEnd are fields from the events advanced search form ( /advanced_search )
    $('#dateBegin').datepicker({
        showOn: 'both',
        buttonImage: '/images/icon-calendar.gif',
        buttonImageOnly: true

    });

    $('#dateEnd').datepicker({
        showOn: 'both',
        buttonImage: '/images/icon-calendar.gif',
        buttonImageOnly: true
    });

    // Set the start & end input fields to use the jquery UI datepicker.
    $('#start').datepicker({
        showOn: 'both',
        buttonImage: '/images/icon-calendar.gif',
        buttonImageOnly: true,
		onSelect: function(dateText, inst) { $('#start').css('color','black'); }
    });

    $('#end').datepicker({
        showOn: 'both',
        buttonImage: '/images/icon-calendar.gif',
        buttonImageOnly: true,
		onSelect: function(dateText, inst) { $('#end').css('color','black'); }
    });

    // Add popup preview balloons to line listings on category pages
    $('.line_listing td a').each(function () {
            $(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
    $('table#search_event_results td a').each(function () {
            $(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
    $('.line_listing tr:even').addClass('even');

    if ($.browser.msie) {
        $('.image_listing tr:last-child td').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.
if ( typeof( window[ 'site_width' ] ) == "undefined" ) {
				site_width = 990;
   }

   // Adds the "selected" class to all the items to get the same result and saves the original "selected" into "selectedLi"
   var selectedLi = $('#main_nav > li.selected');
   $('#main_nav > li').each(function (){ $(this).addClass('selected'); });

   $('#main_nav > li > ul').each(function () {
        $('#main_nav li li ul').parent().addClass('dropdown');
        var currentNav = $(this);
        var currentNavWidth = getNavWidth($(this));
        if (currentNavWidth > site_width) {
            // Get the overage in width
            var widthDifference = currentNavWidth - site_width;

            // 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) < site_width-20) {
                        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)) > site_width) {
            $(this).children('li:contains(" ")').not('li:contains("&")').each(function () {
                $(this).children('a').each(function () {
                    if (getNavWidth(currentNav) < site_width) {
                        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)) > site_width) {
            // 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) < site_width) {
                    return;
                }
                allListItems.eq(i).children('a').removeClass('two_line').children('br').replaceWith(' ');
                currentNav.find('ul.more_tabs').append(allListItems.eq(i));
            }
        }
		$('#main_nav li ul li').css('visibility','visible'); // nav fix
		$('#header').css('overflow','visible'); // nav fix
		$('#main_nav > li > ul').css('width',(site_width - 2) + 'px'); // nav fix
    });

    // Removes the "selected" class for all the not-selected items
    $('#main_nav > li').each(function (){ $(this).removeClass('selected'); });
    selectedLi.addClass('selected');

    $('#main_nav > li > ul').children('li:has(br)').children('a').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
    $('#main_nav li li ul').parent().children('a').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();
}

