/* - - - - - - - - - - - - - - - - - -
jQuery & java scripting by Winston Hoy
For Brehm Lab LTD, brehmlab.com

jQuery.js contains core + ui effects core & exists function
contact me if you want to use it
winstonhoy@gmail.com
 - - - - - - - - - - - - - - - - - - -*/

// Execute when all downloads are complete
    $(window).load(function () {
    
    });

// Execute when document structure is ready
    $(document).ready(function () {
    
    // Define Classes, functions, vars
        // Side tabs functions
            $.fn.open_tab = function (tab_to_open) {
            // Close any tabs that are open
                $(".tab").each(function() {
                    if (this.open == true) {
                        $.fn.close_tab(this);
                    }
                });
                
            // Is it relative or absolute position?
                if ($(tab_to_open).parent().parent().css("position") == "absolute") {
                    var right = "0";
                }
                else if ($(tab_to_open).parent().parent().css("position") == "relative") {
                    var right = "245px";
                }
            
            // Open this tab
                $(tab_to_open).parent().parent().animate(
                    {
                        right:right
                    },
                750);
                
                $(tab_to_open).find(".arrow").animate({
                    rotate: '+=180deg'
                }, 750);
                
            // Mark this tab as opened
                tab_to_open.open = true;
            }
            
            $.fn.close_tab = function (tab_to_close) {
            // Is it relative or absolute position?
                if ($(tab_to_close).parent().parent().css("position") == "absolute") {
                    var right = "-245px";
                }
                else if ($(tab_to_close).parent().parent().css("position") == "relative") {
                    var right = "0";
                }
            
            // Close this tab
                $(tab_to_close).parent().parent().animate(
                    {
                        right:right
                    },
                750);
                
                $(tab_to_close).find(".arrow").animate({
                    rotate: '0deg'
                }, 750);
                
            // Mark it as closed
                tab_to_close.open = false;
            }
            
            $.fn.tabs = function () {
            // Set vars
                $(".tab").each(function() {
                    this.open = false;
                });
                
            // Bind click events
                $(".tab").click(function() {
                // If it's open, close it
                    if (this.open == true) {
                    // close tab
                        $.fn.close_tab(this);
                    }
                // If it's closed, open it
                    else {
                    // open tab
                        $.fn.open_tab(this);
                    }
                })
            }
    
        // Preload images
            function preload_images () {
                $(".preload").each(function (){
                    var item_id = $(this).attr("id");
                    var src = $(this).find("span.src").html();
                    var alt = $(this).find("span.alt").html();
                    
                    $(this).prepend('<img src="'+src+'" alt="'+alt+'" />');
                   
                    $("#photo_container").find("."+item_id).attr({src: src, alt: alt });
                    
                     $(this).find("span.src, span.alt").remove();
                });
            }
    
        // Navigation class
            Default_nav = function () {
            // configure 
                $.address.wrap(true);
            
            // properties
                var self = this;
                this.initialized = false;
                this.active_page = default_page;
                this.active_body = null;
                this.active_overlay = null;
                this.active_primary = null;
                this.active_secondary = null;
                this.page_container = $("#page_container");
                this.hidden_container = $("#hidden_container");
                this.overlay_bg = $("#overlay_bg");
            
            // methods
                this.initialize_site = function () {
                // Only allow initalize once
                    if (this.initialized == false) {                    
                    // Set nav props
                        this.process_url();
                        
                    // update active_nav
                        self.set_active_primary();
                    
                    // update secondary_nav
                        self.set_active_secondary();
                        
                    // Toggle nav
                        this.toggle_nav();
                    
                    // Navigation bind, anytime our path changes             
                        $.address.change(function () {
                        // Not on initial load
                            if (self.initialized === true) {
                            // Set nav props
                                if (self.process_url() == true) {
                                // Transition
                                    self.nav_transition();
                                }
                            }                    
                        });
                        
                    // Anytime a link is clicked, change the path
                        $('a[rel=ajaxcms]').live("click", function (e) {
                            e.preventDefault();
                            if (self.anchor_bind_extend(this) !== false) {
                                self.anchor_bind(this);
                            }
                        });
                    
                    // set initialized flag
                        this.initialized = true;
                    }
                }
                
                this.anchor_bind = function (anchor) {
                    if ($(anchor).attr("href").search("/") === 0) {
                        $.address.value($(anchor).attr('href'));
                    }
                    else if ($(anchor).attr("href").search("/") === -1) {
                        var path_pop = $.address.pathNames().pop();
                        var path = $.address.path();
                        if ($(anchor).attr("href") !== path_pop) {
                            $.address.value(path + "/" + $(anchor).attr('href'));   
                        }
                        else {
                            var path_change = path.replace($(anchor).attr('href'), "")
                            $.address.value(path_change); 
                        }
                    }
                }
                
                this.anchor_bind_extend = function (anchor) { return true;}
                
                this.nav_transition = function () {
                // close overlays
                    self.close_overlay();
                
                // Queue
                    self.overlay_bg.queue(function () {
                    // Overlay or page
                        if (self.active_overlay !== null) {
                            self.show_overlay();
                        }
                    // show body
                        self.show_body();
                    
                    // show page
                        self.show_page();
                    
                    // update active_nav
                        self.set_active_primary();
                        self.set_active_secondary();
                        
                    // Toggle nav
                        self.toggle_nav();
                        
                    // dequeue
                        $(this).dequeue();
                    });
                }
                
                this.show_page = function () {
                // Is this page already showing?
                    if (self.page_container.find(".page").attr("id") !== self.active_page) {
                        self.page_container.fadeOut(animation_duration/2, function () {
                            self.page_container.find(".page").appendTo($("#hidden_container"));
                            $("#"+self.active_page).appendTo(self.page_container);
                            self.page_container.fadeIn(animation_duration/2);
                        });
                    }
                }
                
                this.show_body = function () {
                // Body being called for
                    if (self.active_body !== null || $(".body."+self.active_page).exists()) {
                        var body = self.active_body !== null ? $("#"+self.active_body) : $(".body."+self.active_page);
                        // Is there a body to get rid of?
                            if ($("#"+self.active_page+" .body").exists()) {
                            // is this body already active?
                                if ($("#"+self.active_page+" .body").attr("id") !== body.attr("id")) {
                                    $("#"+self.active_page+" .body").fadeOut(animation_duration/2, function () {
                                        $(this).appendTo(self.hidden_container);
                                        body.hide().appendTo("#"+self.active_page);
                                        body.fadeIn(animation_duration/2);
                                    });
                                }
                            }
                        // no body to hide
                            else {
                            // is this body already active?
                                if ($("#"+self.active_page+" .body").attr("id") !== body.attr("id")) {
                                    body.appendTo("#"+self.active_page);
                                    body.fadeIn(animation_duration);
                                }
                            }
                    }
                }
                
                this.show_overlay = function () {
                // Fade overlay bg in
                    if (!$("body>#overlay_bg:visible").exists()) {
                        if ($.browser.msie) { self.overlay_bg.css('filter', 'alpha(opacity=60)'); }
                        self.overlay_bg.fadeIn(animation_duration/5);
                    }
                    self.overlay_bg.queue(function () {
                    // Fade .overlay content in
                        $("#"+self.active_overlay).hide().appendTo("body").fadeIn(animation_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
                                    $("#"+self.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(animation_duration/2, function () {
                        // unbind the esc key
                            $(document).unbind("keyup");
                        
                        // move it back to the hidden container
                            $(this).appendTo(self.hidden_container);
                            
                            if (self.active_overlay == null) {
                            // fade out the overlay bg
                                self.overlay_bg.fadeOut(animation_duration/5);
                            }
                        });
                    }
                }
            
            // helper methods
                this.set_active_primary = function () {
                    
                }
                
                this.set_active_secondary = function () {
                    
                }
                
                this.toggle_nav = function () {
                // Check the active page for admin
                    if ($("#"+this.active_page).hasClass("admin")) {
                        $("#primary_nav").addClass("nav_hiding");
                        $("#primary_nav").fadeOut(animation_duration/2);
                    }
                    else {
                        $("#primary_nav").removeClass("nav_hiding");
                        $("#primary_nav").fadeIn(animation_duration/2);
                    }
                }
                this.process_url = function () {
                // if there was a path passed
                    var pathNames = $.address.pathNames();
                
                 // No path or path isn't page, show default
                    if (pathNames.length == 0 || (pathNames.length == 1 && $("#"+pathNames[pathNames.length-1]+".page, #"+pathNames[pathNames.length-1]+".admin, #"+pathNames[pathNames.length-1]+".overlay").length == 0)) {
                        this.active_body = null;
                        this.active_overlay = null;
                        this.active_page = default_page;
                        this.active_primary = default_active_primary;
                        this.active_secondary = default_active_secondary;
                        
                        return true;
                    }
                    else {
                        var path_pop = pathNames.pop();
                    
                    // Reset nav values
                        this.active_page = null;
                        this.active_body = null;
                        this.active_overlay = null;
                        this.active_primary = null;
                        this.active_secondary = null;
                        
                    // Set the content type we're looking at
                        if ($("#"+path_pop).hasClass("page")) {
                            this.active_page = path_pop;
                        }
                        else if ($("#"+path_pop).hasClass("body")) {
                            this.active_body = path_pop;
                        }
                        else if ($("#"+path_pop).hasClass("overlay")) {
                            this.active_overlay = path_pop;
                        }
                    
                    // Check if it also applies to nav
                        if ($(".secondary_nav>."+path_pop).exists()) { this.active_secondary = path_pop; }
                        else if ($("#primary_nav>."+path_pop).exists()) { this.active_primary = path_pop; }
                        
                    // Set active nav items if applicable
                        while (pathNames.length > 0) {
                            path_pop = pathNames.pop();
                            
                        // are these nav items?
                            if ($(".secondary_nav>."+path_pop).exists()) { this.active_secondary = path_pop; }
                            else if ($("#primary_nav>."+path_pop).exists()) { this.active_primary = path_pop; }
                            
                        // are these page items?
                            if (this.active_page == null && $("#"+path_pop).hasClass("page")) {
                                this.active_page = path_pop;
                            }
                            if (this.active_body == null && $("#"+path_pop).hasClass("body")) {
                                this.active_body = path_pop;
                            }
                        }
                        
                    // return true if path pop is a page/body/overlay, false otherwise
                        path_pop = $.address.pathNames().pop();
                        if ($("#"+path_pop+".page, #"+path_pop+".overlay, #"+path_pop+".body").exists()) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                }
            }
    });
