$(document).ready(function() {
    
    /***********************NAVIGATION SCRIPTS***********************/
    $('#primarynav li ul:first').css({'top' : '37px', 'left' : '0'});
    $('#primarynav a:first:not(a:only-child)').css({'background-color' : 'black'});
    $('#primarynav li a:not(a:only-child)').each(
        function()
        {
            var linkLeftPos = $(this).position().left;
            var linkWidth = $(this).width();
            linkWidth += parseInt($(this).css("padding-left"), 10) + parseInt($(this).css("padding-right"), 10);
            var ulWidth = 0;
            $(this).siblings().children(':not(.navspacer)').each(
                function()
                {
                    ulWidth += $(this).width();
                    //have to manually add in padding
                    ulWidth += parseInt($(this).css("padding-left"), 10) + parseInt($(this).css("padding-right"), 10);
                }
            );
            var spacerWidth = linkLeftPos - (ulWidth/2 - linkWidth/2);
            var mainWidth = $('#main').width() + parseInt($('#main').css("padding-left"), 10) + parseInt($('#main').css("padding-right"), 10);
            if(spacerWidth < 0)
            {
                spacerWidth = 0;
            }
            else if(spacerWidth + ulWidth > mainWidth)
            {
                spacerWidth = mainWidth - ulWidth;
            }
            $(this).siblings().children('.navspacer').width(spacerWidth + 'px');
        }
    );
    $('#primarynav li a:not(a:only-child)').hover(
        function()
        {
            $('#primarynav a').css({'background-color' : 'transparent'});
            $(this).css({'background-color' : 'black'});
            $('#primarynav li ul').css({'top' : '-999px', 'left' : '-999px'});
            $(this).siblings().css({'top' : '37px', 'left' : '0px'});
        }
    );
    //this would cause menu items to go away when the mouse left the ul area - right now we have it stay
    /*$('#primarynav').hover(
        function(){},
        function()
        {
            $(this).children('li').children('ul').css({'top' : '-999px', 'left' : '-999px'});
            $('#primarynav li ul:first').css({'top' : '37px', 'left' : '0'});
        }
    );*/
    
    /***********************EMAIL SIGNUP SCRIPTS***********************/
    var emailText = 'Enter your email address';
    $('#TxtBoxEmail').val(emailText);
    $('#TxtBoxEmail').click(
        function()
        {
            if($(this).val() == emailText)
            {
                $(this).val('');
            }
        }
    );
    $('#TxtBoxEmail').blur(
        function()
        {
            if($(this).val() == '')
            {
                $(this).val(emailText);
            }
        }
    );
    $('#SignupSubmitBtn').click(
        function()
        {
            if($('#TxtBoxEmail').val() == emailText)
            {
                return false;
            }
        }
    );
    
    /***********************HOVER SCRIPTS***********************/
    //add hover ability to any rollerImg's
    $('.rollerImg').hover(
        function() {
            $(this).attr('src', $(this).attr('onmouseover_src'));
        }
        ,
        function() {
            $(this).attr('src', $(this).attr('onmouseout_src'));
        }
    );
    //add ahover background to iggli/other social networking list items (sidebar)
    $('ul.socialNetworking li').ahover();
    
    /***********************GENERAL ACCORDION MENU SCRIPTS***********************/
    $('.collapse_menu li.collapse_title a.collapse_toggle').click(
        function() {
            var opened = $(this).parents('.collapse_menu li.collapse_title').siblings('.collapse_menu li.collapse_title').children('.collapse_menu li.collapse_title a.opened');
            opened.toggleClass('opened');
            opened.next('ul.collapse_section').slideToggle('normal');
            $(this).toggleClass('opened');
            $(this).next('ul.collapse_section').slideToggle('normal');
        }
    );
    $('.collapse_menu ul.collapse_section').hide();
    $('.collapse_menu li.collapse_title a.opened').next('ul.collapse_section').slideToggle('normal');
});