﻿
// Finalist Controls
var finalist = {
    init : function(){
        this.run();
    },
    run : function(){
    
        // Adds on class
        $("#finalist li a.finalistAnchor").click(function(e){
            $(this).parent().toggleClass('on');
            e.preventDefault();
        });
    
        // Hover control
        $("#finalist li").hoverIntent(function () {
            $(this).css({
                'z-index': '100'
            });
            $(this).find('img.gridImage').addClass("hover").stop().animate({
                top: '-20px',
                left: '-20px',
                height: '147px',
                width: '147px',
                padding: '10px'
            },200);
        },function () {
            if( !$(this).hasClass('on') ){
                $(this).find('img.gridImage').removeClass("hover").stop().animate({
                    top: '0',
                    left: '0',
                    height: '127px',
                    width: '127px',
                    padding: '0'
                },200, function(){
                    $(this).parent().parent().css({
                        'z-index': '0'
                    });
                });
            }
        });   
       
        // Closes Voting Div
        $('#finalist li div.finalVote a.close').live('click', function(e){
       
            $(this).parents('div.votingDiv').parent().find('img.gridImage').removeClass("hover").stop().animate({
                top: '0',
                left: '0',
                height: '127px',
                width: '127px',
                padding: '0'
            }, 200, function(){
                $(this).parents('div.votingDiv').parent().css({'z-index': '0'});
            });
            
            $(this).parents('div.votingDiv').parent().removeClass('on');
            $(this).parents('div.votingDiv').html('');
            
            e.preventDefault();
        });
        
        
        // Clears e-mail input
        $('div.finalVote input.text').live('click', function(){
            if($(this).val() == 'Email'){
                $(this).val('');
            }
        });
        
        //Random clicking.  argh.
        $('ul#finalist li a').click(function(){
            $('div.votingDiv').html('');
            $('ul#finalist li').removeClass('on').css({'z-index':'0'});
            $(this).parent().addClass('on').css({'z-index': '100'});
            $('ul#finalist li:not(li.on)').find('img.gridImage').addClass("hover").stop().animate({
                top: '0',
                left: '0',
                height: '127px',
                width: '127px',
                padding: '0'
            }, 200);
        });
        
        // Opens Next Voting Div
        $('div.finalVote ul.controls li.next a').live('click', function(e){
            
            // close current
                $(this).parents('div.votingDiv').parent().find('img.gridImage').removeClass("hover").stop().animate({
                    top: '0',
                    left: '0',
                    height: '127px',
                    width: '127px',
                    padding: '0'
                }, 200);
                $(this).parents('div.votingDiv').parent().removeClass('on').css({'z-index': '0'});
                
            // Open Next
                $(this).parents('div.votingDiv').parent().next().find('img.gridImage').addClass("hover").stop().animate({
                    top: '-20px',
                    left: '-20px',
                    height: '147px',
                    width: '147px',
                    padding: '10px'
                }, 200);
                $(this).parents('div.votingDiv').parent().next().addClass('on').css({'z-index': '100'});
                
            // Clear Current
                $(this).parents('div.votingDiv').html('');
                
            // Stops Click
                e.preventDefault();
                
        });
        
        
        // Opens Previous Voting Div
        $('div.finalVote ul.controls li.previous a').live('click', function(e){
            
            // close current
                $(this).parents('div.votingDiv').parent().find('img.gridImage').removeClass("hover").stop().animate({
                    top: '0',
                    left: '0',
                    height: '127px',
                    width: '127px',
                    padding: '0'
                }, 200);
                $(this).parents('div.votingDiv').parent().removeClass('on').css({'z-index': '0'});
                
            // Open Previous
                $(this).parents('div.votingDiv').parent().prev().find('img.gridImage').addClass("hover").stop().animate({
                    top: '-20px',
                    left: '-20px',
                    height: '147px',
                    width: '147px',
                    padding: '10px'
                }, 200);
                $(this).parents('div.votingDiv').parent().prev().addClass('on').css({'z-index': '100'});
            
            // Clear Current
                $(this).parents('div.votingDiv').html('');
            
            // Stops Click
                e.preventDefault();
        });
    }
}


