﻿

// Dropdown controller
var dropdownControl = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#header ul.dropDown li').hover(
            function() {
                $(this).addClass('on');
            },
            function() {
                $(this).removeClass('on');
            }
        );
    }
}


// Table Display Control
var tableDisplay = {
    
    off : 'true',

    init : function(){
        if(jQuery.support.opacity){ 
            $('.tableDisplay.homepage').hide();
            $('.tableDisplay.homepage').fadeTo('fast',0.0);
        }else{
            $('.tableDisplay.homepage').hide();
        }
        
        if(jQuery.support.opacity){ 
            $('.tableDisplay.newSearch').fadeTo('fast',0.5);
        }
        
    },
    show : function(){
        if(jQuery.support.opacity){
            $('.tableDisplay').show();
            $('.tableDisplay').fadeTo('normal',0.5);
        }else{
            $('.tableDisplay').show();
        }
        this.off = false;
    },
    activate : function(){
        if(jQuery.support.opacity){
            $('.tableDisplay').fadeTo('normal',1.0);
        }
    },
    deactivate : function(){
        if(jQuery.support.opacity){
            $('.tableDisplay').fadeTo('normal', 0.5);
        }
    }
}


// Search Button Control
var searchButton = {
    init : function(){
        if(jQuery.support.opacity){ 
            $('.btnSearch').fadeTo('fast',0.5);
        }
        $('.btnSearch').removeClass('on');
        
        // Interrupts click if the button is not on
        $('.btnSearch').click(function(){
            if(!$(this).hasClass('on')){
                return false;
            }
        });
    },
    activate : function(){
        if(jQuery.support.opacity){
            $('.btnSearch').fadeTo('fast',1.0);
        }
        $('.btnSearch').addClass('on');
    },
    deactivate : function(){
        if(jQuery.support.opacity){
            $('.btnSearch').fadeTo('fast',0.5);
        }
        $('.btnSearch').removeClass('on');
    }
}


// Shortcut for activating and deactivating the search features
var search = {
    activate : function(){
        tableDisplay.activate();
        searchButton.activate();
    },
    deactivate : function(){
        tableDisplay.deactivate();
        searchButton.deactivate();
    }
}


// Welcome Pane Control
var welcomeScreen = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#welcomeClose').click(this.hide);
    },
    hide : function(){
        //if(jQuery.support.opacity){
            $('#welcomeTop').fadeOut('normal');
        //}else{
            $('#welcomeTop').addClass('off');
        //}
        
        var newTab = $('#tabs #providers a').attr('rel');
        tabControl.currentTab = newTab
                
        $('#tabs li a').removeClass('on');
        $('#tabs #providers a').addClass('on');

        tableDisplay.show();

        // Changes the panes
        if(jQuery.support.opacity){
            $('.paneItem.on').removeClass('on').slideUp('normal', function(){
                $('#div' + newTab).addClass('on').slideDown('normal');
            });
        }else{
            $('.paneItem.on').removeClass('on').css('display','none');
            $('#div' + newTab).addClass('on').css('display','block');
        }
    }
}


// Footer controller
var footerControl = {
    init : function(){
        this.run();
    },
    run : function(){
        $('#footer ul li ul').hover(
            function(){
                $(this).parent().addClass('on');
            },
            function(){
                $(this).parent().removeClass('on');            
            }
        );
    } 
}


// Tab controller
var tabControl = {

    currentTab : '',

    init : function(){
        $('.pane div.on').show();
        this.run();
    },
    run : function(){

        $('#tabs li a:not(#tabs.home li a)').addClass('disabled');

        $('#tabs li a').click(function(){
            if($(this).hasClass('disabled') == false){
                var newTab = $(this).attr('rel');
                tabControl.currentTab = newTab
                
                $('#tabs li a').removeClass('on');
                $(this).addClass('on');
                
                // Hides the welcome screen.  Shows the table display.
                if (tableDisplay.off){
                
                    if(jQuery.support.opacity){
                        $('#welcomeTop').fadeOut('normal');
                    }else{
                        $('#welcomeTop').addClass('off');
                    }
                    
                    tableDisplay.show();
                }                
                                                    
                // Changes the panes
                if(jQuery.support.opacity){
                    $('.paneItem.on').removeClass('on').slideUp('normal', function(){
                        $('#div' + newTab).addClass('on').slideDown('normal');
                    });
                }else{
                    $('.paneItem.on').removeClass('on').css('display','none');
                    $('#div' + newTab).addClass('on').css('display','block');
                }
                
                searchControl.validate();
                
                return false
            }
        });
    }
}


