/* - - - - - - - - - - - - - - - - - -
jQuery & java scripting by Winston Hoy

Thanks:
http://jQuery.com                                               : John Resig & jquery
http://plugins.jquery.com/project/backgroundPosition-Effect     : Alexander Farkas

contact me if you want to use it
winstonhoy@gmail.com
 - - - - - - - - - - - - - - - - - - -*/
$(document).ready(function () {
// ---- Immediately
    $('body').css('visibility', 'hidden');
    // When everything is ready
        $(window).load(function() {
        // initialize site
            nav.initialize_site();
            nav.set_font_size();
            nav.set_pullouts();
            
            $('body').css('visibility', 'visible');
            
            nav.nav_transition();
        });
        
// ----- CMS
    // Create our nav object and overwrite methods
        window.nav = new Default_nav();
        
        nav.set_font_size = function () {
            $('body').css('font-size', $(window).width()*.0125);
        }
        
        nav.bind_resize = function () {
            $(window).bind('resize', function () {
                nav.set_font_size();
            });
        }
        nav.anchor_bind = function (anchor) {
            if ($(anchor).attr('href') == '/') {
                $.address.value(default_page);
            }
        // absolute link, page
            else if ($(anchor).attr("href").search("/") === 0) {
                $.address.value($(anchor).attr('href'));
            }
        // relative link (not a new page)
            else {
                var path_pop = $.address.pathNames().pop();
                var path = $.address.path();
            // if theres already a pullout
                if ($("#"+path_pop).hasClass('pullout')) {
                // if the pullout is the same as requested
                    if (path_pop == $(anchor).attr('href')) {
                    // close the pullout
                        var path_change = path.replace($(anchor).attr('href'), "")
                        $.address.value(path_change);
                    }
                    else {
                        var path_change = path.replace(path_pop, "") + "/" + $(anchor).attr('href');
                        $.address.value(path_change);
                    }
                }
                else {
                    $.address.value(path + "/" + $(anchor).attr('href')); 
                }
            }
        }
        
        nav.set_active_primary = function () {
        }
        
        nav.process_url = function () {
        // clear values
            nav.active_pullout = null;
            nav.active_video = null;
        
        // if there was a path passed
            pathNames = $.address.pathNames();
            
            if (pathNames.length == 2) {
                nav.active_page = pathNames.shift();
                nav.active_pullout = pathNames.shift();
                if ($("#"+nav.active_pullout).hasClass('video')) {
                    nav.active_video = nav.active_pullout;
                    nav.active_pullout = null;
                }
            }
            
            else if (pathNames.length == 1) {
                nav.active_page = pathNames.shift();
                if ($("#"+nav.active_page).hasClass('pullout')) {
                    nav.active_pullout = nav.active_page;
                    nav.active_page = nav.default_page;
                }
            }
            
            else if (pathNames.length == 3) {
                nav.active_page = pathNames.shift();
                nav.active_video = pathNames.shift();
                nav.active_pullout = pathNames.shift();
            }
            
            else {
                $.address.path(default_page);
            }
            
        // Validate active contents
            if (!$("#"+nav.active_page).exists()) { nav.active_page = default_page; };
            if ((nav.active_video !== null && !$("#"+nav.active_video).exists()) || (nav.active_pullout !== null && !$("#"+nav.active_pullout).exists() && nav.active_page == 'player')) { nav.active_page = 'landing'; $.address.value('landing'); nav.active_video = null; };
            if (nav.active_pullout !== null && !$("#"+nav.active_pullout).exists()) { nav.active_pullout = null; };
            
            return true;
        
        /*
            var path = $.address.pathNames()[0];
            
            if (path !== '') {
                this.active_page = path;
                this.active_primary = path;
                return true;
            }
        */
        }
        
        nav.nav_transition = function () {
            active_page = $("#"+nav.active_page);
            
            if (!nav.active_pullout && $(".active.pullout").exists()) {
                nav.remove_pullout();
            }
            
            nav.get_page();
            
        }
        
    // page functions
        nav.get_page = function () {
        // Does a page already exist
            if ($("body>.page").exists()) {
            // Not this page, remove it
                if ($("body>.page").attr("id") !== nav.active_page) {
                    nav.remove_page();
                }
                else {
                    if (nav.active_video) {
                        nav.get_video();
                    }
                    else if (nav.active_pullout) {
                        nav.get_pullout();
                    }
                }
            }
            else {
                nav.show_page();
            }
        }
        
        nav.show_page = function () {
        // add class to body
            $('body').addClass(nav.active_page);
            
        // Get video
            if (nav.active_video) {
                nav.get_video();
            }
            
            active_page = $("#"+nav.active_page);
            active_page.hide().appendTo('body');
            active_page.fadeIn(nav.animation_duration, function () {
                if (!nav.active_video && nav.active_pullout) {
                    nav.get_pullout();
                }
            });
            
            if ($("#primary_nav").css("left") !== "0px") { $("#primary_nav").animate({ right: 0 }, animation_duration/2); }
        // Call enter function
            if (nav.active_page == 'videos') { gallery.enter(); }
            else if (nav.active_page == 'player') { player.enter(); }
            else if (nav.active_page == 'landing') { landing.enter(); }
            
        }
        
        nav.remove_page = function () {
        // remove class from body
            $('body').removeClass($('body>.page').attr('id'));
        
        // Call leave function
            if ($('body>.page').attr('id') == 'videos') { gallery.leave(); }
            else if ($('body>.page').attr('id') == 'player') { player.leave(); }
            else if ($('body>.page').attr('id') == 'landing') { landing.leave(); }
            
            if ($('.pullout.active').exists()) {
                $('.pullout.active').queue(function () {
                    $("body>.page").fadeOut(animation_duration/2, function () {
                        $(this).appendTo("#page_container");
                        nav.show_page();
                    });
                    $(this).dequeue();
                });
            }
            else {
                $("body>.page").fadeOut(animation_duration/2, function () {
                    $(this).appendTo("#page_container");
                    nav.show_page();
                }); 
            }
        }
        
    // video functions
        nav.get_video = function () {
        // clear and set active thumb
            if ($("#playlist .active").exists()) player.thumb_hoverOut($("#playlist .active").removeClass("active")[0]);
            $("#playlist ."+nav.active_video).addClass("active");
            
            var video = $("#"+nav.active_video);
        // Does a video already exist
            if ($("#video").attr("src") !== '') {
            // Not this video, remove it
                if (video.html()+'&player_id=video' !== $("#video").attr("src")) {
                    nav.remove_video();
                }
            // is this video
                else {
                    if (nav.active_pullout) {
                        nav.get_pullout();
                    }
                }
            }
            else {
                nav.show_video();
            }
        }
        
        nav.remove_video = function () {
        // change cursor
            $("body").css("cursor", "progress");
        
        // Pullout open?
            if ($('.pullout.active').exists()) {
                $('.pullout.active').queue(function () {
                    $("#video, #description >.description").fadeOut(animation_duration/2, function () {
                        $("#description >.description").appendTo("#descriptions");
                        nav.show_video();
                    }); 
                    $(this).dequeue();
                });
            }
            else {
                $("#video, #description >.description").fadeOut(animation_duration/2, function () {
                    $("#description >.description").appendTo("#descriptions");
                    nav.show_video();
                }); 
            }
        }
        
        
        nav.show_video = function () {
            $("#video").attr('src', $("#"+nav.active_video).html()+'&player_id=video');
            $("#video").bind('load', function () {
                //console.log('bind_load');
                if (player.load == true) {
                    $(this).fadeIn(animation_duration/2, function () {
                        if (nav.active_pullout) {
                            nav.get_pullout();
                        }
                        $("body").css("cursor", "auto");
                    });
                    player.video_loaded();
                    player.load = false;
                }
                else {
                    player.load = true;
                }
                
                 $(this).unbind('load');
            });
            
        // description
            $("#descriptions ."+nav.active_video).appendTo("#description");
            $("#description .description").fadeTo(animation_duration/2, 1);
            
            
        }
    // Pullout functions
        nav.get_pullout = function () {
            active_pullout = $("#"+nav.active_pullout);
            if ($(".active.pullout").exists()) {
                nav.remove_pullout();
            }
            else {
                nav.show_pullout();
            }
        }
        
        nav.remove_pullout = function () {
            $(".active.pullout").stop().animate({
                top: -$(".active.pullout").outerHeight()
            }, animation_duration/2, function () {
                $(this).removeClass("active");
                $("#pullout_container").height(0);
                if (nav.active_pullout) {
                    nav.show_pullout();
                }
            });
        }
        
        nav.show_pullout = function () {
            active_pullout = $("#"+nav.active_pullout);
            $("#pullout_container").height(active_pullout.outerHeight()+20);
            active_pullout.addClass("active");
            active_pullout.stop().animate({
                top: 20
            }, animation_duration/2);
        }
        


    // bind pullouts primary nav
        nav.set_pullouts = function () {
            $('#pullout_container .pullout').each(function (key, val) {
                $(val).css('top', -$(val).outerHeight());
            });
        }

// nav object calls
    nav.bind_resize();
// navigation hovers
    $("#primary_nav li").hover(function () {
        if ($(this).hasClass('active') == false) {
            if (!$.browser.msie || parseInt($.browser.version) > 8) { $('h3', this).stop().fadeTo(animation_duration, 1); }
            if (!$('.nav.bg', this).exists()) { $(this).append('<div class="nav bg"></div>'); }
            $('.nav.bg', this).stop().fadeTo(animation_duration/2, 1);
        }
    }, function () {
        if ($(this).hasClass('active') == false) {
            if (!$.browser.msie || parseInt($.browser.version) > 8) { $('h3', this).stop().fadeTo(animation_duration, .7); }
            $('.nav.bg', this).stop().fadeTo(animation_duration/2, 0, function () {
                $(this).remove();
            });
        }
    });
    
// link hovers
    $(".pullout a, .description a").hover(function () {
        $(this).stop().animate({
            color: '#ccc'
        },animation_duration/2);
    }, function () {
        $(this).stop().animate({
            color: '#fff'
        },animation_duration/2);
    });

// Client login form
    $("#client input[name=user]").data('default', 'username');
    $("#client input[name=pass]").data('default', 'password');
    
    if ($('#client input').val() == '') {
        $('#client input').each(function (key, val) {
            $(val).val($(this).data('default'));
        });
    }
    
    $("#client input").focus(function () {
        if ($(this).val() == $(this).data('default')) { $(this).val(''); }
        $(this).css('border', '#fff 1px solid');
    });
    
    $("#client input").blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).data('default'));
            $(this).css('border', '1px solid red');
        }
        else if ($(this).val() == $(this).data('default')) {
            $(this).css('border', '1px solid red');   
        }
        else {
            $(this).css('border', '1px solid #333');
        }
    });
    
    $("#client input").keypress(function (e) {
        if (e.which == 13) {
            $("#client button").trigger('click');
        }
        else if (e.which == 9) {
            $(this).sibling("input").trigger('focus');
        }
    });
    
    $("#client button").click(function () {
        valid = true;
        $('#client input').each(function (key, val) {
            if ($(val).val() == '' || $(val).val() == $(val).data('default')){
                valid = false;
                $(val).css('border', 'red 1px solid');
            }
        });
        
        if (valid == true) {
            data = 'user='+$("#client input[name=user]").val()+'&pass='+$("#client input[name=pass]").val();
            $.ajax({
                type: "GET",
                url: "/index.php",
                data: data,
                dataType : "json",
                success: function(response) {
                    if (response.logged_in == true) {
                        response.class_string = 'response success';
                        reload = function () {
                            window.location = '/'+response.client_dir;
                        }
                        setTimeout(reload, 2000);
                    }
                    else {
                        response.class_string = 'response fail';
                    }
                    
                    $('#client').append('<h4 class="'+response.class_string+'">'+response.msg+'</h4>');
                    $("#pullout_container").data('height', $('#pullout_container').height());
                    $("#pullout_container").animate({
                        height: $("#pullout_container #client").outerHeight()
                    }, animation_duration/2, function () {
                        $("#pullout_container").delay(animation_duration*3).animate({
                            height: $("#pullout_container").data('height')
                        }, animation_duration/2, function () {
                            $('.response', this).remove();
                        });
                    });
                }
            });   
        }
        else {
            alert('Fill in the fields please');
        }
    });