// Word Counter
var topNavController = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#overall #header #topNav li#nav-login').live('mouseover', function(){
            $(this).addClass('hovering');
        });
        $('#overall #header #topNav li#nav-login').live('mouseout', function(){
            $(this).removeClass('hovering');            
        });
        $('#overall #header #topNav li#nav-login input#input-ID').live('click', function(){
            if($(this).val() == 'Email/User ID'){
                $(this).val('');
            }
        });
        $('#overall #header #topNav li#nav-login input#input-ID').blur(function(){
            if($(this).val() == ''){
                $(this).val('Email/User ID');
            }
        });
        $('#overall #header #topNav li#nav-login input#input-Password').live('click', function(){
            $(this).css('backgroundPosition', 'bottom left');
        });
        $('#overall #header #topNav li#nav-login input#input-Password').live('keyup', function(){
            $(this).css('backgroundPosition', 'bottom left');
        });
        $('#overall #header #topNav li#nav-login input#input-Password').blur(function(){
            if($(this).val() == ''){
                $(this).css('backgroundPosition', 'top left');
            }
        });
        $('#overall #header #mainNav ul li a').hover(function(){
            $(this).addClass('hovering');
        },
        function(){
            $(this).removeClass('hovering');
        });
    }
}


// Let It Grow Controller
var letItGrowController = {
    init : function(){
        this.run();
    },
    run : function(){
        var state = '';
        
        
        // Change Dates
        $('div#tourMap map area').hover(function(){
            state = $(this).attr('alt')
            $('p#nextStop span').hide();
            $('p#nextStop span#dates_' + state + '').show();
        }, function(){
            $('p#nextStop span').hide();
            $('p#nextStop span.on').show();
        });
        
        // Open Modal
        $('div#tourMap map area').click(function(e){
        
            if(jQuery.support.opacity){
                $(".modal").fadeOut('fast');
            }else{
                $(".modal").hide();            
            }
        
            state = $(this).attr('alt')
            if(jQuery.support.opacity){
                $('#modal_' + state).fadeIn('fast');
            }else{
                $('#modal_' + state).show();
            }
            e.preventDefault();
        });
        
        // Close Modal
        $('div#tourMap div.modal a.close').click(function(e){
            if(jQuery.support.opacity){
                $(".modal").fadeOut('fast');
            }else{
                $(".modal").hide();            
            }
            e.preventDefault();
        });
        
        // Previous
        $('div#tourMap div.modal ul.controls li a.previous').click(function(e){
            $(".modal").fadeOut('fast');
            $(this).parents('div.modal').prev().fadeIn('fast');
            e.preventDefault();
        });
        
        // Next
        $('div#tourMap div.modal ul.controls li a.next').click(function(e){
            $(".modal").fadeOut('fast');
            $(this).parents('div.modal').next().fadeIn('fast');
            e.preventDefault();
        });        
        
        // Color Map
        $('div#tourMap #overlayMap area').hover(function(){
            state = $(this).attr('alt')
            if (state != 'HTX'){
                $('div#state_' + state).addClass('hovering');
                $('a#button_'+ state).addClass('on');
            }else{
                $('div#state_TX').addClass('hovering');
                $('a#button_HTX').addClass('on');                
            }                
        },
        function(){
            if (state != 'HTX'){
                $('div#state_' + state).removeClass('hovering');
                $('a#button_'+ state).removeClass('on');
            }else{
                $('div#state_TX').removeClass('hovering');
                $('a#button_HTX').removeClass('on');            
            }
            
        });
    }
}   


// Wahl Products Controller
var productsController = {
    init : function(){
        this.run();
    },
    run : function(){    
        $('#wahlProducts #wahlNav li').hoverIntent(function() {
            $(this).find('div.info').stop(false,true).fadeIn('fast');
        }, function(){
            $(this).find('div.info').stop(false,true).fadeOut('fast');
        });
    }
}


// Forms
var formController = {
    init : function(){
        this.run();
    },
    run : function(){
        $('a#selectAll').click(function(e) {
            $('div.friendSelect ul li input.styled').attr('checked', 'true');
            $('div.friendSelect ul li span.checkbox').css('background-position', '0px -50px');
            e.preventDefault();
        });
        $('a#selectNone').click(function(e) {
            $('div.friendSelect ul li input.styled').removeAttr('checked');
            $('div.friendSelect ul li span.checkbox').css('background-position', '0px 0px');
            e.preventDefault();
        });
        
        // Reset Button
        $('div.controls input#resetButton').click(function(){
            $('#lCol input[type=text], #lCol input[type=password], #lCol textarea').val('');
            $('#lCol input.styled').removeAttr('checked');
            $('#lCol span.checkbox, #lCol span.radio').css('background-position', '0px 0px');
            $('#lCol span.select').html('--');
            $('#lCol select').attr('selectedIndex', 0);
            return false;
        });
    }
}


// Cufon Shenanigans
var cufonInitialization = {
    init : function(){
        $('ul#topNav').mouseover(function(){
            Cufon.replace('ul#topNav li#nav-welcome p');
        });
    }
}


