/*global jQuery, document, window */
(function($) {

    // Reveal text for any graphic menu items
    function unfade_text(row) {
        var anchors = row.children('.cell').children('a.graphic');
        anchors.stop().animate({opacity:1},250);
    }

    // Fade text back again
    function fade_text(row) {
        var anchors = row.children('.cell').children('a.graphic');
        anchors.stop().animate({opacity:0});
    }

    // Pull out row, optionally unfading items at the end
    function expand_menu(row,force,unfade) {
        row.data('menu_expanded',true);
        row.stop().animate({left: '0px'}, force ? 1 : 500,'', function() {
            if(unfade) { unfade_text(row); }
        });
    }

    // Move a row back to starting point
    function contract_menu(row,force) {
        row.data('menu_expanded',false);
        if(typeof row.data('timeout_expand') === "number") {
            window.clearTimeout(row.data('timeout_expand'));
            row.data('timeout_expand',null);
        }
        row.stop().animate({left:(row.data('cell_width')-row.width())+'px'}, force ? 1 : 1000,'',function() {
            fade_text(row);
        });
    }

    // Start a timer to expand the row
    function delay_expand_menu(row,force) {
        if(typeof row.data('timeout_contract') === "number") {
            window.clearTimeout(row.data('timeout_contract'));
            row.data('timeout_contract',null);
        }
        if(typeof row.data('timeout_expand') === "number") { return; }
        row.data('timeout_expand',window.setTimeout(function(msg) {
            row.data('timeout_expand',null);
            expand_menu(row,force,true);
        },force ? 1 : 250));
    }

    // Start a timer to contract the row
    function delay_contract_menu(row,force) {
        if(typeof row.data('timeout_expand') === "number") {
            window.clearTimeout(row.data('timeout_expand'));
            row.data('timeout_expand',null);
        }
        if(typeof row.data('timeout_contract') === "number") { return; }
        row.data('timeout_contract',window.setTimeout(function(msg) {
            row.data('timeout_contract',null);
            contract_menu(row,force);
        },force ? 1 : 500));
    }

    // Start a timer to expand the row
    function delay_expand_contract_menu(row) {
        row.data('timeout_expand',window.setTimeout(function(msg) {
            expand_menu(row,false,true);
        },250));
        row.data('timeout_contract',window.setTimeout(function(msg) {
            row.data('timeout_contract',null);
            contract_menu(row,false);
        },5000));
    }

    // Set the background color of anchors to a translucent version of the cell
    // colour, so when visible background images are muted.
    function set_translucent_background(row) {
        var graphic_links;
        var background_color = row.children('.cell').css('background-color');
        if(background_color) {
            graphic_links = row.children('.cell').children('a.graphic');
            graphic_links.css('background-color',background_color);
        }
    }

    $(document).ready(function() {
        // Set widths on every menu and move them to their starting point
        var all_drawer_rows = [];
        $("div.layer.drawer .row").each(function(i) {
            var right;
            var row_width = 0;
            var cell_width = 0;
            $(this).children('.cell').each(function(j) {
                if(!cell_width) { cell_width = $(this).width(); }
                right = $(this).position().left+cell_width;
                if(right > row_width) { row_width=right; }
            });
            $(this).css('width',row_width+'px');
            $(this).data('cell_width',cell_width);
            if($(this).hasClass('theme-home')) {
                expand_menu($(this), true);
            } else {
                set_translucent_background($(this));
                contract_menu($(this), true);
                all_drawer_rows.push($(this));
            }
        });

        // If on the homepage, cascade everything out
        if($("body.homepage").length > 0) {
            $.each(all_drawer_rows, function(i) {
                var drawer_row = $(this);
                var hl_row = $("div.layer.home-links ."+drawer_row[0].className.split(' ').join('.'));
                window.setTimeout(function() { expand_menu(drawer_row, false); }, i*100);
                // Hovering over the menu forces out
                hl_row.hover ( function() { unfade_text(drawer_row); }
                             , function() { fade_text(drawer_row); }
                             );
                drawer_row.hover ( function() { unfade_text(drawer_row); }
                                 , function() { fade_text(drawer_row); }
                                 );
            });
        } else {
            // Wire up home links to menus
            $.each(all_drawer_rows, function(i) {
                var drawer_row = $(this);
                var hl_row = $("div.layer.home-links ."+drawer_row[0].className.split(' ').join('.')+" a");
                // Hover over home link gracefully expands
                hl_row.hover( function() { delay_expand_menu(drawer_row, false); }
                            , function() { delay_contract_menu(drawer_row, false); }
                            );
                // Hovering over the menu forces out
                drawer_row.hover ( function() { delay_expand_menu(drawer_row, true); }
                                 , function() { delay_contract_menu(drawer_row, false); }
                                 );
                // Tap expands menu
                hl_row.bind('touchstart', function() {
                    if (drawer_row.data('menu_expanded')) {
                        window.location = $(this).attr('href');
                    } else {
                        $(this).click(function(){return false;});
                        delay_expand_contract_menu(drawer_row);
                    }
                    });
            });
        }
    });

}(jQuery));

