var BLUE = function()
{

    var valid = true;
    
    /**
     * Grab a url parameter
     * @param {String} name
     */
    function gup(name)
    {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null) 
            return "";
        else 
            return results[1];
    }
    
    function siteSearch()
    {
        var $siteSearch = $('form#frm_site_search');
        var q = gup('q');
        q = q.replace('+', ' ');
        
        $('form#frm_site_search .header_search_box').attr('value', q);
        
        //setup "advanced" options for site search (include/exclude forums, etc)
        $('#search input[type=checkbox]').bind('click', function()
        {
            var checkValue = '|' + $(this).attr('value');
            var formHiddenValue = $('form#frm_site_search input[name^=data]').attr('value');
            if ($(this).is(':checked')) {
                $('#frm_site_search input[name^=data]').attr('value', formHiddenValue + checkValue);
            } else {
                $('#frm_site_search input[name^=data]').attr('value', formHiddenValue.replace(checkValue, ''));
            }
        });
        
        $siteSearch.submit(function()
        {
        
            var query = $('input.header_search_box').val();
            query = $.trim(query);
            
            if (query == "") {
                alert('You must enter a valid search term prior to submitting your site search.');
                return false;
            }
            
            return true;
            
        });
    }
    
    return {
    
        init: function()
        {
            $('.javascript').addClass('show');
            BLUE.personalize();
            BLUE.print();
            BLUE.login();
            BLUE.jobs();
            siteSearch();
        },
        
        jobs: function()
        {
            var $careers = $('div#careers');
            
            //if we are on the careers/jobs page
            if ($careers.length > 0) {
            
                //items have to be selected in order to be printable
                //on jobs view page
                $('dt, dd').addClass('printHide');
                
                //toggle the jobs listing when dt is clicked
                ///careers/view_public
                $('dt').click(function()
                {
                    //see javascript at the bottom of this file
                    $(this).nextUntil('dt').toggle();
                });
                
                //prevent propagation to dt, and prepares items for printing
                $('input.jobsPrint').bind('click', function(e)
                {
                    $dd = $(this).parent().parent();
                    $dd.toggleClass('printHide');
                    
                    //check to see if there are any checked jobs in this set of jobs
                    var count = 0;
                    $dd.prev().nextUntil('dt').find("input:checked").each(function()
                    {
                        count++;
                    });
                    
                    if (count > 0) {
                        $dd.prevAll("dt:first").removeClass('printHide');
                    } else {
                        $dd.prevAll("dt:first").addClass('printHide');
                    }
                });
                
                //Adding a job (/career_center_ads/add)
                //make sure that there is a terms of service checkbox
                $tos = $('#CareersTosAgreement');
                
                if ($tos.length > 0) {
                
                    $submit = $("input[type='submit']");
                    
                    $tos.click(function()
                    {
                    
                        if ($(this)[0].checked == true) {
                            $submit.attr('disabled', '');
                        } else {
                            $submit.attr('disabled', 'disabled');
                            alert('You must agree to our terms of service before you can submit this job');
                        }
                    });
                }
            }
        },
        
        login: function()
        {
        
            var $mbrForm = $('form#MemberLoginForm');
            
            //if we are on the member login page
            if ($mbrForm.length > 0) {
            
                $mbrForm.submit(function()
                {
                
                    var usr = $('input#menc_bklogin').val();
                    usr = $.trim(usr);
                    
                    var pass = $('input#menc_bkpw').val();
                    pass = $.trim(pass);
                    
                    if (usr == "" || pass == "") {
                        $("div.err").html("<p>Please enter a member id and password.</p>").show();
                        return false;
                    }
                    $('div#login_mmc').append('<div class="login_feedback"><img src="/img/bg/ajax-loader.gif" />Connecting to Member Services.  This might take a few seconds.</div>');
                    return true;
                    
                });
            }
        },
        
        personalize: function()
        {
        
            var $home = $("div#home");
            
            //if we're on the home page of the site
            if ($home.length > 0) {
            
                //grab cookie data with visitor stats
                var visits = Cookie.getCookie('MENCVis');
                
                if (visits) {
                    //$("div#home div.whats").addClass('hide');
                    
                    var obj = Base64.decode(visits);
                    
                    //convert to useable json object
                    //produces a native javascript object
                    obj = eval("(" + obj + ")");
                    
                    //find the section with the most visits
                    var maxTitle, maxLength = 0;
                    
                    $.each(obj, function(section, count)
                    {
                        if (count > maxLength) {
                            maxLength = count;
                            maxTitle = section;
                        }
                    });
                    
                    //load the what's happening section of the front page with content from the
                    //what's happening section of the most frequently visited section of the site (for this visitor)
                    $home.load("/s/" + maxTitle + "/ div.whats");
                }
                
            }
        },
        
        //set up page print
        print: function()
        {
            $('a.print').click(function()
            {
                javascript: window.print();
            });
        }
        
    };
    
}();

$(document).ready(function()
{
    BLUE.init();
});

//http://www.nabble.com/A-Beginner-Question-td15024797s27240.html
/******** next until > ****************/
$.fn.nextUntil = function(expr)
{
    var match = [];
    
    // We need to figure out which elements to push onto the array
    this.each(function()
    {
        // Traverse through the sibling nodes
        for (var i = this.nextSibling; i; i = i.nextSibling) {
            // Make sure that we're only dealing with elements
            if (i.nodeType != 1) 
                continue;
            
            // If we find a match then we need to stop
            if (jQuery.filter(expr, [i]).r.length) 
                break;
            
            // Otherwise, add it on to the stack
            match.push(i);
        }
    });
    
    return this.pushStack(match, arguments);
};
