/**
 * AWD Primary Javascript
 **/

    if(typeof AWD == 'undefined') AWD = {};

    AWD.Site = {
        Init : function() {
            if(!AWD.Site._instance) AWD.Site._instance = new AWD.UI.Site();
            return AWD.Site._instance;
        },
        GetInstance : function() {
            return AWD.Site.Init();
        },
        Settings : {
            MainNavBarHoverOpacity : 0.20,
            MainNavItemHoverOpacity : 0.08,
            MainNavFadeSpeed : 100,
            MainNavHoverTextColor : '#EFC899',
            ContentFadeSpeed : 150,
            ContentSlideSpeed : 300,
            ContentViewSettings : {
                'awards'    : { 'Float' : 'left' , 'Width' : '550px', 'JustifyText' : false },
                'contact'   : { 'Float' : 'left' , 'Width' : '450px', 'JustifyText' : false },
                'profile'   : { 'Float' : 'right', 'Width' : '350px', 'JustifyText' : true  },
                'getDisplaySettings' : function(contentKey) {
                    contentKey = contentKey.replace(/[()<>]/g, '');
                    var _objString = 'AWD.Site.Settings.ContentViewSettings.' + contentKey;
                    if(eval('typeof ' + _objString) != 'undefined') {
                        return eval(_objString);
                    } else {
                        return null;
                    }
                }
            },
            ContentScrollPixelsPerMillisecond : 2,
            ModalFadeSpeed : 200,
            ModalOverlayOpacity : 0.7
        }
    }

    AWD.Utilities = {
        FadeToAndClearPending : function($element, strFadeSpeed, strFadeValue) {
            $element.fadeTo(strFadeSpeed, strFadeValue, function() { $(this).queue([]); });
        },
        ImageLoader : {
            Images : [],
            LoadImageSet : function(arrImagePaths, callback) {
                if(typeof arrImagePaths == 'string') arrImagePaths = [ arrImagePaths ];
                var _imageCount = arrImagePaths.length;
                var _loadedImageCount = 0;
                $.each(arrImagePaths, function(index, strImagePath) {
                    if(AWD.Utilities.ImageLoader.GetImage(strImagePath) == null) {
                        var objImage = new AWD.Utilities.Image(strImagePath);
                        AWD.Utilities.ImageLoader.Images.push(objImage);
                        objImage.$image.load(function() {
                            _loadedImageCount++;
                            if(_loadedImageCount == _imageCount)
                                callback();
                        });
                    } else {
                        _imageCount--;
                        if(_imageCount == 0 && typeof callback == 'function') callback();
                    }
                });
            },
            IsImageLoaded : function(strImagePath) {
                if(typeof strImagePath != 'string') return false;
                return AWD.Utilities.ImageLoader.GetImage(strImagePath).isLoaded;
            },
            GetImage : function(strImagePath) {
                var _image = null;
                $(AWD.Utilities.ImageLoader.Images).each(function(index, objImage) {
                    if(objImage.strPath == strImagePath) _image = objImage;
                });
                return _image;
            }
        },
        Image : function(strPath) {
            var _this = this;
            this.strPath = (strPath ? strPath : '');
            this.isLoaded = false;
            this.$image = $('<img>').attr('src', _this.strPath);
            this.$image.load(function() {
                _this.isLoaded = true;
            });
        }
    }

    AWD.UI = {

        Site : function() {
            var _this = this;

            this.ProjectNav = new AWD.UI.ProjectNav('#projectNavigation');
            this.NavBar = new AWD.UI.NavBar('#mainNavigation');
            this.ContentNav = new AWD.UI.ContentNav('#sideNavigation');
            this.ViewManager = new AWD.UI.ViewManager('#viewContainer', '#mainContent', '.view');
            this.ProjectDisplay = new AWD.UI.ProjectDisplay();
            this.ContentDisplay = new AWD.UI.ContentDisplay();
            this.IsAnimating = false;

            this.arrProjectData = [];
            
            this.arrContentData = [];

            this._init = function() {
                _this._getProjectData();
                _this._setupCustomEvents();
                _this._getContentData();
            }

            this._getProjectData = function() {
                $.each(_this.ProjectNav.$container.children('div'), function(index, catElement) {
                    var _$catElement = $(catElement);
                    var _categoryName = _$catElement.attr('id');
                    if(!_this.arrProjectData[_categoryName]) _this.arrProjectData[_categoryName] = [];
                    $.each(_$catElement.children('div'), function(index, projElement) {
                        var _$projElement = $(projElement);
                        var _newProject = new AWD.UI.Project(_$projElement.attr('id'), _$projElement.find('.architectName').text());
                        $.each(_$projElement.children('.imagePath'), function(index, imgElement) {
                            var _strImagePath = $(imgElement).html();
                            _newProject.addImagePath(_strImagePath);
                        });
                        _this.arrProjectData[_categoryName].push(_newProject);
                    });
                });

            }
            
            this._getContentData = function() {
                $('#hiddenContentContainer').find('div').each(function() {
                    var _$divContent = $(this);
                    var _contentKey = _$divContent.attr('id');
                    var _contentHtml = _$divContent.html();
                    var _contentBgImagePath = '/_data/' + _contentKey + '/bg.jpg';
                    var _objContent = new AWD.UI.Content(_contentKey, _contentHtml, _contentBgImagePath);
                    _this.arrContentData.push(_objContent);
                });                
            }
            
            this.getContent = function(contentKey) {
                var _content = null;
                $.each(_this.arrContentData, function(index, content) {
                    if(content.contentKey == contentKey) _content = content; 
                });
                return _content;
            }

            this.getProject = function(categoryName, projectName) {
                var _project = null;
                if(_this.arrProjectData[categoryName]) {
                    $.each(_this.arrProjectData[categoryName], function(index, project) {
                        if(project.strName == projectName) _project = project;
                    });
                }
                return _project;
            }

            this._setupCustomEvents = function() {
                $('#home').click(function() {
                    if(AWD.Site.GetInstance().isAnimating) return false;
                    if(_this.ViewManager.activeViewID != 'homeSplash') {
                        AWD.Site.GetInstance().isAnimating = true;
                        AWD.Site.GetInstance().NavBar.navBoxDefaultOpacity = 1;
                        AWD.Site.GetInstance().NavBar.linkDefaultOpacity = 0;
                        AWD.Site.GetInstance().ContentNav.$contentLinks.filter('.active').removeClass('active');
                        AWD.Site.GetInstance().NavBar.$navBar.hover();
                        _this.ViewManager.showView(
                            'homeSplash',
                            function() {
                                AWD.Site.GetInstance().isAnimating = false;
                            },
                            function() {
                                AWD.Site.GetInstance().ProjectNav.hide();
                            }
                        );
                        _this.NavBar.$navBoxes.filter('.active').removeClass('active');
                    }
                });
                // Attempt to prevent email spam via direct scraping of href
                $('a[email]').live('click', function() {
                    document.location = 'mailto:' + $(this).attr('email');
                });
                var _modalCredits = new AWD.UI.Modal('#modalCredits');
                $('#creditsLink').click(_modalCredits.show);
                $('#modalCredits .modalClose').click(_modalCredits.hide);
            }

            this._init();
        },
        
        Content : function(contentKey, html, bgImagePath) {
            var _this = this;
            
            this.contentKey = (contentKey ? contentKey : '');
            this.html = (html ? html : '');
            this.bgImagePath = (bgImagePath ? bgImagePath : '');
        },

        Project : function(name, author, arrImagePaths) {
            var _this = this;

            this.strName = (typeof name == "string" ? name : '');
            this.strAuthor = (typeof author =="string" ? author : '');
            this.arrImagePaths = ((arrImagePaths && arrImagePaths.length > 0) ? arrImagePaths : []);

            this.addImagePath = function(imagePath) {
                if($.inArray(imagePath, _this.arrImagePaths) == -1)
                    _this.arrImagePaths.push(imagePath);
            }
        },

        ProjectNav : function(cssSelector) {
            var _this = this;

            this.$container = $(cssSelector);
            this.$projectLinks = this.$container.find('a');
            this.activeProjectCategory;

            this.showCategory = function(projectCategory) {
                _this.hideActiveCategory();
                _this.$container.children('#' + projectCategory).show();
                _this.activeProjectCategory = projectCategory;
            }

            this.hideCategory = function(projectCategory) {
                this.$container.children('#' + projectCategory).hide();
            }

            this.hideActiveCategory = function() {
                if(typeof _this.activeProjectCategory == 'string' && _this.activeProjectCategory != '')
                    _this.hideCategory(_this.activeProjectCategory);
                _this.activeProjectCategory = '';
            }

            this.show = function() {
                _this.$container.parents('.rightFrame:first').show();
                _this.$container.fadeIn(AWD.Site.Settings.ContentFadeSpeed);
            }

            this.hide = function() {
                _this.$container.fadeOut(AWD.Site.Settings.ContentFadeSpeed, function() { _this.$container.parents('.rightFrame:first').hide(); });
            }

            this.setFirstLinkActive = function() {
                var _visibleProjectLinks = _this.$container.children('#' + _this.activeProjectCategory).find('a');
                _visibleProjectLinks.filter('.blue').removeClass('blue');
                _visibleProjectLinks.filter(':first').addClass('blue');
            }

            this.getFirstActiveProjectName = function() {
                return (_this.$container.children('#' + _this.activeProjectCategory).children('div:first').attr('id'));
            }

            this.$projectLinks.click(function() {
                if(AWD.Site.GetInstance().isAnimating) return false;
                AWD.Site.GetInstance().isAnimating = true;
                var _$triggerLink = $(this);
                var _projectName = _$triggerLink.parent('div:first').attr('id');
                _this.$projectLinks.filter('.blue').removeClass('blue');
                _$triggerLink.addClass('blue');
                AWD.Site.GetInstance().ViewManager.showView(
                    'loading',
                    function() {
                        AWD.Site.GetInstance().ProjectDisplay.setupDisplay(
                            _projectName,
                            function() {
                                AWD.Site.GetInstance().ViewManager.showView('stage', function() {
                                    AWD.Site.GetInstance().isAnimating = false;
                                });
                            }
                        );
                    }
                );
            });
        },

        ButtonBox : function() {
            var _this = this;
            this._$buttonBox = $('<div class="buttonBox textCenter"><div class="centered clearfix"></div></div>');
            this._$buttonTemplate = $('<div class="button textCenter left" />');

            this.addButton = function(buttonText, cssClass, wrapTextInLink, clickFunction) {
                if(typeof wrapTextInLink != 'boolean') wrapTextInLink = true;
                if(typeof cssClass != 'string') cssClass = '';
                var _$button = _this._$buttonTemplate.clone();
                _$button.addClass(cssClass);
                if(wrapTextInLink)
                    _$button.append($('<a>' + buttonText + '</a>'));
                else
                    _$button.text(buttonText);
                _this._$buttonBox.children('div:first').append(_$button);
            // Click Handler
                if(typeof clickFunction == 'function')
                    _$button.click(clickFunction);
            // Center button box, each button is 18 pixels wide and has a 3px margin on
            // each side, so the width of each button is 6 + 18 = 24px
                var _btnCount = parseInt(_this._$buttonBox.find('.button').length, 10);
                var _btnWidth = ($.browser.msie == true && parseInt($.browser.version) < 7 ? 26 : 24);
                var _btnBoxWidth = _btnCount * _btnWidth;
                _this._$buttonBox.children('div:first').css('width', _btnBoxWidth + 'px');
            // Return the newly created button
                return _$button;
            }
            
            this.setButtonClickHandler = function(clickFunction) {
                if(typeof clickFunction == 'function') {
                    _this._$buttonBox.find('.button').click(clickFunction);
                }
            }        
        },

        ProjectDisplay : function() {
            var _this = this;

            this._$stage = $('#stage');
            this._$architectNameContainer = $('#architectNameContainer');

            this.setupDisplay = function(projectName, callback) {
                _this._$stage.html('');
                var _objProject = AWD.Site.GetInstance().getProject(
                                                                        AWD.Site.GetInstance().ProjectNav.activeProjectCategory,
                                                                        projectName
                                                                   );
                var _imagePaths = _objProject.arrImagePaths;
                _this._$architectNameContainer.hide();
                _this._$architectNameContainer.text('');
                if(_objProject.strAuthor != "") {
                    this._$architectNameContainer.html('Architect: <br />' + _objProject.strAuthor);
                    this._$architectNameContainer.show();
                }
                AWD.Utilities.ImageLoader.LoadImageSet(_imagePaths, function() {
                    _this._$stage.append(AWD.Utilities.ImageLoader.GetImage(_imagePaths[0]).$image.clone());
                    var _objButtonBox = new AWD.UI.ButtonBox();
                    for(var i = 0; i < _imagePaths.length; i++) {
                        var _cssClass = (i == 0) ? 'active' : '';
                        _objButtonBox.addButton((i + 1), _cssClass, true);
                    }
                    _objButtonBox.setButtonClickHandler(function() {
                        $(this).siblings('.active').removeClass('active');
                        _imagePathIndex = parseInt($(this).text(), 10) - 1;
                        _this._$stage.find('img').remove();
                        _this._$stage.prepend($(AWD.Utilities.ImageLoader.GetImage(_imagePaths[_imagePathIndex]).$image.clone()));
                        $(this).addClass('active');
                    });
                    _this._$stage.append(_objButtonBox._$buttonBox);
                    if(typeof callback == 'function') callback();
                })
            }
        },
        
        ContentDisplay : function() {
            var _this = this;
             
            this._$stage = $('#stage');
            
            this.setupDisplay = function(contentKey, callback) {
                // Clear the stage.
                _this._$stage.html('');
                var _objContent = AWD.Site.GetInstance().getContent(contentKey);
                if(!_objContent) return false;
                AWD.Utilities.ImageLoader.LoadImageSet([ _objContent.bgImagePath ], function() {
                    var _$divContentFrame = $('<div id="contentStage" class="clearfix" />');
                    var _$divScrollPane = $('<div id="scrollPane" />');
                    var _displaySettings = AWD.Site.Settings.ContentViewSettings.getDisplaySettings(contentKey);
                    var _scrollPaneFloat = _displaySettings.Float;
                    var _scrollPaneWidth = _displaySettings.Width;
                    if(_displaySettings.JustifyText)
                        _$divScrollPane.addClass('textJustify')
                    else
                        _$divScrollPane.addClass('textLeft');
                    _$divScrollPane.addClass(_scrollPaneFloat).css('width', _scrollPaneWidth);
                    _$divContentFrame.append(_$divScrollPane);
                    _$divScrollPane.html(_objContent.html);
                    _$divContentFrame.css('background', 'transparent url(' + _objContent.bgImagePath + ') no-repeat scroll top left');
                    _this._$stage.append(_$divContentFrame);
                    if(typeof callback == 'function') callback();
                });
            }
        },

        ViewManager : function(containerCssSelector, fadeFrameCssSelector, viewCssSelector) {
            var _this = this;

            this.$container = $(containerCssSelector);
            this.$fadeFrame = $(fadeFrameCssSelector);
            this.viewCssSelector = (viewCssSelector ? viewCssSelector : '');

            this.arrViews = [];

            this.activeViewID = '';

            this.showView = function(viewID, afterFadeCallback, beforeFadeCallback, showProjectNav) {
                if(typeof afterFadeCallback != 'function') afterFadeCallback = function() {};
                if(typeof showProjectNav != 'boolean') showProjectNav = false;
                var _newView = _this._getView(viewID);
                // 5px = padding on content container which doesn't get factored into the height
                // calc, thus pad it by 5px;
                var _newViewHeight = _newView.$container.outerHeight(true) + 5;
                var _activeView = _this._getView(_this.activeViewID);
                // Now cleanly fade the frame out
                _this.$fadeFrame.fadeOut(AWD.Site.Settings.ContentFadeSpeed, function() {
                    _activeView.$container.hide();
                    _newView.$container.show();
                    if(typeof beforeFadeCallback == 'function') beforeFadeCallback();
                    _this.$container.animate(
                        { 'height' : _newViewHeight },
                        AWD.Site.Settings.ContentSlideSpeed,
                        'linear',
                        function() {
                            var _$fadeElements = _this.$fadeFrame;
                            if(showProjectNav)
                                AWD.Site.GetInstance().ProjectNav.show();
                            _$fadeElements.fadeIn(AWD.Site.Settings.ContentFadeSpeed, afterFadeCallback);
                        }
                    );
                    _this.activeViewID = _newView.id;
                });
            }

            this._getView = function(viewID) {
                var _view;
                $.each(_this.arrViews, function(index, view) {
                    if(view.id == viewID) _view = view;
                });
                return _view;
            }

            $(this.viewCssSelector).each(function(index, eleView) {
                var _$eleView = $(eleView);
                var _view = new AWD.UI.View(_$eleView.attr('id'), _$eleView);
                _this.arrViews.push(_view);
                if(_view.$container.outerHeight() > 0 && _view.$container.css('display') != 'none') {
                    _this.activeViewID = _view.id;
                }
            });

        },

        View : function(id, $container) {
            var _this = this;

            this.id = (id ? id : '');
            this.$container = $container;
        },
        
        ContentNav : function(cssSelector) {
            var _this = this;
            
            this.$container = $(cssSelector);
            this.$contentLinks = this.$container.find('a');
            
            this._setupContentLinks = function() {
                this.$contentLinks.each(function() {
                    $(this).click(function() {
                        if(AWD.Site.GetInstance().isAnimating) return false;
                        AWD.Site.GetInstance().isAnimating = true;
                        var _$triggerLink = $(this);
                        var _contentKey = _$triggerLink.attr('id');                        
                        _this.$contentLinks.filter('.active').removeClass('active');
                        $(this).addClass('active');
                        AWD.Site.GetInstance().ProjectNav.hide();
                        AWD.Site.GetInstance().NavBar.linkDefaultOpacity = 1;
                        AWD.Site.GetInstance().NavBar.navBoxDefaultOpacity = AWD.Site.Settings.MainNavBarHoverOpacity;
                        AWD.Site.GetInstance().NavBar.$navBar.hover();
                        AWD.Site.GetInstance().NavBar.$navBoxes.filter('.active').removeClass('active');
                        AWD.Site.GetInstance().ViewManager.showView(
                            'loading',
                            function() {
                                AWD.Site.GetInstance().ContentDisplay.setupDisplay(_contentKey, function() {
                                    AWD.Site.GetInstance().ViewManager.showView('stage', function() {
                                        var _$scrollPane = $('#scrollPane');
                                        // Need a scroller?
                                        if(_$scrollPane.outerHeight() < _$scrollPane[0].scrollHeight) {
                                            // @todo: Refactor approach here to use some sort of library based
                                            // scroll pane
                                            var _objButtonBox = new AWD.UI.ButtonBox();
                                            var _$btnUp = _objButtonBox.addButton('', 'arrowUp', false);
                                            var _$btnDown = _objButtonBox.addButton('', 'arrowDown', false)
                                            var _scrollIntervalID;
                                            
                                            var _clearScrollInterval = function() {
                                                if(typeof _scrollIntervalID != 'undefined') {
                                                    clearInterval(_scrollIntervalID);
                                                }
                                            }                                          
                                            
                                            // Setup scroll pane click events
                                            _$btnUp.mousedown(function() {
                                                _scrollIntervalID = setInterval(function() {
                                                    var _curPos = _$scrollPane.scrollTop();
                                                    if((_curPos - AWD.Site.Settings.ContentScrollPixelsPerMillisecond) > 0) {
                                                        _$scrollPane.scrollTop(_curPos - AWD.Site.Settings.ContentScrollPixelsPerMillisecond);
                                                    }
                                                }, 1);
                                            });
                                            _$btnUp.mouseup(_clearScrollInterval);
                                            _$btnUp.mouseout(_clearScrollInterval);
                                            _$btnDown.mousedown(function() {
                                                _scrollIntervalID = setInterval(function() {
                                                    var _curPos = _$scrollPane.scrollTop();
                                                    var _maxScrollHeight = _$scrollPane[0].scrollHeight;
                                                    if((_curPos + AWD.Site.Settings.ContentScrollPixelsPerMillisecond) < _maxScrollHeight) {
                                                        _$scrollPane.scrollTop(_curPos + AWD.Site.Settings.ContentScrollPixelsPerMillisecond);
                                                    }
                                                }, 1);
                                            });
                                            _$btnDown.mouseup(_clearScrollInterval);
                                            _$btnDown.mouseout(_clearScrollInterval);
                                            $('#stage').append(_objButtonBox._$buttonBox);
                                        }
                                        AWD.Site.GetInstance().isAnimating = false;
                                    });
                                });
                            }
                        );
                    });
                });
            }
            
            this._setupContentLinks();
        },

        NavBar : function(cssSelector) {
            var _this = this;

            this.$navBar = $(cssSelector);
            this.$navBoxes = this.$navBar.find('.navBox');
            
            this.navBoxDefaultOpacity = 1;
            this.linkDefaultOpacity = 0;

            this._getAllImages = function() {
                var _arrImages = [];
                $.each(_this.$navBoxes, function(index, navBox) {
                    _arrImages.push($(navBox).find('img'));
                });
                return _arrImages;
            }

            this._getAllLinks = function() {
                var _arrLinks = [];
                $.each(_this.$navBoxes, function(index, navBox) {
                    _arrLinks.push($(navBox).find('.link'));
                });
                return _arrLinks;
            }

            this._setupHover = function() {
                _this.$navBar.hover(
                    function() {
                    // Fade all of the images in (hard part)
                        $.each(_this._getAllImages(), function(index, image) {
                            var _$image = $(image);
                            // Only fade if its not already being faded in... The
                            // individual navbox fade will bubble up first
                            if(_$image.queue().length == 0)
                                AWD.Utilities.FadeToAndClearPending(_$image, AWD.Site.Settings.MainNavFadeSpeed, AWD.Site.Settings.MainNavBarHoverOpacity);
                        });
                    // Fade the links in (easy part)
                        $.each(_this._getAllLinks(), function(index, link) {
                            AWD.Utilities.FadeToAndClearPending($(link), AWD.Site.Settings.MainNavFadeSpeed, 1);
                        });
                    },
                    function() {
                    // Update with NavBox object... _this.NavBar.getActiveNavBox().Image (better way to filter those we need to animate)
                        var _$hoverImage;
                        $.each(_this._getAllImages(), function(index, image) {
                            var _$image = $(image);
                            if(_$image.queue().length > 0)
                                _$hoverImage = _$image;
                        });
                    // We wait for the image that is hovered over to fade out, then fade out
                    // the images.   This way there is no jump in the fade for that which was
                    // hovered over.
                        var _imageFadeFunction = function() {
                            $.each(_this._getAllImages(), function(index, image) {
                                AWD.Utilities.FadeToAndClearPending($(image), AWD.Site.Settings.MainNavFadeSpeed, _this.navBoxDefaultOpacity);
                            });
                        }
                    // Queue the fade after the image finishes fading (if we found one)
                        if(typeof _$hoverImage.queue == 'function') {
                            _$hoverImage.queue(function() {
                                _imageFadeFunction()
                                $(this).dequeue();
                            });
                        } else {
                            _imageFadeFunction();
                        }

                    // Fade the links back out
                        $.each(_this._getAllLinks(), function(index, link) {
                            AWD.Utilities.FadeToAndClearPending($(link), AWD.Site.Settings.MainNavFadeSpeed, _this.linkDefaultOpacity);
                        });
                    }
                )
            }

            this._setupNavBoxHover = function() {
                _this.$navBoxes.hover(
                    function() {
                        $(this).find('img').fadeTo(AWD.Site.Settings.MainNavFadeSpeed, AWD.Site.Settings.MainNavItemHoverOpacity);
                        $(this).find('.link a').css('color', AWD.Site.Settings.MainNavHoverTextColor);
                    },
                    function() {
                        $(this).find('img').fadeTo(AWD.Site.Settings.MainNavFadeSpeed, AWD.Site.Settings.MainNavBarHoverOpacity);
                        $(this).find('.link a').css('color', '');
                    }
                );
            }

            this._setupNavBoxClickEvents = function() {
                _this.$navBoxes.click(function() {
                    if(AWD.Site.GetInstance().isAnimating) return false;
                    var _$triggerNavBox = $(this);
                    var _projectCategory = _$triggerNavBox.find('a:first').attr('id');
                    if(_projectCategory != AWD.Site.GetInstance().ProjectNav.activeProjectCategory) {
                        AWD.Site.GetInstance().isAnimating = true;                    
                        // Set the active class
                        _this.$navBoxes.filter('.active').removeClass('active');
                        _$triggerNavBox.addClass('active');
                        AWD.Site.GetInstance().NavBar.linkDefaultOpacity = 1;
                        AWD.Site.GetInstance().NavBar.navBoxDefaultOpacity = AWD.Site.Settings.MainNavBarHoverOpacity;
                        AWD.Site.GetInstance().ContentNav.$contentLinks.filter('.active').removeClass('active');
                        AWD.Site.GetInstance().ProjectNav.hide();
                        AWD.Site.GetInstance().ViewManager.showView(
                            'loading',
                            function() {
                                AWD.Site.GetInstance().ProjectNav.showCategory(_projectCategory);
                                AWD.Site.GetInstance().ProjectNav.setFirstLinkActive();
                                AWD.Site.GetInstance().ProjectDisplay.setupDisplay(
                                    AWD.Site.GetInstance().ProjectNav.getFirstActiveProjectName(),
                                    function() {
                                        AWD.Site.GetInstance().ViewManager.showView('stage', function() { AWD.Site.GetInstance().isAnimating = false }, null, true);
                                    }
                                );
                            }
                        );
                    }
                });
            }

            this._setupHover();
            this._setupNavBoxHover();
            this._setupNavBoxClickEvents();
        },
        
        Modal : function(cssSelector) {
            var _this = this;
            
            this._$modalWindow = $(cssSelector);
            this._$modalOverlay = $('<div class="modalOverlay hidden" />');
            
            this.show = function() {
                if($('.modalOverlay').length == 0) {
                    $('body').append(_this._$modalOverlay);
                }
                _this._$modalOverlay.css('opacity', AWD.Site.Settings.ModalOverlayOpacity);
                _this._setOverlayDimensionsAndCenterModal();
                _this._$modalOverlay.fadeIn(AWD.Site.Settings.ModalFadeSpeed, function() {
                    _this._$modalWindow.fadeIn(AWD.Site.Settings.ModalFadeSpeed);
                });
            }
            
            this._setOverlayDimensionsAndCenterModal = function() {
                _this._$modalOverlay.css({
                    'top'       : '0px',
                    'width'     : $(document).width(),
                    'height'    : $(document).height()
                });
                _this._$modalWindow.css({
                    'top' : ($(window).height() - parseInt(_this._$modalWindow.outerHeight())) / 2,
                    'left' : ($(window).width() - parseInt(_this._$modalWindow.outerWidth())) / 2
                });
            }
            
            this.hide = function() {
                _this._$modalWindow.fadeOut(AWD.Site.Settings.ModalFadeSpeed, function() {
                    _this._$modalOverlay.fadeOut(AWD.Site.Settings.ModalFadeSpeed); 
                });
            }
            
            $(window).bind('resize', _this._setOverlayDimensionsAndCenterModal);
            
        }
    }
