var fitchTraining = new Object;
fitchTraining.courseCount = 0;
fitchTraining.courseOffset = 0;
fitchTraining.courseTickerDelay = 3000;
//by default run the course ticker automtically
fitchTraining.courseTickerAuto = true;
fitchTraining.timeoutID = null;

$(document).ready(

    function ()
    {
    
        fitchTraining.courseCount = 0;
        fitchTraining.courseOffset = 0;
        if(fitchTraining.timeoutID != null)
        {
            clearTimeout(fitchTraining.timeoutID);
            fitchTraining.timeoutID = null;
        }


      /*********Main Menu**********/
        /*left hand main menu activation code*/
        $("ul.mainMenu > li").mouseenter(function ()
        {
            /*change menu item style to show selection*/
            $(this).find("a:first").addClass("mainMenuItemSel");
            /*if the menu does not have a sub menu render it so that the selection
            highlights the whole column width*/
            if ($(this).hasClass("mainMenuNoSub"))
            {
                $(this).find("a:first").addClass("mainMenuNoSubSel");
            }
            /*style the popup sub menu with alternating styles*/
            $("div.subMenu li:odd").addClass("backgroundOdd");
            $("div.subMenu li:even").addClass("backgroundEven");
            /*show the popup*/
            $(this).find("div.subMenu").css("display", "block");
        });
        
        /*left hand main menu deactivation code*/
        $("ul.mainMenu > li").mouseleave(function ()
        {
            /*remove the selection style*/
            $(this).find("a:first").removeClass("mainMenuItemSel");
            /*remove the selection style from main menu item without sub menu*/
            if ($(this).hasClass("mainMenuNoSub"))
            {
                $(this).find("a:first").removeClass("mainMenuNoSubSel");
            }
            /*hide the popup*/
            $(this).find("div.subMenu").css("display", "none");
        });

        /*support clicking anywhere within the sub menu list item area to load
        linked anchor hyperlink*/        
        $("ul.subMenuItem li,div.subMenu ul li").click(function ()
        {
            var link = $(this).children("a").attr("href");
            if(link != undefined && link != ''){document.location = link;}
        });
        $("ul.subMenuItem li > a,div.subMenu ul li > a").click(function (event)
        {
            event.stopPropagation();
        });        

      /*********Sub Menu**********/
        /*highlight submenu items when hovered over*/
        $("li.mainMenu li").mouseenter(function ()
        {
            $(this).addClass("subMenuSelected");
            $(this).find("a:first").addClass("subMenuAnchorSelected");
        });
        /*remove highlight away from submenu items without mouse hover*/
        $("li.mainMenu li").mouseleave(function ()
        {
            $(this).removeClass("subMenuSelected");
            $(this).find("a:first").removeClass("subMenuAnchorSelected");
        });

        /*inject a sub menu pointer image for each submenu hyperlink*/
        $("ul.subMenuItem li").prepend("<span class='subMenuLink'>&gt;&nbsp;</span>");

        
      /*********Main Page Header Menu**********/      
        $("#headerContentMenuBox li.headerMenuItem:not(.menuItemTwoLine)").append("<span class='oneRowPointer'/>");
        $("#headerContentMenuBox li.headerMenuItem.menuItemTwoLine").append("<span class='twoRowPointer'/>");

        /*Style menu links with alternative styles*/
        $("#headerContentMenuBox li:odd").addClass("headerMenuBackgroundOdd");
        $("#headerContentMenuBox li:even").addClass("headerMenuBackgroundEven");
        /*highlight menu items and change main menu display section when mouse over occurs*/

        $("#headerContentMenuBox li").hover(function(){headerMenuOn($(this));}, function(){headerMenuOff($(this));});

        function headerMenuOn(headerMenu)
        {
            var menu = headerMenu.attr("data-menu");
            //highlight menu item
            headerMenu.addClass("mainPageMenuSel");
            headerMenu.find("span").css("display","block");
            //fade in menu slide only if animations running are complete and that the current
            //slide is not already showing

            //make sure all menus are now hidden
            $("div[class*='style'][data-menu]").css("display","none");
            //load related menu section
            $("div[class*='style'][data-menu='" + menu + "']").show();
        };
               
        function headerMenuOff(headerMenu)
        {
            headerMenu.removeClass("mainPageMenuSel");
            headerMenu.find("span").css("display","none");
        };
        
        
        if($("#TickerBox").length == 1)
        {
            initialiseCourseTicker();
        }
        $("a.tickerMore").click(function(){nextPageClick();});
        $("a.tickerPrev").click(function(){previousPageClick();});
        
        $("div.divDatesLocsPricesRight span").click(function(){courseListing();});
        /*support an ajax click of course to be selected*/        
        $("span.imgAddToBasket.stayOpen a").click(function (event)
        {
            event.preventDefault();         
            var link = $(this).attr("href");
            $.ajax({url:link, 
                    success:function(){alert('Course Added');}
                   });            
        });
    }
);

