/**
 * Mytton Williams JS
 *
 * Author: Gravitywell
 */
if (typeof('console') == 'undefined') { console = { log: function() { } }; } 


/**
 * Mytton Williams Namespace
 *
 */
mw = {
    
    /**
     * On DOM Ready
     *
     */
    domReady: function() {
        // Before anything, we need to ensure all smallColumn boxes are the same height
        var maxHeight = 0;
        $('.largeColumnList .smallColumn').each(function(i, el){
            if($(this).height() > maxHeight) {
                maxHeight = $(this).height();
            }
        });
        $('.largeColumnList .smallColumn').height(maxHeight);
        
        
        // Work: More Information
        $('.moreInformation').hide();
        $('.moreInformationLink a').click(function(e) {
            e.preventDefault();
            $('.moreInformation').slideToggle();
        });
        
        // Homepage: Client Rotator
        mw.Misc.clientRotator();
        
        // Init rotater
        this.Rotator.init();
        
        this.Misc.linkBox();
        this.Misc.smallImageHover();
        this.Misc.largeImageHover();
        mw.Book.init();
    },    
    
    
    Misc: {
        
        clientRotator: function() {
            
            // How many clients are there
            aClients = $('#clientsBox .smallImageRotate img').length;
            curPos = aClients;
            
            if(aClients > 1) {
                setInterval(function() {
                    if(curPos == 1) {
                        curPos = aClients;
                        $('#clientsBox .smallImageRotate img:eq(' + (aClients - 1) + ')').fadeIn(400, function() {
                            $('#clientsBox .smallImageRotate img').show();
                        });
                    } else {
                        $('#clientsBox .smallImageRotate img:eq(' + --curPos + ')').fadeOut(400);
                    }
                }, 4000);
            }
            
            
            
        },
        
        
        linkBox: function() {
        
            // All classes of .smallColumn.lb will look for h2 > a and use that to link the box
            els = $('.smallColumn.lb');
            
            els.each(function(i, el){
                href = $('h2 a', el).attr('href');
                
                if(typeof $('h2 a', el).attr('href') != "undefined") {
                    
                    $(el)
                    .css('cursor', 'pointer')
                    .click(function() {
                        window.location.href = $('h2 a', el).attr('href');
                    });
                    
                    // Also, on hover of the el, the h2/a needs to have active applied/removed
                    $(el).hover(function() {
                        $('h2 a', this).addClass('active');
                    }, function() {
                        $('h2 a', this).removeClass('active');
                    });
                }
                
            });
        },
        
        
        smallImageHover: function() {
            $('.smallImageHover').each(function(i, el) {
                if($('img', this).length > 1)  {
                    $(this).parents('.smallColumn').hover(function() {
                        $('.smallImageHover img:last-child', this).fadeOut(200);
                    }, function() {
                        $('.smallImageHover img:last-child', this).fadeIn(200);
                    }).css('cursor', 'pointer');
                }
            });
            
        },
        
        
        largeImageHover: function() {
            $('.homeImageHover').hover(function() {
                $('img:last-child', this).fadeOut(200);
            }, function() {
                $('img:last-child', this).fadeIn(200)
            });
        }
    },
    
    
    
    /**
     * Mytton Williams Image rotator with info box
     *
     */
    Rotator: {
        
        /* Slides */
        slides:         null,
        currentSlide:   null,
        nextSlide:      null,
        
        /* Timings */
        timeout:        4000,
        transition:     1000,
        
        /* Timeouts */
        timeouts: {
            info:       null,
            item:       null,
            limiter:    null
        },
        
        /* Rate Limiter */
        canSwitch: true,
        
        
        /**
         * Init
         *
         */
        init: function() {
            
            _ = this;
            
            if($('#header .item').length > 1) {
                _.slides       = $('#header');
                _.currentSlide = $('.item:first', _.slides);
                
                // 'Remove' all info boxes
                $('.item .info', _.slides).each(function(i, e) {
                    $(this).css('left', '-' + $(this).width() + 'px');
                    $(this).show();
                });
                
                // Hide all slides apart from the first one
                $('.item:gt(0)', _.slides).hide();
                
                _.currentSlide = $('.item:first', _.slides);
                
                // init rotator
                _.rotate();
            } else {
                $('#header .next').hide();
            }
        },
        
        /**
         * Rotates
         *
         */
        rotate: function() {
            
            _ = this;
            
            // Determine next slide
            if(_.currentSlide.next('.item').length > 0) {
                _.nextSlide = _.currentSlide.next('.item');
            } else {
                _.nextSlide = $('.item:first', _.slides);
            }
            
            // Show the info after the image is faded in     
            if($('.info', _.currentSlide).length > 0) {
                
                $('.info', _.currentSlide).animate({
                    left: 0
                }, {
                    duration: 500,
                    queue: false,
                    easing: 'easeOutBack',
                    complete: function() {
                        
                        // Do the transition
                        _.timeouts.item = setTimeout('_.next()', _.timeout);
                        
                    }
                });
            }
            
            else {
                // Just set the timeout
                _.timeouts.item = setTimeout('_.next()', _.timeout);
            }
        
        },
        
        /**
         * Cycles to the next one
         *
         */
        next: function() {
            _ = this;
            
            if(_.canSwitch) {
                
                // Clear the timeouts
                clearTimeout(_.timeouts.item);
                $('.item', _.slides).stop(true, true);
                $('.info', _.slides).stop(true, true);
                
                if($('.info', _.currentSlide).length > 0) {
                    $('.info', _.currentSlide).animate({
                        left: '-' + $('.info', _.currentSlide).width() + 'px'
                    }, {
                        duration: 300,
                        queue: false,
                        easing: 'easeInBack',
                        complete: function () {
                            _.initTransition();
                        }
                    });
                }
                
                else {
                    _.initTransition();                  
                }
            }
            
            // Rate Limiter
            if(_.canSwitch == true) {
                _.canSwitch = false;
                
                _.timeouts.limiter = setTimeout(function() {
                    _.canSwitch = true;
                }, _.transition);
            }
            
        },
        
        
        initTransition: function() {
            if(_.nextSlide.hasClass('num-1')) {
                _.nextSlide.fadeIn(0, function() {
                    _.currentSlide.fadeOut(_.transition, function() {
                        _.currentSlide = _.nextSlide;
                        _.rotate();
                    });
                });  
            } else {
                _.nextSlide.fadeIn(_.transition, function() {
                    _.currentSlide.fadeOut(0, function() {
                        _.currentSlide = _.nextSlide;
                        _.rotate();
                    });
                });  
            }
            
        }
        
    }
};


mw.Book = {
    init: function() {
        $('.smallColumn:has(a[href*=#bookofnos])').click(function(e) {
            e.preventDefault();
            url = $('[a[href*=#bookofnos]', this).attr('href');
            url = url.replace(/#bookofnos-/, '');
            url = parseInt(url, 10) - 1;
            url = '/bookofnos/index.html#image' + url.toString();
            $.fancybox(
                url,
                {
                    'autoDimensions'	: false,
                    'width'         	: 780,
                    'height'        	: 680,
                    'transitionIn'		: 'none',
                    'transitionOut'		: 'none',
                    'type'              : 'iframe'
                }
            );
        });
    }
};

// domReady
$(function() { mw.domReady(); });
