<!--
var customers = '';
var customersCurrent = 0;
var customersInt = '';
var customersTime = 10000;
var customersPaused = false;
var animInProg = false;
var player;
var states;
var bar;

function locationsFunction() {
    if($("#locationsContainer").attr("data-loaded") == "false") {
        $("#bkgdLocations").animate({"bottom": "0", "left": "140px"}, 1500);
        
        //Locations pop-up window for returned results - search by state
        $("area").click(function () {
            $("#locationsContainer .locationsSearch").fadeOut(1000);
            var searchValue = $(this).attr("data-state");
            var locationsSelected = "locationsWindow_state.html?stateSearch=" + searchValue + " .locationsWindowContent";
            $("#locationsContainer .locationsWindow").load(locationsSelected, function() {
                locationsLoad(searchValue);
            });
            
            googleEvent('Locations', 'Map', searchValue);
            return false;
        });
        
        //Clear zip code field and error alert when input gets focus
        $("#searchZipForm p input#zip_code").focus(function() {
            $(this).val("");
            $("#formAlert").empty();
        });
        
        //Locations pop-up window for returned results - search by zip code
        $("#searchZipForm").submit(function() {
            var searchValue = $(this).children("p").children("input#zip_code").val();
            
            if (validateField(searchValue, "zip")) {
                alert("true");
                googleEvent('Locations', 'Zip Code', searchValue);
            } else {
                $("#formAlert").text("Please enter a 5-digit zip code.");
            }
            
            return false;
        });
        
        $("#locationsContainer").attr("data-loaded", "true")
    }
}

function locationsLoad(search) {
    $(this + ".locationsWindowHeader h3 span").html(stateFull(search));
    
    //TEMPORARY FOR OREGON STORES
    if (search == "OR") {
        $(".address").prepend("<p><b>COMING FEBRUARY 2012</b></p>");
    }
    
    $("#locationsContainer .locationsWindow .locationsWindowHeader").show();
    $("#locationsContainer .locationsWindow #close").click(function() {
        $("#locationsContainer .locationsSearch").fadeIn(1000);
        $("#locationsContainer .locationsWindow").fadeOut(1000, function() {
            $(this).empty();
        });
        return false;
    });
    
    //Future Update
    //$("#locationsContainer .locationsWindow .binStatus").show();
    //$("#locationsContainer .locationsWindow .binMeterValue").each(function() {
    //    var val = $(this).data("value");
    //    var valMeter = 10 + 90*((5-val)/5);
    //    if (val < 1) {
    //        var valColor = "#333";
    //    } else if (val >= 1 && val < 2) {
    //        var valColor = "#C77";
    //    } else if (val >= 2 && val < 3) {
    //        var valColor = "#CC7";
    //    } else if (val >= 3) {
    //        var valColor = "#7C7";
    //    }
    //    $(this).css("width", valMeter+"%").css("background", valColor);
    //});
    
    $("#locationsContainer .locationsWindow").fadeIn(1000, function() {
        var myOptions = {
            zoom: 14,
            mapTypeControl: false,
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.SMALL
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById('locationsMapCanvas'), myOptions);
        google.maps.event.addListener(map, 'click', function() {
            infowindow.close();
        });
        var bounds = new google.maps.LatLngBounds();
        var infowindow = new google.maps.InfoWindow();
        var mapLocation = [0];
        var mapMarker = [0];
        $(".address").each(function(i) {
            mapLocation[i] = new google.maps.LatLng($(this).children('.name').attr('data-lat'), $(this).children('.name').attr('data-lng'));
            bounds.extend(mapLocation[i]);
            map.setCenter(bounds.getCenter());
            if($(".address").length > 1) {
                map.fitBounds(bounds);
            }
            mapMarker[i] = new google.maps.Marker({
                position: mapLocation[i],
                map: map,
                icon: "images/mapicon.png",
                shadow: "images/mapicon_shadow.png"
            });
            var addressName = $(this).children('.name').html();
            var addressNumber = $(this).children('.number').html();
            var addressCity = $(this).children('.city').html();
            var addressDirections = $(this).children('.details').children('.directions').html();
            google.maps.event.addListener(mapMarker[i], 'click', function() {
                infowindow.setContent(addressName + "<br />" + addressNumber + "<br />" + addressCity + "<br />" + addressDirections);
                infowindow.open(map,mapMarker[i]);
            });
            $(this).hover(function() {
                mapMarker[i].setAnimation(google.maps.Animation.BOUNCE);
            }, function() {
                mapMarker[i].setAnimation(null);
            });
            $(this).click(function() {
                infowindow.setContent(addressName + "<br />" + addressNumber + "<br />" + addressCity + "<br />" + addressDirections);
                infowindow.open(map,mapMarker[i]);
            });
        });
    });
}

