﻿/* sitewide javascript code, should get pulled in on all pages (probably counts on existence of jquery) */
/* there may be some small plugins included at bottom of this page to save on HTTP requests */


/* BEGIN MISC SMALL TASKS ---------------------------------- */
$(document).ready(function () {

    /* look for the first page-width rounded corner on the page, and mark it so it can be styled differently */
    $('.pagecorner.cornerptop').first().addClass('cornerptopfirst').removeClass('cornerptop');

    /* manage the auto-hint of the search box */
    $('#searchbar .pagesearchtext').autoHint();

    /* manage the "large fonts" toggle of the font size button */
    $('#toolfontsize').click(function (evt) {
        evt.preventDefault();
        $('body').toggleClass('largefonts');
        return false;
    });

});
/* END MISC SMALL TASKS ------------------------------------ */


/*
 navigation setup
 crafted by Brian 07/16/2010
 tested on jQuery 1.4.2
*/
$(document).ready(function () {

    // collect references so we don't repeat lookups
    var nav = $('#navigation');
    var topmenu = nav.children('ul.menutop');


    // auto-size the widths of the dropdowns to give a minimum size
    var minwidth = 150;
    topmenu.children('li').each(function () {
        var mysubmenu = $(this).children('ul.menusub');
        var estwidth = mysubmenu.outerWidth();
        if (estwidth < minwidth) {
            mysubmenu.css('width', minwidth+'px');
        }
    });


    // configure the menu hover activities
    nav.find('li').each(function (index) {
        var myItem = $(this);
        var isFirstLevel = $(this).parent().hasClass('menutop');
        var mySubMenu = $(this).children('ul');

        // FirstLevel menus are the ones that slide up vertically from the nav
        if (isFirstLevel) {
            myItem.hover(
	            function () {
		            myItem.addClass('hovered');
		            if (!mySubMenu.hasClass('animating')) {
			            mySubMenu.addClass('animating');
			            mySubMenu.stop(true, true).slideDown({
				            duration: 150,
				            complete: function () {
					            mySubMenu.removeClass('animating');
				            }
			            });
		            }
	            },
	            function () {
		            myItem.removeClass('hovered');
		            mySubMenu.addClass('animating');
		            mySubMenu.stop().slideUp({
			            duration: 150,
			            complete: function () {
				            mySubMenu
					            .css('height', 'auto')
					            .css('overflow', '');
				            mySubMenu.removeClass('animating');
			            }
		            });
	            }
            );
        }
        else {	// !isFirstLevel
            myItem.hover(
					function () {
					    if (!myItem.hasClass('hovered')) {
					        mySubMenu.show(0);
					        myItem.addClass('hovered');
					    }
					},
					function () {
					    if (myItem.hasClass('hovered')) {
					        myItem.removeClass('hovered');
					        mySubMenu.hide(0);
					    }
					}
				);
        }

    }); // end each

});



/*
 shared tab menus setup
 crafted by Brian 07/21/2010
 tested on jQuery 1.4.2
*/
$(document).ready(function () {

    // set click events on each tab
    $('#bodyfeature .sharedtabmenu a.sharedtablink').click(function (evt) {
        evt.preventDefault();
        var clickedlink = $(this);
        var contentname = clickedlink.attr('name');
        var contentitem = $('.sharedtabbody.' + contentname);
        if ($.trim(contentitem.text()).length < 5) {
            contentitem.append('&nbsp;');
        }

        // 1. de-select all tabs, then select proper tab
        $('.sharedtabmenu a.sharedtablink').removeClass('sharedtablinkselected');
        clickedlink.addClass('sharedtablinkselected');

        // 2. hide all tab body items, then show proper body item
        $('.sharedtabbody').removeClass('sharedtabbodyselected');
        contentitem.addClass('sharedtabbodyselected');

        return false;
    });

});


// add enter-to-submit functionality to an element, pass in the element you wish to be 'clicked' when enter is pressed
// crafted by Brian 01/13/2011, tested on jQuery 1.4.4
jQuery.fn.satEnterSubmit = function (submitelement) {
    jQuery(this).keypress(function (e) {
        if (e.keyCode == 13) {
            jQuery(submitelement).click();
            return false;
        }
    });
};

/*
 auto-hint functionality for textboxes
 crafted by Brian 07/26/2010
 tested on jQuery 1.4.2
*/
jQuery.fn.autoHint = function() {
    
    // loop through all our items and set them up one by one
    $(this).each( function(idx) {
        var mytextbox = $(this);
        var myhint = mytextbox.attr('title');
        // needs a title attribute to be set to the hint value
        if ( myhint ) {
            // on focus, automatically clear the hint
            mytextbox.bind('focus', function() {
                if ( $(this).val() == myhint ) {
                    $(this).val('');
                }
            });

            // on blur, replace the hint if we have no text
            mytextbox.bind('blur', function() {
                if ( $(this).val() == '' ) {
                    $(this).val(myhint);
                }
            });
        }
    });
};


