jQuery(document).ready(function() {
    $ = jQuery;

    // News Ticker
    $("#news-items").cycle({
        fx: "turnUp",
        width: 286,
        fit: true,
        height: 65,
        delay: 1000,
        speed: 1500,
        timeout: 4000
    });
    
    $(".othertext").css({"color": "#999"}).click(function() {
        $(this).css({"color": "#000"}).val("");
    });
       
    $('#nav .expandable').dropdown('.submenu', 400);
    // Set the height of the first UL in submenu to the height of the
    // submenu
    // 
    // The height of the submenu is affected by the min-height, but this
    // causes the jquery slide up and down to not work smoothly
    var submenu = $('#nav .expandable .submenu');
    submenu.each(function(index, value) {
        var elem = $(value);
        elem.find('ul:first').height(elem.height());
        elem.css('min-height', 0);
    });
    
    $('.home #body .wrapper').height($(window).height()-127);
    $('.brands #nav-modules').height($(window).height()-178);
    
    if ($('body.home').length > 0) {
        $('#nav .expandable').bind('afterslidedown', function() {
            // Pause background image slideshow and news ticker
            if (!vars.is_paused) {
                api.playToggle();
            }
            $("#news-items").cycle("pause");
        });
        $('#nav .expandable').bind('afterslideup', function() {
            // Resume background image slideshow and news ticker
            if (vars.is_paused) {
                api.playToggle();
            }
            $("#news-items").cycle("resume");
        });
    }

    if ($('#slides').length > 0) {
        $('#nav .expandable').bind('afterslidedown', function() {
            // Pause slideshow
            $('#slides, #content').cycle('pause');
        });
        $('#nav .expandable').bind('afterslideup', function() {
            // Resume slideshow
            $('#slides, #content').cycle('resume');
        });
    }
    
    // Search label - grey out on focus and disappear on text entry
    $("label .in-field-label").inFieldLabels();

    var hovered = false

    $('#search').hover(function(){hovered=true;}, function(){hovered=false;});

    $('label[for="search-query"], #search-query, #search-submit, #searchbox_currentfolder_only, label[for="searchbox_currentfolder_only"], #search .searchSection').blur(function() {
        if (!hovered) {
            $('#search .searchSection').hide();
        }
    }).focus(function() {
        $('#search .searchSection').show();
    });

    // Fix anchors not working due to base href
    $("a[href^='\#']").click(function(e) {
        e.preventDefault();
        document.location.hash = this.href.substr(this.href.indexOf('#') + 1);
    }).keypress(function(e) {
        if (e.which == 13) {
            // user pressed enter, which means they are going to the link
            e.preventDefault();
            document.location.hash = this.href.substr(this.href.indexOf('#') + 1);
        }
    });

    $('.brand-module .text').click(function(){
        // To make the titles clickable in IE
        window.location.href = $(this).closest('A').attr("href");
    });

    // Remove target="_blank" (which is there for those users without JS
    $('a[rel*=external]').removeAttr('target');
    
    $('a[rel*=external]').click(function(){
        // To make external links open in a new window/tab
        window.open($(this).attr('href'));
        return false;
    });

});

