// Initializes User Scripts:
$(document).ready(function() {
    // Selects text in Search Box when focused:
    $('input[type=text].clearText').each(function() {
        var txtSearchText = $(this).val();
        $(this).focus(function() {
            if ($(this).val() == txtSearchText) {
                $(this).val('');
            }
        });
        $(this).blur(function() {
            if ($(this).val() == '') {
                $(this).val(txtSearchText);
            }
        });
        
        if ($(this).hasClass('validate')) {
            $(this).parent().parent().submit(function() {
                if ($(this).find('input[type=text]').val() == txtSearchText) {
                    alert('Please enter a valid search term')
                    return false;
                }
            });
        }
    
    });
    
    // Opens up links in new window.
    $('.lnkBlank').click(function() {
        window.open($(this).attr('href'), 'newWindow'+ new Date().getTime(), '');
        return false;
    });
    
    if ( document.URL.indexOf('EditListItem.aspx') == -1 ) {
        $("img").each(function() {
            var img = $(this).attr("src");
            if ( typeof(img) == 'undefined' || img == '' || img == '/' || img.substr(0, 1) == '?' || img.substr(0, 2) == '/?' ) $(this).hide();
        });
        $("img").error(function() {
            $(this).hide();
        });
    }
    
    // hide text in spans (text replacement). Adds sprite class for 'image'.
    $('.imgText').each(function() {
        $(this).find('>span').hide();
        $(this).addClass('spriteTheme');
    });
    
    // Site Navigation / dropdowns:
    pixelsilk2.renderSkin({skin: '['+'[Menu1]'+']', path: ''}, function(html) {
        $("#nav").after('<div id="subNavigation" style="display:none;"></div>');
        $("#subNavigation").html(html);
        var sections = $("#subNavigation div");
        var topElements = $("#nav>ul>li:not(.sep)");
        for (z = 0; z < sections.length; z++) {
            var li = topElements[z];
            var lihtml = li.innerHTML;
            li.innerHTML = lihtml + sections[z].innerHTML;
        }
        $("#nav").navMenu({
            menuWidth: 200,
            containerElement: "nav#nav>ul",
            menuElement: "ul",
            rightArrow: ' &raquo;'
        });
        $('#nav ul li').hover(function(){
            $(this).addClass('over');
        }, function(){
            $(this).removeClass('over');
        });
        $("#subNavigation").remove();
        
        // Remove the bottom border on all last items in ULs:
        $('nav#nav').find('ul').each(function() {
            $(this).find('li:last-child').css({'border-bottom':'0'});
        });
        
    });
    
    // IE 8 and below specific stuff:
    if (jQuery.browser.msie && jQuery.browser.version <= 8 ) {
        $('.txtSearch').addClass('spriteTheme');
        var corners = {
            "topLeft":        "<div class=\"themeCorner white topLeft spriteTheme\"></div>",
            "topRight":        "<div class=\"themeCorner white topRight spriteTheme\"></div>",
            "bottomLeft":    "<div class=\"themeCorner white bottomLeft spriteTheme\"></div>",
            "bottomRight":    "<div class=\"themeCorner white bottomRight spriteTheme\"></div>"
        }
        $('#footer').append(corners.topLeft + corners.topRight + corners.bottomLeft + corners.bottomRight);
        $('#postFooter li:last').css({'border':'0'});
    }
    
});