// Landing page
    Landing = function () {
        this.hold = false;
        this.w_small = $(window).width()/8;
        this.w_big = $(window).width()/4;
        this.initialized = false;
        
        this.enter = function () {
            this.w_small = $(window).width()/8;
            this.w_big = $(window).width()/4;
            this.bind_key();
            
            
            this.position_images();
            this.set_bars();
            this.bind_resize();
            nav.bind_resize();
        }
        
        this.leave = function () {
            this.unbind();
        }
        this.initialize = function () {
            this.bind_hover();
            
            this.initialized = true;
        }
        
        this.hoverIn = function (bar) {            
        // Set the selected item to be on top
            $(bar).css('z-index', '20');
            
        // Animate the selected item
            $(bar).stop().animate({
                width : landing.w_big,
                left : $(bar).data('l_big')
            }, animation_duration/5, function () {
            // Animate the title
                $('.landing_title', bar).animate({
                    bottom : 0 
                }, animation_duration/5);
            });
            
        // fade out the stripes, in the img
            $('.pattern_bars', bar).stop().fadeTo(animation_duration*1.5, 0.0);
            $('img', bar).stop().fadeTo(animation_duration*1.5, 1.0);
        
        // Animate the rest of em
            $('#landing .bar').not(bar).each(function (key, val) {
                if ($(this).data('pos') < $(bar).data('pos')) {
                    left = $(this).data('l_small');
                }
                else {
                    left = $(this).data('l_small') + $(window).width()/8;
                }
                
                $(val).stop().animate({
                    width : landing.w_small,
                    left : left
                }, animation_duration/5);
            });
        }
        
        this.hoverOut = function (bar) { 
        // Animate the rest of em up
            $('#landing .bar').not(bar).each(function (key, val) {
                $(val).stop().animate({
                    width : $(this).data('w'),
                    left : $(this).data('l')
                }, animation_duration/8);
            });
            
        // Animate the big one down
            $(bar).stop().animate({
                width : $(bar).data('w'),
                left : $(bar).data('l')
            }, animation_duration/8,
            function () {
            // Put it back down
                $(this).css('z-index', '0');
            });
            
        // Animate the title
            bottom = $('.landing_title', bar).data('bottom');
            $('.landing_title', bar).animate({
                bottom : bottom
            }, animation_duration/8);
            
        // fade the bars back in and out the img
            $('.pattern_bars', bar).stop().fadeTo(animation_duration, 1);
            $('img', bar).stop().fadeTo(animation_duration, 0.4);
        }
        
        this.bind_hover = function () {
            $("#landing .bar").hover(function () {
                landing.hoverIn(this);
            }, function () {
                landing.hoverOut(this);
            });
        }
        
        this.position_images = function () {
            $("#landing .bar img").each(function (key, obj) {
                left = -$(obj).width()/2;
    
                $(obj).css("margin-left", left+"px");
            });
        }
        
        
        this.bind_key = function () {
            $(window).bind('keydown', function (e) { if (e.which == 32) { e.preventDefault(); } });
        }
        
        this.bind_resize = function () {
            $(window).bind('resize', function () {
                landing.w_small = $(window).width()/8;
                landing.w_big = $(window).width()/4;
                
                landing.position_images();
                landing.set_bars();
                
            });
        }
        this.set_bars = function () {
        // Set opacity
            $("#landing .bar .pattern_bars").fadeTo(animation_duration/2, 1);
            $("#landing .bar img").fadeTo(animation_duration/2, .4);
            
        
        // Set the position for landing titles
            $('#landing .bar .landing_title').each(function (key, val) {
                $(val).data('bottom', -$(val).height());
                
                $(val).css('bottom', $(val).data('bottom'));
                $(val).css('width', landing.w_big);
            });
            
        // Set data for bars to animate
            $('#landing .bar').each(function (key, val) {
            // Set the position from left
                $(val).data('pos', key);
                
            // The width & distance from left default 
                if ($(val).data('pos') == 0) {
                    $(val).data('w', $(window).width()*.146+1);
                    $(val).data('l', 0);
                }
                else if ($(val).data('pos') == 6) {
                    $(val).data('w', $(window).width()*.135+1);
                    $(val).data('l', $(window).width()*.865);
                }
                else {
                    $(val).data('w', $(window).width()*.146+1);
                    $(val).data('l', $(window).width()*.135+($(window).width()*.146*($(val).data('pos')-1)));   
                }
                
            // The width & distance from left when big
                $(val).data('l_big', $(val).data('pos')*$(window).width()/8);
            
            // The width & distance from left when small
                $(val).data('l_small', $(window).width()/8*$(val).data('pos'));
                
            // set the left val for cross browser
                $(val).css("left", $(val).data('l'));
                $(val).css('width', $(val).data('w'));
            });
        }
        this.unbind = function () {
            $(window).unbind('resize');
            $(window).unbind('keydown');
        }
    }

