﻿// Background Controller
var background = {
    bgWidth : Number,
    init    : function(){
        this.run();
    },
    run     : function(){
        this.bgWidth = $(window).width();
        
        if (this.bgWidth <= 1024) {
            $('body').addClass('r1024');
        } else if (this.bgWidth > 1024 && this.bgWidth <= 1280) {
            $('body').addClass('r1280');
        } else if (this.bgWidth > 1280 && this.bgWidth <= 1680) {
            $('body').addClass('r1680');
        } else {
            $('body').addClass('r1920');
        }
    }
}


// Dropdown controller
var dropdownControl = {
    init    : function(){
        $('#header ul.dropDown li').hover(function() {
            $(this).addClass('on');
        },function() {
            $(this).removeClass('on');
        });
    }
}


// Footer controller
var footerControl = {
    init    : function(){
        $('#footer ul li ul').hover(function(){
            $(this).parent().addClass('on');
        },function(){
            $(this).parent().removeClass('on');            
        });
    } 
}


// external anchor controller
var anchorController = {
    init    : function(){
        $('a[rel*=external]').click(function(){
            window.open(this.href);
            return false;
        });
    }
}


// Navigation Control
var navControl = {
    init    : function(){
        $('#lCol li a').click(function(){
            if($(this).parents('li').hasClass('off')){
                return false;
            }
        });
    }
}


// Scrollbar Init
var scrollControl = {
    init    : function(){
        $('#rCol .holder').jScrollPane({
            'scrollbarWidth' : '17',
            'scrollbarMargin' : '0',
            'showArrows' : true,
            'arrowSize' : '17'
        });
        
        $('#cCol .holder').jScrollPane({
            'scrollbarWidth' : '17',
            'scrollbarMargin' : '5',
            'showArrows' : true,
            'arrowSize' : '17'
        });
    },
    reinitialise : function(){
        $('#rCol .holder').jScrollPane({
            'scrollbarWidth' : '17',
            'scrollbarMargin' : '0',
            'showArrows' : true,
            'arrowSize' : '17'
        });
        $('#rCol .holder')[0].scrollTo($('#rCol .holder').data('jScrollPaneMaxScroll'));
    }
}

// Score Slider
var scoreControl = {
    average: Number,
    you: Object,
    init: function() {
        $('.scoreInformation').hide();
        $('.score').css('display', 'block');
        this.you = $('.you');
        this.run();
    },
    run: function() {
        this.calcAv();
    },
    calcAv: function() {
        scoreControl.average = 100 - (parseInt($('#hidScore').val()));
        scoreControl.you.css('left', scoreControl.average + '%');
        $('.you div').removeClass();
        if (scoreControl.average >= 70) {
            $('.you div').addClass('good');
            if (scoreControl.average >= 90) {
                $('.you div').text('great');
            } else {
                $('.you div').text('good');
            }
        } else if (scoreControl.average < 70 && scoreControl.average > 30) {
            $('.you div').text('average').addClass('ok');
        } else {
            $('.you div').addClass('bad');
            if (scoreControl.average > 10 && scoreControl.average <= 30) {
                $('.you div').text('fair');
            } else {
                $('.you div').text('poor');
            }
        }
    }
}


// Login Controller
var login = {
    toggler : Object,
    panel   : Object,
    init    : function(){
        this.toggler = $('#loginTab div h4 a, #loginNestNest a.close');
        this.panel = $('#logincontainer #login');
        this.run();
    },
    run     : function(){
        this.toggler.live('click', function(){
            if(login.panel.is(':hidden')){
                login.panel.slideDown('fast');
            }else{        
                login.panel.slideUp('fast');
            }
        });
    }
}