function howFunction() {
    
    if($("#howContainer").attr("data-loaded") == "false") {
        var step = $(".howContent .howStep");
        var stepCurrent = 0;
        googleEvent('How', 'Viewed', 'step 1');
            
        $(".howContent .howNav input").click(function() {
            $(this).siblings().css("visibility", "visible");
            $(step[stepCurrent]).hide();
            switch($(this).attr("id")) {
                case "howNext":
                    stepCurrent++;
                    if (stepCurrent == step.length - 1) {
                        $(this).css("visibility", "hidden");
                    }
                break;
                case "howPrev":
                    stepCurrent = stepCurrent - 1;
                    if (stepCurrent == 0) {
                        $(this).css("visibility", "hidden");
                    }
                break;
            }
            
            var margin = stepCurrent * (-300);
            $(step[stepCurrent]).show();
            $(".howContent #screenHow").css("margin-left", margin);

            googleEvent('How', 'Viewed', 'step ' + (stepCurrent+1));            
            return false;
        });
        
        $("#howContainer").attr("data-loaded", "true")
    }
}

function sizesFunction() {
    
    if($("#sizesContainer").attr("data-loaded") == "false") {
        $(".sizesContent li").click(function() {
            var sizeChoice = this;
            var margin = $(this).attr("data-order") * (-301);
            var sizesState = $(this).children("p").css("display");
            var bkgdSizes = $("#bkgdSizes #sizes_sprite img");
            
            if (sizesState == "none") {
                $(sizeChoice).siblings().css("list-style-image", "url(../images/list_closed.jpg)").children("p").hide();
                $(sizeChoice).css("list-style-image", "url(../images/list_open.jpg)").children("p").show();
                bkgdSizes.hide().css("margin-left", margin).show();
            }
            
            googleEvent('Sizes', 'Viewed', $(sizeChoice).attr("id"));
        });

        $("#sizesContainer").attr("data-loaded", "true")
    }
}

function customersFunction () {
    customersInt = window.setInterval("customersSwap(-1)", customersTime);
    
    if($("#customersContainer").attr("data-loaded") == "false") {
        customers = $('.customersContent .customersDescription .profile');
    
        //Populate nav progress bar
        for (i=customers.length; i>0; i--) {
            $('<a href="#" class="customersProgress" id="customersNav'+(i-1)+'"></a>').insertAfter(".customersPrev");
            $('#customersNav0').css("background-position", "-84px");
        }
        
        $(".customersContent .customersNavigation").show();
            
        googleEvent('Customers', 'Viewed', 'Customer 1');
    
        //Activate customers animations and navigation controls
        $(".customersNavigation a").click(function() {
            window.clearInterval(customersInt);
            var clicked = $(this).attr("class");
            if (animInProg == false) {
                switch (clicked) {
                    case "customersPause":
                        $(this).hide();
                        $("#resume").css("display", "inline-block");
                        customersPaused = true;
                    break;
                    case "customersResume":
                        customersPaused = false;
                        $(this).hide();
                        $("#pause").show();
                    break;
                    case "customersNext":
                        customersSwap(-1);
                    break;
                    case "customersPrev":
                        if (customersCurrent > 0) {
                            var customersNext = customersCurrent - 1;
                        } else {
                            customersNext = customers.length - 1;
                        }
                        customersSwap(customersNext);
                    break;
                    case "customersProgress":
                        var customersNext = $(this).attr("id");
                        customersNext = customersNext[customersNext.length - 1];
                        customersSwap(customersNext);
                    break;
                }
            }
            if (customersPaused == false) {
                customersInt = window.setInterval("customersSwap(-1)", customersTime);
            }
            return false;
        });
        
        $("#customersContainer").attr("data-loaded", "true")
    }
}

