/**
 * Bath Half JS
 * Author: Stuart Forster, http://gravitywell.co.uk
 *
 */

/** Main **/
Application = function() {
    
    // On Ready
    $(function() {
        
        // -- Search
        $('#query').focus(function() {
            if($(this).val() == 'Search...') {
                $(this).val("");
            }
        });
        $('#query').blur(function() {
            if($(this).val() == '') {
                $(this).val("Search...");
            }
        });
        
        
        // -- Menu
        $('#menu>ul>li').hover(function() {
            $(this).addClass('hover');
            $('ul', this).show();
        }, function() {
            $(this).removeClass('hover');
            $('ul', this).hide();
        });
        
        
        // -- We need to ensure all smallColumn boxes are the same height
        var maxHeight = 0;
        $('.smallColumn').each(function(i, el){
            if($(this).height() > maxHeight) {
                maxHeight = $(this).height();
            }
            
            // Also, for ones with lb (linkbox)
            if($(el).hasClass('lb')) {
                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');
                    });
                }
            }
        });
        $('.smallColumn').height(maxHeight);
        
        
        // -- Offsite links. Ensure always target of _blank
        $("a[href^='http:']").not("[href*='" + BASEDOMAIN + "']").attr('target','_blank');
        
        
        $('.smallImageHover').each(function(i, el) {
            if($('img', this).length > 1)  {
                $(this).parents('.smallColumn').hover(function() {
                    $('.smallImageHover img:last-child', this).fadeOut(0);
                }, function() {
                    $('.smallImageHover img:last-child', this).fadeIn(500);
                }).css('cursor', 'pointer');
            }
        });
        
        $('.sponsorImageHover').each(function(i, el) {
            if($('img', this).length > 1)  {
                $(this).hover(function() {
                    $('img:last-child', this).fadeOut(0);
                }, function() {
                    $('img:last-child', this).fadeIn(500);
                }).css('cursor', 'pointer');
                $(this).click(function() {
                    window.location.href = $(this).attr('rel');
                });
            }
        });
    });
    
    
};


Application.prototype.FeatureBox = {
    
    timer : null
    ,currentSlide: 1
    ,intervalPace: 6000
    ,maxSlides: 3
    
    // Apply events
    ,init: function() {
        _ = this;
        $(function() {
            if($('#homepageFeatures').length > 0) {
                _.resetTimer();
            }
            
            // Also, make sure events are paused when over embed
            $('#featuresRightInner .feature').each(function() {
                if($('object', this).length > 0) {
                    $(this).hover(function() {
                        clearInterval(_.timer);
                    }, function() {
                        _.resetTimer();
                    });
                    
                     $('#featuresLeftInner .feature[rel=' + $(this).attr('rel') + ']').hover(function() {
                        clearInterval(_.timer);
                    }, function() {
                        _.resetTimer();
                    });
                }
            });
        });
    }
    
    /**
     * Moves to a specific slide
     *
     */
    ,gotoSlide: function (slide) {
        
        leftPosLeft  = (slide == 1) ? "0px" : "-" + 225 * (slide - 1) + "px";
        leftPosRight = (slide == 1) ? "0px" : "-" + 490 * (slide - 1) + "px";
        
        $('#featuresLeftInner').animate({
            left: leftPosLeft
        }, 400, function() {
            $('#featuresRightInner').animate({
                left: leftPosRight
            }, 300);
            
            $('#featuresLeftPosition a').removeClass('active');
            $('#featuresLeftPosition a[rel=' + slide + ']').addClass('active');
        });
        
        _.currentSlide = slide;
        
        // Reset the timer now
        _.resetTimer();
    }
    
    ,resetTimer: function() {
        _ = this;
        if(null != _.timer) clearInterval(_.timer);
        _.timer = setInterval(
            function() {
                slide = (_.maxSlides == _.currentSlide) ? 1 : _.currentSlide + 1;
                _.gotoSlide(slide);
            }
        , _.intervalPace);
    }
}


BathHalf = new Application();
BathHalf.FeatureBox.init();
