/*
Convenience function that automatically creates combined classes 
for <IE7 since it does not handle multiple classes like other browsers.
*/

$.fn.getSectionClasses = function (classes)
{
    var section = this.attr('section')
    
    if (!section)
    {
        section = this.attr('subsection')
    }
    
    if (section)
    {
        return classes.split(' ').map(function (c)
        {
            return section+'_'+c
        }).join(' ')
    }
    
    return ''
}

$.fn.addSectionClass = function (classes)
{
    this.addClass(classes)
    this.addClass(this.getSectionClasses(classes))
    return this
}

$.fn.removeSectionClass = function (classes)
{
    this.removeClass(classes)
    this.removeClass(this.getSectionClasses(classes))
    return this
}



if (typeof cooper == "undefined")
{
    cooper = {}
}


cooper.Navigator = function (scrolling)
{
    this.scrolling = scrolling
    this.selected_section = null
    this.selected_subsection = null
    this.sections = $('#content .section')
    this.menu_swap_delay = 1200
    this.body = $('body')
    this.section_changed = new Event()
    
    this.setup_menu()
    this.setup_image_queue()
    
    var me = this
    
    if (scrolling)
    {
        this.mouse = {
            x: 0,
            y: 0
        }
        
        $(document).mousemove(function (e)
        {
            this.mouse.x = e.pageX
            this.mouse.y = e.pageY
        }.bind(this))
        
        this.setup_scrolling()
        
        var mouse_moved = false
        var set_mouse_moved = function ()
        {
            mouse_moved = true
        }
        
        this.section_scroll_start.bind(function ()
        {
            $(document).bind('mousemove', set_mouse_moved)
        })
        
        this.section_scroll_complete.bind(function (change)
        {
            $(document).unbind('mousemove', set_mouse_moved)
            
            if (!mouse_moved)
            {
                this.mouse.x += change.left
                this.mouse.y += change.top
            }
            
            mouse_moved = false
        }.bind(this))
        
        var update_sections_offset = function ()
        {
            var sections_offset = parseInt(($(window).width()-1020)/2)
            
            this.sections.eq(0).css(
                'margin-left',
                 sections_offset + 'px'
            )
            
            this.sections.eq(this.sections.length).css(
                'margin-right',
                 sections_offset + 'px'
            )
            
            $('#content').css('width', 
                ((1070 * this.sections.length) + (sections_offset * 2) + 10)+'px')
            
        }.bind(this)
        
        update_sections_offset()
        
        $(window).resize(update_sections_offset)
        
        this.setup_section_detection()
        this.setup_horizontal_nav()
        this.setup_scrolling_bounds()
        
        setTimeout(function () 
        {
            this.set_section(this.selected_section, this.selected_subsection, false)
            $('#content').addClass('visible')
        }.bind(this), 50)
    }
    else
    {
        $('#content').addClass('visible').css('width', $(window).width())
        
        $(window).resize(function ()
        {
            $('#content').css('width', $(window).width())
        })
        
        $('#content .section').css({
                marginLeft: 'auto',
                marginRight: 'auto'
        })
        
        $('[section-link]').click(function (e)
        {
            var target = $(e.target)
            
            if (target.is('a'))
            {
                return
            }
            
            var parts = $(this).attr('section-link').split(':')
            window.location.href = '/'+parts[0]+(parts[1] ? '/'+parts[1]+'.html' : '.html')
        })
    }
    
    $('.section.work div.slides').each(function ()
    {
        new cooper.SlideNavigator($(this))
    })
}