//Called function to swap to next or chosen customers
function customersSwap(next) {
    animInProg = true;
    $("#bkgdCustomers").children().fadeOut(function() {
        $(customers[customersCurrent]).hide();
        $("#customersNav" + customersCurrent).css("background-position", "-105px");
        if (next >= 0) {
            customersCurrent = next;
        } else {
            customersCurrent++;
            if(customersCurrent == customers.length) {
                customersCurrent = 0;
            }
        }
        $("#customersNav" + customersCurrent).css("background-position", "-84px");
        $(customers[customersCurrent]).show();
        $("#bkgdCustomers").children().css("margin-left", customersCurrent*$("#bkgdCustomers").width()*(-1)).fadeIn(function() {
            animInProg = false;
        });
    });
            
    googleEvent('Customers', 'Viewed', 'Customer ' + (customersCurrent+1));
}

function mediaFunction() {
    if ($("#mediaContainer").attr("data-loaded") == "false") {
        var tag = document.createElement('script');
        tag.src = "http://www.youtube.com/player_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
        
        $(".mediaContent .mediaThumb").click(function () {
            tssplayer.loadVideoById($(this).attr("data-videoID"));
        });
        
        $("#mediaContainer").attr("data-loaded", "true");
    }
}

//Initialize YouTube Player
function onYouTubePlayerAPIReady() {
    tssplayer = new YT.Player('player', {
        height: '558',
        width: '600',
        videoId: $(".mediaContent .mediaThumb:first").attr("data-videoID"),
        playerVars: {
            'enablejsapi': 1,
            'rel': 0,
            'hd': 1,
            'color': 'white',
            'theme': 'light',
        },
        events: {
            'onStateChange': onPlayerStateChange
        }
    });
}

function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
        var playerID = ($("iframe").attr("src")).split('?');
        playerID = playerID[0].split('/');
        playerID = playerID[playerID.length - 1];
        
        googleEvent('Media', 'Viewed', $(".mediaThumb[data-videoID = '"+playerID+"']").attr("id"));
    }
}

function valuesFunction() {    
    if ($("#valuesContainer").attr("data-loaded") == "false") {
        states = $(".state");
        bar = $(".bar");
        googleEvent('Values', 'Viewed', 'capital');
        
        capitalMapAnim();
        $(".valuesContent #capital").css("color", "#07C").css("font-weight", "bold");
        $(".valuesNav a").click(function() {
            var selected = $(this).attr("id");
            
            $(this).css("color", "#07C").css("font-weight", "bold").parent().siblings().children().css("color", "").css("font-weight", "");
            $(".valuesContent  #" + selected + "Value").show().siblings().not(".valuesNav").hide();
            switch (selected) {
                case "capital":
                    capitalMapAnim();
                break;
                case "customers":
                    $(".valuesContent #customersValue .chart").hide().fadeIn(3000);
                break;
                case "environment":
                    var barWidth = Math.floor(($(".graph").width()/bar.length) - 10);
                    for (i=0; i<bar.length; i++) {
                        $(bar[i]).height("0").parent().css("left", i*(barWidth+10)).css("width", barWidth);
                        $(bar[i]).animate({"height": $(bar[i]).attr("amount")*2}, 3000);
                    }
                break;
            }
            
            googleEvent('Values', 'Viewed', selected);
            return false;
        });
        
        $("#valuesContainer").attr("data-loaded", "true");
    }
}

function capitalMapAnim() {
    $(".map").html("");
    for (x=0; x<states.length; x++) {
        for (i=0; i<$(states[x]).attr("amount"); i++) {
            $(states[x]).clone().appendTo('.map').attr("alt", "").animate({"margin-top": -(i+1)}, 2000);
        }
    }
}

function detailsFunction() {
    if ($("#detailsContainer").attr("data-loaded") == "false") {
        $(".detailsContent .faqsList #faqsListFirst").css("list-style-image", "url(images/list_open.jpg)").children("p").show();
        $("#faqs").css("color", "#07C").css("font-weight", "bold");
        googleEvent('Details', 'Viewed', 'faqs');
        
        $(".detailsContent .faqsList li").click(function() {
            var faqState = $(this).children("p").css("display");
            $(".detailsContent .faqsList li").css("list-style-image", "url(images/list_closed.jpg)").children("p").hide();
            if (faqState == "none") {
                $(this).css("list-style-image", "url(images/list_open.jpg)").children().show();
            }
        });

        $(".detailsNav a").click(function() {
            var selected = $(this).attr("id");
            $(this).css("color", "#07C").css("font-weight", "bold").parent().siblings().children().css("color", "").css("font-weight", "");
            $(".detailsContent  #" + selected + "Detail").show().siblings().not(".detailsNav").hide();
            
            googleEvent('Details', 'Viewed', selected);
            return false;
        });
        
        $("#detailsContainer").attr("data-loaded", "true");
    }
}

