/* - - - - - - - - - - - - - - - - - -
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
 - - - - - - - - - - - - - - - - - - -*/

/* - - - - - - - - - - - - - - - - - -
Hooks
example: $(document).bind('hook_name', function () { //put function here });
- pre_initialize
- post_initialize
- 
 - - - - - - - - - - - - - - - - - - -*/

// Execute when document structure is ready
    $(document).ready(function () {
    // Navigation class
        Default_nav = function () {
        // properties
            this.initialized = false;
            this.last_page = null;
            this.active_page = default_page;
            this.active_overlay = null;
            this.active_project = default_project;
            this.overlay_bg = $("#overlay_bg");
            this.history_supported = false;//!!(window.history && history.pushState);
        
        // methods
            this.initialize_site = function () {
        //console.log('initializing site');
            // Only allow initalize once
                if (this.initialized == false) {                    
                // Trigger the hook for pre initialization
                    $(document).trigger('pre_initialize');
    
                // Init url
                    this.init_url();
                    
                // Scroll to where you belong
                    $('body').scrollTo($('#page_'+nav.active_page), 0);
                
                // Navigation bind, anytime our path changes             
                    this.bind_change_address();
                    
                // Anytime a link is clicked, change the path
                    $('a').live("click", function (e) {
        //console.log(e);
                        var href = $(this).attr('href');
                        
                        if (
                        // No mailto links
                            href.search("mailto") === -1
                        // No absolute or external links
                            && href.search("http") === -1
                        // No admin links
                            && href.search("admin") === -1
                        // No node or user edit links
                            && (
                                (
                                href.search("node") === -1
                                || href.search("user") === -1
                                )
                            && href.search("edit") === -1
                            )
                        ) {
                        // Prevent the default action
                            e.preventDefault();
                        
                        // Check if there is an override binding function
                            href = nav.anchor_bind_extend(this);
                            if (href !== false) {
                            // Bind it
                                nav.anchor_bind(href);
                            }
                        }
                    });
                
                // Bindings for closing overlay
                    $("#overlay_bg").click(function (){$("#"+nav.active_overlay + ' a.close_overlay').trigger('click');});
                
                // Fire change address
                    this.change_address();
                
                // Trigger the post initialization
                    $(document).trigger('post_initialize');
                
                // set initialized flag
                    this.initialized = true;
                }
            }
            
            this.change_address = function (url) {
        //console.log('changing address');
                if (nav.url_initialized == true && url) {
                    nav.url = url;
                    
                    if (this.history_supported) {
                    // Creat the history entry
                        history.pushState(null, null, url);
                    
                    // Fire
                        $(window).trigger('popstate');
                    }
                    else {
                        $.address.value(url);
                    }
                }
            }
            
            this.bind_change_address = function () {
        //console.log('binding address change'); 
                var callback = function () {
                // Not on initial load
                    if (nav.initialized === true && nav.url) {
        //console.log(nav.url);
                    // Set nav props
                        if (nav.process_url(nav.url) == true) {
                        // Transition
                            nav.nav_transition();
                        }
                    }    
                }
                
            // HTML5
                if (this.history_supported) {
                    window.onpopstate = function () {
                        nav.url = history.state;
                        callback();
                    };
                }
            // No HTML5
                else {
                    $.address.change(function () {
                        nav.url = $.address.value();
                        callback();              
                    });
                }
            }
            
            this.anchor_bind = function (href) {                    
            // absolute path
                if (href.search("/") === 0) {
                    nav.change_address(href);
                }
            // relative path
                else if (href.search("/") === -1) {
                // Get the current url, remove the last one and compare it to the link clicked
                    var path_names = $.address.pathNames();
                    var path_pop = path_names.pop();
                
                // If not the link clicked, replace it with the link clicked
                    if (href !== path_pop) {
                        var path = path_names.join('/')+'/'+href;
                        nav.change_address(path);   
                    }
                }
            }
            
            this.anchor_bind_extend = function (anchor) {
            // Trigger the hook for modifying href
                    $(document).trigger('anchor_bind_extend', anchor);
                    var href = $(anchor).data('href');
                    return href ? href : $(anchor).attr('href');
                    return $(anchor).attr('href');
            }
            
            this.nav_transition = function () {              
            // close overlays
                nav.close_overlay();
            
            // Queue
                nav.overlay_bg.queue(function () {                    
                // What page we on?
                    if (nav.active_page == 'home') {
                        nav.show_page();
                    }
                    else if (nav.active_page == 'portfolio') {
                    // Show the project, will call show page if necessary
                        nav.show_project();
                    }
                    else if (nav.active_page == 'about') {
                    // Show the fragment, will call show page if necessary
                        nav.show_fragment();
                        
                        nav.about.show_test();
                    }
                    
                // Exit function for last page
                // Enter function for this page
            //console.log(nav.initialized);
            //console.log(nav.active_page);
                    if (!nav.initialized) {
                        nav[nav.active_page].enter();
                    }
                    else if(nav.active_page !== nav.last_page) {
                        nav[nav.last_page].exit();
                        nav[nav.active_page].enter();
                    }
                
                // Overlay
                    if (nav.active_overlay !== null) {
                        $("#page_"+nav.active_page).queue(function () {
                            nav.show_overlay();
                            $(this).dequeue();
                        });
                    }
                    
                // dequeue
                    $(this).dequeue();
                });
            }
            
            this.show_page = function () {
            // Is this page already showing?
                if ($('.page.active').attr('id') !== 'page_'+nav.active_page) {
                    var duration = $('#page_'+nav.last_page).exists() ? window.duration * Math.abs($('#page_'+nav.active_page).offset().left - $('#page_'+nav.last_page).offset().left)/nav.w_width : 0;
        //console.log(duration);
                    var axis = nav.last_page == 'gallery' || nav.last_page == 'timeline' ? 'yx' : 'xy';
                    $('body').stop().scrollTo($('#page_'+nav.active_page), duration, {axis: axis, queue:true, easing: 'easeInOutQuad'});
                }
            }
            
            this.show_project = function () {
            // Check last categories
                if (nav.last_categories !== nav.active_categories) {
                    nav.update_cat_menu();
                    nav.portfolio.set_stage();
                }
            
            // Check last page
                if (nav.last_page !== 'portfolio') {
                    nav.show_page();
                }
                
            // Make sure the project exists
                if (nav.active_project !== null || $(".project."+nav.active_page).exists()) {
                    var project = nav.active_project !== null ? $("#"+nav.active_project) : $(".project."+nav.active_page);
                    
                // is this project already active?
                    if (!project.hasClass('active')) {                        
                    // project change function
                        nav.portfolio.project_change(project);
                    }
                }
                else {
            //console.log('resetting url');
                    nav.change_address(nav.url+'/'+$('#stage > .project').first().attr('id'));
                }
            }
            
            this.update_cat_menu = function () {                                    
            // If all
                if (nav.active_categories == 'all') {
                // Deactivate all the rest
                    $('#categories li.active').not('.all').removeClass('active').find('h1').stop().animate({
                        backgroundColor: '#999999'
                    }, duration/2);
                    
                // Activate button
                    $('#categories li.all').not('.active').addClass('active').find('h1').stop().animate({
                        backgroundColor: '#FFB900'
                    }, duration/2);
                }
                else {
                    var categories = nav.active_categories.split('_');
                    
                // Go through each menu item and see activate/deactivate
                    $('#categories li').each(function () {
                    // Active
                        if ($(this).hasClasses(categories)) {
                            $(this).addClass('active').find('h1').stop().animate({
                                backgroundColor: '#FFB900'
                            }, duration/2);
                        }
                    // Not active
                        else {
                            $(this).removeClass('active').find('h1').stop().animate({
                                backgroundColor: '#999999'
                            }, duration/2);
                        }
                    });
                }
            }
            
            this.show_fragment = function () {
                nav.about.show_fragment();
            }
            
            this.show_overlay = function () {
            // Fade overlay bg in
                if (!$("body>#overlay_bg:visible").exists()) {
                    if ($.browser.msie) { nav.overlay_bg.css('filter', 'alpha(opacity=60)'); }
                    nav.overlay_bg.fadeIn(duration/5);
                }
                nav.overlay_bg.queue(function () {
                // Fade .overlay content in
                    $("#"+nav.active_overlay).hide().appendTo("body").fadeIn(duration/2, function () {
                    // bind esc key
                        $(document).bind("keyup", function(e) {
                        // Check if esc key was pressed
                            if (e.which == 27) {
                            // Trigger the close overlay link
                                $("#"+nav.active_overlay+' a.close_overlay').trigger('click');
                            }
                        });
                    });
                    
                // dequeue
                    $(this).dequeue();
                });
            }
            
            this.close_overlay = function () {
            // Is overlay visible
                if ($("body>.overlay").exists()) {
                // fade out the overlay content
                    $("body>.overlay").fadeOut(duration/2, function () {
                    // unbind the esc key
                        $(document).unbind("keyup");
                    
                    // move it back to the hidden container
                        $(this).appendTo(nav.hidden_container);
                        
                        if (nav.active_overlay == null) {
                        // fade out the overlay bg
                            nav.overlay_bg.fadeOut(duration/5);
                        }
                    });
                }
            }
        
        // helper methods
            this.init_url = function () {                    
                var callback = function () {
                // Not on initial load
                    if (nav.url) {
        //console.log(nav.url);
                    // Set nav props
                        if (nav.process_url(nav.url) == true) {
                        // Transition
                            nav.nav_transition();
                        }
                    }    
                }
                
            // HTML5
                if (this.history_supported) {
                    nav.url = history.state;
                    callback();
                }
            // No HTML5
                else {
                    nav.url = $.address.value();
                    callback();              
                }
                
        //console.log('init url: '+nav.url);
                nav.url_initialized = true;   
            }
            
            this.process_url = function (url) {
        //console.log('processing url: '+url);
            // last page
                this.last_url = this.url;
                this.last_page = this.active_page;
                this.last_categories = this.active_categories;
                this.last_project = this.active_project;
                
            // If there is a url
                if (url) {                    
                // Remove leading /
                    url = url.search('/') == 0 ? url.substring(1) : url;
                
                // Prepare an array of url values
                    var pathNames = url.search('/') == -1 ? new Array(url) : url.split('/');
                }
            
             
            // Set defaults
                this.active_overlay = default_overlay;
                this.active_page = default_page;
                this.active_project = default_project;
                this.active_fragment = default_fragment;
                this.active_categories = default_categories;
                this.active_testimonial = null;
             
             // No path or path isn't page, show default
                if (!url || pathNames.length == 0 || (pathNames.length == 1 && $("#page_"+pathNames[pathNames.length-1]+".page, #"+pathNames[pathNames.length-1]+".admin, #"+pathNames[pathNames.length-1]+".overlay").length == 0)) {                        
                    return true;
                }
                    
            // possible
            // about/awards-testimonials/momentum12
            // portfolio/music_promotions/mlb_bon_jovi
            // home
            // about/about_company
                
            // Page only
                if (pathNames.length == 1) {
                    if (pathNames[0] == 'portfolio') {                            
                    // Before we request another url, rollback the actives
                        // Reset the active to the lasts
                        this.active_page = this.last_page;
                        this.active_categories = this.last_categories;
                        this.active_project = this.last_project;
                    
                    // If we're returning
                        if (nav.portfolio.last_categories && nav.portfolio.last_project) {
                            nav.change_address('/portfolio/'+nav.portfolio.last_categories+'/'+nav.portfolio.last_project);
                        }
                    // If we're entering the website by /portfolio
                        else {
                            nav.change_address('/portfolio/'+$('#stage > .project').first().attr('id'));
                        }
                        
                        return false;
                    }
                    else {
                        this.active_page = pathNames[0];   
                    }
                }
            // about page and fragment or
            // portfolio + category or portfolio plus project name
                else if (pathNames.length == 2) {
                    if (pathNames[0] == 'about') {
                        this.active_page = 'about';
                        this.active_fragment = pathNames[1];
                    }
                    else if(pathNames[0] == 'portfolio') {
                        this.active_page = 'portfolio';
                        
                        if ($('#'+pathNames[1]+'.project').exists()) {
                            this.active_project = pathNames[1];
                        }
                        else {
                            this.active_categories = pathNames[1];
                            var cats = this.active_categories == 'all' ? '' : '.'+this.active_categories.replace(/_/g, ', .project.');
                            this.active_project = $('.project'+cats).first().attr('id');
                        }
                    }
                }
            // portfolio, category + project
            // about, fragement + testimonial
                else if (pathNames.length == 3) {
                    if (pathNames[0] == 'about') {
                        this.active_page = 'about';
                        this.active_fragment = pathNames[1];
                        this.active_testimonial = pathNames[2];
                    }
                    else if(pathNames[0] == 'portfolio') {
                        this.active_page = 'portfolio';
                        this.active_categories = pathNames[1];
                        this.active_project = pathNames[2];
                    }
                }
                    
            // return true if path pop is a page/body/overlay, false otherwise
                if ($("#page_"+this.active_page+".page, #"+this.active_overlay+".overlay, #"+this.active_project+".project").exists()) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }
    });
