/*
** JS for tonbridge-school.co.uk
*/
$(document).ready(function() {
    jQuery.fn.extend(
		{
			getTallest: function() {
                var tallestHeight = 0;
                var tallestEl = undefined;
                this.each(function () {
                    var height = $(this).height();
                    if (height > tallestHeight) {
                        tallestHeight = height;
                        tallestEl = this
                    }
                });
                return $(tallestEl);
            },
            resizeToTallest: function() {
                var height = this.getTallest().height();
                if (!height) {
                    return;
                }
                this.height(height)
            }
		}
	);

    // Hide "Other links"
    /*
    (function () {
        if (document.location.pathname != '/') {
            var $toggler = $('<ul class="menu"><li><a href="#">Other sites</a></li></ul>')
                .find('a')
                    .mouseover(function (e) {
                        $('#nav_other').stop(true, true).slideDown();
                    })
                    .click(function (e) {
                        e.preventDefault();
                        $('#nav_other').slideToggle();
                    })
                .end()
            ;
            $('#nav_other')
                .before($toggler)
                .hide()
            ;
        }
    })();
    */
    
    // Menu hover
    (function () {
        var current_li;
        var $current_child;
        
        function save_child($child) {
            if ($child && $child.timeout) {
                clearTimeout($child.timeout);
                $child.timeout = undefined;
            }
        }
        function timeout_child($child) {
            if ($child) {
                $child.timeout = setTimeout(function () { remove_child($child) }, 100);
            }
        }
        function remove_child($child) {
            if (!$child) {
                return;
            }
            $child.fadeOut('fast', function () {
                $child.remove()
            });
            if ($child == $current_child) {
                $current_child = undefined;
                current_li = undefined;
            }
        }
        
        $('ul#nav_main > li').hover(
            function () {
                if (current_li) {
                    // Save current child for now
                    save_child($current_child);
                    
                    // Hide or keep?
                    if (current_li == this) {
                        return;
                    } else {
                        remove_child($current_child);
                        $current_child = undefined;
                    }
                }
                
                // Clone
                current_li = this;
                
                var $this_parent = $(this).parent();
                var $this_child = $(this).find('ul').clone().addClass('hover');
                $current_child = $this_child;
                $('body').append($current_child);
                $current_child
//                    .css('top', $(this).offset().top)
//                    .css('left', $(this).offset().left+$(this).width())
                    .css('top', $(this).offset().top)
                    .css('left', $this_parent.offset().left + $this_parent.width() )
                    .fadeIn('fast')
                    .hover(function() {
                        if ($current_child != $this_child) {
                            // Hovered over a closing item - ignore
                            return;
                        }
                        // Keep
                        save_child($current_child);
                        
                    }, function() {
                        // Set to remove
                        if ($this_child.timeout) {
                            return;
                        }
                        timeout_child($this_child)
                    })
                ;
                
            }, function () {
                timeout_child($current_child);
            }
        );
        
    })();
    
    // Footer links
    (function () {
        var $extra = $('#extra_links');
        if (!$extra || $('.index_links_menu')) {
            return;
        }
        var $footer = $('#footer');
        $extra
            .insertBefore($footer)
            .show()
        ;
        
    })();
    
    // Index page links
    (function () {
        
        var $links = $('.index_links_menu');
        var $summary = $('.index_links_summary');
        
        if ($links.length && $summary.length) {
            var $links_con = $links.wrap('<div class="con"></div>').parent();
            var $summary_con = $summary.wrap('<div class="con"></div>').parent();
            var $index_con = $('<div class="index_links"></div>')
                .insertBefore($links_con)
                .append($links_con)
                .append($summary_con)
                .mouseout( function(e) {
                    function has_parent(element, parent) {
                        if (element == parent) {
                            return true;
                        }
                        var parents = $(element).parents();
                        for (var i=0; i<parents.length; i++) {
                            if (parents[i] == parent) {
                                return true;
                            }
                        }
                        return false;
                    }
                    if (has_parent(e.relatedTarget, $index_con.get(0))) {
                        return;
                    }
                    $summary_con.children().hide();
                    $summary.show()
                })
            ;
            
            //$links_con.appendTo($index_con);
            //$summary_con.appendTo($index_con);
            
            var $summary_section;
            
            // Make a list of all summary boxes
            
            var $all = $($summary);
            $links.find('ul.menu > li > a')
                .each(function () {
                    // Find siblings
                    var $this = $(this);
                    var $this_siblings = $this.siblings();
                    
                    // Build summary
                    var $this_summary = $('<div></div>')
                        .append(
                            $('<div class="summary_back"><a href="#">Close</a></div>')
                        )
                        .append(
                            $('<h3></h3>').text( $this.text() )
                        )
                    ;
                    
                    if ($this_siblings.length) {
                        $this_summary
                            .append(
                                $this_siblings
                                    .filter('ul')
                                        .addClass('menu')
                                    .end()
                            )
                        ;
                    }
                    
                    $this_summary
                        .appendTo($summary_con)
                        .hide()
                    ;
                    
                    // Store on <a> and in $all
                    this.$summary = $this_summary;
                    $all = $all.add($this_summary)
                })
                .mouseover(function (e) {
                    // Hide
                    $summary_con.children().hide();
                    
                    // Add to page
                    $summary_section = this.$summary
                        .show()
                        .find('.summary_back a')
                            .click( function (e) {
                                e.preventDefault();
                                $summary_con.children().hide();
                                $summary.show()
                            })
                        .end()
                    ;
                    
                    
                })
            ;
            
                
            // Make $all the same height
            $all.resizeToTallest()
        }
    })();
    
    // Calendar past/future control
    (function () {
        // Must be a calendar with future and past days
        var $past = $('.day_past');
        if ($('.calendar').length && $('.day_future').length && $past.length) {
            var $reveal = $('<ul class="menu"><li><a href="#">Show events on '
                + $past.length + ( ($past.length == 1) ? ' day' : ' days' )
                + ' before today</a></li></ul>'
            );
            $reveal
                .find('a')
                    .click(function(e) {
                        e.preventDefault();
                        $reveal.remove();
                        $past.slideDown();
                    })
                .end()
                .insertBefore($past[0])
            ;
            $past.hide();
        }
        
    })();
    
    // Columnise tables
    $('table.columnise_2').each(function () {
        var $table = $(this);
        var $rows = $table.find('tr');
        
        // Look for header on the top line
        var $rowOne = $rows.slice(0,1);
        var header = '';
        if ($rowOne.find('th').length) {
            header = '<tr>' + $rowOne.html() + '</tr>';
            $rows = $rows.slice(1);
        }
        
        // Decide where to split
        var splitBefore = Math.ceil($rows.length / 2);
        
        // Start the replace
        var replace = '<div class="column_con"><div class="column_2"><div class="column_left"><table>' + header;
        
        for (var i=0; i<splitBefore; i++) {
            replace += '<tr>' + $rows[i].innerHTML + '</tr>';
        }
        replace += '</table></div></div><div class="column_2"><div class="column_right"><table>' + header;
        
        for (var i=splitBefore; i<$rows.length; i++) {
            replace += '<tr>' + $rows[i].innerHTML + '</tr>';
        }
        replace += '</table></div></div></div>';
        
        $table.replaceWith(replace);
    });
});