// Search Controller
var searchControl = {

    init : function() {
        this.run();
    },
    run : function() {
        $('#lCol .disabled .pane select').attr('disabled', true);
    },
    validate : function() {
    
        var currentPane = tabControl.currentTab;
    
        switch(true)
        {
            case currentPane == "Providers":
                this.providers();
                break;
            case currentPane == "Incentives":
                this.incentives();
                break;
            case currentPane == "Name":
                this.name();
                break;
            default:
        }
    },
    providers : function() {
    
        providersPanel.checkService();
        
        if(providersPanel.isValid()){
            search.activate();
        }else{
            search.deactivate();
        }
    },
    incentives : function() {
        if(incentivesPanel.isValid()){
            search.activate();
        }else{
            search.deactivate();
        }
    },
    name : function() {
        if(namePanel.isValid()){
            search.activate();
        }else{
            search.deactivate();
        } 
    }
}


// Providers panel controller
var providersPanel = {

    valid : false,
    
    init : function() {
        this.run();
    },
    run : function() {
    
        providersPanel.checkService();
                
        $('#divProviders select').change(function(){
        
            providersPanel.checkService();
            
            if(providersPanel.isValid()){
                search.activate();
            }else{
                search.deactivate();
            }
        });
    },
    checkService : function() {
        var providerIndex = $('#providerIncentives').attr('selectedIndex');
        
        
        if (providerIndex == 1){
            if(jQuery.support.opacity){
                $('div#divProviders div.on:not(#residentialClass)').slideUp('normal').removeClass('on');
                $('#residentialClass').slideDown('normal').addClass('on');
            }else{
                $('div#divProviders div.on').hide().removeClass('on');
                $('#residentialClass').show().addClass('on');
            }
            $('#countyLabel').text("I live in the following county:");        
        }else if (providerIndex == 2){
            if(jQuery.support.opacity){
                $('div#divProviders div.on:not(#businessClass)').slideUp('normal').removeClass('on');
                $('#businessClass').slideDown('normal').addClass('on');
            }else{
                $('div#divProviders div.on').hide().removeClass('on');
                $('#businessClass').show().addClass('on');
            }
            $('#countyLabel').text("My business is in the following county:");
        }else if (providerIndex == 3){
            if(jQuery.support.opacity){
                $('div#divProviders div.on:not(#renewableClass)').slideUp('normal').removeClass('on');
                $('#renewableClass').slideDown('normal').addClass('on');
            }else{
                $('div#divProviders div.on').hide().removeClass('on');
                $('#renewableClass').show().addClass('on');
            }  
            $('#countyLabel').text("My renewable energy project is located in the following county:");
        }else{
            if(jQuery.support.opacity){
                $('div#divProviders div.on').slideUp('normal').removeClass('on');
            }else{
                $('div#divProviders div.on').hide().removeClass('on');
            }
            $('#countyLabel').text("I live in the following county:");
        }
    }, 
    isValid : function() {
        var providerIndex = $('#providerIncentives').attr('selectedIndex');
        var countiesIndex = $('#providerCounties').attr('selectedIndex');
        var businessCategoriesIndex = $('#businessCategories').attr('selectedIndex');
        var residentialCategoriesIndex = $('#residentialCategories').attr('selectedIndex');
        var renewableCategoriesIndex = $('#renewableCategories').attr('selectedIndex');
        
        if (providerIndex != 0 && countiesIndex != 0){
            if (providerIndex == 1 && residentialCategoriesIndex != 0){
                this.valid = true;
            }else if (providerIndex == 2 && businessCategoriesIndex != 0){
                this.valid = true;
            }else if (providerIndex == 3 && renewableCategoriesIndex != 0){
                this.valid = true;
            }else{
                this.valid = false;
            }
        }else{
            this.valid = false;
        }
                
        return this.valid;
    }
}