function contactFunction() {
    
    if ($("#contactContainer").attr("data-loaded") == "false") {
        
        $('#contactForm #contact_type').change(function() {
            if ($(this).attr('value') == "tss_customer") {
                $("#business").fadeOut();
            } else {
                $("#business").fadeIn();
            }
        });
        
        
        
        $('#contactForm').validate({
            rules: {
                name_first: "required",
                name_last: "required",
                email: {
                    required: true,
                    email: true
                },
                company: {
                    required: {
                        depends: function(a) {
                            return $("#business").css("display") != "none";
                        }
                    }
                },
                title: {
                    required: {
                        depends: function(b) {
                            return $("#business").css("display") != "none";
                        }
                    }
                },
                phone: {
                    required: {
                        depends: function(c) {
                            return $("#business").css("display") != "none";
                        }
                    },
                    minlength: 10,
                    digits: true
                },
                zip_code: {
                    required: true,
                    minlength: 5,
                    digits: true
                },
                question: "required"
            },
            messages: {
                phone: {
                    minlength: "Please enter a 10 digit phone number, numbers only.",
                    digits: "Please enter a 10 digit phone number, numbers only."
                }
            },
            submitHandler: function(form) {
                var d = new Date();
                var dateAdded = d + " | " + $("#question").val();
                $("#question").val(dateAdded);
                $(form).ajaxSubmit(function () {
                    googleEvent('Contact', 'Sent Form', $("#contactForm #contact_type").val());
                    $("#contactFormBox").empty().hide().html("<div id=\"contactResponse\"><h2>Thank you for contacting us.</h2><p>We will respond to your message promptly.</p></div>").fadeIn();
                    $("#contactContainer").attr("data-loaded", "false");
                });
                
                return false;
            }
        });

        return false;
        
        $("#contactContainer").attr("data-loaded", "true");
    }
}

function stateFull(abbr) {
    switch(abbr) {
        case "AL": return "Alabama";
        case "AK": return "Alaska";
        case "AZ": return "Arizona";
        case "AR": return "Arkansas";
        case "CA": return "California";
        case "CO": return "Colorado";
        case "CT": return "Connecticut";D
        case "DE": return "Delaware";
        case "DC": return "District of Columbia";
        case "FL": return "Florida";
        case "GA": return "Georgia";
        case "HI": return "Hawaii";
        case "ID": return "Idaho";
        case "IL": return "Illinois";
        case "IN": return "Indiana";
        case "IA": return "Iowa";
        case "KS": return "Kansas";
        case "KY": return "Kentucky";
        case "LA": return "Louisiana";
        case "ME": return "Maine";
        case "MD": return "Maryland";
        case "MA": return "Massachusetts";
        case "MI": return "Michigan";
        case "MN": return "Minnesota";
        case "MS": return "Mississippi";
        case "MO": return "Missouri";
        case "MT": return "Montana";
        case "NE": return "Nebraska";
        case "NV": return "Nevada";
        case "NH": return "New Hampshire";
        case "NJ": return "New Jersey";
        case "NM": return "New Mexico";
        case "NY": return "New York";
        case "NC": return "North Carolina";
        case "ND": return "North Dakota";
        case "OH": return "Ohio";
        case "OK": return "Oklahoma";
        case "OR": return "Oregon";
        case "PA": return "Pennsylvania";
        case "RI": return "Rhode Island";
        case "SC": return "South Carolina";
        case "SD": return "South Dakota";
        case "TN": return "Tennessee";
        case "TX": return "Texas";
        case "UT": return "Utah";
        case "VT": return "Vermont";
        case "VA": return "Virginia";
        case "WA": return "Washington";
        case "WV": return "West Virginia";
        case "WI": return "Wisconsin";
        case "WY": return "Wyoming";
    }

    return abbr;
}

function validateField(value, kind) {
    var testExp;
    if (kind == "zip") { testExp = /^\d{5}$/; }
    
    if (testExp.test(value)) { return true; } else { return false; }
}

function activateInternalLinks(page) {
    var pageLinks = page + " .navLink";
    
    $(pageLinks).click(function() {
        var target = ".nav #" + $(this).attr("data-target");
        $(target).click();
        
        return false;
    });
}

function getQueryVar(a) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == a) {
            return pair[1];
        }
    };
    
    return false;
}

function googleEvent(category, action, label) {
    _gaq.push(['_trackEvent', category, action, label]);
}
-->