// Accordion Controller
var accordionController = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#accordionHolder, #questionHolder').accordion({ autoHeight: false });
        $('#accordionHolder li a.title').click(function(e){
            e.preventDefault();
        });
    }
}


// Banner Rotator
var bannerRotate = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#mainBanner.rotator').innerfade({
            speed: 'slow',
            timeout: 5000,
            containerheight: '392px'
        });
    }
}


// Button Hover
var buttonHoverControl = {
    init : function(){
        this.run();
    },
    run : function(){
        $('li#nav-login input#input-Button').live('mouseover', function(){
            $(this).attr('src', '/Content/images/btn-login-hover.gif');
        });
        $('li#nav-login input#input-Button').live('mouseout', function(){
            $(this).attr('src', '/Content/images/btn-login.gif');        
        });
        
        $('#resetButton').hover(function(){
            $(this).attr('src', '/Content/images/btn-reset-hover.gif');
        },function(){
            $(this).attr('src', '/Content/images/btn-reset.gif');
        });
        
        $('#submitButton').hover(function(){
            if($(this).hasClass('submitted') == false){
                $(this).attr('src', '/Content/images/btn-submit-hover.gif');
            }
        },function(){
            if($(this).hasClass('submitted') == false){
                $(this).attr('src', '/Content/images/btn-submit.gif');
            }
        });

        $('#submitButton').mousedown(function(){
            if($(this).hasClass('lostPassword') == false){
                $(this).attr('src', '/Content/images/btn-submit-loading.gif');
                $(this).addClass('submitted');
            }
        });

        $('#submitTwo').hover(function(){
            $(this).attr('src', '/Content/images/btn-submit2-hover.gif');
        },function(){
            $(this).attr('src', '/Content/images/btn-submit2.gif');
        });        
        
        
    }
}


// Voting Box Control
var votingControl = {
    init : function(){
        this.run();
    },
    run : function(){
    
        // Adds on class
        $("ul.votingBlock li a.voteAnchor").click(function(){
            $(this).parent().addClass('on');        
        });
    
        // Hover control
        $("ul.votingBlock li").hoverIntent(function () {
            $(this).css({
                'z-index': '100'
            });
            $(this).find('img.gridImage').addClass("hover").stop().animate({
                marginTop: '-52px',
                marginLeft: '-52px',
                top: '50%',
                left: '50%',
                width: '75px',
                height: '75px',
                padding: '15px'
            },200);
        },function () {
            if( !$(this).hasClass('on') ){
                $(this).css({
                    'z-index': '0'
                });
                $(this).find('img.gridImage').removeClass("hover").stop().animate({
                    marginTop: '0',
                    marginLeft: '0',
                    top: '0',
                    left: '0',
                    width: '64px',
                    height: '64px',
                    padding: '0'
                },200);
            }
        });   
    

        //Random clicking.  argh.
        $('ul#accordionHolder li ul li a').click(function(){
            $('div.votingDiv').html('');
            $('ul#accordionHolder li ul li').removeClass('on').css({'z-index':'0'});
            $(this).parent().addClass('on').css({'z-index': '100'});
            $('ul#accordionHolder li ul li:not(li.on)').find('img.gridImage').addClass("hover").stop().animate({
                marginTop: '0',
                marginLeft: '0',
                top: '0',
                left: '0',
                width: '64px',
                height: '64px',
                padding: '0'
            }, 200);
        });
    

        // Closes Voting Div
        $('div.vote a.close').live('click', function(e){
            $(this).parents('div.votingDiv').parent().find('img.gridImage').removeClass("hover").stop().animate({
                marginTop: '0',
                marginLeft: '0',
                top: '0',
                left: '0',
                width: '64px',
                height: '64px',
                padding: '0'
            }, 200);
            $(this).parents('div.votingDiv').parent().css({'z-index': '0'});
            $(this).parents('div.votingDiv').parent().removeClass('on');
            $(this).parents('div.votingDiv').html('');
            
            e.preventDefault();
        });
        
        
        // Clears e-mail input
        $('div.vote input.text').live('click', function(){
            if($(this).val() == 'Email'){
                $(this).val('');
            }
        });
        
        
        // Opens Next Voting Div
        $('div.vote ul.controls li.next a').live('click', function(e){
            
            // close current
                $(this).parents('div.votingDiv').parent().find('img.gridImage').removeClass("hover").stop().animate({
                    marginTop: '0',
                    marginLeft: '0',
                    top: '0',
                    left: '0',
                    width: '64px',
                    height: '64px',
                    padding: '0'
                }, 200);
                $(this).parents('div.votingDiv').parent().removeClass('on').css({'z-index': '0'});
                
            // Open Next
                $(this).parents('div.votingDiv').parent().next().find('img.gridImage').addClass("hover").stop().animate({
                    marginTop: '-52px',
                    marginLeft: '-52px',
                    top: '50%',
                    left: '50%',
                    width: '75px',
                    height: '75px',
                    padding: '15px'
                }, 200);
                $(this).parents('div.votingDiv').parent().next().addClass('on').css({'z-index': '100'});
                
            // Clear Current
                $(this).parents('div.votingDiv').html('');
                
            // Stops Click
                e.preventDefault();
                
        });
        
        
        // Opens Previous Voting Div
        $('div.vote ul.controls li.previous a').live('click', function(e){
            
            // close current
                $(this).parents('div.votingDiv').parent().find('img.gridImage').removeClass("hover").stop().animate({
                    marginTop: '0',
                    marginLeft: '0',
                    top: '0',
                    left: '0',
                    width: '64px',
                    height: '64px',
                    padding: '0'
                }, 200);
                $(this).parents('div.votingDiv').parent().removeClass('on').css({'z-index': '0'});
                
            // Open Previous
                $(this).parents('div.votingDiv').parent().prev().find('img.gridImage').addClass("hover").stop().animate({
                    marginTop: '-52px',
                    marginLeft: '-52px',
                    top: '50%',
                    left: '50%',
                    width: '75px',
                    height: '75px',
                    padding: '15px'
                }, 200);
                $(this).parents('div.votingDiv').parent().prev().addClass('on').css({'z-index': '100'});
            
            // Clear Current
                $(this).parents('div.votingDiv').html('');
            
            // Stops Click
                e.preventDefault();
        });
    }
}