// Checks to see if the incentives pane is valid
var incentivesPanel = {
    
    valid : false,
    
    init : function() {
        this.run();
    },
    run : function() {
    
        incentivesPanel.checkService();
    
        $('#divIncentives select').change(function(){
        
            incentivesPanel.checkService();
        
            if(incentivesPanel.isValid()){
                search.activate();
            }else{
                search.deactivate();
            }
        });
    },
    checkService : function(){
        var incentivesIndex = $('#incentiveIncentives').attr('selectedIndex');
        
        if (incentivesIndex == 1){
            $('#inctCountyLabel').text("I live in the following county:");        
        }else if (incentivesIndex == 2){
            $('#inctCountyLabel').text("My business is in the following county:");
        }else if (incentivesIndex == 3){
            $('#inctCountyLabel').text("My renewable energy project is located in the following county:");
        }else{
            $('#inctCountyLabel').text("I live in the following county:");
        }
    },
    isValid : function() {
        var incentiveIndex = $('#incentiveIncentives').attr('selectedIndex');
        var countiesIndex = $('#incentiveCounties').attr('selectedIndex')
        
        switch(true)
        {
            case incentiveIndex != 0 && countiesIndex != 0:
                this.valid = true;
                break;
            default:
                this.valid = false;
        }

        return this.valid;

    }
}


// Checks to see if the name pane is valid
var namePanel = {

    valid : false,
    
    init : function(){
        this.run();
    },
    run : function(){
        $('#divName input[type=text]').focus( function(){
            if($(this).val() == '- enter name here -'){
                $(this).val('');
                
                if(namePanel.isValid){
                    search.activate();
                }
            }
        });
        $('#divName input[type=text]').blur( function(){
            if($(this).val() == ''){
                $(this).val('- enter name here -');  
                search.deactivate();
            }
        });
    },
    isValid : function() {
        
        var inputValue = $('#divName input[type=text]').attr('value');
        
        switch(true)
        {
            case inputValue == '- enter name here -':
                this.valid = false;
                break;
            case inputValue == '':
                this.valid = false;
                break;
            default:
                this.valid = true;
        }
                
        return this.valid;
    }
    
}


// Utility Panel Controls
var utilityControl = {
    init : function(){
        this.run();
    },
    run : function(){
        $('ul.utilitySelect li input').click(function(){
            $('a.selectBtn').removeClass('off');
        });
    }
}


// controls the list
var listingControl = {
    init : function(){
        this.run();
    },
    run : function(){
        
        var listType = $('.backDrop').attr('id');

        $('#results .listDisplay ul li:first').addClass('first');
        
        $('#incentivesList ul li a.linkMore').click(function(){
            if($(this).parent().hasClass('on')){
                $(this).parent().removeClass('on')
            }else{
                $(this).parent().addClass('on')
            }
        });

        $('#results .listDisplay ul li').hover(
            function(){
                $(this).addClass('hovering');
            },
            function(){
                $(this).removeClass('hovering');
            }
        );

            $('#results .listDisplay ul li input[type=text]').focus(function() {
            if($(this).val() == 'enter zip code'){
                $(this).val('');
            }
        });
        $('#results .listDisplay ul li input[type=text]').blur(function() {
            if($(this).val() == ''){
                $(this).val('enter zip code');
            }
        });

//        $('#results .listDisplay ul li a.linkResults, #results .listDisplay ul li input.button').click(function() {
//            $('#results .listDisplay #resultsTable').removeClass('off');
//        });

//        $('#results #resultsTable a.linkResults').live('click', function() {
//            $('#results .listDisplay #resultsTable').addClass('off');
//            $('#results .listDisplay #resultsTable').html('');
//        });

//        $('#results #resultsTable a#closeTable').live('click', function() {
//            $('#results .listDisplay #resultsTable').addClass('off');
//            $('#results .listDisplay #resultsTable').html('');
//        });
        
    }
}


