var currentMenuID = 0;
var gTimeOut = null;

$(document).ready(function () {

    $('#HeaderImages').cycle({
        fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });

    $('#Articles').cycle({
        fx: 'scrollUp',
        pager: '#ArticleNav',
        timeout: 9000,
        speed: 2000
    });

    $('.NewsBlurb').click(function () {
        $('#Articles').cycle("pause");
    });

});


function delay(id,callback) {
  if (currentMenuID) hideMenu(currentMenuID);
  var timeout = null;
  timeout = setTimeout(callback, 300);
  elm('Tab' + id).onmouseout = function () {
    // Clear any timers set to timeout
    clearTimeout(timeout);
    hideMenu(id);
  }
}

function displayMenu(id) {
  
  elm('SubMenu' + id).style.display = "block";
  var x = getLocation(elm('Tab' + id), "X") - 3;
  if (id == 11) x -= 90;
  if (id == 12) x -= 130;
  elm('SubMenu' + id).style.left = x + "px";
  elm('SubMenu' + id).style.top = getLocation(elm('Tab' + id), "Y") + 33 + "px";
  if (!$('#Tab' + id).hasClass("Selected")) $('#Tab' + id).addClass("Hovered");
  currentMenuID = id;
}

function hideMenu(id) {
  gTimeOut = window.setTimeout("forceHideMenu(" + id + ")", 300);
}

function forceHideMenu(id) {
    elm('SubMenu' + id).style.display = "none";
    $('#Tab' + id).removeClass("Hovered");
}

function cancelHideMenu(id) {
    if (gTimeOut) window.clearTimeout(gTimeOut);
}

function displayInfo(id, event) {
    var srcElement = event.target || event.srcElement;
    $('div.Logo').removeClass("Selected");
    $(srcElement).parents('div').addClass("Selected");
    $('div.CompanyBlurb').hide();
    $('#' + id).show();
    //$(window).scrollTop($($(srcElement).parents('div')[0]).position().top);

    var to = setInterval(function () {
        var top = $(window).scrollTop();
        var dest = $('#' + id).position().top;
        var minTop = dest - $(window).height() + $('#' + id).height() + 60;
        if (top < minTop) {
            $(window).scrollTop(top + 10);
        }
        else window.clearInterval(to);
    }, 10);

}