// landing page
    window.landing = new Landing();
    landing.initialize();
    
// Gallery page
    Gallery = function () {
        this.duration = animation_duration;
        this.delay = 20;
        
// -- Event fires
    // Window loads
        this.initialize = function () {
            this.bind_rollover();
            this.set_filter_height();
            this.bind_filter();
        }
    
    // Entering Gallery
        this.enter = function () {
            $("#hidden_container").css("display", 'none');
            $('body').css("overflow-y", 'auto');
            this.hold = false;
            this.reset_filter();
            this.bind_resize();
            this.set_thumbnail_height();
            this.set_title_pos();
            this.set_container_height();
            nav.bind_resize();
        }
        
    // Leaving gallery
        this.leave = function () {
            $('body').css('overflow', 'hidden');
            this.unbind();
        } 
// -- Set prop and Active methods
        this.reset_filter = function () {
            this.filter = 'all';
            $('.filter>h4').html('Filter by category');
            $('#videos .thumbnail_container a').removeAttr('style');
        }
        this.set_thumbnail_height = function () {
           $("#videos .thumbnail_container a").each(function () {
                $(this).css('height', $(this).width()*9/16);
           });
        }
        
        this.set_title_pos = function () {
            $('#videos .thumbnail_container a h4').each(function (key, val) {
                $(val).data('bottom', -$(val).height() - parseInt($(val).css('padding-top')));
                //$(val).css('bottom', $(val).data('bottom'));
            });
        }  
        
        this.thumb_hoverIn = function (elem) {
            if (gallery.hold == true) { return false; }
            // fade this in
                $(elem).stop().fadeTo(animation_duration, 1);
                
            // Roll the title up
                $('h4', elem).stop().animate({
                    bottom : 0
                }, animation_duration/4);
                
            // fade the rest
                //$(this).parent().find('a').not(this).stop().fadeTo(animation_duration/4, .6);
        }
        
        this.thumb_hoverOut = function (elem) {
            if (gallery.hold == true || $(elem).hasClass('active')) { return false; }
            // fade this out
                $(elem).stop().fadeTo(animation_duration/4, .8);
                
            // Roll the title up
                $('h4', elem).stop().animate({
                    bottom : $('h4', elem).data('bottom')
                }, animation_duration/4);
                
            // fade the rest
                //$(this).parent().find('a').not(this).stop().fadeTo(animation_duration/4, .8);
        }

        this.hide_thumbs = function () {
            thumb = $('#videos .thumbnail_container a:visible').not(':animated').last();
            if (thumb.exists()) {
                thumb.stop().fadeTo(gallery.duration, 0, function () {
                    $(this).hide();
                });
            }
            else {
                clearInterval(gallery.interval);
            // fade thumbs
                $("#videos .thumbnail_container a:animated").first().queue(function () {
                    gallery.duration = 1000/$('#videos .thumbnail_container a.'+gallery.filter+':hidden').length;
                    gallery.interval = setInterval(gallery.show_thumbs, gallery.delay);
                    $(this).dequeue();
                });
            }
        }
        
        this.show_thumbs = function () {
            var thumb = $("#videos .thumbnail_container a."+gallery.filter+":hidden").first();
            if (thumb.exists()) {
                thumb.show();
                thumb.stop().fadeTo(gallery.duration, .8);
            }
            else {
                clearInterval(gallery.interval);
                gallery.hold = false;
            }
        }
        
        this.set_filter_height = function () {
            height = $(".filter").height();
            $(".filter").data('height', height);
            $('.filter').css('height', 32);
        }
        this.set_container_height = function () {
        /*
            container = $("#videos .container");
            thumbs = container.children('.thumbnail_container');
            height =
            + $(window).height()
            - parseInt(container.css('margin-top')) 
            - parseInt(container.css('padding-top'))
            - parseInt($(container).children('h2').css('margin-bottom'))
            - container.children('h2').height()
            - parseInt(thumbs.css('margin-top'))
            - parseInt(thumbs.css('margin-bottom'));
            
            thumbs.css('min-height',  height);
        */
            $("#videos .thumbnail_container").removeAttr('style');
            $("#videos .thumbnail_container").css('min-height', $("#videos .thumbnail_container").height());
        }
// -- Bindings               
        this.bind_rollover = function () {
            $('#videos .thumbnail_container a').hover(function () {
                gallery.thumb_hoverIn(this);
            }, function () {
                gallery.thumb_hoverOut(this);
            });
        }
        
        this.bind_filter = function () {
        // hover
            $(".filter").hover(function () {
                $(this).stop().animate({
                    height: $(this).data('height')
                }, animation_duration/4);
            }, function () {
                $(this).stop().animate({
                    height: 32
                }, animation_duration/4);
            });
            
        /*
        // clicks
            $(".filter li").click(function () {
                filter = $(this).attr('class');
            
            // fade thumbs
                $('#videos .thumbnail_container a:visible').stop().fadeTo(animation_duration/2, 0, function () {
                    $('#videos .thumbnail_container a').not('.'+filter).hide();
                    $('#videos .thumbnail_container a.'+filter).stop().fadeTo(animation_duration/2, .8);
                }); 
            });
        */
    
         // clicks
            $(".filter li").click(function () {
                if (gallery.filter !== $(this).attr('class') && gallery.hold !== true) {
                    gallery.hold = true;
                    gallery.filter = $(this).attr('class');
                    filter = $(this);
                    $('.filter>h4').fadeTo(500, 0, function () {
                        $(this).html(filter.html());
                        $(this).fadeTo(500, 1);
                    });
                
                // fade thumbs
                    gallery.duration = 1000/$('#videos .thumbnail_container a:visible').length;
                    gallery.interval = setInterval(gallery.hide_thumbs, gallery.delay);    
                }
            });
        }
        
        this.bind_resize = function () {
            $(window).bind('resize', function () {
                gallery.set_thumbnail_height();
                gallery.set_title_pos();
                gallery.set_container_height();
            });
        }
        
        this.unbind = function () {
            $(window).unbind('resize');
            $(window).unbind('keydown');
        }
    }
    
    window.gallery = new Gallery();
    gallery.initialize();
    