// Email Modal Controller
var emailControl = {
    init : function(){
        this.run();
    },
    run : function(){
        $('a#closeBtn, a.closeBtn').live("click", function(){
           $('div.email').html(''); 
        });
        
        $('div.email input#toName').live("click", function(){
            if($(this).val() == 'Recipient\'s First Name'){
                $(this).val('');
            }
        });
        $('div.email input#toName').live("keyup", function(){
            if($(this).val() == 'Recipient\'s First Name'){
                $(this).val('');
            }
        });
        
        $('div.email input#toEmail').live("click", function(){
            if($(this).val() == 'Recipient\'s Email'){
                $(this).val('');
            }
        });
        $('div.email input#toEmail').live("keyup", function(){
            if($(this).val() == 'Recipient\'s Email'){
                $(this).val('');
            }
        });
        
        $('div.email input#fromName').live("click", function(){
            if($(this).val() == 'Your First Name'){
                $(this).val('');
            }
        });
        $('div.email input#fromName').live("keyup", function(){
            if($(this).val() == 'Your First Name'){
                $(this).val('');
            }
        });
        
        $('div.email input#fromEmail').live("click", function(){
            if($(this).val() == 'Your Email'){
                $(this).val('');
            }
        });
        $('div.email input#fromEmail').live("keyup", function(){
            if($(this).val() == 'Your Email'){
                $(this).val('');
            }
        });
        
        $('div.email textarea#message').live("click", function(){
            if($(this).val() == 'Your Message:'){
                $(this).val('');
            }
        });
        $('div.email textarea#message').live("keyup", function(){
            if($(this).val() == 'Your Message:'){
                $(this).val('');
            }
        });
                
    }
}


// Word Counter
var wordCounter = {
    init : function(){
        this.run();
    },
    run : function(){
        $('div.email div.emailNest textarea#message').live("keyup", function(){
            var charCount = $(this).val().length;
            $('div.email div.emailNest p.wordCount span').html(charCount);
            if (charCount > 300){
                $('div.email div.emailNest p.wordCount').css('color', '#ff0000');
            }else{
                $('div.email div.emailNest p.wordCount').css('color', '#0095D3');
            }
        });
    }
}


// modal controller
var detailsModal = {
    init : function(){
        this.run();
    },
    run : function(){
    
        $('#divDetails #overlay a#close').live("click", function(){
            $('#divDetails').html('');
        });
    
        $('#overlay').live("mouseover", function(){
            
            var tabsArray = $('#details .detailsColumn #detailsTabs li');
            var windowWidth = $('.detailsContent').width()
            
            
            $('#details .detailsColumn #detailsTabs li').width((windowWidth / tabsArray.length)-2);
            
            if($('#details .detailsColumn #detailsTabs li').hasClass('on') == false){
                $('#details .detailsColumn #detailsTabs li:first').addClass('on');
            }
            if($('#details .detailsColumn div.detailsContent div.panel').hasClass('on') == false){
                $('#details .detailsColumn div.detailsContent div.panel:first').addClass('on');
            }
            
            $('#details .detailsColumn #detailsTabs li').click(function(){
            
                $('#details .detailsColumn div.detailsContent div.panel').removeClass('on');
                $('#details .detailsColumn #detailsTabs li').removeClass('on');
                $(this).addClass('on');
                
                var newPanel = $('#details .detailsColumn #detailsTabs li.on a').attr('rel');
                $('#details .detailsColumn div.detailsContent div.' + newPanel).addClass('on');
                
            }); 
        });
    }
}

