/* Global JavaScripts */


// Page Load
$(document).ready(function() {
    // Fix the last menu item from having a right border
    $('#header ul li:last-child').addClass("last");

    // Run preload image function
    preloadImages();

    // Equalize Homepage column heights
    if ($("#graybarabout").length > 0) {
        equalHeight($("#graybarabout,#graybarnews,#graybarcareers"));
    }

    if ($("#lblImgCounter").length > 0) {
        iMax = $("#homeMain .rotating").size();
        $("#lblImgCounter").html(iCurrent + "/" + iMax);
    }

    // Cleanup Sitefinity Events and News empty p tags
    $(".sf_newsList p, .sf_singleNews p, .sf_eventsList p, .sf_singleEvent p").each(
      function() {
          var elem = $(this);
          if ($.trim(elem.html()).length == 0) {
              elem.remove();
          }
      }
    );
});

// Rollover functions
function RollOver(x) {
    x.setAttribute("src", x.getAttribute("rolloverimage"));
}

function RollBack(x) {
    x.setAttribute("src", x.getAttribute("shadedimage"));
}


/* Just add class="preload" to any images so that they'll be loaded in the background for quicker transitions */
function preloadImages() {
    if (!document.getElementById) return;
    var aPreLoad = new Array();
    var aImages = document.getElementsByTagName('img');
    for (var i = 0; i < aImages.length; i++) {
        if (aImages[i].className == 'preload') {
            var src = aImages[i].getAttribute('src');
            aPreLoad[i] = new Image();
            aPreLoad[i].src = src;
        }
    }
}


/* Set values for gallery auto-rotation */
var iCurrent = 1; // Sets the first gallery image
var iMax; // Sets the max gallery image

/* Function to switch promos and restart timer */
function ChangePromo(iOld, iNew) {
    // If the user clicks on a different item than the current item
    if (iOld != iNew) {

        $("#rotate" + iOld).fadeOut("slow");
        $("#rotate" + iNew).fadeIn("slow");

        iCurrent = iNew;

        if ($("#lblImgCounter").length > 0) $("#lblImgCounter").html(iCurrent + "/" + iMax);
    };
};
/* Gets ID of next promo for auto-rotate */
function getNextID() {
    iCurrent == iMax ? iCurrent = 1 : iCurrent++;
    return iCurrent;
}

/* Gets ID of prev promo for auto-rotate */
function getPrevID() {
    iCurrent == 1 ? iCurrent = iMax : iCurrent--;
    return iCurrent;
}





/* Function to equalize column heights */
function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
