var cssOff = true;

$(document).ready(setUpPage);

function setUpPage() {
    //Check for CSS

    if ($('#footerwrapper').css('backgroundImage') != 'none')
        cssOff = false;

    if (!cssOff) {
        // Set up animation of main navigation
        $('#mainnav > ul > li').each(function () {
            if (!$(this).hasClass('hover')) {
                $(this).addClass('hover');
                $('> ul', this).hide();
            }

            $(' > a', this).click(function (event) {
                event.preventDefault();
                $(this).parent().siblings().find(' > a').removeClass('hoverimg');
                $(this).parent().siblings().find(' > ul:visible').slideUp(500);
                if ($(this).parent().find('> ul').is(":hidden")) {
                    $(this).addClass('hoverimg');
                    $(this).parent().find('> ul:hidden').slideDown(500);
                }
                else {
                    $(this).removeClass('hoverimg');
                    $(this).parent().find('> ul:visible').slideUp(500);
                }
            });
        });

        var overlay = $("<div id='menuoverlay'></div>");
        $("body").append(overlay);

        $('#topnav li.menu').each(function () {
            //        if (!$(this).hasClass('ihover')) {
            //            $(this).addClass('ihover');
            //            $('.subnavl', this).hide();
            //        }
            $(this).mouseenter(function () {
                //            $(' .subnavl', this).slideDown(200);
                $('.subnavl', this).show();
                $(this).css("z-index", "1000");
                overlay.css("opacity", 0.6);
                overlay.stop(true, true).fadeIn(150);

            });
            $(this).mouseleave(function () {
                overlay.stop(true, true).fadeOut(150);
                $(this).css("z-index", "");
                $('.subnavl', this).hide();
                $(this).removeClass('focus');
            });
            $(this).focusin(function () {
                if (!$(this).hasClass('focus')) {
                    $(this).addClass('focus');
                    $(this).mouseenter();
                }
            });
            $(this).focusout(function () {
                var $this = $(this);
                window.setTimeout(function () {
                    if ($this.has(document.activeElement).length == 0) {
                        $this.mouseleave();
                        $this.removeClass('focus');
                    }
                }, 10);
            });
        });

        //Check tables
        $('#content table').each(function () {
            $('tr:odd').addClass('alternate');
            if ($(this).innerWidth() > 610) {
                $(this).width('600px');
                $(this).css('table-layout', 'fixed');
            }
        });

        if ($('#homecontent').length > 0) {
            $('#nav4 > a').addClass('hoverimg');
            $('#nav4 > ul').show();

            //Setup carousel on homepage
            //$('#homecontent .carousel > div.nav span').hide();
            //$('#homecontent .carousel>ul li>div').css("opacity", 0.8);
            //$('#homecontent .carousel ul').accessibleCarousel({ autoRotate: true, rotateWaitTime: 8000, navigationElement: $('#homecontent .carousel > div.nav') });
        }


        //    //Make sure all text boxes & text areas are encoded on submission
        //    $("#aspnetForm").submit(function () {
        //        $("#aspnetForm input:text,#aspnetForm textarea").each(function () {
        //            var text = $(this).val();
        //            if (text.length > 0) {
        //                $(this).val(text.replace(/</g, "&lt;").replace(/>/g,"&gt;"));
        //            }
        //        });
        //    });
    }
}

function zIndexFix() {
    if (!cssOff) {
        $('#topnav li.menu').unbind();
        $('#topnav li.menu > a').unbind();
        $('#topnav li.menu').each(function () {
            $(this).mouseenter(function () {
                $(this).addClass('focus');
                $('.subnavl', this).css("border-bottom", "1px solid #efefef").css("border-right", "1px solid #efefef");
                $('.subnavl', this).slideDown(300);
            });
            $(this).mouseleave(function () {
                $('.subnavl', this).hide();
                $(this).removeClass('focus');
            });
            $(this).focusin(function () {
                if (!$(this).hasClass('focus')) {
                    $(this).mouseenter();
                }
            });
            $(this).focusout(function () {
                var $this = $(this);
                window.setTimeout(function () {
                    if ($this.has(document.activeElement).length == 0) {
                        $this.mouseleave();
                    }
                }, 10);
            });
        });
        //Added for autocomplete issues
        $("#searchbox [id$='SearchText']").bind("autocompleteopen", function (event, ui) {
            $('#topnav').css("z-index", "2");
            $('#topnav > ul').css("z-index", "1");
        });
        $("#searchbox [id$='SearchText']").bind("autocompleteclose", function (event, ui) {
            $('#topnav').css("z-index", "");
            $('#topnav > ul').css("z-index", "1000");
        });
    }
}

function ieFix() {
    zIndexFix();
    scroller();
}

function scroller() {
    if (!cssOff) {
        //Setup any scrolling lists
        if ($('#content .scrollist').height() > 270)
            $('#content .scrollist').height(260);
    }
}

$(function () {
    var $watermarked = $("input:text.watermark,textarea.watermark");
    $watermarked.each(function () {
        var title = $(this).attr('title');
        if ($(this).val() == '' && title != '')
            $(this).val(title);
    });
    $watermarked.focusin(function () {
        if ($(this).val() == $(this).attr('title')) {
            $(this).val('');
            $(this).css({ 'color': '#404040', 'font-style': 'normal' });
        }
    });
    $watermarked.focusout(function () {
        var val = $(this).val();
        if ($.trim(val) == '') {
            var title = $(this).attr('title');
            $(this).val(title);
            $(this).css({ 'color': '', 'font-style': '' });
        }
    });
});