// GA Shenanigans
var googleAnalytics = {
    init: function() {
        this.run();
    },
    run: function() {
        
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
        
        $('a').live("click", function() {
		    var href = $(this).attr('href');
		                        
            if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
                var extLink = href.replace(/^https?\:\/\//i, '');
				pageTracker._trackEvent('External', 'Click', extLink);
			} else if (href.match(/^mailto\:/i)){
				var mailLink = href.replace(/^mailto\:/i, '');
				pageTracker._trackEvent('Email', 'Click', mailLink);
			} else if (href.match(filetypes)){
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				var filePath = href.replace(/^https?\:\/\/(www.)accoheroes.com\.com\//i, '');
				pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
			} else {
			    pageTracker._trackEvent('Internal Link', 'Click', '', href);
			}
		});
    }
}



// Privacy Policy
var guidelines = {
    toggler     : Object,
    winHeight   : Object,
    init        : function(){
        //this.toggler = $('#legal ul li#privacy a');
		this.toggler = $('#searchParameters ul li#guidelines a');
        this.run();
    },
    run         : function(){    
        guidelines.toggler.click(function(){
            guidelines.winHeight = $(document).height();
            $('body').prepend('<div id="modal-overlay" style="height:' + guidelines.winHeight + 'px;"><div id="modal-container"><a href="#" class="close">Close</a><div class="holder"></div></div></div>');
            $('#modal-container .holder').load($(this).attr('href'), '', function(){
                $(this).jScrollPane({
                    'scrollbarWidth' : '17',
                    'scrollbarMargin' : '10',
                    'showArrows' : true,
                    'arrowSize' : '17'
                });
            });
            $('#modal-overlay').fadeIn('fast');
            return false;
        });
        
        $('#modal-container a.close').live('click', function(){
            $('#modal-overlay').fadeOut('fast', function(){
                $(this).remove();
            });
            return false;
        });
        
        $('#modal-overlay').live('click', function(e){
            if(e.target.id == 'modal-overlay'){
                $('#modal-overlay').fadeOut('fast', function(){
                    $(this).remove();
                });
            }

        });
        
    }
}






// 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($(this).attr('href'), '', function(){
                $(this).jScrollPane({
                    'scrollbarWidth' : '17',
                    'scrollbarMargin' : '10',
                    'showArrows' : true,
                    'arrowSize' : '17'
                });
            });
            $('#modal-overlay').fadeIn('fast');
            return false;
        });
        
        $('#modal-container a.close').live('click', function(){
            $('#modal-overlay').fadeOut('fast', function(){
                $(this).remove();
            });
            return false;
        });
        
        $('#modal-overlay').live('click', function(e){
            if(e.target.id == 'modal-overlay'){
                $('#modal-overlay').fadeOut('fast', function(){
                    $(this).remove();
                });
            }

        });
        
    }
}


// Provider Descriptions
var provider = {
    trigger : Object,
    init    : function(){
        this.trigger = $('.descTrigger');
        this.run();
    },
    run     : function(){
        this.trigger.hover(function(){
            $(this).next().css('display','block');
            $('#header').css('position','relative');
            $('#header').css('z-index','0');
        }, function(){
            $(this).next().css('display','none');
            $('#header').css('z-index','99999');
        });
    }
}


// Banners
var banners = {
    banners     : Object,
    init        : function(){
        this.banners = $('#bannerAds a, #bannerAds img:not(#bannerAds a img)');
        this.run();
    },
    run         : function(){
        if(this.banners.length > 1){        
            setInterval(function(){
                if(banners.banners.eq(1).is(':hidden')){
                    banners.banners.eq(1).fadeIn('normal')
                }else{
                    banners.banners.eq(1).fadeOut('normal')
                }
            },5000);
        }
    }
}

// Page Initialization
function initialize(){
    dropdownControl.init();
    welcomeScreen.init();
    tableDisplay.init();
    searchButton.init();
    tabControl.init();
    footerControl.init();
    utilityControl.init();
    searchControl.init();
    listingControl.init();
    providersPanel.init();
    incentivesPanel.init();
    namePanel.init();
    emailControl.init();
    wordCounter.init();
    detailsModal.init();
    googleAnalytics.init();
    privacy.init();
	guidelines.init();
    provider.init();
    banners.init();
}



// Start
$(document).ready(function(){
    initialize();
});