// Error Control
var errorControl = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#error #message a.close').live('click', function(e){
            $('#error').remove();
            e.preventDefault();
        });
    }
}


// Preload Images
var preloadImages = {
    init : function(){
        this.run();
    },
    run : function(){
        $.preloadImages(
        "/Content/images/bg-prodBeard.png",
        "/Content/images/bg-prodBody.png",
        "/Content/images/bg-prodEarNose.png",
        "/Content/images/bg-prodGoatee.png",
        "/Content/images/bg-prodLithiumIon.png",
        "/Content/images/bg-prodMustache.png",
        "/Content/images/bg-prodShopTools.png")
    }
}


// Moty Nav Controller
var manOfTheYearController = {
    init : function(){
        this.run();
    },
    run : function(){
        
        $('#manOfTheYearNav ul li.future a').click(function(e){
            e.preventDefault();
        });
    }
}


// Let it grow strip
var letItGrowStrip = {
    init : function(){
        this.run();
    },
    run : function(){
        $("ul.vote li").hoverIntent(function () {
            $(this).css({
                'z-index': '100'
            });
            $(this).find('img').addClass("hover").stop().animate({
                marginTop: '-55px',
                marginLeft: '-55px',
                top: '50%',
                left: '50%',
                width: '85px',
                height: '85px',
                padding: '15px'
            },200);
        },function () {
            $(this).css({
                'z-index': '0'
            });
            $(this).find('img').removeClass("hover").stop().animate({
                marginTop: '0',
                marginLeft: '0',
                top: '0',
                left: '0',
                width: '55px',
                height: '55px',
                padding: '0'
            },200);
        });
    }
}


// GA Initialization
var trackExternalLinks = {
    init : function(){
        this.run();
    },
    run : function(){
        $('a').each(function(){
            if (this.hostname.toLowerCase() != window.location.hostname.toLowerCase()) {
                $(this).click(function() {
                    var trackAddress = '/EXTERNAL/' + escape($(this).attr('href'));
                    pageTracker._trackPageview(trackAddress);
                });
            }
        });
    }
}


// experts popup
var experts = {
    init    : function(){
        this.run();
    },
    run     : function(){
        $('#expertRoll li div.holder').click(function(){
            $('#expertRoll li.on').removeClass('on').addClass('off'); 

            if($(this).parent().hasClass('off')){
                $(this).parent().removeClass('off').addClass('on');
            }else{
                $(this).parent().removeClass('on').addClass('off');
            }
        });

        $('#expertRoll a.close').click(function(e){
            $(this).parents('li').removeClass('on').addClass('off');
            e.preventDefault();
        });

        $('#expertRoll li').hover(function(){
            $(this).addClass('hover');
        }, function(){
            $(this).removeClass('hover');
        });

    }
}