// Player page
    Player = function () {
        this.delay = 2000;
        this.motion = false;
    
// Event fires    
    // Creation of obj
        this.initialize = function () {
            this.load = true;
            this.bind_rollover();
            this.bind_thumb_click();
            //this.buggy = ( $.browser.webkit && !/macintosh/.test(navigator.userAgent.toLowerCase()) ) ? true : false;
            
        /*
            $.browser.mac = navigator.userAgent.search('Macintosh') !== -1 ? true : false;
            $.browser.safari = ( $.browser.webkit && !/chrome/.test(navigator.userAgent.toLowerCase()) ) ? true : false;
            if ($.browser.safari == true) {
                $("#playlist").css('overflow-y', 'auto');
                $("#playlist .thumbnail_container").css({
                    height: 'auto',
                    'overflow-y': 'auto'
                });
            }
        */
        }
    // Entering page
        this.enter = function () {
            this.timeout = null;
            this.loaded = false;
            this.playing = false;
            
            this.set_playlist_desc_height();
            this.set_thumbnail_height();
            this.set_playlist_padding();
            this.show_ui(true);
            this.bind_mousemove();
            this.bind_scroll();
            this.thumb_hoverIn($("#playlist .active")[0]);
            this.bind_playlist_mouse();
            this.bind_resize();
            nav.bind_resize();
        }
        
    // Leaving page
        this.leave = function () {
            player.unbind();
            clearTimeout(player.timeout);
            $("#logo").stop().fadeTo(animation_duration/2, .8);
        }
    // Video loaded
        this.video_loaded = function () {
            player.initial_load = true;
            player.loaded = true;
            player.first = true;
            player.ui_hold = false;
            player.last_playing = null;
            player.playing = false;
            player.video = new Froogaloop($("#video").get(0));
            player.hover = $("#play_pause");
            
            //Froogaloop.init(document.getElementsByTagName('iframe'));
            player.thumb_hoverIn($("#playlist .active")[0]);
            player.scroll_playlist_active();
            
            player.bind_api_event_listener();
            $("#play_pause div").removeAttr('class');
            $("#play_pause div").addClass('play'); 
            $("#play_pause").stop().fadeTo(animation_duration, 1);
            player.bind_play_pause();
            var g = setTimeout(player.play_pause, 3000);
            
        }
        
// Set prop and active methods
    // Playlist
        this.set_thumbnail_height = function () {
           this.thumb_height = ($("#player .thumbnail_container a").width()*9/16);
           $("#player .thumbnail_container a").css('height', player.thumb_height);
        }
        
        this.set_playlist_padding = function () {
            height = this.thumb_height;
            this.padding = (($("#playlist").height() - height)/2);
            $('#player #playlist .thumbnail_container').css({
                padding: this.padding+'px 0',
                height : height
            });   
        }
        this.set_playlist_desc_height = function () {
            $("#description").height($(window).height()-42);
            $("#playlist").height($(window).height()-52);
        }
        this.thumb_hoverIn = function (elem) {
            // fade this in
                $(elem).stop().fadeTo(animation_duration, 1);
                
            // Roll the title up
                $('h4', elem).stop().animate({
                    bottom : 0
                }, animation_duration/4);
                
            // fade the rest
                //$(this).parent().find('a').not(this).stop().fadeTo(animation_duration/4, .6);
        }
        
        this.thumb_hoverOut = function (elem) {
            if ($(elem).hasClass('active')) { return false; }
            // fade this out
                $(elem).stop().fadeTo(animation_duration/4, .8);
                
            // Roll the title up
                $('h4', elem).stop().animate({
                    bottom : '-1.25em'
                }, animation_duration/4);
                
            // fade the rest
                //$(this).parent().find('a').not(this).stop().fadeTo(animation_duration/4, .8);
        }
        this.playlist_auto_scroll = function () {            
            $("#playlist .thumbnail_container").scrollTo(player.direction+Math.sqrt(player.magnitude));
        }
        this.scroll_playlist_active = function () {
            $("#playlist .thumbnail_container").scrollTo($("#playlist .active"), animation_duration, {offset: -player.padding});
        }
        
        this.scroll_up = function () {
            $("#playlist .thumbnail_container").scrollTo("-=10px");
        }
        
        this.scroll_down = function () {
           $("#playlist .thumbnail_container").scrollTo("+=10px");
        }
    
    // Player
        this.check_motion = function () {
            if (player.playing == true) {
                if (player.motion == true) {
                    player.show_ui();
                }
                else {
                    var x = setTimeout(player.set_motion, 250);
                }
            }
        }
        this.set_motion = function () {
            player.motion = true;
            var y = setTimeout(player.unset_motion, 250);
        }
        
        this.unset_motion = function () {
            player.motion = false;
        }
        this.hide_ui = function (bypass) {
            player.ui_hold = false;
        
        // clear interval
            if (player.timeout) {
                clearTimeout(player.timeout);
                player.timeout = null;
            }
            
        // if playing
            if (player.playing == true || bypass) {                
            //Fade play button out
                $("#play_pause").stop().fadeTo(animation_duration/2, 0);
                
            // Fade logo out
                $("#logo").stop().fadeTo(animation_duration/2, .4, function () {
                    $('#play_pause').css('cursor', 'none');
                });
            
            // Push nav in
                if ($(".pullout.active").exists()) {
                // pullout
                    $('#primary_nav').not(player.hover).find('a[href='+$(".pullout.active").attr('id')+']').first().trigger('click');
                // nav
                    $("#primary_nav").not(player.hover).stop().animate({
                        right: -573
                    }, animation_duration/2);
                }
                else {
                    $("#primary_nav").not(player.hover).stop().animate({
                        right: -573
                    }, animation_duration/2);
                }
                
                
                // Push pullout in
                if ($(".pullout.active").exists()) {  }
                
            // Push playlist in
                $("#playlist").not(player.hover).stop().animate({
                    left: -$("#playlist").width()
                }, animation_duration/2);
            
            // push desc in
                $("#description").not(player.hover).stop().animate({
                    right: -$("#description").width()
                }, animation_duration/2);
            }
        }
        
        this.show_ui = function (bypass) {
            if (!bypass && this.playing == true) {
            // Clear previous
                window.clearTimeout(player.timeout);
                
            // hide after 2 seconds of inactivity
                player.timeout = window.setTimeout(player.hide_ui, 1000);
            }
    
            if (this.ui_hold == false) {
                this.ui_hold = true;
            // curser
                $('#play_pause').css('cursor', 'pointer');
                
            // Fade logo in
                $("#logo").stop().fadeTo(animation_duration/2, 1);
            
            // Fade the play/pause button
                if (player.loaded == true) { $("#play_pause").stop().fadeTo(animation_duration/2, 1); }
            
            // Pull nav out
                $("#primary_nav").stop().animate({
                    right: 0
                }, animation_duration/2);
            
            // Pull playlist out
                $("#playlist").stop().animate({
                    left: 0
                }, animation_duration/2);
                
            // pull desc
                $("#description").stop().animate({
                    right: 0
                }, animation_duration/2);            
            }
        }
        this.play_pause = function () {              
            //console.log(player.video);
            //console.log('play/pause fired');
        
        // First load bind listeners
            if (player.first == true) {
                player.bind_api_event_listener();
                player.first = false;
            }
            
        // Webkit bug detection
            if ($.browser.webkit === true && $.browser.mac === true && player.last_playing === player.playing && player.alerted !== true) {
                player.alerted = true;
                alert("Your browser is experiencing a known bug. Please try refreshing the website or viewing in the latest version of Firefox for best results!");
            };
            
            player.last_playing = player.playing;
            
            if (player.playing == true) {
                //console.log('video is playing, now attempting to pause via API');
                player.video.api('pause');
                if ($.browser.msie && parseInt($.browser.version) < 9) {
                    player.onPause();
                };
            }
            else {
                //console.log('video is paused, now attempting to play via API');
                player.video.api('play');
                if ($.browser.msie && parseInt($.browser.version) < 9) {
                    player.onPlay();
                };
            };
        }
    
        this.prev_video = function () {
            $("#playlist .active").prev().trigger('click');
            this.show_ui();
        }
        
        this.next_video = function () {
            $("#playlist .active").next().trigger('click');
            this.show_ui();
        }
        
        this.onPlay = function () {
            //console.log('play event fired');
            //player.received = true;
            player.playing = true;
            $("#play_pause div").removeAttr('class');
            $("#play_pause div").addClass('pause');
            player.hide_ui();
        }
        
        this.onPause = function () {
            //player.received = true;
            //console.log('pause event fired');
            player.playing = false;
            $("#play_pause div").removeAttr('class');
            $("#play_pause div").addClass('play');
            window.clearTimeout(player.timeout);
            player.show_ui();
        }
        
        this.onFinish = function () {
            player.playing = false;
            $("#play_pause div").removeAttr('class');
            $("#play_pause div").addClass('play');
            //player.show_ui(true);
            if ($('#playlist .active').next().exists()) $('#playlist .active').next().trigger('click');   
            else ($('#playlist a').first().trigger('click'));
        }

// Bindings        
    // Playlist
        this.bind_playlist_mouse = function () {
            //if ($.browser.safari == false) {
                $("#playlist, #logo").bind('mousemove', function (e) {
                    if (e.pageX > $("#playlist").width()) { clearInterval(player.interval); return false; }
                    player.px_from_top = ($(window).height()/2) - (player.thumb_height/2) - 20;
                    player.px_from_bottom = player.px_from_top+player.thumb_height + 30;
                    if (player.px_from_bottom < e.pageY) {
                        clearInterval(player.interval);
                        player.magnitude = e.pageY - player.px_from_bottom;
                        player.direction = '+=';
                        player.interval = setInterval(player.playlist_auto_scroll, 15);
                    }
                    else if (e.pageY < player.px_from_top && $("#playlist .thumbnail_container").scrollTop() !== 0 ) {
                        clearInterval(player.interval);
                        player.magnitude = player.px_from_top - e.pageY;
                        player.direction = "-=";
                        player.interval = setInterval(player.playlist_auto_scroll, 15);
                    }
                    
                    else {
                        clearInterval(player.interval);
                    }
                });
                
                $("#playlist, #logo").bind('mouseleave', function (e) {
                    if (e.pageX > $("#playlist").width() || $(this).is("#playlist")) clearInterval(player.interval);
                });
            //}
        }
                    
        
        this.bind_scroll = function () {
        // Mousewheel
            if ($.browser.safari !== true) {
                $(window).bind("mousewheel", function(event, delta, deltaX, deltaY) {
                    clearInterval(player.interval);
                    var distance = Math.abs(deltaY)*25;
                    var sign = (deltaY > 0) ? "-" : "+";
                    $("#playlist .thumbnail_container").scrollTo(sign+"="+distance);
                });
            }
            else {
                $("#playlist").bind("mousewheel", function(event, delta, deltaX, deltaY) {
                    clearInterval(player.interval);
                    var distance = Math.abs(deltaY)*25;
                    var sign = (deltaY > 0) ? "-" : "+";
                    $("#playlist .thumbnail_container").scrollTo(sign+"="+distance);
                });
            }
        }
        this.bind_rollover = function () {
            $('#player .thumbnail_container a').hover(function () {
                player.thumb_hoverIn(this);
            }, function () {
                player.thumb_hoverOut(this);
            });
            $("#play_pause").hover(function () {
                if (player.loaded == true) {
                    $("#play_pause:visible").stop().fadeTo(animation_duration/2, 1, function () {
                        if (player.playing == true) {
                            setTimeout(player.hide_ui, 1500);
                        }
                    });
                }
            }, function () {
                if (player.loaded == true) {
                    if (player.playing == true) {
                        setTimeout(player.hide_ui, 2000);
                    }
                    else {
                        $("#play_pause:visible").stop().fadeTo(animation_duration/2, .7);
                    }
                }
            });
        }
        
        this.bind_play_pause = function () {
            player.unbind_play_pause();
            $("#play_pause").bind('click', function () {
                player.play_pause();
            });
            
            $(window).bind('keydown', function (event) {
            // space bar
                if (event.which == 32) {
                    event.preventDefault();
                    player.play_pause();
                }
            // left and up arrow 
                else if (event.which == 37 || event.which == 38) {
                    event.preventDefault();
                    player.prev_video();
                }
            // right and down arrow
                else if (event.which == 39 || event.which == 40) {
                    event.preventDefault();
                    player.next_video();
                }
            }); 
        }
        
        this.unbind_play_pause = function () {
            $("#play_pause").unbind('click');
            $(window).unbind('keydown');
            
        }
        
        this.bind_thumb_click = function () {
            $("#playlist a").bind('click', function () {
                player.playing = false;
                player.loaded = false;
                
                $("#play_pause").stop().fadeTo(animation_duration/2, 0);
            });
        }
    
    // Player
        this.bind_mousemove = function () {
            $('#playlist, #play_pause, #description, #logo, #primary_nav').bind('mousemove',function (e) {
                player.hover = $(this);
                if ( $.browser.msie ) {
                    player.show_ui();
                }
                else {
                    player.check_motion();
                }
            });
            $("#play_pause").bind('mouseleave', function () {
               $(this).stop().fadeTo(animation_duration/2, 0); 
            });
        }       
        
        this.bind_api_event_listener = function () {
        // remove all event listeners
            player.video.removeEvent('play');
            player.video.removeEvent('pause');
            player.video.removeEvent('finish');
            
        // add event listeners
            player.video.addEvent('play', player.onPlay);
            player.video.addEvent('pause', player.onPause);
            player.video.addEvent('finish', player.onFinish);
        };    
        
        this.bind_resize = function () {
            $(window).bind('resize', function () {
                player.set_playlist_desc_height();
                player.set_thumbnail_height();
                player.set_playlist_padding();
                player.bind_playlist_mouse();
            });
        }
        this.unbind = function () {
            $(window).unbind("mousewheel");
            $(window).unbind('keydown');
            $(window).unbind('keypress');
            
            $('#playlist, #play_pause, #description, #logo, #primary_nav').unbind('mousemove');
            $(window).unbind('mouseleave');
            $(window).unbind('resize');
            this.unbind_play_pause();
        }
    }

    window.player = new Player();
    player.initialize();
});
