/* - - - - - - - - - - - - - - - - - -
Website produced by Curiouser Creative Studio:
    Design by Liza Gipsova
    jQuery & java scripting by Winston Hoy
    For Event Resources, erny.com

jquery.js contains core + ui effects core & exists function

Thanks:
    http://jQuery.com                                               : John Resig & jquery
    http://flesler.blogspot.com/2007/10/jqueryscrollto.html         : Ariel Flesler
    http://www.asual.com/jquery/address/                            : Rostislav Hristov
    http://gsgd.co.uk/sandbox/jquery/easing/                        : Robert Penner

contact me if you want to use something or with questions
winston@curiousercreative.com
 - - - - - - - - - - - - - - - - - - -*/

// ---- Immediately    

// ---- When the document is ready
    $(document).ready(function () {
    // Webkit hack
        if ($.browser.webkit) {
            var style = '<style type="text/css">';
        
        // Letter-spacing doesn't get specific enough
            style += '.nav h1 { letter-spacing: 0.2em; }';
            
        // Adjust the margin for extra letter-spacing
            style += '.nav li { margin: 0 0 0 3.9em; }';
            
            style += '</style>';
            
            $('head').append(style);
        }
    
    // Hide everything
        $("body").css('visibility', 'hidden');
        
    // In case it never shows, show it
        timeout = setTimeout(function () {
            $(window).unbind('load');
            nav.window_loaded = true;
            nav[nav.active_page].enter();
            nav.resize();
            $("body").css('visibility', 'visible');
        }, 20000);
    
    // ---- When all downloads have completed, the window is loaded
        $(window).bind('load', function() {
        // Clear timeout
            clearTimeout(timeout);
        
        // Show everything
            if (nav.portfolio.first_photo_loaded) {
                nav.window_loaded = true;
                nav[nav.active_page].enter();
                nav.resize();
                $("body").css('visibility', 'visible');
            }
        });
        
    // Ajaxcms bind to pre init
        $(document).bind('pre_initialize', function () {
        // Check the window size
            nav.check_screen_size();
            
            nav.initial_width = $(window).width();
            
            nav.set_page_size();
            nav.set_font_size();
            nav.set_arrow_pos();
            
        // Bind resize
            $(window).resize(nav.resize);
        });
        
        $(document).bind('post_initialize', function () {
            nav.bind_arrow_hovers();
            
        // Bind keypress window
            nav.bind_keys();
            
        // init each page
            nav.home.init();
            nav.portfolio.init();
            nav.about.init();
        });
        
    
    // ----- CMS
    
    // ----- Display functions
    // Portfolio section functions
        Portfolio = function () {            
            this.top_fraction;
            this.bottom_fraction;
            this.slideshow_mode = true;
            this.hide_ui_on = true;
            this.initial_width = $(window).width();
            
            this.init = function () {
                if (this.initialized) { return; }
        
        //console.log('init portfolio');
                this.set_stage();
                
            // Bind dots
                $('.photo_indicator li').live('click', function (e) {
                    nav.portfolio.dot_click(e, this);
                });
                
                this.bind_cat_clicks();
                this.reset_projects();
                this.init_projects();
                
                this.initialized = true;
            }
            
            this.enter = function () {
        //console.log('enter port');
                if (!this.initialized) { this.init(); }                
                if (!nav.window_loaded) { return; }
                
                this.resize();
                this.reset_projects();
                this.play_slideshow();
                
                $(window).bind('resize', nav.portfolio.resize);
                $(window).bind('keypress', nav.portfolio.key_binding);                
            }
            
            this.exit = function () {
        //console.log('exit port');
                this.last_categories = nav.last_categories;
                this.last_project = nav.last_project;
                
                this.pause_slideshow(true);
                
                $(window).unbind('keypress', nav.portfolio.key_bind);
                $(window).unbind('resize', nav.portfolio.resize);
            }
            
            this.reset_projects = function (project_id) {
        //console.log('resetting projects: '+project_id);
                project_id = project_id ? '#'+project_id : '';
            
            // Reset the scroll on each project
                $(project_id+'.project .photos').scrollTo(0, {axis: 'x', duration: 0});
                
            // Reset project details pos
                $(project_id+'.project .project_details').removeAttr('style');
                
            // Add default active
                $(project_id+'.project .active').removeClass('active').find('img:last-child').fadeTo(duration/2, 0.0);
                $(project_id+'.project .photos img:first-child').addClass('active');
                $(project_id+'.project .photo_indicator>li:first-child').addClass('active').find('img:last-child').fadeTo(duration/2, 1.0);
            }
            
            this.dot_click = function (e, target, not_clicked) {
            // Don't do anything if it's already active
                if ($(target).hasClass('active')) { return; }
            
            // Pause the slideshow
                if (!not_clicked) {
                    nav.portfolio.pause_slideshow(false);
                }
            
            // Remove the active class and Fade out the old active
                nav.portfolio.dot_deactivate($(target).parent().find('li.active'));
            
            // Add the active class and fade in the new active
                nav.portfolio.dot_activate(target);
    
            // Scroll to the corresponding photo by getting it's index
                var photos = $(target).parents('.project').find('.photos');
                $('.active', photos).removeClass('active');
                photos.stop().scrollTo(photos.find('img').eq($(target).index()).addClass('active'), duration/2, {axis: 'x', easing: 'easeInOutQuad'});
            }
            
            
            this.dot_activate = function (dot) {
                return $(dot).addClass('active').find('img:last-child').fadeTo(duration/2, 1.0);
            }
            
            this.dot_deactivate = function (dot) {
                return $(dot).removeClass('active').find('img:last-child').fadeTo(duration/2, 0.0);
            }
            
            this.bind_cat_clicks = function () {
                $(document).bind('anchor_bind_extend', function (e, anchor) {
                // Return if it's not a category menu item
                    if (!$(anchor).parents('#categories').exists()) { return; }
                
                    var li = $(anchor).parents('li');
                
                // What's this category?
                    var cat = $(anchor).attr('href').replace('/portfolio/', '');
        //console.log(cat);
                // If we clicked all, deactivate the rest
                    if (cat == 'all') {
                        var href = '/portfolio/all';
                    }
                // If all is active
                    else if (nav.active_categories == 'all') {                        
                        var href = $(anchor).attr('href');
                    }
                    else {
                    // Category array and classes string
                        var categories = nav.active_categories.split('_');
                        
                    // Is it active, do we add this category or subtract
                        // Active
                        if (li.hasClass('active')) {
                        // If only one category is active and it's being turned off, revert to all
                            if (categories.length == 1) {
                                categories[0] = 'all';
                            }
                            else {
                            // Go through the array and remove this value
                                $.each(categories, function (key, val) {
                                    if (cat == val) {
                                        categories.splice(key, 1);
                                    }
                                });
                            }
                        }
                        // Inactive
                        else {                        
                        // Add this to the array
                            categories.push(cat);
                        }
                        
            //console.log(categories);
                        var href = '/portfolio/'+categories.join('_');
                    }
                    
                // Alter the anchor data href to pass along the correct url
                    $(anchor).data('href', href);
                });
            }
            
            this.set_stage = function () {
            // Is stage already set?
                if (this.last_categories && nav.active_categories == this.last_categories && nav.active_categories !== 'all') {
                    return;
                }
                
        //console.log('setting stage');
            // Get projects with a class matching any category selected and append it to the stage
            // Set the stage
                if (nav.active_categories !== 'all') {
                // Remove projects from stage
                    $('#stage .project').each(function () {
                        if (!$(this).hasClasses(nav.active_categories.split('_'))) {
                            $(this).appendTo('#backstage');
                        }
                    });
                    
                // Add projects to stage
                    $('#backstage .project').each(function () {
                        if ($(this).hasClasses(nav.active_categories.split('_'))) {
                            $(this).prependTo('#stage');
                        }
                    });
                }
                else {
                // Add all projects to the stage
                    $('#backstage .project').prependTo('#stage');
                }
                
            // Update the fraction
                nav.portfolio.set_bottom_fraction($('#stage .project').length);
                if ($('#stage .project').length == 0) {
                    alert('no projects');
                    nav.change_address(nav.last_url);
                }
                
            // Center photo pos
                this.pos_photos();
                
            // Redirect
                if (this.initialized) {nav.change_address(nav.url+'/'+$('#stage > .project').attr('id'));}
            }
            
            this.pos_photos = function () {
                $('#stage .photos img').each(function () {
                    $(this).css('top', nav.w_height/2-$(this).height()/2);
                });
            }
            
            this.resize = function () {
                $('#stage').scrollTo($('#'+nav.active_project), 1);
                $('#'+nav.active_project+' .photos').scrollTo($('#'+nav.active_project+' .photos .active'), 0)
                
            // pos photos
                nav.portfolio.pos_photos();
            }
            
            this.key_binding = function (e) {
                if (e.which == 32) {
                    nav.portfolio.play_pause_slideshow(false);
                }
            }
            
            this.mousemove = function (e) {
                if(nav.portfolio.lastX !== e.clientX || nav.portfolio.lastY !== e.clientY){
            //console.log('mousemove');
                    nav.portfolio.show_ui();
                    clearTimeout(nav.portfolio.ui_timer);
                    nav.portfolio.ui_timer = setTimeout(nav.portfolio.hide_ui, hide_timer);
                }   
                nav.portfolio.lastX = e.clientX;
                nav.portfolio.lastY = e.clientY;
            }
            
            this.play_slideshow = function () {
        //console.log('play slideshow');
                if (nav.active_page !== 'portfolio') { return; }
            // set the slideshow flag
                nav.portfolio.slideshow_mode = true;
                
            // interval
                var interval = photo_interval;
               
            // Clear interval
                clearInterval(nav.portfolio.slideshow_int);
            
            // Set the interval
                nav.portfolio.slideshow_int = setInterval(nav.portfolio.next_photo, interval);
                
            // Set the timer for ui hide and mousemove
                if (nav.portfolio.hide_ui_on) {
                    nav.portfolio.ui_timer = setTimeout(nav.portfolio.hide_ui, hide_timer);
                    $('#page_portfolio').bind('mousemove', nav.portfolio.mousemove);
                }
            }
            
            this.pause_slideshow = function (show_ui) {
        //console.log('pause slideshow');
            // set the slideshow flag
                nav.portfolio.slideshow_mode = false;
                
            // clear the slideshow interval
                clearInterval(nav.portfolio.slideshow_int);
                
            // unbind the mousemove and clear the hide timer and show ui
               if (nav.portfolio.hide_ui_on && show_ui) {
                    clearTimeout(nav.portfolio.ui_timer);
                    $('#page_portfolio').unbind('mousemove', nav.portfolio.mousemove);
                    nav.portfolio.show_ui();
                }
            }
            
            this.next_photo = function (pause, prev) {
            // Pause the slideshow, don't show ui
                if (pause) {
                    nav.portfolio.pause_slideshow(false);
                }
                
        //console.log('next photo in slideshow');
            // Get eq of next photo
                var next = prev ? $('#'+nav.active_project+' .photo_indicator li.active').prev() : $('#'+nav.active_project+' .photo_indicator li.active').next();
                
            // Go to the next photo or the first or the next project    
                if (next.exists()) {
                // Go to next photo
                   nav.portfolio.dot_click(null, next, true); 
                }
                else {                    
                    if(go_to_next_project) {
                        if (nav.portfolio.hide_ui_on) {
                        // Show the ui
                            nav.portfolio.show_ui();
                        }
                            
                        $('#page_portfolio .footer').queue(function () {                            
                        // Go to next project
                            if (prev) {
                                $('#page_portfolio .back').click();
                            }
                            else {
                                $('#page_portfolio .forward').click();
                            }
                            
                            $(this).dequeue();
                        });
                    }
                    else {
                    // Go to first photo
                        nav.portfolio.dot_click(null, $('#'+nav.active_project+' .photo_indicator li').first(), true);
                    }
                }
            }
            
            this.play_pause_slideshow = function (show_ui) {
        //console.log('play/pause '+nav.portfolio.slideshow_mode);
                if (nav.portfolio.slideshow_mode == true) {
                    nav.portfolio.pause_slideshow(show_ui);
                }
                else if (nav.portfolio.slideshow_mode == false) {
                    nav.portfolio.play_slideshow();
                }
            }
            
            this.project_change = function (project) {            
        //console.log('showing project');
                if (this.hide_ui_on) { nav.portfolio.show_ui(); }
                
            // Wait for the footer and then show the next project
                $('#page_portfolio .footer').stop().queue(function () {
                // duration
                    var duration = $('.project.active').exists() ? window.duration*0.5 : 0;
                
                // Update active class
                    $('.project.active').removeClass('active');
                    project.addClass('active');
                
                // Update last project
                    nav.portfolio.last_project = nav.active_project;
                
                // Update fraction
                    nav.portfolio.set_top_fraction(project.index() + 1);
                
                // Update arrows
                    nav.portfolio.update_arrows();
            
                // show project
                    $("#stage").stop().scrollTo(project, duration, {axis: 'x', easing: 'easeInOutQuad'});
                
                // reset and start slideshow
                    $('#stage').queue(function () {
                    // Pause the previous
                        nav.portfolio.pause_slideshow(true);
                        
                    // Reset photos from previous project if on last photo
                        if ($('#'+nav.active_project).prev().find('.photo_indicator li:last-child').hasClass('active')) {
                            var project_id = $('#'+nav.active_project).prev().attr('id');
                            nav.portfolio.reset_projects(project_id);
                        }
                        
                    // Play new
                        nav.portfolio.play_slideshow();
                        
                       $(this).dequeue(); 
                    });
                });
            }
            
            this.hide_ui = function () {
            // Hide mouse
                $('#page_portfolio').css('cursor', 'none');
            
            // footer
                $('#page_portfolio .footer, .project.active .project_details, .footer_bg').stop().animate({
                    bottom: '-22%'
                }, duration/3, 'easeInOutQuad');
            }
            
            this.init_projects = function () {                
        //console.log('init projects');
            // Skip the first project, 
                $('.project').each(function (key, val) {                
                // For first project, load all photos, hide all but first
                    if ($(this).attr('id') == nav.active_project) {
                        $('.photos img', this).each(function (key, val) {
                            if (key == 0) {
                                nav.portfolio.load_photo(this, false, false);
                            }
                            else {
                                nav.portfolio.load_photo(this, true, false);
                            }
                        });
                    }
                // For other projects
                    else {
                    // last photo?
                        var last = key == $('.project').length -1 ? true : false;
                        
                    // Load first photo only
                        nav.portfolio.load_photo($('.photos img', this).first().get(0), false, last);
                        
                    // move the rest to the preloader
                        $('.photos img', this).slice(1).appendTo('#preloader');                        
                    }
                    
                // Bind a load event for each project once
                    $(this).one('project_loaded', function () {
            //console.log('project loaded: '+$(this).attr('id'));
                    // Set flag
                        $(this).data('loaded', true);
                        
                    // Load next project on stage
                        if ($(this).next().exists()) {
                            nav.portfolio.load_project($(this).next().attr('id'));
                        }
                    // Find the next project anywhere and load it
                        else {
                            $('.project').each(function() {
                                if (!$(this).data('loaded')) {
                                    nav.portfolio.load_project($(this).attr('id'));
                                    return false;
                                }
                            });
                        }
                    });
                });            
            }
            
            this.load_project = function (project) {
        //console.log('loading project: '+project);
            // Load all photos from this project
                $('#preloader img.'+project).each(function () {
                    nav.portfolio.load_photo(this, false); 
                });
            }
            
            this.load_next_photo = function () {
        //console.log('loading next photos');
            // Load the first photo from each project in the preloader that's not already loading
                var projects = $('.project').filter(function () {
                    return !$(this).data('loaded') ? true : false;
                })
                
                projects.each(function (key, val) {
                // last photo?
                    var last = key == projects.length -1 ? true : false;
                    
                // Find all the photos that aren't loaded yet and aren't loading already
                    $('#preloader img.'+$(this).attr('id')).each(function () {
                        if (!$(this).data('status')) {                        
                            nav.portfolio.load_photo(this, false, last);
                            return false;
                        }
                    });
                });
            }
            
            this.check_photo_loaded = function (img) {                
                // During the onload event, IE correctly identifies any images that
                // weren’t downloaded as not complete. Others should too. Gecko-based
                // browsers act like NS4 in that they report this incorrectly.
                    if (!img.complete) {
                        return false;
                    }
            
                // However, they do have two very useful properties: naturalWidth and
                // naturalHeight. These give the true size of the image. If it failed
                // to load, either of these should be zero.
            
                    if (img.naturalWidth == 0) {
                        return false;
                    }
                    
            // No other way of checking: assume it’s ok.
                return true; 
            }
            
            this.check_project_loaded = function (project) {                
                var loaded = true;
            
            // Check all photos in the project for loaded
                $('img.'+project).each(function () {
                    if (!nav.portfolio.check_photo_loaded(this)) {
                        return loaded = false;
                    }
                });
                
                return loaded;
            }
            
            this.load_photo = function (img, hide, last) {
        //console.log('loading photo');
            // Make sure it's not already loading
                if (!this.check_photo_loaded(img)) {
                // Set status
                
                // default hide value
                    hide = hide !== true ? false : true;
                    last = last ? last : false;
                
                // Bind a callback for when it's loaded
                    $(img).bind('load', function () {                        
                //console.log('photo loaded, parent: '+$(this).data('project'));
                    // Attach it to it's parent
                        var parent = $('.project#'+$(this).data('project'));
                        if ($(this).parents('#preloader').exists()) {
                            $('.photos', parent).append(this);
                        }
                        
                    // Show the corresponding dot
                        if (!$('.photo_indicator li', parent).eq($(this).index()).exists()) {
                            var dot = $('.photo_indicator li', parent).last();
                            dot.after(dot.clone().css('opacity', '0.0').fadeTo(duration/4, 1.0));
                            nav.portfolio.dot_deactivate($('.photo_indicator li', parent).last().get(0));
                        }
                        
                    // Center it vertically
                        $(this).css('top', nav.w_height/2-$(this).height()/2);
                      
                    // If it's the last one, load next photo for each project
                        if (last) {
        //console.log('last photo');
                            nav.portfolio.first_photo_loaded = true;
                            nav.portfolio.load_next_photo();
                        }
                        
                    // Check project loaded
                        if (nav.portfolio.check_project_loaded($(this).data('project'))) {
                            $('#'+$(this).data('project')).trigger('project_loaded');
                        };
                        
                    // Unbind the load function
                        $(this).unbind('load');
                    }).bind('error', function () {
                        if ($(this).data('error')) {
                            $(this).remove();
                        }
                        else {
                            $(this).data('error', true);
                            $(this).attr('src', $(this).attr('src')+'&time='+new Date().getTime());
                        }
                    });
                    
                // Set it's project so we can reattach it and a status for it's loading
                    $(img).data({
                        project: $(img).attr('class'),
                        status: 'loading'
                    });
                
                // Move into the preloader until it's ready
                    if (hide) { $(img).appendTo('#preloader'); }
                
                // Parse the src and send the request
                    var src = $(img).attr('data-src');
                    $(img).attr('src', src+'&width='+nav.w_width);
                }
                else {
                    $(img).data({
                        status: 'loaded'
                    });
                }
            }
        
            this.show_ui = function () {            
            // Show mouse
                $('#page_portfolio').css('cursor', 'auto');
            
            // footer
                $('#page_portfolio .footer, .project.active .project_details, .footer_bg').stop().animate({
                    bottom: 0
                }, duration/3);
            }
            
            this.update_arrows = function () {
        //console.log('portfolio updating arrows');
                var temp = $.address.pathNames();
                var path_pop = temp.pop();
                if ($('#'+path_pop+'.project').exists()) {
                    
                }
                
                if (this.top_fraction == this.bottom_fraction) {
            //console.log('last');
                    nav.update_arrow_link('portfolio', 'forward', '/about', 'about us');
                    
                    if ($('#'+nav.active_project).prev().exists()) {
                        var prev = $('#'+nav.active_project).prev().attr('id');
                        var title = 'prev project';
                    }
                    else {
                        var prev = '/home';
                        var title = 'home';
                    }
                    
                    nav.update_arrow_link('portfolio', 'back', prev, title);
                }
                else if (this.top_fraction == 1) {
                    nav.update_arrow_link('portfolio', 'back', '/home', 'home');
                    
                    var next = $('#'+nav.active_project).next().attr('id');
                    nav.update_arrow_link('portfolio', 'forward', next, 'next project');
                }
                else {
                    var next = $('#'+nav.active_project).next().attr('id');
                    nav.update_arrow_link('portfolio', 'forward', next, 'next project');
                    
                    var prev = $('#'+nav.active_project).prev().attr('id');
                    nav.update_arrow_link('portfolio', 'back', prev, 'prev project');
                }
            }
            
            this.set_top_fraction = function (i) {
                this.top_fraction = i;
                $('#current_project').html(i);
            }
            
            this.set_bottom_fraction = function (i) {
                this.bottom_fraction = i;
                $('#show_count').html(i);
            }
        }
        
    // About page functions
        About = function () {
            this.init = function () {
                if (this.initialized) { return; }
        
        //console.log('init about');        
                this.bind_bio_hover();
                this.init_tests();
                
            // If the awards section isn't below the second row of headshots, move it
                var top1 = $('#michael_chico').position().top;
                var top2 = $('#awards').position().top;
                if (top2 < top1) {
                    this.resize_awards = true;
                }
                
                this.initialized = true;
            }
            
            this.enter = function () {
        //console.log('enter about'); 
                if (!this.initialized) { this.init(); }
                if (!nav.window_loaded) { return; }
                
                this.resize();
                this.update_arrows();
            }
            
            this.exit = function () {
        //console.log('exit about');
                delete this.calc;
            }
            
            this.init_tests = function () {
                $('#client_testimonials .testimonial_container').append($('#client_testimonials .testimonial').first().addClass('active'));
                
                $('#client_testimonials .testimonial_container .active.testimonial').find('a.next').attr('href', '/about/awards-testimonials/'+$('#client_testimonials .hidden_container .testimonial').first().attr('id'));
                
                
            }
            
            this.size_tests = function () {
                var height = 0;
                
            // Go through tests to find the longest and set the container height
                $('#client_testimonials .testimonial').each(function () {
                    height = $(this).height() > height ? $(this).height() : height;
                });
                
                $('#client_testimonials .testimonial_container').height(height);
            }
            
            this.show_test = function () {
            // Return if no testimonial requested
                if (nav.active_testimonial == null || $('#'+nav.active_testimonial).hasClass('active')) { return; }
                
            // Slide out the old, remove active class and put it back
                $('#client_testimonials .testimonial_container .testimonial.active').removeClass('active').stop().animate({
                    left: '-100%'
                }, duration/4, 'easeInOutQuad', function () {
                // put it back
                    $(this).appendTo($('#client_testimonials .hidden_container')).removeAttr('style');
                });
                
            // Slide in the active test, add active class, position for animation, append to container, animate it and update the read next link
                $('#client_testimonials #'+nav.active_testimonial+'.testimonial').addClass('active').css('left', '100%').appendTo($('#client_testimonials .testimonial_container')).stop().animate({
                    left: '0%'
                }, duration/4, 'easeInOutQuad').find('a.next').attr('href', '/about/awards-testimonials/'+$('#client_testimonials .hidden_container .testimonial').first().attr('id'));
            }
            
            this.show_fragment = function () {
        //console.log('showing fragment: '+nav.active_fragment);
                if (nav.last_page !== 'about') {
                    nav.show_page();
                    var duration = 1;
                
                // Get the awards in the right place
                    var right = -(nav.w_width - nav.container_w)/2 - $('#awards-testimonials').width() - nav.font_size*1.5;
                    $('#awards-testimonials').css('right', right).data('right', right);
                }
                else {
                    var duration = window.duration/3;
                }
                
                if (nav.active_fragment == 'awards-testimonials') {
            //console.log('showing awards-test');
            //console.log(duration);
                    var left = -(nav.w_width - nav.container_w)/2 - $('#about-company').width();
                // animate all those columns
                    $('#about-company').stop().animate({
                        left: left
                    }, duration, 'easeInOutQuad');
                    
                    $('#page_about .two-thirds').stop().animate({
                        left: '0%'
                    }, duration, 'easeInOutQuad');
                    
                    $('#awards-testimonials').stop().animate({
                        right: '0'
                    }, duration, 'easeInOutQuad');
                
                // update arrows
                    nav.about.update_arrows();
                }
                else if (nav.active_fragment == 'about-company') {
        //console.log('showing about-company');
                    var left = -(nav.w_width - nav.container_w)/2 - $('#about-company').width();
                // animate all those columns
                    $('#about-company').stop().animate({
                        left: 0
                    }, duration, 'easeInOutQuad');
                    
                    $('#page_about .two-thirds').stop().animate({
                        left: '34.5%'
                    }, duration, 'easeInOutQuad');
                    
                    $('#awards-testimonials').stop().animate({
                        right: $('#awards-testimonials').data('right')
                    }, duration, 'easeInOutQuad');
                
                // update the arrows
                    nav.about.update_arrows();
                }
            }
            
            this.update_arrows = function () {
            // Clear the queue for the arrow button hits
                $('#page_portfolio .footer').stop(true, true);
                
                if (nav.active_fragment == 'awards-testimonials') {
            //console.log('awards active');
                // Forward
                    $('#page_about .forward').css('display', 'none');
                    nav.update_arrow_link('about', 'forward', '/about/awards-testimonials', 'awards & testimonials');
                
                // Back
                    nav.update_arrow_link('about', 'back', '/about/about-company', 'about company');
                }
                else {
            //console.log('about active');
                // Forward
                    $('#page_about .forward').css('display', 'block');
                    nav.update_arrow_link('about', 'forward', '/about/awards-testimonials', 'awards & testimonials');
                    
                // Back
                    nav.update_arrow_link('about', 'back', '/portfolio', 'portfolio');
                }
            }
            
            this.bind_bio_hover = function () {
                $('#profiles .profile').hover(function () {                
            //console.log('hover in');
                // Check for calc
                    if (!nav.about.calc) { nav.about.calc_profile_hover(); }
                    
                // Fade out the previously active
                    if ($('.profile_hover.active').exists()) {
                        var time = new Date().getTime() - $('.profile_hover.active').data('time');
                        var dur = duration/4 - time > 0 ? duration/4 - time : 1;
                        
                //console.log(dur);
                        $('.profile_hover.active').removeClass('active').animate({
                            'opacity': 0.0
                        }, dur, function () {
                //console.log('callback');
                            $(this).removeAttr('style');
                        });   
                    }
                
                // Set width, height and display for the profile hover layer
                    $('.profile_hover, .profile_hover>*', this).width(nav.portfolio.calc.width1).height(nav.portfolio.calc.height).filter('.profile_hover').addClass('active').css({display: 'block', zIndex: 6});
                   
                // Fade the overlay in
                    $('#profiles .overlay').addClass('active').stop().fadeTo(duration/4, 1.0);
                   
                // If it's a right side
                    if ($(this).hasClass('right')) {
                        $('.bio', this).css({
                            right: nav.portfolio.calc.width1*1.106
                        });
                    }
                    else if ($(this).hasClass('left')) {
                        $('.bio', this).css({
                            left: nav.portfolio.calc.width1*1.106
                        });
                    }

                // Animate the bio out from behind the headshot
                    $('.profile_hover', this).stop().animate({
                        width: nav.portfolio.calc.width2,
                        'opacity': 1.0
                    }, duration/4, 'easeInOutQuad');
                }, function () {
            //console.log('hoverout');
                    var profile_hover = $('.profile_hover', this);
                    
                // Fade the overlay out
                    $('#profiles .overlay').stop().fadeOut(duration/4, 0.0, function () {
                        $(this).removeClass('active');
                    
                    // Make the profile hover layer invisible
                        $('.profile_hover').removeClass('active').css({
                            display: 'none',
                            'opacity': 1.0   
                        });
                    });
                    
                // Animate the bio back behind the headshot
                    profile_hover.stop().css('z-index', 5).animate({
                        width: nav.portfolio.calc.width1
                    }, {duration: duration/4, easing: 'easeInOutQuad', queue: false}).data('time', new Date().getTime());
                });
            }
            this.set_font_size = function () {
                this.font_size = $(window).height()*0.015556;
                var style = '<style>#about-company p { font-size:'+this.font_size+'px;}</style>';
                $('head').append(style);
            }
            
            this.resize = function () {
                this.set_font_size();
                
                var right = -(nav.w_width - nav.container_w)/2 - $('#awards-testimonials').width() - nav.font_size*1.5;
                $('#awards-testimonials').css('right', right).data('right', right);
                
                delete this.calc;
                
                this.size_tests();
                
                if (nav.active_fragment == 'awards-testimonials') {
                    var left = -(nav.w_width - nav.container_w)/2 - $('#about-company').width();
                
                // position all those columns
                    $('#about-company').css({
                        left: left
                    });
                    
                    $('#page_about .two-thirds').css({
                        left: '0%'
                    });
                    
                    $('#awards-testimonials').css({
                        right: '0'
                    });
                }
                
            //console.log(this.resize_awards);
                if (this.resize_awards) {
                    var top1 = $('#michael_chico').position().top;
                    $('#awards').css({
                        position: 'absolute',
                        top: top1,
                        marginTop: '-1em'
                    });
                }
            }
            this.calc_profile_hover = function () {
                var d = Object();
                d.width1 = $('#profiles .profile').first().width();
                d.width2 = $('#profiles').width();
                d.height = $('#profiles .profile').first().height();
        //console.log(d);
                nav.portfolio.calc = d;
            }
        }
        
    // Home page functions
        Home = function () {
            this.init = function () {
                if (this.initialized) { return; }
            
        //console.log('init home');
                this.init_testimonials();
                this.bind_login();
                
                this.initialized = true;
                
            // Load the right size thumbnail
                $('#page_home .featured_project img').each(function () {
                    $(this).attr('src', $(this).attr('data-src')+'&width='+$(this).width());
                
                // Bind thumb clicks
                    $(this).bind('click', function () {
                        $(this).parents('.third').find('a').click();
                    });
                });
            }
            
            this.enter = function () {
        //console.log('enter home');
                if (!this.initialized) { this.init(); }
                if (!nav.window_loaded) { return; }
                
                this.resize();
                this.play_testimonials();
                this.bind_keys();
            }
            
            this.exit = function () {
        //console.log('exit home');
                this.pause_testimonials();
            }
            
            this.bind_keys = function () {
                $(window).bind('keyup', function () {
                    
                });
            }
            
            this.unbind_keys = function () {
                $(window).unbind('keyup');
            }
            
            this.init_testimonials = function () {
            // Set up the first one
                var testimonial = $('#short_testimonials .hidden_container .testimonial').first().addClass('active');
                $('#short_testimonials .testimonial_container').append(testimonial);
            }
            
            this.play_testimonials = function () {
                nav.home.pause_testimonials();
            
            // Set the interval for replacing the slideshow
                nav.home.testimonial_int = setInterval(function () {
                    nav.home.next_test();
                }, testimonial_interval);
            }
            
            this.next_test = function () {
            // Remove the old one
                $('#short_testimonials .testimonial_container .testimonial').removeClass('active').stop().animate({
                    left: '-100%'
                }, duration/4, 'easeInOutQuad', function () {
                    $(this).appendTo('#short_testimonials .hidden_container');
                });
                
            // Get the new one
                var testimonial = $('#short_testimonials .hidden_container .testimonial').first().css('left', '100%').addClass('active');
                
                testimonial.appendTo('#short_testimonials .testimonial_container').stop().animate({
                    left: "0%"
                }, duration/4, 'easeInOutQuad');
            }
            
            this.pause_testimonials = function () {
                clearInterval(nav.home.testimonial_int);
            }
            
            
            this.size_tests = function () {
                var height = 0;
                
            // Go through tests to find the longest and set the container height
                $('#short_testimonials .testimonial').each(function () {
                    height = $(this).height() > height ? $(this).height() : height;
                });
                
                $('#short_testimonials .testimonial_container').height(height);
                
            // Now center them
                $('#short_testimonials .testimonial').each(function () {
                    $(this).css({
                        marginTop: -$(this).height()/2+'px'
                    });
                });
            }
            
            this.bind_login = function () {
                $("form#client_login button").click(function (e) {
                    e.preventDefault();
                    nav.home.submit_login();
                });
                
                $("form#client_login input").keyup(function (e) {
                    if (e.which == 13) {
                        e.preventDefault();
                        nav.home.submit_login();
                    }
                });
            }
            
            this.submit_login = function () {
                var user = $('form#client_login input[name=user]').val();
                var pass = $('form#client_login input[name=pass]').val();
                window.location = 'http://office.erny.com:8000/?login='+user+':'+pass;
            }
        
            this.resize = function () {
                this.size_tests();
            }
        }
        
    // Create our nav object and overwrite methods
        window.nav = new Default_nav();
        
    // Creat Page objects
        nav.portfolio = new Portfolio();
        nav.about = new About();
        nav.home = new Home();
        
    // General methods
        nav.update_arrow_link = function (page, dir, href, title) {
            href = href ? href : '/home';
        //console.log('page: '+page+", dir: "+dir+", href: "+href+", title: "+title);
            $('#page_'+page+' a.'+dir).attr('href', href).find('.title').html(title);
        }
        nav.set_page_size = function () {
            nav.w_height = $(window).height();
            nav.w_width = $(window).width();
            nav.container_w = nav.w_height*4/3;
        
        // Keep a 4/3 aspect ratio for the container on each page
            $('.container').css({
                width: nav.container_w
            });
            
            $('.page > .container').not('.footer').css({
                height: nav.w_height
            });
            
        // Make sure the pages are always filling the screen
            $('.page, .project').css({
                height: nav.w_height,
                width: nav.w_width
            });
        }
        
        nav.set_font_size = function () {
            nav.font_size = nav.container_w*0.01;
            //font_size = font_size < 12 ? 12 : font_size;
            $('body').css('font-size', nav.font_size+'px');
        }
        
        nav.set_arrow_pos = function () {
        // Setting the pos of the arrow container
            var margin = (nav.w_width - nav.container_w)/2;
            
        // Set the arrow height
            var h = $('#page_home .logo').height() > 0 ? $('#page_home .logo').height()*0.8 : nav.container_w*0.655*0.27*0.8;
            var w = h*0.3777;
            h = h > 233 ? 233 : h;
            w = w > 88 ? 88 : w;
            
        // Check if aspect ratio is too tall and make mods
            if (margin > w) {
            // Set the arrow column width, height and pos
                $('.forward, .back').css({
                    height: '100%',
                    top: '0',
                    zIndex: '1',
                    width: margin+'px'
                });
                
                $('.forward>div, .back>div').css({
                    top: '31%',
                    height: h+'px'
                });
            }
            else {
            // Set the arrow column width, height and pos
                $('.forward, .back').css({
                    height: h+'px',
                    top: '31%',
                    zIndex: '5',
                    width: w+'px'
                });
                
                $('.forward>div, .back>div').css({
                    top: '0'
                });
            }
            
            $('.arrows').css({
                height: h+'px',
                width: w+'px'
            });
    //console.log(h);
            
            //$('.forward>div, .back>div').height(h*0.8);
        /*
            $('.arrows').css({
                height: h+'px',
                width: w+'px'
            });
        */
        
        // Set the arrow hover pos
            var left = $('#page_home .forward .arrows>img:first-child').position().left;
            $('.arrows>img.hover').css('left', left);
        }
        
        nav.check_logo_size = function () {
        // If the window is resized by much, let's get a new graphic for it
        /*
            if (nav.w_width > nav.initial_width && nav.w_width/nav.initial_width > 1.25) {
                $('img.logo, .logo img').each(function () {
                    var src = $(this).attr('src').split('?');
                    src[1] = 'w='+$(this).width();
                    src = src.join('?');
                    $(this).clone().attr('src', src).data('parent', this).appendTo($('#preloader')).bind('load', function () {
                        if ($(this).height() > 0) {
                            $(this).data('parent').replaceWith(this).remove();
                            $(this).data('parent', null);
                            $(this).unbind('load');
                        }
                    });
                });
        */
        }
        
        nav.resize = function () {
    //console.log('window resizing');
            nav.set_page_size();
            nav.set_font_size();
            nav.set_arrow_pos();
            nav.check_logo_size();
            $('body').scrollTo($('#page_'+nav.active_page), 0);
            nav[nav.active_page].resize();
        }

        nav.bind_keys = function () {
        // Prevent defaults
            $(window).bind('keyup keydown keypress', function (e) {
            // Left
                if (e.which == 37 || e.which == 39) {
                    e.preventDefault();
                }
            });
        
        // Bind arrow keys
            $(window).bind('keyup', function (e) {
            // Left
                if (e.which == 37) {
                    if (nav.active_page == 'portfolio') {
                        if (e.altKey || e.ctrlKey || e.shiftKey) {
                            nav.portfolio.next_photo(true, true);
                            return;
                        }
                        
                        else if (nav.portfolio.slideshow_mode) {
                            nav.portfolio.show_ui();
                            $('#page_portfolio .footer').queue(function () {
                                $('#page_'+nav.active_page+' .back').click();
                                
                                $(this).dequeue();
                            });
                            
                            return;
                        }
                    }
                        
                    $('#page_'+nav.active_page+' .back').click();
                }
            // Right
                else if(e.which == 39) {
                    if (nav.active_page == 'portfolio') {
                        if (e.altKey || e.ctrlKey || e.shiftKey) {
                            nav.portfolio.next_photo(true, false);
                            return;   
                        }
                        
                        else if (nav.portfolio.slideshow_mode) {
                            nav.portfolio.show_ui();
                            $('#page_portfolio .footer').queue(function () {
                                $('#page_'+nav.active_page+' .forward').click();
                                
                                $(this).dequeue();
                            });
                            
                            return;
                        }
                    }
                    
                    $('#page_'+nav.active_page+' .forward').click();
                }
            // Up
                if (e.which == 38 && nav.active_page == 'portfolio') {
                // Go to prev photo
                    nav.portfolio.next_photo(true, true);
                }
            // Down
                if (e.which == 40 && nav.active_page == 'portfolio') {
                // Go to next photo
                    nav.portfolio.next_photo(true, false);
                }
            });
        }
    
    
        nav.bind_arrow_hovers = function () {
        // Bind arrow hovers
            $('.forward').hover(function () {
                $('.title', this).stop().animate({
                   left: 0
                }, duration/4, 'easeInOutQuad');
                $('.hover', this).stop().fadeTo(1000, 1.0); 
            }, function () {
                $('.title', this).stop().animate({
                   left: '-112%'
                }, duration/4, 'easeInOutQuad');
                $('.hover', this).stop().fadeTo(1000, 0.0); 
            });
            
            $('.back').hover(function () {
                $('.title', this).stop().animate({
                   right: 0
                }, duration/4, 'easeInOutQuad');
                $('.hover', this).stop().fadeTo(1000, 1.0); 
            }, function () {
                $('.title', this).stop().animate({
                   right: '-112%'
                }, duration/4, 'easeInOutQuad');
                $('.hover', this).stop().fadeTo(1000, 0.0); 
            });           
        }
        nav.check_screen_size = function () {
        // Get the doc dimensions
            var w_d = $(window).width();
            var h_d = $(window).height();
        
        // Get screen dimensions
            var w_s = screen.width;
            var h_s = screen.height;
            
        // Get window dimensions
            var w_w = window.outerWidth;
            var h_w = window.outerHeight;
            
            if ((w_d < 1280 || w_d/h_d < 1.4) && (w_s > w_w || h_s > h_w)) {
                if (!warned) { alert('For the best viewing experience, please view this website in a maximized window or full sreen mode'); }
        /*
            // Calculate the browser window padding
                var w_p = w_w - w_d;
                var h_p = h_w - h_d;
                
            // Calculate the max dimensions for the document (inner window)
                var w = w_s - w_p;
                var h = h_s - h_p;
                    
            // At full screen, is the ratio above 1.45?
                if (w/h > 1.45) {
                    var w_new = w_s;
                    var h_new = h_s;
                }
                
            // With full width / 1.6, does it fit the screen?
                else if (w/1.6 <= h) {
                    var w_new = w_s;
                    var h_new = w/1.6+h_p;
                }
                
            // If not, take full height and multiply by 1.6
                else {
                    var h_new = h_s;
                    var w_new = h*1.6+h_p;
                }
                
            // Ask if the user wishes for the window to be resized and refreshed.
                if (confirm('Your browser\'s window can resized to better view this website. Click OK to have your window resized please.')) {
            console.log('w: '+w_new+' h: '+h_new);
                // Move the window
                    //window.moveTo(0, 0);
                
                // Resize the window
                    window.resizeTo(w_new, h_new);
                    
                // Reload
                    //window.location = 'http://www.erny.com';
                }
        */
            }
        }
    // initialize site
        nav.initialize_site();
});
 