// Questions popup
var questions = {
    init    : function(){
        this.run();
    },
    run     : function(){
        $('#questionHolder li li a.opener, #questionHolder li li a.close, #styleGrid li a:not(p a), #searchResults li a.opener,  #searchResults li a.close').click(function(e){
            $('li.on').removeClass('on').addClass('off'); 

            if($(this).parent().hasClass('off')){
                $(this).parent().removeClass('off').addClass('on');
            }else{
                $(this).parent().removeClass('on').addClass('off');
            }

            e.preventDefault();
        })
    }
}

var modal_controls = {
    prev    : Object,
    next    : Object,
    init    : function(){
        this.prev = $('.modal_controls a.prev_link');
        this.next = $('.modal_controls a.next_link');
        this.run();
    },
    run     : function(){
        this.prev.click(function(e){
            $(this).parents('li.on').removeClass('on').prev().addClass('on');
            e.preventDefault();
        });

        this.next.click(function(e){
            $(this).parents('li.on').removeClass('on').next().addClass('on');
            e.preventDefault();
        });
    }
}



// Sidebar Question
var sidebarQuestion = {
    trigger : Object,
    popup   : Object,
    close   : Object,
    init    : function(){
        this.trigger = $('#sidebarQuestion a.opener');
        this.popup = $('#questionFormDiv');
        this.close = $('#questionFormDiv a.close');
        this.run();
    },
    run     : function(){
        this.trigger.click(function(e){
            sidebarQuestion.popup.css('display','block');
            e.preventDefault();
        });

        this.close.live('click', function(e){
            sidebarQuestion.popup.css('display','none');
            e.preventDefault();
        });
    }
}


var tracker = {
    track   : function(){
        // Tracks Main Navigation
        $('#mainNav a').click(function(){
            _gaq.push(['_trackEvent', 'Internal Link','click','Main Navigation',$(this).attr('href')]);
        });

        // Tracks Bottom Navigation
        $('#bottomNav a, #legalNav a').click(function(){
            _gaq.push(['_trackEvent', 'Internal Link','click','Bottom Navigation',$(this).attr('href')]);
        });

        // Tracks Banner Clicks
        $('#mainBanner a').click(function(){
            _gaq.push(['_trackEvent', 'Internal Link','click','Banner',$(this).attr('href')]);
        });

        // Tracks Social Navigation
        $('#socialNav a').click(function(){
            _gaq.push(['_trackEvent', 'External Link','click','Social Navigation',$(this).attr('href')]);
        });

        // Tracks External Links
        $('#lCol a:external').click(function(){
            _gaq.push(['_trackEvent', 'External Link','click','Links on ' + window.location.href,$(this).attr('href')]);
        });

        // Tracks Question Form 
        $('#questionFormDiv a.submit').click(function(){
            _gaq.push(['_trackEvent', 'Form','Submit','Question Form',window.location.href]);
        });

        // Tracks Registration Form 
        $('#submitButton').click(function(){
            _gaq.push(['_trackEvent', 'Form','Submit','Registration Form']);
        });

        // Tracks stupid reset button
        $('#resetButton').click(function(){
            _gaq.push(['_trackEvent', 'Form','Reset','Registration Form']);
        });
    }
}


// Page Initialization
function initialize(){
    topNavController.init();
    productsController.init();
    formController.init();
    letItGrowController.init();
    cufonInitialization.init();
    accordionController.init();
    bannerRotate.init();
    buttonHoverControl.init();
    votingControl.init();
    errorControl.init();
    preloadImages.init();
    manOfTheYearController.init();
    letItGrowStrip.init();
    trackExternalLinks.init();
    finalist.init();
    experts.init();
    questions.init();
    sidebarQuestion.init();
    modal_controls.init();
    tracker.track();
}


// Start
$(document).ready(function(){
    initialize();
});

// Cufon
Cufon.replace('h2, #contentBuffer h3, #timeLine h4, #rCol h3, a.join, ul#accordionHolder li.accordion a.title, #rCol div.pane ul#contestButtons li a, #mainBanner.homepage p.totalTrimmed, #mainBanner.homepage #nextCity h3, div.modalBottom h4, fieldset label, fieldset p.title, #mainBanner.manOfTheYear h3, div.moty div, div.blogEntry h4, li#nav-welcome p, #facialHair h3, #facialHair p');
Cufon.replace('#rCol div#letItGrowNav.pane ul li a, #finalistNav li a, #expertRoll h4, #latestQuestion h3, #latestQuestion .question, .FHE h4, .FHE3 h4, #questionHolder .answer .question, #searchResults .answer .question');
Cufon.replace('#FacialHairNav li a, #questionHolder h3 a',{hover:true});


// Creating custom :external selector
$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
            && (obj.hostname != location.hostname);
};