$(document).ready(function() {

//    ////////////////////////////
//    // Welcome Image Rotation //
//    ////////////////////////////



    //////////////////////////
    // NEWS PANEL           //
    //////////////////////////
    // if we have all two news articles or more show our panel
    
    if($('div#newspanel-content .newsitem1 h3').text().length > 0 && ($('div#newspanel-content .newsitem2 h3').text().length > 0
      || $('div#newspanel-content .newsitem3 h3').text().length > 0)){
    // hide all news panels
    $('div#newspanel-content div.news-active').hide();

    // get the story headlines
    var storyHeadline1 = $('div#newspanel-content .newsitem1 h3').text();
    var storyHeadline2 = $('div#newspanel-content .newsitem2 h3').text();
    var storyHeadline3 = $('div#newspanel-content .newsitem3 h3').text();

    // get the anchor location so we can show the correct link for the sub nav
    var storyAnchor1 = $('div#newspanel-content .newsitem1 h3 a').attr("href");
    var storyAnchor2 = $('div#newspanel-content .newsitem2 h3 a').attr("href");
    var storyAnchor3 = $('div#newspanel-content .newsitem3 h3 a').attr("href");

    // add news navigation
    // insert after the h2 'News Headlines'
    var strNewsPanel = '\r\n'
    + '<div class="newsheadline-navigation">\r\n'
    + '<ul id="news-links">\r\n';

    if(storyHeadline1.length > 0)
      strNewsPanel += '<li class="story1 news-link-active"><h3><a href="' + storyAnchor1 + '" title="' + storyHeadline1 + '">' + storyHeadline1 + '</a></h3></li>\r\n';
    if(storyHeadline2.length > 0)
      strNewsPanel += '<li class="story2 news-link-active"><h3><a href="' + storyAnchor2 + '" title="' + storyHeadline2 + '">' + storyHeadline2 + '</a></h3></li>\r\n';
    if(storyHeadline3.length > 0)
      strNewsPanel += '<li id="last" class="story3 news-link-active"><h3><a href="' + storyAnchor3 + '" title="' + storyHeadline3 + '">' + storyHeadline3 + '</a></h3></li>\r\n';
    
    strNewsPanel += '</ul>\r\n'
                  + '</div>\r\n';
    
    $('div#newspanel div#newspanel-content h2').after(strNewsPanel);

    // show by default item 1
    $('div#newspanel-content .newsitem1').show();
    //$('div#newspanel-content div.newsheadline-navigation ul li.story1 h3 a').css('background-color', '#97b3c5');
    $('div#newspanel-content div.newsheadline-navigation ul li.story1 h3 a').addClass('newshoverstate');
    }

    function ResetNewsItems() {
        // hide all news items
        $('div#newspanel-content div.news-active').hide();
        // reset nav colours
        $('ul#news-links li.story1 h3 a').removeClass("newshoverstate");
        $('ul#news-links li.story2 h3 a').removeClass("newshoverstate");
        $('ul#news-links li.story3 h3 a').removeClass("newshoverstate");
    }

    // Add the hover functionality to the headlines
    // Headline 1 Hover event
    $('div#newspanel-content div.newsheadline-navigation ul li.story1 h3 a').hover(
      function() {
          ResetNewsItems();

          // show the newsitem and change the selected state
          $('div#newspanel-content .newsitem1').show();
          $(this).addClass("newshoverstate");
      }
   );

    // Headline 2 Hover event
    $('div#newspanel-content div.newsheadline-navigation ul li.story2 h3 a').hover(
      function() {
          ResetNewsItems();

          // show the newsitem and change the selected state
          $('div#newspanel-content .newsitem2').show();
          $(this).addClass("newshoverstate");
      }
   );

    // Headline 3 Hover event
    $('div#newspanel-content div.newsheadline-navigation ul li.story3 h3 a').hover(
      function() {
          ResetNewsItems();

          // show the newsitem and change the selected state
          $('div#newspanel-content .newsitem3').show();
          $(this).addClass("newshoverstate");
      }
   );
    // END 
    // NEWS PANEL

    //////////////////////////
    // NOTICE-BOARD         //
    //////////////////////////
    // get rid of the news stories
    $('div#noticeboard ul li div.notice-content').hide();
    // ...but show the active story
    $('div#noticeboard ul li.active-notice div.notice-content').css('display', 'inline');
    // Setup the click event on the notice board items
    $('div#noticeboard ul li h3 a').click(
        function() {
            // If the notice clicked on is the active one, then close it
            if ($(this).parent().parent().attr("class") == "active-notice") {
                $('div.notice-content').slideUp("slow");
                $('li.active-notice').attr("class", "");
                return false;
            }
            // If the notice clicked on isn't the active one, close all the others then open it
            else {
                $('div#noticeboard ul li').each(function(i) {
                    $(this).attr("class", "");
                });
                $('div.notice-content').hide();
                $(this).parent().parent().attr("class", "active-notice");
                $('li.active-notice div.notice-content').slideDown("slow");
                return false;
            }
        }
   );



    //////////////////////////
    // FACILITIES           //
    //////////////////////////
    // get rid of the facilities item content
    $('div#hirefacilities ul li div.facilities-content').hide();
    // ...but show the active story
    $('div#hirefacilities ul li.active-hirefacilities div.facilities-content').css('display', 'inline');
    // Setup the click event on the notice board items
    $('div#hirefacilities ul li h3 a').click(function() {
        // If the notice clicked on is the active one, then close it
        if ($(this).parent().parent().attr("class") == "active-hirefacilities") {
            $('div.facilities-content').slideUp("slow");
            $('li.active-hirefacilities').attr("class", "");
            return false;
        }
        // If the notice clicked on isn't the active one, close all the others then open it
        else {
            $('div#hirefacilities ul li').each(function(i) {
                $(this).attr("class", "");
            });
            $('div.facilities-content').hide();
            $(this).parent().parent().attr("class", "active-hirefacilities");
            $('li.active-hirefacilities div.facilities-content').slideDown("slow");
            return false;
        }
    }
   );
   
   // Hide collapsing menus
   $('ul.subsubmenu').hide();
   $('ul.submenu').hide();
   
   var test=false;
   
   
   $('li.collapse').click(function() 
   {
      // if the other variable isn't true, then do this
      if(!test){
         $('ul.submenu', this).toggle("slow");
         if( $('h3', this).attr("class") =="closed")
         {
            $('h3.closed', this).removeClass("closed");
            $('h3', this).addClass("open");
         }
         else
         {
            $('h3.open', this).removeClass("open");
            $('h3', this).addClass("closed");         
         }
      }
      test=false;
   }
   ); 
   
   $('li.collapse-sub').click(function() 
      {
         $('ul.subsubmenu', this).toggle("slow");
         if( $('h4', this).attr("class") =="closed")
         {
            $('h4.closed', this).removeClass("closed");
            $('h4', this).addClass("open");
         }
         else
         {
            $('h4.open', this).removeClass("open");
            $('h4', this).addClass("closed");         
         }
         // set a variable to true here
         test = true;
      }
   );
   
      // Expand any Sub menus and set the selected state of the nav
      // var strDocTitle = document.title;
      // var aryDocTitle = strDocTitle.split("|");
      
      // strMenuToExpand = aryDocTitle[1];
      // strMenuToExpand = strMenuToExpand.replace(/^\s*|\s*$/g,'');
      //alert(strMenuToExpand);
      // $("a.selected").parent().("ul.submenu").show();
      var elemLI;
      var elemUL;
      elemLI = $("a.selected").parent();
      elemUL = elemLI.parent();
      elemUL.show();
      if(elemUL.attr("class")=="subsubmenu")
      {
         var elemLI2;
         var elemUL2;
         elemLI2 = elemUL.parent();
         elemUL2 = elemLI2.parent();
         elemUL2.show();
         //alert('bugger');
      }
      
      if(elemLI.attr("class")=="collapse")
      {
         //alert('ace');
         //var elemLI3 = $("").parent();
         $('ul.submenu',elemLI).show();
      }
      //alert(elemUL.attr("class"));
      // strSelectedNav = aryDocTitle[0];
      // strSelectedNav = strSelectedNav.replace(/^\s*|\s*$/g,'');
      // $("a:contains('" + strSelectedNav + "')").addClass('selected');
      
      
      // Hide Buttons that can use js to autopostback
      $(':input.js-hide').hide();
   }
);