/*
 simple expander (accordion-style) functionality for LCW site
 crafted by Brian 08/10/2010
 tested on jQuery 1.4.2
*/
jQuery.fn.plusExpand = function () {
    var headeritems = $(this).children(':even');
    var contentitems = $(this).children(':odd');

    // collapse all headers initially
    headeritems.addClass('collapsed');
    // hide all content initially
    contentitems.hide();

    // add click handler
    headeritems.click(function () {
        // did we click one that was already open?
        var clickedItemWasClosed = $(this).hasClass('collapsed');
        // close and unmark all the uncollapsed headers/content
        headeritems.not('.collapsed').addClass('collapsed');
        contentitems.not($(this).next()).slideUp(150);
        // optionally show and mark the item we clicked
        if (clickedItemWasClosed) {
            $(this).removeClass('collapsed');
            $(this).next().slideDown(150);
        }
        else {
            $(this).next().slideUp(150);
        }
    });

    // pre-click the first item =)
    headeritems.first().click();
}


/*
faux lightbox behavior for 'send page' link
crafted by Brian 07/27/2010
tested on jQuery 1.4.2
*/
$(document).ready( function() {
    $('#toolsendpage').click( function(evt) {
        // block standard activity
        evt.preventDefault();

        // add the overlay
        $('body').append('<div id="fauxlightboxmask"></div>');
        var mask = $('#fauxlightboxmask');

        // fade the overlay into view
        mask.fadeTo( 500, 0.75, function() {
            // no callback for now
        });

        // add the content holder
        $('body').append('<div id="fauxlightbox"></div>');
        var box = $('#fauxlightbox');

        // put in a small loading message
        box.html(   '<div class="fauxlightboxload" style="width:100px;margin:50px auto;padding:10px;background-color:#666;color:#ccc;">' +
                    '<img src="images/icon_loading_666.gif" border="0" alt="" /><br/>Loading...' +
                    '</div>' );

        // load the page contents via ajax request!
        jQuery.ajax({
            type: "GET",
            url: "sendpage.aspx",
            success: function (strhtml) {
                box.html(strhtml);
            }
        });

        // hook up click events on rest of document so they auto-destroy the lightbox
        $(document).bind('click.hidefauxlightbox', function (evt) {
            // we need to get the raw DOM node to use jQuery.contains
            safeContainer = $('#fauxlightbox').get(0);
            if (!jQuery.contains(safeContainer, evt.target)) {
                // if we do get a click on non-safe element... kill the event handler and destroy the lightbox items
                $(document).unbind('click.hidefauxlightbox');
                fauxlightbox_destroy();
            }
        });


        // let's tell browser ignore the click (otherwise we might load a whole new page)
        return false;
    });


    $('#toolsendpagemain').click(function (evt) {
        // block standard activity
        evt.preventDefault();

        // add the overlay
        $('body').append('<div id="fauxlightboxmask"></div>');
        var mask = $('#fauxlightboxmask');

        // fade the overlay into view
        mask.fadeTo(500, 0.75, function () {
            // no callback for now
        });

        // add the content holder
        $('body').append('<div id="fauxlightbox"></div>');
        var box = $('#fauxlightbox');

        // put in a small loading message
        box.html('<div class="fauxlightboxload" style="width:100px;margin:50px auto;padding:10px;background-color:#666;color:#ccc;">' +
                    '<img src="images/icon_loading_666.gif" border="0" alt="" /><br/>Loading...' +
                    '</div>');

        // load the page contents via ajax request!
        jQuery.ajax({
            type: "GET",
            url: "sendpage.aspx",
            success: function (strhtml) {
                box.html(strhtml);
            }
        });

        // hook up click events on rest of document so they auto-destroy the lightbox
        $(document).bind('click.hidefauxlightbox', function (evt) {
            // we need to get the raw DOM node to use jQuery.contains
            safeContainer = $('#fauxlightbox').get(0);
            if (!jQuery.contains(safeContainer, evt.target)) {
                // if we do get a click on non-safe element... kill the event handler and destroy the lightbox items
                $(document).unbind('click.hidefauxlightbox');
                fauxlightbox_destroy();
            }
        });


        // let's tell browser ignore the click (otherwise we might load a whole new page)
        return false;
    });

});

// call this to SMASH the lightbox
function fauxlightbox_destroy() {
    $('#fauxlightbox').remove();
    $('#fauxlightboxmask').remove();
}