cooper.Navigator.prototype = 
{
    fps: 32,
    rate: 1.1214,
    
    scroll_timeout: null,
    
    is_mouse_over: function (element)
    {
        var pos = element.offset(),
            w = element.outerWidth(),
            h = element.outerHeight()
        
        var diff = {
                x: {
                    x1: pos.left,
                    x2: pos.left + w,
                    m: this.mouse.x
                },
                y: {
                    y1: pos.top,
                    y2: pos.top + h,
                    m: this.mouse.y
                }
        }
        
        if ((pos.top <= this.mouse.y && this.mouse.y <= pos.top + h) &&
            (pos.left <= this.mouse.x && this.mouse.x <= pos.left + w))
        {
            return true
        }
        
        return false
    },
    
    get_previous_section: function (section)
    {
        var prev = this.sections.filter('[section='+section+']').prev()
        
        if (!prev.is('.spacer'))
        {
            return prev
        }
    },
    
    get_next_section: function (section)
    {
        var next = this.sections.filter('[section='+section+']').next()
        
        if (!next.is('.spacer'))
        {
            return next
        }
    },
    
    get_previous_subsection: function (section)
    {
        var prev = this.get_previous_section(section)
        
        if (!prev || prev.length == 0)
        {
            return
        }
        
        return { section: prev, subsection: this.detect_subsection(prev) }
    },
    
    get_next_subsection: function (section)
    {
        var next = this.sections.filter('[section='+section+']').next()
        
        if (!next || next.length == 0)
        {
            return
        }
        
        return { section: next, subsection: this.detect_subsection(next) }
    },
    
    detect_subsection: function (section)
    {
        var subsections = section.find('.subsection'),
            selected = null,
            win = $(window),
            scrolltop = win.scrollTop(),
            midline = parseInt(win.height() / 2)
        
        
        for (var i = 0; i < subsections.length; i++)
        {
            var selected = subsections.eq(i),
                pos = selected.offset(),
                height = selected.outerHeight()
            
            if ((pos.top > scrolltop &&
                pos.top < midline + scrolltop)
                ||
                (pos.top < scrolltop &&
                pos.top + height > midline + scrolltop))
            {
                break
            }
        }
        
        return selected
    },
    
    set_section: function (section, subsection, animate, scroll)
    {
        if (!$('.section_'+section).length)
        {
            section = 'home'
            subsection = null
        }
        
        if (!subsection)
        {
            subsection = this.sub_menus
                .find('ul[section="'+section+'"] li:first-child a')
                .attr('subsection')
        }
        
        this.set_menu(section, subsection)
        
        var old_section = this.selected_section,
            old_subsection = this.selected_subsection
        this.selected_section = section
        this.selected_subsection = subsection
        
        if (!this.scrolling)
        {
            this.load_section_images(section, subsection)
            return
        }
        
        var hash = '#'+section
        
        if (subsection)
        {
            hash += ':'+subsection
        }
        
        window.location.hash = hash
        
        this.section_changed.fire(old_section, old_subsection)
        
        if (typeof scroll == 'undefined' || scroll)
        {
            this.scroll_to(section, subsection, animate)
        }
    },
    
    set_menu: function (section, subsection)
    {
        this.main_menu
            .find('a.selected')
                .removeSectionClass('selected')
            .end().find('a.viewed')
                .removeSectionClass('viewed')
            .end().find('a[section="'+section+'"]')
                .addSectionClass('selected')
                
        this.view_menu(section, subsection)
    },
    
    view_menu: function (section, subsection)
    {
        this.main_menu
            .find('a.viewed')
                .removeSectionClass('viewed')
            .end().find(
                'a[section='+section+']:not(.selected)'
            ).addSectionClass('viewed')
            
        this.sub_menus
            .find('ul:not([section="'+section+'"])')
                .hide()
            .end().find('ul[section="'+section+'"]:hidden')
                .fadeIn(250)
        
        var sub_menu = this.sub_menus.find('ul[section="'+section+'"]')
        
        if (sub_menu.length == 0)
        {
            this.menu_bottom.removeClass('active')
            return
        }
        
        sub_menu
            .find('a.selected')
                .removeSectionClass('selected')
            .end().find('a[subsection="'+subsection+'"]')
                .addSectionClass('selected')
        
        this.menu_bottom.addClass('active')
    },
    
    _load_image: function (src, callback)
    {
        if (this.images_loaded[src])
        {
            if (callback)
            {
                callback()
            }
            
            return
        }
        
        this.images_loaded[src] = 1
        
        var img = new Image()
        
        img.onload = function ()
        {
            if (callback)
            {
                callback()
            }
        }.bind(this)
        
        img.src = src
    },
    
    load_image: function (elm, callback)
    {
        var src = elm.attr('deferred-src')
        elm.removeAttr('deferred-src')
        
        if (src)
        {
            this._load_image(src, function ()
            {
                elm.css('background-image', 'url('+src+')')
                
                if (callback)
                {
                    callback()
                }
            }.bind(this))
        }
        else if (callback)
        {
            callback()
        }
    },
    
    load_background_image: function (elm, callback)
    {
        var src = elm.attr('deferred-background')
        elm.removeAttr('deferred-background')
        
        if (src)
        {
            this._load_image(src, function ()
            {
                elm.css(
                    'background',
                    'transparent url('+src+') no-repeat top left'
                )
                
                if (callback)
                {
                    callback()
                }
            })
        }
        else if (callback)
        {
            callback()
        }
    },
    
    load_next_image: function ()
    {
        if (!this.image_queue_paused && this.image_queue.length > 0)
        {
            var elm = $(this.image_queue.shift())
            
            var next = function ()
            {
                setTimeout(this.load_next_image.bind(this), 0)
            }.bind(this)
            
            if (elm.attr('deferred-src'))
            {
                this.load_image(elm, next)
            }
            else if (elm.attr('deferred-background'))
            {
                this.load_background_image(elm, next)
            }
            else
            {
                next()
            }
        }
    },
    
    pause_image_queue: function ()
    {
        this.image_queue_paused = true
    },
    
    resume_image_queue: function ()
    {
        this.image_queue_paused = false
        this.load_next_image()
    },
    
    load_section_images: function (section, subsection)
    {
        this.pause_image_queue()
        
        var elm
        
        if (subsection)
        {
            elm = this.sections.filter('[section='+section+']').find('.subsection_'+subsection+'')
        }
        else
        {
            elm = this.sections.filter('[section='+section+']')
        }
        
        var foreground_elms = elm.find('[deferred-src]').get()
        var background_elms = elm.find('[deferred-background]').get()
        
        var foreground_left = foreground_elms.length
        var background_left = background_elms.length
        
        var check_if_done = function ()
        {
            if (foreground_left < 1 && background_left < 1)
            {
                this.resume_image_queue()
            }
        }.bind(this)
        
        foreground_elms.forEach(function (elm)
        {
            this.load_image($(elm), function ()
            {
                foreground_left--
                check_if_done()
            })
        }.bind(this))
        
        background_elms.forEach(function (elm)
        {
            this.load_background_image($(elm), function ()
            {
                background_left--
                check_if_done()
            })
        }.bind(this))
        
        check_if_done()
    },
    
    user_scroll: function ()
    {
        this.cancel_scroll()
    },
    
    cancel_scroll: function ()
    {
        if (this.scroll_timeout)
        {
            clearTimeout(this.scroll_timeout)
            this.in_scroll_animation = false
        }
    },
    
    scroll_to: function (section, subsection, animate)
    {
        this.cancel_scroll()
        
        if (typeof animate == 'undefined')
        {
            animate = true
        }
        
        var selector = 'div[section="'+section+'"]'
        
        var offset = {
            top: -53
        }
        
        if (subsection)
        {
            selector += ' div[subsection="'+subsection+'"]'
        }
        
        offset.left = -1 * ((this.window_width - 1020) / 2)
        
        var elm = $(selector)
        var win = $(window)
        var axes = ['top', 'left']
        var scrollfn = {
            top: win.scrollTop.bind(win),
            left: win.scrollLeft.bind(win)
        }
        var limits = {
            top: $('#content').height(),
            left: $('#content').width()
        }
        var pos = elm.offset() 
        var scroll = {
            top: {},
            left: {}
        }
        
        axes.forEach(function (axis)
        {
            scroll[axis].start = scrollfn[axis]()
            scroll[axis].end = pos[axis] + (offset[axis] ? offset[axis] : 0)
            scroll[axis].distance = (scroll[axis].end - scroll[axis].start)
            scroll[axis].direction = scroll[axis].distance < 0 ? -1 : 1
            scroll[axis].distance = Math.abs(scroll[axis].distance)
        })
        
        if (scroll.top.distance < 1 && scroll.left.distance < 1)
        {
            return
        }
        
        var change = {
            top: parseInt(scroll.top.distance * scroll.top.direction),
            left: parseInt(scroll.left.distance * scroll.left.direction)
        }
        
        this.section_scroll_start.fire()
        
        if (!animate)
        {
            this.in_scroll_animation = true
            
            win.scrollTop(scroll.top.end)
            win.scrollLeft(scroll.left.end)
            
            setTimeout(function ()
            {
                this.section_scroll_complete.fire(change)
            }.bind(this), 50)
            
            return
        }
        
        scroll.frame_length = 1000 / this.fps
        
        axes.forEach(function (axis)
        {
            var duration = this.easing.outquint((0.35 * limits[axis]), scroll[axis].distance, limits[axis])
            scroll[axis].num_frames = Math.floor(duration / scroll.frame_length)
        }.bind(this))
        
        var order
        
        if (scroll.top.distance && scroll.left.distance)
        {
            order = ['left', 'top']
            scroll.left.ease = this.easing.inquint
            scroll.top.ease = this.easing.outquint
        }
        else if (scroll.top.num_frames > scroll.left.num_frames)
        {
            order = ['left', 'top']
            scroll.left.ease = this.easing.inquint
            scroll.top.ease = this.easing.outquint
        }
        else
        {
            order = ['top', 'left']
            scroll.left.ease = this.easing.outquint
            scroll.top.ease = this.easing.inquint
        }
        
        scroll.cur_frame = 1
        
        var step = function (i)
        {
            axis = order[i]
            
            if (scroll.cur_frame >= scroll[axis].num_frames)
            {
                scrollfn[axis](scroll[axis].end)
                scroll.cur_frame = 1
                i += 1
            }
            else
            {
                scrollfn[axis](
                    scroll[axis].start + 
                    (scroll[axis].ease(
                        scroll[axis].distance,
                        scroll.cur_frame,
                        scroll[axis].num_frames
                    ) * scroll[axis].direction)
                )
                scroll.cur_frame += 1
            }
            
            if (i > 1)
            {
                this.section_scroll_complete.fire(change)
                return
            }
            
            this.scroll_timeout = setTimeout(function ()
            {
                step(i)
            }, scroll.frame_length)
        }.bind(this)
        
        this.in_scroll_animation = true
        this.pause_image_queue()
        
        this.scroll_timeout = setTimeout(function ()
        {
            step(0)
        }, 0)
    },
    
    setup_menu: function ()
    {
        this.main_menu = $('#main-menu')
        this.sub_menus = $('#sub-menus')
        this.menu_bottom = $('#menu-bottom')
        
        if (this.scrolling)
        {
            var me = this
            var link_prefix = '#'
            
            me.main_menu.find('a:not(.external)').each(function ()
            {
                var link = $(this)
                var section = link.attr('section')
                
                link.attr('href', link_prefix+section)
                
                me.sub_menus.find('.'+section+' a').each(function ()
                {
                    var sublink = $(this)
                    var subsection = sublink.attr('subsection')
                    
                    sublink.attr('href', link_prefix+section+':'+subsection)
                })
            })
        }
        
        var me = this
        var menu_swap_timer = null
        var hover_wait_timer = null
        var one_time_swap_block_toggle = false
        
        var clear_menu_swap_timer = function ()
        {
            if (menu_swap_timer)
            {
                clearTimeout(menu_swap_timer)
                menu_swap_timer = null
            }
        }
        
        var set_menu_swap_timer = function ()
        {
            if (one_time_swap_block_toggle)
            {
                one_time_swap_block_toggle = false
                return
            }
            
            if (!menu_swap_timer)
            {
                menu_swap_timer = setTimeout(function ()
                {
                    me.set_menu(me.selected_section, me.selected_subsection)
                }, this.menu_swap_delay)
            }
        }
        
        this.main_menu.find('a')
            .hover(
                function ()
                {
                    clear_menu_swap_timer()
                    
                    if (hover_wait_timer)
                    {
                        clearTimeout(hover_wait_timer)
                    }
                    
                    hover_wait_timer = setTimeout(function ()
                    {
                        me.view_menu($(this).attr('section'))
                    }.bind(this), 200)
                },
                set_menu_swap_timer
            )
            .click(function (e)
            {
                var link = $(this)
                clear_menu_swap_timer()
                one_time_swap_block_toggle = true
                
                if (!link.is('.external'))
                {
                    me.set_section(link.attr('section'))
                }
            })
        
        this.sub_menus
            .find('a')
                .hover(
                    function ()
                    {
                        if (hover_wait_timer)
                        {
                            clearTimeout(hover_wait_timer)
                        }
                        
                        clear_menu_swap_timer()
                    },
                    set_menu_swap_timer
                )
            .end().find('ul')
                .each(function ()
                {
                    var menu = $(this)
                    var section = menu.attr('section')
                    
                    menu.find('a[subsection]').click(function (e)
                    {
                        clear_menu_swap_timer()
                        one_time_swap_block_toggle = true
                        me.set_section(section, $(this).attr('subsection'))
                    })
                })
    },
    
    setup_image_queue: function ()
    {
        this.images_loaded = {}
        var deferred_images = $('#content').find('.deferred,[deferred-background]')
        this.image_queue = deferred_images.get()
        
        deferred_images.each(function ()
        {
            var elm = $(this)
            
            if (elm.attr('width') && elm.attr('height'))
            {
                elm.css({
                    width: elm.attr('width') + 'px',
                    height: elm.attr('height') + 'px'
                })
            }
        })
        
        this.load_next_image()
    },
    
    setup_scrolling: function ()
    {
        var me = this
        var win = $(window)
        
        this.section_scroll_start = new Event()
        this.section_scroll_complete = new Event()
        
        this.in_scroll_animation = false
        
        this.section_scroll_complete.bind(function ()
        {
            this.in_scroll_animation = false
            this.load_section_images(this.selected_section, this.selected_subsection)
            last_left_scroll = win.scrollLeft()
        }.bind(this))
        
        $('[section-link]').click(function (e)
        {
            var target = $(e.target)
            
            if (target.is('a') && !target.attr('section-link'))
            {
                return
            }
            
            var parts = $(this).attr('section-link').split(':')
            me.set_section(parts[0], parts[1] ? parts[1] : null)
        }).each(function ()
        {
            var elm = $(this)
            elm.attr('href', '#' + elm.attr('section-link'))
        })
        
        var update_window_dimensions = function ()
        {
            this.window_width = win.width()
            this.window_height = win.height()
        }.bind(this)
        
        win.resize(function ()
        {
            update_window_dimensions()
            this.scroll_to(this.selected_section, this.selected_subsection, false)
        }.bind(this))
        
        update_window_dimensions()
        
        this.selected_section = 'home'
        this.selected_subsection = null
        var hash = window.location.hash
        
        if (hash && hash != '#')
        {
            parts = hash.substr(1).split('?')[0].split(':')
            this.selected_section = parts[0]
            
            if (parts.length == 2)
            {
                this.selected_subsection = parts[1]
            }
        }
    },
    
    setup_section_detection: function ()
    {
        var win = $(window)
        var last_left_scroll = win.scrollLeft()
        var viewport_midline_vert = parseInt(win.height() / 2)
        var viewport_midline_horiz = parseInt(win.width() / 2)
        var update_timeout = null
        
        var check = function (section, subsection)
        {
            if (section != this.selected_section || subsection != this.selected_subsection)
            {
                var old_section = this.selected_section
                var old_subsection = this.selected_subsection
                this.set_section(section, subsection, false, false)
            }
        }.bind(this)
        
        var _update = function ()
        {
            if (this.in_scroll_animation)
            {
                return
            }
            
            var section = null
            var scrollleft = win.scrollLeft()
            
            if (scrollleft != last_left_scroll)
            {
                for (var i = 0; i < this.sections.length; i++)
                {
                    var s = this.sections.eq(i)
                    var sname = s.attr('section')
                    
                    if (!sname)
                    {
                        continue
                    }
                    
                    if (sname == this.selected_section)
                    {
                        continue
                    }
                    
                    var pos = s.offset()
                    var width = s.width()
                    
                    if ((pos.left > scrollleft &&
                        pos.left < viewport_midline_horiz + scrollleft)
                        ||
                        (pos.left < viewport_midline_horiz + scrollleft &&
                        pos.left + width > viewport_midline_horiz + scrollleft))
                    {
                        section = s
                        break
                    }
                }
            }
            
            last_left_scroll = scrollleft
            
            if (section == null || section.is('.spacer'))
            {
                section = this.sections.filter('[section='+this.selected_section+']')
            }
            
            var subsections = section.find('.subsection')
            
            if (subsections.length > 0)
            {
                var found_match = false
                
                for (var i=0; i<subsections.length; i++)
                {
                    var s = subsections.eq(i)
                    var sname = s.attr('subsection')
                    
                    if (sname == this.selected_subsection && section.attr('section') == this.selected_subsection)
                    {
                        continue
                    }
                    
                    var pos = s.offset()
                    var height = s.height()
                    var scrolltop = win.scrollTop()
                    
                    if ((pos.top > scrolltop &&
                        pos.top < viewport_midline_vert + scrolltop)
                        ||
                        (pos.top < scrolltop &&
                        pos.top + height > viewport_midline_vert + scrolltop))
                    {
                        found_match = true
                        check(section.attr('section'), sname, -1)
                        break
                    }
                }
                
                if (!found_match)
                {
                    check(section.attr('section'), sname, -1)
                }
            }
            else
            {
                check(section.attr('section'), null, -1)
            }
        }.bind(this)
        
        var update = function ()
        {
            if (update_timeout)
            {
                clearTimeout(update_timeout)
            }
            
            update_timeout = setTimeout(_update, 150)
        }
        
        win
            .resize(function ()
            {
                viewport_midline_vert = parseInt(win.height() / 2)
                viewport_midline_horiz = parseInt(win.width() / 2)
            })
            .scroll(update)
        
        this.section_scroll_complete.bind(update)
        this.detect_section = update
    },
    
    setup_horizontal_nav: function ()
    {
        var button_left = E('div', 'horizontal-nav horizontal-nav-left',
                E('img').attr('src', '/images/lateral_nav_left.png')
            ).appendTo(this.body)
        var button_right = E('div', 'horizontal-nav horizontal-nav-right',
                E('img').attr('src', '/images/lateral_nav_right.png')
            ).appendTo(this.body)
        var menu_height = $('#menu-top').height()
        var win = $(window)
        var arrow_height = button_left.find('img').height()
        
        var update_button_size = function ()
        {
            var w = Math.floor((win.width() - 1020) / 2) - 50
            var h = (win.height() - 26)
            var arrow_t = ((h / 2) - (arrow_height / 2))
            
            button_left.css({
                width: w + 'px',
                height: h + 'px'
            }).find('img').css('margin-top', arrow_t + 'px')
            
            button_right.css({
                width: w + 'px',
                height: h + 'px'
            }).find('img').css('margin-top', arrow_t + 'px')
        }
        
        setTimeout(update_button_size, 10)
        
        $(window).resize(update_button_size)
        
        var prev, next
        
        var update_button_targets = function ()
        {
            prev = this.get_previous_subsection(this.selected_section)
            next = this.get_next_subsection(this.selected_section)
            
            if (prev)
            {
                button_left.show()
            }
            else
            {
                button_left.hide()
            }
            
            if (next)
            {
                button_right.show()
            }
            else
            {
                button_right.hide()
            }
        }.bind(this)
        
        this.section_changed.bind(update_button_targets)
        this.section_scroll_complete.bind(update_button_targets)
        
        button_left.click(function (e)
        {
            e.preventDefault()
            
            prev = this.get_previous_subsection(this.selected_section)
            
            if (prev)
            {
                this.set_section(
                    prev.section.attr('section'),
                    prev.subsection ? prev.subsection.attr('subsection') : null
                )
            }
        }.bind(this))
        
        button_right.click(function (e)
        {
            e.preventDefault()
            
            next = this.get_next_subsection(this.selected_section)
            
            if (next)
            {
                this.set_section(
                    next.section.attr('section'),
                    next.subsection ? next.subsection.attr('subsection') : null
                )
            }
        }.bind(this))
    },
    
    setup_scrolling_bounds: function ()
    {
        var win = $(window)
        var last_left_scroll = win.scrollLeft()
        var vscroll_bound = 0
        var handle_section_change = function ()
        {
            vscroll_bound = this.sections
                .filter('[section='+this.selected_section+']')
                .height() - 350
        }.bind(this)
        
        handle_section_change()
        
        this.section_changed.bind(handle_section_change)
        
        var vsnap_timeout = null
        
        win.scroll(function ()
        {
            if (vsnap_timeout)
            {
                clearTimeout(vsnap_timeout)
            }
            
            last_left_scroll = win.scrollLeft()
            
            if (this.in_scroll_animation)
            {
                return
            }
            
            win.scrollLeft(last_left_scroll)
            
            if (win.scrollTop() > vscroll_bound)
            {
                vsnap_timeout = setTimeout(function ()
                {
                    $.scrollTo(vscroll_bound, {
                        axis: 'y',
                        duration: 500
                    })
                }, 50)
            }
        }.bind(this))
    },
    
    easing: {
        linear: function (distance, cur, total)
        {
            return distance * (cur / total)
        },
        inquint: function (distance, cur, total)
        {
            return distance * ((cur /= total) * cur * cur * cur * cur)
        },
        outquint: function (distance, cur, total)
        {
            return distance * ((cur = cur / total - 1) * cur * cur * cur * cur + 1)
        }
    }
}


