if (typeof Cav === 'undefined')
{
    var Cav = {};
}

(function()
{
    
    
    /*
     * jQuery uuid function
     * http://plugins.jquery.com/project/uuid
     * 
     *   Usage 1: define the default prefix by using an object with the property prefix as a parameter which contains a string value; {prefix: 'id'}
     *   Usage 2: call the function jQuery.uuid() with a string parameter p to be used as a prefix to generate a random uuid;
     *   Usage 3: call the function jQuery.uuid() with no parameters to generate a uuid with the default prefix; defaul prefix: '' (empty string)
     */
    
    /*
     *   Generate fragment of random numbers
     */
    jQuery._uuid_default_prefix = '';
    jQuery._uuidlet = function () {
        return(((1+Math.random())*0x10000)|0).toString(16).substring(1);
    };
    /*
     *   Generates random uuid
     */
    jQuery.uuid = function (p)
    {
        if (typeof(p) == 'object' && typeof(p.prefix) == 'string')
        {
            jQuery._uuid_default_prefix = p.prefix;
        } 
        else
        {
            p = p || jQuery._uuid_default_prefix || '';
            return(p+jQuery._uuidlet()+jQuery._uuidlet()+"-"+jQuery._uuidlet()+"-"+jQuery._uuidlet()+"-"+jQuery._uuidlet()+"-"+jQuery._uuidlet()+jQuery._uuidlet()+jQuery._uuidlet());
        };
    };
    
    /*
     * Element wrapper; acts as getter/setter for uuid
     */
    jQuery.fn.uuid = function()
    {
           if (!this[0].uuid)
           {
               this[0].uuid = jQuery.uuid(this);
           }
           
           return this[0].uuid;
    }
    

    
    Cav.ExpanderList = function(parent, container, options)
    {
        var expanderList = this;
        
        this.parent    = parent;
        this.container = $(container);
        this.options   = $.extend({}, options);
        this.expanders = [];
        this.current   = {};
        
        this.container.find(options.expanderSelector).each(function()
        {
            var expander = $(this), 
                expandTarget = expander.find(options.expandTarget+':first').hide();
            
            $(this).find(options.clickTarget).click(function()
            {
                if (options.exclusive && expanderList.current.length)
                {
                    //expanderList.toggle(expanderList, expandTarget);
                }
                expanderList.toggle(expander, expandTarget);
            });
        });
    };
    
    Cav.ExpanderList.prototype = {
        toggle: function(expander, expandTarget)
        {
            var transitionIndex;
            
            // Are we opening or closing?
            if (expander.hasClass('on')) 
            {
                expander.removeClass('on');
                transitionIndex = 2;
            }
            else 
            {
                expander.addClass('on');
                transitionIndex = 3;
            }
            
            // Do the animations
            for (var i = 0, l = this.options.transitions.length; i < l; i++) 
            {
                var transition = this.options.transitions[i], animations = {};
                animations[transition[1]] = transition[transitionIndex];
                expander.find(transition[0]).animate(animations);
            }
            // Slide the body down
            expandTarget.slideToggle();
        }
    };
    
    Cav.Resume = function(container)
    {
        return {
            init: function()
            {
                var resume          = this;
                this.sections       = $('#content div.block').hide();
                this.currentExperience = {};
                this.currentSection = null;
                
                this.expanderList = new Cav.ExpanderList(this, '#experience div.body > ul:first',
                {
                    exclusive: true,
                    expanderSelector: '> li',
                    clickTarget: 'div.title',
                    expandTarget: 'ul',
                    transitions: [
                        ['div.title', 'backgroundColor', '#ffffff', '#000000'],
                        ['div.title', 'paddingLeft', '0em', '1em'],
                        ['div.title h3', 'color', '#808080', '#ffffff'],
                        ['div.title span', 'color', '#000000', '#808080'],
                        ['div.title strong', 'color', '#000000', '#94C5E3']
                    ]
                }); 
    
                // Rollover element
                this.arrow = Cav.Resume.Arrow.init();
                
                // Add section show/hide animation
                $('#sidenav li a').each(function()
                {
                    var $this = $(this), section = $this.attr("href");
                    
                    // Only inline links
                    if ( !/^#/.test( section ) ) { return; }
                    
                    $this.click(function(e)
                    {
                        e.preventDefault();
                        
                        resume.currentSection = $( section );
                        resume.toggle( resume.currentSection );
                        
                        return false;
                    });
                });
                
                // Delegated rollovers
                $('#experience').mouseover(function(e)
                {
                    return resume.experienceOver(e);
                });
                
                resume.toggle( resume.sections.eq(0) );
            },
            
            // Show/hide a resume section
            toggle: function( el )
            {
                var resume   = this;
                
                this.sections.fadeOut(500);
                el.fadeIn(500);
            },
            
            // Show/hide the arrow
            experienceOver: function(e)
            {
                
                var target = $(e.target),
                    parents,
                    pos,
                    uuid = target.uuid();
                    
                // Cache parent elements for better performance
                if ( typeof this.parentCache === 'undefined' )
                {
                    this.parentCache = {};
                }
                if ( !this.parentCache[ uuid ] )
                {
                    this.parentCache[ uuid ] = target.parents('li.company:first');
                }
                
                parents = this.parentCache[ uuid ];
                
                if (parents.length < 1)
                {
                    return;
                }
                
                target = parents.eq(0);
                
                pos = target.position();
                
                this.arrow.slideTo(-20, pos.top);
            }
    
        };
    };
    
    Cav.Resume.Arrow = function()
    {
        return {
            init: function()
            {
                this.element = $('<div class="experience-arrow"></div>');
                
                $('#experience .body').append(this.element);
                
                return this;
            },
            
            /*
             * slide the arrow to an absolute pixel position
             */
            slideTo: function( left, top )
            {
                this.element.animate({
                    left: left + 'px',
                    top:  top  + 'px'
                }, {
                    duration: 30
                });
                
                return this;
            },
            
            show: function()
            {
                // Prevent slowdown from unnecessary opacity changes
                if ( this.visible === true ) { return; }
                
                this.visible = true;
    
                this.element.show();
                
                return this;
            },
            
            hide: function()
            {
                // Prevent slowdown from unnecessary opacity changes
                if ( !this.visible === false ) { return; }
                
                this.visible = true;
           
                this.element.hide();
    
                return this;
            }
        }
    }();
})();

$(function()
{
    $('#content').css('height', $('#sidenav').height() + 'px'); 
    
    Cav.Resume().init();
});
