jQuery.fn.list_rotate = function(options) {
    
    var settings = jQuery.extend({delay: 4000}, options);    
    var $list = $(this);
    var $list_items = $list.children('li');
    
    if ($list_items.length > 1) {
        window.setTimeout(function() {            
            var $outgoing = $($list_items.get(0));
            var $incoming = $($list_items.get(1));

            $list.css('min-height', 'auto');
            $list.css('height', $outgoing.innerHeight());
            $list_items.css('display', 'block');            
            
            $incoming.css('opacity', '0');
            
            var fade_next = function() {
                $outgoing.animate({'opacity': 0}, 'slow', 'swing', function() {
                    $list.animate({'height': $incoming.innerHeight()}, 'fast', 'swing', function(){
                        $outgoing.detach();
                        $outgoing.removeClass('current');
                        $incoming.animate({'opacity': 1}, 'slow', 'swing', function() {
                            $list.append($outgoing);
                            $outgoing.css('opacity', 1);                
                            $list.list_rotate();
                        });
                    });
                });                
            };
            
            // don't fade the next image in until it's loaded
            var incoming_images = $incoming.find('img');
            if (incoming_images.length) {
                var img = document.createElement('img');
                $(img).load(function(){
                    fade_next();
                });
                img.src = incoming_images.get(0).src;
            } else {
                fade_next();
            }        
            
        }, settings.delay);
    }
}

jQuery.fn.linked_list = function() {
    var $list_items = this.children('li');
    
    if ($list_items.length == 0) {
        return;
    }

    if ($list_items.length == 1) {
        $list_items.get(0).next_item = $list_items.get(0);
        $list_items.get(0).previous_item = $list_items.get(0);
        return;
    }
    
    for (var i=0; i<$list_items.length; i++) {
        if ($list_items.length > i+1) {
            $list_items.get(i).next_item = $list_items.get(i+1);
        } else {
            $list_items.get(i).next_item = $list_items.get(0);
        }
        
        if (i > 0) {
            $list_items.get(i).previous_item = $list_items.get(i-1);
        } else {
            $list_items.get(i).previous_item = $list_items.get($list_items.length-1);
        }        
    }
}

$(document).ready(function(){
    
    $('ul.list_rotate').list_rotate();
    
    $('ul.text_links li a.image_link_trigger').mouseover(function(e){
        $('#'+this.id.replace(/text_link_/,'image_link_')).addClass('selected');
    }).mouseout(function(e){
        $('#'+this.id.replace(/text_link_/,'image_link_')).removeClass('selected');
    });
    
    $('ul.image_links li a.text_link_trigger').mouseover(function(e){
        $('#'+this.id.replace(/image_link_/,'text_link_')).addClass('selected');
    }).mouseout(function(e){
        $('#'+this.id.replace(/image_link_/,'text_link_')).removeClass('selected');
    });
    
    var mailtoMatch = function() {    
        return (null != this.href.match(/mailto:.+/));
    }
    
    $('body#people ul.text_links li a.image_link_trigger').click(mailtoMatch);    
    $('body#people ul.image_links li a.text_link_trigger').click(mailtoMatch);
    
    $('ol#pagination').linked_list();
    if ($('ol#pagination li').length) {
        var $hero = $('img#hero');
        if ($hero.length) {
            $hero.get(0).next_item = $('ol#pagination li').get(0).next_item;
            $hero.css('cursor', 'pointer');
            $hero.click(function() {
                $(this.next_item).children('a').trigger('click');
            });
        }
    }
    
    if (window.location.hash) {
        var $hashElement = $(""+window.location.hash);
        if ($hashElement.length) {
            // console.log($hashElement.closest("ul"));
            $hashElement.closest("div.paged").children("div.page").css("display", "none");
            $hashElement.closest("div.page").css("display", "block");
        }        
    }

    $("body#project div.paged").children("div.page").css("display", "none");
    // console.log($("body#project div.paged").length);
    $("body#project div.paged").children("div.page").first().css("display", "block");

    $("body#contact ul#offices li h2 a").click(function() {
        $clickedLink = $(this);
        $officesList = $clickedLink.closest('ul');
        $officesList.find('a.selected').removeClass('selected');
        $officesList.find('img.map').css('display', 'none');
        
        $office = $clickedLink.closest('li');
        $office.find('img.map').css('display', 'block');
        $clickedLink.addClass('selected');
        
        return false;
    });    
    
    $("body#project a.trigger").click(function() {
        $(this).closest('ol').find('a.selected').removeClass('selected');
        $(this).addClass('selected');
        var $fragment = $(this.href.replace(/[^#]*(#[^#]+$)/, "$1"));
        $fragment.closest("div.paged").children("div.page").css("display", "none");
        $fragment.closest("div.page").css("display", "block");
        return false;
    });

    var multicolumns = function (){ 
        var newDiv = document.createElement('div');
        $(newDiv).css('width', '40px').css('-webkit-column-count', '2').css('-moz-column-count', '2').css('column-count', '2');

        var inner1 = document.createElement('div');
        inner1.style.height = '20px';
        var inner2 = document.createElement('div');
        inner2.style.height = '20px';

        newDiv.appendChild(inner1);
        newDiv.appendChild(inner2);

        document.body.appendChild(newDiv);
        var divHeight = newDiv.offsetHeight;
        document.body.removeChild(newDiv);

        return divHeight < 40;
    }

    if (!multicolumns()) {
      $('#content div#text_column.text_column_2').columnize({ columns: 2 });
      $('#content div#text_column.text_column_4').columnize({ columns: 4 });
    }

});