cooper.SlideNavigator = function (container)
{
    this.container = container
    this.scrolling_container = null
    
    if ($.browser.msie)
    {
        var version = $.browser.version.substr(0,1)
        
        if (version == '6' || version == '7')
        {
            var container = $(document.createElement('div'))
                .addClass('slides-inner-container')
                .prependTo(this.container)
            this.container.find('ul.slides').appendTo(container)
            this.scrolling_container = container
        }
    }
    
    this.container
        .find('.slide.alt')
            .addClass('slide_alt')
        .end()
    
    var selected = this.container.find('.controls .selected')
    
    if (selected.length > 0)
    {
        if (selected.is('.alt'))
        {
            selected.addClass(selected.attr('slide')+'_selected_alt')
        }
        else
        {
            selected.addClass(selected.attr('slide')+'_selected')
        }
        
        this.container.find('li.slide[slide='+selected.attr('slide')+']').addClass('selected')
    }
    
    if (!this.scrolling_container)
    {
        this.scrolling_container = container
    }
    
    var me = this
    
    this.container.find('.controls a').click(function (e)
    {
        e.preventDefault()
        
        var slide = me.container.find('li.slide[slide='+$(this).attr('slide')+']')
        
        if (slide.length)
        {
            me.select_slide(slide)
        }
    })
    
    this.container
        .find('.arrow.next')
            .click(function (e)
            {
                e.preventDefault()
                
                var next = me.container.find('li.slide.selected').next()
                
                if (!next.length)
                {
                    next = me.container.find('li.slide:first-child')
                }
                
                me.select_slide(next)
            })
       .end()
       .find('.arrow.previous')
            .click(function (e)
            {
                e.preventDefault()
                
                var prev = me.container.find('li.slide.selected').prev()
                
                if (!prev.length)
                {
                    prev = me.container.find('li.slide:last-child')
                }
                
                me.select_slide(prev)
            })
        .end()
        .find('a.arrow')
            .hover(
                function ()
                {
                    $(this).stop(true).removeClass('hidden').css('opacity', 0.01).animate({opacity: 1}, 250)
                },
                function ()
                {
                    $(this).stop(true).animate({opacity: 0.01}, 250)
                }
            )
}

cooper.SlideNavigator.prototype = 
{
    select_slide: function (slide)
    {
        this.scrolling_container.scrollTo(slide, {
            axis: 'x',
            duration: 750,
            easing: 'easeOutQuint'
        })
        
        var selected_slide = this.container.find('li.slide.selected')
        
        if (selected_slide.length > 0)
        {
            selected_slide.removeClass('selected')
            
            var controls = this.container.find('.controls .selected')
            
            controls.removeClass('selected')
            
            if (controls.length > 0)
            {
                if (controls.is('.alt'))
                {
                    controls.removeClass(selected_slide.attr('slide')+'_selected_alt')
                }
                else
                {
                    controls.removeClass(selected_slide.attr('slide')+'_selected')
                }
            }
        }
        
        var selected = this.container.find('.controls [slide='+slide.attr('slide')+']')
        selected.addClass('selected')
        
        if (selected.is('.alt'))
        {
            selected.addClass(slide.attr('slide')+'_selected_alt')
        }
        else
        {
            selected.addClass(slide.attr('slide')+'_selected')
        }
                
        slide.addClass('selected')
    }
}