// Score Explanation Controller
var scoreExplanation = {
    togglers : Object,
    panel   : Object,
    init    : function(){
        this.togglers = $('#scoreTitle a, #scoreExplanation a.close');
        this.panel = $('#scoreExplanation');
        this.run();
    },
    run     : function(){
        this.togglers.click(function(){
            if(scoreExplanation.panel.is(':hidden')){
                scoreExplanation.panel.slideDown('normal', function(){
                    $('#scoreExplanation a.close').fadeIn('normal');
                });
            }else{
                scoreExplanation.panel.slideUp('normal', function(){
                    $('#scoreExplanation a.close').fadeOut('normal');
                });
            }
        });
    }
}

// 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);
                });
            }
        });
    }
}

// Email
var email = {
    footerController: Object,
    footerControllee: Object,
    resultsController: Object,
    resultsControllee: Object,
    clearer: Object,
    init: function(){
        this.footerController = $('#emailYourFriends a');
        this.footerControllee = $('#emailYourFriends .emailForm');
        this.resultsController = $('#btnEmail a');
        this.resultsControllee = $('#btnEmail .emailForm');
        this.clearer = $('.emailForm input, .emailForm textarea');
        this.run();
    },
    run: function(){
        this.footerController.click(function(e){
            email.footerControllee.toggle();
            e.preventDefault();
        });
        
        this.resultsController.click(function(e){
            email.resultsControllee.toggle();
            e.preventDefault();
        });
        
        this.clearer.focus(function(){
            if($(this).val() == $(this).attr('defaultValue')){
                $(this).val('');
            }
        });
 
        this.clearer.blur(function(){
            if($(this).val() == ''){
                $(this).val($(this).attr('defaultValue'));
            }
        });
        
        
        // footer
        $('#emailYourFriends .wordCount span').html($('#emailYourFriends .emailForm textarea').val().length);

        $('#emailYourFriends .emailForm textarea').keyup(function(){
            $('#emailYourFriends .wordCount span').html($(this).val().length);
            if($(this).val().length > 300){
                $('#emailYourFriends .wordCount').css('color','#ff0000');
            }else{
                $('#emailYourFriends .wordCount').css('color','#0194D3');            
            }
        });
        
        
        // results
        if($('#btnEmail').length != 0){
            $('#btnEmail .wordCount span').html($('#btnEmail .emailForm textarea').val().length);

            $('#btnEmail .emailForm textarea').keyup(function(){
                $('#btnEmail .wordCount span').html($(this).val().length);
                if($(this).val().length > 300){
                    $('#btnEmail .wordCount').css('color','#ff0000');
                }else{
                    $('#btnEmail .wordCount').css('color','#0194D3');            
                }
            });
        }
        
        
    }
}


// Privacy Policy
var privacy = {
    toggler     : Object,
    winHeight   : Object,
    init        : function(){
        this.toggler = $('#legal ul li#privacy a');
        this.run();
    },
    run         : function(){
        privacy.toggler.click(function(){
            privacy.winHeight = $(document).height();
            $('body').prepend('<div id="modal-overlay" style="height:' + privacy.winHeight + 'px;"><div id="modal-container"><a href="#" class="close">Close</a><div class="holder"></div></div></div>');
            $('#modal-container .holder').load('/home/privacy/', '', function(){
                $(this).jScrollPane({
                    'scrollbarWidth' : '17',
                    'scrollbarMargin' : '10',
                    'showArrows' : true,
                    'arrowSize' : '17'
                });
            });
            $('#modal-overlay').fadeIn('fast');
        });
        
        $('#modal-container a.close').live('click', function(){
            $('#modal-overlay').fadeOut('fast', function(){
                $(this).remove();
            });
        });
        
        $('#modal-overlay').live('click', function(e){
            if(e.target.id == 'modal-overlay'){
                $('#modal-overlay').fadeOut('fast', function(){
                    $(this).remove();
                });
            }

        });
        
    }
}


// Page Initialization
function initialize(){
    background.init();
    dropdownControl.init();
    footerControl.init();
    anchorController.init();
    scoreControl.init();
    navControl.init();
    scrollControl.init();
    login.init();
    scoreExplanation.init();
    trackExternalLinks.init();
    email.init();
    privacy.init();
}


// Start
$(document).ready(function(){
    initialize();
});
