// All JavaScript copyright Jon Raasch, a Portland-based JS Ninja (http://jonraasch.com)

$.fn.equals = function(compareTo) {
    if (!compareTo || !compareTo.length || this.length!=compareTo.length) return false;
    for (var i=0; i<this.length; i++) {
        if (this[i]!==compareTo[i]) return false;
    }
    return true;
};

var Site = {
    toolkit: {
        whichChild : function( $child, $parent, $children ) {
            // if you have to determine children
            if ( typeof $children == 'undefined' ) {
                if ( typeof $parent == 'undefined' ) $parent = $child.parent();
                $children = $parent.children();
            }
            
            for ( var i = 0; i < $children.length; i++ ) {
                if ( $child.equals( $($children[i]) ) ) return i;
            }
        }
    },
    
    menu : {
        obj : {},
        pos : [],
        active : 0,
        atRest : 0,
        tickHalfWidth : 6,
        
        init : function() {
            Site.menu.obj.wrapper = $('#menu');
            Site.menu.obj.items = Site.menu.obj.wrapper.children('li');
            
            var posLeft = 0;
            
            Site.menu.obj.items.each(function(i) {
                var $this = $(this);
                if ( $this.hasClass('active') ) Site.menu.active = i;
                
                // calculate widths / positions
                var theWidth = $(this).width();
                
                Site.menu.pos[i] = posLeft + ( theWidth / 2 ) - Site.menu.tickHalfWidth;
                posLeft += theWidth;
            });
            
            // append tick
            Site.menu.obj.tick = $('<li class="tick"></li>').prependTo( Site.menu.obj.wrapper );
            
            Site.menu.obj.tick.css('left', Site.menu.pos[ Site.menu.active ]);
            
            // add loaded class to menu
            Site.menu.obj.wrapper.addClass('loaded');
                        
            Site.menu.obj.items.hover( Site.menu.mouseover, Site.menu.mouseout );
        },
        
        mouseover : function(ev) {
            clearTimeout( Site.menu.to );
            
            $this = $(this);
            var i = Site.toolkit.whichChild($this, null, Site.menu.obj.items);
            
            Site.menu.move_it(i);
        },
        
        mouseout : function(ev) {
            Site.menu.to = setTimeout( Site.menu.revert, 200 );
        },
        
        revert : function() {
            Site.menu.move_it( Site.menu.active );
        },
        
        move_it : function(i) {
            Site.menu.obj.tick.queue([]);
            Site.menu.obj.tick.animate({ left : Site.menu.pos[i]}, 500, 'swing');
        }
    }
};

/*** 
Simple jQuery Slideshow Script
Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
    var $active = $('DIV.active', $slideshow);

    if ( $active.length == 0 ) $active = $('DIV:last', $slideshow);

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('DIV:first', $slideshow);

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

var $slideshow;

$(function() {
    Site.menu.init();
    
    $('a.youtubin').youtubin({
        swfWidth : 560,
        swfHeight : 340
    });
    
    if ( !$.browser.msie ) {
    $('a.youtubin-click').youtubin({
        replaceTime : 'click',
        swfWidth : 560,
        swfHeight : 340
    });
    }
    
    $slideshow = $('#slideshow');
        
    if ($slideshow) setInterval( slideSwitch, 4000 );
});