//Variable Definitions
var productOut = 0;
var industOut = 0;
var timer = 0;
var animating = 0;

var listLocation = 3;


//jQuery.easing.def = "easeInOutExpo";

//Font Replacement
Cufon.replace('.heading');

/*------ jQuery Code ------*/

$(document).ready(function() {

    //Hide Menus
    $("#menu-products").hide();
    $("#menu-industries").hide();
    $("#response-products span").hide();
    $("#slide-menu-second ul").hide();
    $(".first-list").hide();
    $(".second-list").hide();
    $(".third-list").hide();
    $(".first-list").parent().find("ul").hide();
    $("#video-list-top ul").hide();
    

    /* ----- Sub Page Link Code ----- */
    
    $("#rolltek-link").hover(function() {
        $("#response-products span").slideDown("fast");
    }, function() {});
    
    /* ----- End Sub Page Link Code ----- */


    /* ----- Interactive Menu Code ----- */

    //Hover Intent for Products
    var config = {    
        over: makeProductsTall, // function = onMouseOver callback (REQUIRED)    
        timeout: 300, // number = milliseconds delay before onMouseOut    
        out: makeShort // function = onMouseOut callback (REQUIRED)    
    };
    
    $("#products").hoverIntent( config );
    $("#products").live("mouseenter", function() {
        $(this).css("backgroundPosition", "-58px 0");
        $("#industries").css("backgroundPosition", "0 0");
        $("#video").css("backgroundPosition", "0 0");
    });
    
    var config2 = {    
        over: makeIndustriesTall, // function = onMouseOver callback (REQUIRED)    
        timeout: 300, // number = milliseconds delay before onMouseOut    
        out: makeShort // function = onMouseOut callback (REQUIRED)    
    };

    $("#industries").hoverIntent( config2 );
    $("#industries").live("mouseenter", function() {
        $(this).css("backgroundPosition", "-58px 0");
        $("#products").css("backgroundPosition", "0 0");
        $("#video").css("backgroundPosition", "0 0");
    });
    
    //Mouse out of interactive menu
    $("#interactive-menu").live('mouseleave', function() {
        closeAll();
        $("#slide-video-menu").slideUp("fast", function() {
            $(".first-list").parent().find("ul").css("top", "0px");
            $(".first-list").parent().find("ul").hide();
            
            /*if (!$(".school-bus-vidlist").hasClass("first-list")) {
                $(".school-bus-vidlist").addClass("first-list");
            }
            if (!$(".emergency-response-vidlist").hasClass("second-list")) {
                $(".emergency-response-vidlist").addClass("second-list");
            }
            if (!$(".heavy-truck-vidlist").hasClass("third-list")) {
                $(".heavy-truck-vidlist").addClass("third-list");
            } */           
        });
    });
    
    /* ----- Interior Menu -----*/
    
    var rolloverConfig = {
        over: makeSubTall, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: makeShort // function = onMouseOut callback (REQUIRED)    
    };
    
    $(".rollover").hoverIntent( rolloverConfig );
        
    /* ----- End Interior Menu -----*/
    
    /* ----- End Interactive Menu ----- */
    
    
    
    /* ----- Video Menu ----- */
    
    var videoConfig = {
        over: makeVideoOut,
        timeout: 300,
        out: makeShort
    };
    
    $("#video").hoverIntent( videoConfig );
    $("#video").live("mouseenter", function() {
        $(this).css("backgroundPosition", "-58px 0");
        $("#industries").css("backgroundPosition", "0 0");
        $("#products").css("backgroundPosition", "0 0");
    });
    /* ----- End Video Menu ----- */
    
    
    externalLinks();
    
    
    
    /* ---------- Video Slider Controls ---------- */
    
    $("#video-list-nav span").live("click", function() {
        var listName = $(this).attr("id");
        var element = $("#video-list-container ."+listName)
        var listHeight = element.height();
        var listElements = element.children().length;
        var listTop = element.css("top");
        var anim = Math.round(listHeight/listElements*3);
        var elemParent = $(this).parent();
        listHeight = parseInt(listHeight.toString().replace("px", ""))*-1;
        listTop = parseInt(listTop.toString().replace("px", "")) - anim;
        
        if (listTop > listHeight) {        
            element.animate({ top: "-="+anim+"px"}, "fast");
        }
        else {
            element.animate({ top: "0px"}, "fast");
        }
    });
    
    /* ----- Next Button -----*/
    $("#video-list-nav #next").live("click", function() {
        var counter = 0;
        $(this).hasClass("test");
        listLocation = (listLocation > 6) ? 0 : listLocation;
        $(".third-list").fadeOut("fast", function() { $(this).removeClass('third-list') })
        $(".second-list").delay(200).fadeOut("fast", function() { $(this).removeClass('second-list'); });
        $(".first-list").delay(400).fadeOut("fast", function() {
            $(this).removeClass('first-list');
            $("#video-list-container ul").each(function (i) {
                var headingClasses = $(this).attr("class");
                var currentClass;
                if (i == listLocation && counter < 3) {
                    var elementId = $(this).attr("id");
                    switch(counter) {
                        case 0:
                            
                            $("#video-list-top ul."+elementId).fadeIn("fast").addClass("first-list");
                            $(this).addClass("first-list").fadeIn("fast");
                            currentClass = 'first-list';
                            break;
                        case 1:
                            $("#video-list-top ul."+elementId).delay(200).fadeIn("fast").addClass("second-list");
                            $(this).addClass("second-list").delay(200).fadeIn("fast");
                            currentClass = 'second-list';
                            break;
                        case 2:
                            $("#video-list-top ul."+elementId).delay(400).fadeIn("fast").addClass("third-list");
                            $(this).addClass("third-list").delay(400).fadeIn("fast");
                            currentClass = 'third-list';
                            break;
                    }
                    listLocation++;
                    counter++;
                    
                    if ($(this).children().length <= 3) {
                        $("#"+currentClass).css("cursor", "arrow");
                        $("#"+currentClass).attr("class", "off");
                    }
                    else {
                        $("#"+currentClass).css("cursor", "pointer");
                        $("#"+currentClass).attr("class", "on");
                    }
                }
            });
            
        });
    });
    
    $("#video-section #video-close").live("click", function() {
        jwplayer("video-container").setup({ 
            flashplayer: "/jwplayer/player.swf", 
            file: "", 
            height: 270, 
            width: 480 
        });
        SWFAddress.setValue('');
        $("#video-container").html();
        $("#video-backdrop").fadeOut("fast");
        $("#video-section").fadeOut("fast");
    });
    
    $(this).keyup(function(e) {
        if (e.which == 27 && $("#video-backdrop").css("display") != "none") {
            jwplayer("video-container").setup({ 
                flashplayer: "/jwplayer/player.swf", 
                file: "", 
                height: 270, 
                width: 480 
            });
            SWFAddress.setValue('');
            $("#video-container").html();
            $("#video-backdrop").fadeOut("fast");
            $("#video-section").fadeOut("fast");
        }
    });
    
    /* --------- End Video Controls ----------- */
    
    
    
    /* --------- Deep Linking for Video ---------- */
    $(".video").live("click", function() {
        SWFAddress.setValue(this.rel);
    });
    
    function handleChange(event) {
        if (event.path != "/") {
            var videoName = event.path;
            $("#video-section").show();
            $("#video-backdrop").fadeTo("fast", .9, function() {
                if (!iPad) {
                    jwplayer("video-container").setup({ 
                        flashplayer: "/jwplayer/player.swf", 
                        file: ".."+videoName+".flv", 
                        height: 270, 
                        width: 480
                    });
                }
                else {
                    $("#video-container").html('<video width="480" height="270" controls="controls" autoplay="true" autobuffer="true"><source src="'+videoName+'.m4v" type="video/mp4" /><source src="'+videoName+'.mp4" type="video/mp4" /> </video> </video>').css("left", "0px");
                }
            });
        }
        else {
            $("#video-container").html();
            $("#video-backdrop").fadeOut("fast");
            $("#video-section").fadeOut("fast");
        }
    }
    
    SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
    
    
    
    /* ---------- News Styling ---------- */
    
    $(".press li a").each(function(i) {
        (i % 2 == 1) ? $(this).addClass("odd") : $(this).addClass("");
    });
    
    
    
    
    /* ---------- Footer More Animation ---------- */
    $("#more-products").live("click", function() {
        $(".more-products").slideToggle("fast", function() { 
            ($(".more-products").css("display") != "none") ? $("#more-products").html("less") : $("#more-products").html("more");
        });
    });
    
    
});

