﻿
// Opens & Closes the category slidey thingers
var categories = {
    trigger     : Object,
    moreInfo    : Object,
    holder      : Object,
    closer      : Object,
    init        : function(){
        this.trigger = $('.category');
        this.moreInfo = $('.controls .opener');
        this.holder = $('.holder');
        this.closer = $('.holder a.close');
        this.run();
    },
    run         : function(){
        this.moreInfo.click(function(){
            $('.category.on').removeClass('on')
            $('.category h2 a.on').removeClass('on');
            
            $(this).addClass('on');
            $(this).parent().parent().addClass('on');
            categories.holder.hide('normal');
            modal.reset($(this).parent().siblings('.holder'));
            $(this).parent().siblings('.holder').show('normal', function(){
                $(this).addClass('on')
                modal.current = $(this);
            });
            return false
        });
  
        this.trigger.hover(function(){
           $(this).find('.controls').fadeIn('fast');
           $(this).find('h2').addClass('on');
        }, function(){
            $(this).find('.controls').fadeOut('fast');
            $(this).find('h2').removeClass('on');
        });
  
        
        
        this.closer.click(function(){
            $('.category h2 a').removeClass('on');
            $(this).parent().hide('normal', function(){
                $(this).removeClass('on');
            });
            return false;
        });
    }
}

// Does some shenanigans inside the slidey thingers.
var modal = {
    pagination  : Object,
    sections    : Object,
    nextBtn     : Object,
    prevBtn     : Object,
    current     : Object,
    init        : function(){
        this.pagination = $('ul.pagination li a');
        this.sections = $('.section');
        this.nextBtn = $('.controls a.next');
        this.prevBtn = $('.controls a.prev');
        this.run();
    },
    run         : function(){

        // Next Button
        this.nextBtn.click(function(){
            if(modal.prevBtn.hasClass('off')){
                modal.prevBtn.removeClass('off');
            }
            $('.section.on').removeClass('on').next().addClass('on');
            $('.pagination li.on').removeClass('on').next().addClass('on');
            
            if(modal.current.find('.section:last').hasClass('on')){
                modal.nextBtn.addClass('off');
            }
            
            return false;
        });

        // Previous Button
        this.prevBtn.click(function(){
            if(modal.nextBtn.hasClass('off')){
                modal.nextBtn.removeClass('off');
            }
            $('.section.on').removeClass('on').prev().addClass('on');
            $('.pagination li.on').removeClass('on').prev().addClass('on');
            
            if(modal.current.find('.section:first').hasClass('on')){
                modal.prevBtn.addClass('off');
            }
            
            return false;
        });
        
        // Pagination Click
        this.pagination.click(function(){
            $('.pagination li.on').removeClass('on');
            $(this).parent().addClass('on');
            $('.section.on').removeClass('on');
            modal.current.find('.section').eq(modal.current.find('.pagination li a').index($(this))).addClass('on');
            
            if(modal.current.find('.section:first').hasClass('on')){
                modal.prevBtn.addClass('off');
                modal.nextBtn.removeClass('off');
            }else if(modal.current.find('.section:last').hasClass('on')){
                modal.nextBtn.addClass('off');
                modal.prevBtn.removeClass('off');
            }else{
                modal.nextBtn.removeClass('off');
                modal.prevBtn.removeClass('off');
            }
            
            return false;
        });

    },
    reset       : function(current){
        current.find('.section.on').removeClass('on');
        current.find('.section:first').addClass('on');
        current.find('.pagination li.on').removeClass('on');
        current.find('.pagination li:first').addClass('on');
        current.find('.controls a.prev').addClass('off');
        current.find('.controls a.next.off').removeClass('off');
    }
}

// Bottom Illustration
var bottom = {
    contentHeight   : Number,
    winHeight       : Number,
    bottom          : Object,
    init            : function(){
        this.contentHeight = $('#overall').height() + $('#footer').height();
        this.winHeight = $(window).height();
        this.bottom = $('#bottomImage');
        this.run();
    },
    run             : function(){
    
        if(bottom.contentHeight < bottom.winHeight){
            bottom.bottom.addClass('absolute')
        }
    
        $(window).resize(function(){
            bottom.winHeight = $(window).height();
            if(bottom.contentHeight < bottom.winHeight){
                bottom.bottom.addClass('absolute')
            }else{
                bottom.bottom.removeClass('absolute')
            }
        });
    }
}

// Highlights
var highlights = {
    length          : Number,
    current         : Number,
    trigger         : Object,
    init            : function(){
        this.length = $('.highlight').length;
        this.current = 1;
        this.trigger = $('#highlights h3 a');
        this.run();
    },
    run             : function(){    
        this.trigger.click(function(){
            if(highlights.current < highlights.length){
                $('.highlight.on').fadeOut('fast', function(){
                    $(this).removeClass('on').next('.highlight').fadeIn('fast', function(){
                        $(this).addClass('on');
                    });
                });
                highlights.current ++;
            }else{
                $('.highlight.on').fadeOut('fast', function(){
                    $(this).removeClass('on');
                    $('.highlight:first').fadeIn('fast', function(){
                        $(this).addClass('on');
                    });
                });
                highlights.current = 1;
            }
            return false;
        });
    }
}


function homeInit(){
    categories.init();
    modal.init();
    bottom.init();
    highlights.init();
}

$(window).ready(function(){
    homeInit();
});