/*display course listing dialog*/
function courseListing()
{
    $(".courseContentDialog").modal(
        {closeClass:'closeDialog',
         overlayId:'popupOverlay',
         opacity:40}
     );
    
}

/*
  initialise ticker.  
  take note of the number of courses loaded
  style even rows with alt background to give striped appearance
  display first three courses (if available)
*/
function initialiseCourseTicker()
{
    fitchTraining.courseCount = $("div.courseRow").length;
    fitchTraining.courseOffset = 0;
    $("div.courseRow").css("display","none");
    $("div.courseRow:even").addClass("courseNewsBackgroundEven");
    $("div.courseRow:lt(3)").css("display","block");
    setNavButtons();
    async(function(){nextPage();});
}

function nextPageClick()
{
    clearTimeout(fitchTraining.timeoutID);
    fitchTraining.timeoutID = null;
    fitchTraining.courseTickerAuto = false;
    nextPage();
}

function previousPageClick()
{
    clearTimeout(fitchTraining.timeoutID);
    fitchTraining.timeoutID = null;
    fitchTraining.courseTickerAuto = false;
    previousPage();
}




function nextPage()
{
    if(fitchTraining.courseOffset + 3 < fitchTraining.courseCount)
    {
        $("div.courseRow").eq(fitchTraining.courseOffset).slideToggle();
        try
        {
            fitchTraining.courseOffset = fitchTraining.courseOffset + 1;
        }
        catch(e)
        {
            fitchTraining.courseOffset = 1;
        }
        $("div.courseRow").eq(fitchTraining.courseOffset+2).slideToggle(
          function(){async(function(){
            if(fitchTraining.courseTickerAuto == true)
            {
              if(fitchTraining.courseOffset + 3 >= fitchTraining.courseCount)
              {
                /*reset the ticker if we reach the end of the courses*/
                initialiseCourseTicker();
              }
              else
              {
                nextPage();
              }
            }        
          }, fitchTraining.courseTickerDelay);
        });
        
        setNavButtons();
    }
    
}

function previousPage()
{
    if(fitchTraining.courseOffset > 0)
    {
        $("div.courseRow").eq(fitchTraining.courseOffset+2).slideToggle();    
        try
        {
            fitchTraining.courseOffset = fitchTraining.courseOffset - 1;
        }
        catch(e)
        {
            fitchTraining.courseOffset = 1;
        }
        $("div.courseRow").eq(fitchTraining.courseOffset).slideToggle();    
        setNavButtons();          
    }
}


function setNavButtons()
{
  if(fitchTraining.courseOffset == 0)
  {
    $(".tickerPrev").removeClass("buttonEnabled");
    $(".tickerPrev").removeClass("buttonEnabledHover");
    $("a.tickerPrev").unbind('mouseenter mouseleave');
  }
  else
  {
    $(".tickerPrev").addClass("buttonEnabled");
    $("a.tickerPrev").hover(function() {$(this).addClass("buttonEnabledHover");}, 
      function() {$(this).removeClass("buttonEnabledHover");});
  }
  
  if(fitchTraining.courseCount > fitchTraining.courseOffset + 3)
  {
    $(".tickerMore").addClass("buttonEnabled");
    $("a.tickerMore").hover(function() {$(this).addClass("buttonEnabledHover");}, 
      function() {$(this).removeClass("buttonEnabledHover");});
  }
  else
  {
    $(".tickerMore").removeClass("buttonEnabled");
    $(".tickerMore").removeClass("buttonEnabledHover");
    $("a.tickerMore").unbind('mouseenter mouseleave');
  }
}

function async(fn)
{
    fitchTraining.timeoutID = setTimeout(fn, fitchTraining.courseTickerDelay);
}