function externalLinks() {   
    if (!document.getElementsByTagName) return;   
        var anchors = document.getElementsByTagName("a");   
        for (var i=0; i<anchors.length; i++) {   
        var anchor = anchors[i];   
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";   
    }   
}

function makeSubTall () {
    $("#slidemenu-container").stop(true).animate({ width: '380px'}, { queue: false, duration: "fast" });
    var subList = $(this).attr("title");
    $("#slide-menu-second ul.on").fadeOut("fast", function() {
        $(this).toggleClass("on");
        $("#"+subList).toggleClass("on");
        $("#slide-menu-second ul.on").fadeIn("fast");
        $(this).dequeue();
    });
    $("#slide-video-menu").slideUp("fast");
}

function makeProductsTall () {
    $("#slidemenu-container").stop(true).animate({ width: '254px'}, { queue: false, duration: "fast" });
    $("#menu-industries").fadeOut("fast");
    $("#menu-industries").queue(function() {
        $("#menu-products").fadeIn("fast");
        $(this).dequeue();
    });
    $("#slide-video-menu").slideUp("fast");
    
}

function makeIndustriesTall() {
    $("#menu-products").fadeOut("fast");
    $("#slidemenu-container").stop(true).animate({ width: '254px'}, { queue: false, duration: "fast" });
    $("#menu-products").queue(function() {
        $("#menu-industries").fadeIn("fast");
        $(this).dequeue();
    });
    $("#slide-video-menu").slideUp("fast");
    
}

function makeVideoOut() {
    $("#slide-video-menu").slideDown("fast", function() {
        $(".first-list").fadeIn("fast");
        $(".second-list").delay(200).fadeIn("fast");
        $(".third-list").delay(400).fadeIn("fast");
    });
    closeAll();
    $("#video").css("backgroundPosition", "-58px 0");
}

function makeShort() { }

function closeAll() {
    $("#products").css("backgroundPosition", "0 0");
    $("#industries").css("backgroundPosition", "0 0");
    $("#video").css("backgroundPosition", "0 0");

    $("#slide-menu-first ul").stop().fadeOut("fast", function() {
        $("#slidemenu-container").stop().animate({ width: '58px'}, "fast");
    });
}

