$(document).ready(function() {
   caption();
});

function setColour (name, classname) {
   var val = $(name).text();
   if (val && val.indexOf(':') > 0) {
      if (val.indexOf(' ') > 0 && val.indexOf(' ') < val.indexOf(':')) {
         $(name).addClass('colourdenoted').addClass(val.substring(val.indexOf(' ') + 1, val.indexOf(':')).toLowerCase().replace(/\W+/g, '')+'-bg');
      }
      $(name).text(val.substring(val.indexOf(':') + 1));
   }
} // setColour

function caption() {

   $('#col2').find('#content').find("p:contains('Caption')").each(function(i) {
   var that = $(this);
   var titlecolour = "plum";
   var firstsevenletters = that.text().toLowerCase().substring(0,7);
   if (firstsevenletters == "caption") {
      that.addClass('caption'+i).prev('p').children('span').addClass("captioncontainer" + i);
      $('.caption'+i).wrap('<span class="caption' + i + '"></span>');
      var whattoadd = $('span.caption'+i).children('p.caption'+i).html();
      $('span.caption'+i).append(''+ whattoadd +'').children('p').remove();
      $('span.caption'+i).addClass('caption').appendTo('.captioncontainer'+i);
      setTimeout( function () {
         var imgwidth = $('span.caption'+i).prev('img').width();
         $('span.caption'+i).css({width: imgwidth-10 + 'px'});
      }, 100 );

      setColour('span.caption'+i);

      if ( $('span.caption'+i).hasClass('colourdenoted') ) {
      } else {
         if ( $('#page-ttl').hasClass("lakeblue-bg") ) { titlecolour = "lakeblue";
            } else if ( $('#page-ttl').hasClass("purple-bg") ) { titlecolour = "purple";   
            } else if ( $('#page-ttl').hasClass("gray-bg") ) { titlecolour = "gray";   
            } else if ( $('#page-ttl').hasClass("plum-bg") ) { titlecolour = "plum";   
            } else if ( $('#page-ttl').hasClass("redrock-bg") ) { titlecolour = "redrock";   
            } else if ( $('#page-ttl').hasClass("firered-bg") ) { titlecolour = "firered";   
            } else if ( $('#page-ttl').hasClass("orangetree-bg") ) { titlecolour = "orangetree";   
            } else if ( $('#page-ttl').hasClass("sunflower-bg") ) { titlecolour = "sunflower";   
            } else if ( $('#page-ttl').hasClass("saguaro-bg") ) { titlecolour = "saguaro";   
            } else if ( $('#page-ttl').hasClass("paloverde-bg") ) { titlecolour = "paloverde";   
            } else if ( $('#page-ttl').hasClass("royalblue-bg") ) { titlecolour = "royalblue";   
            } else if ( $('#page-ttl').hasClass("white-bg") ) { titlecolour = "white";   
            } else if ( $('#page-ttl').hasClass("lightgray-bg") ) { titlecolour = "lightgray";   
         } //if
      } //if
   } //each

   switch(titlecolour){
      case "lakeblue": $('span.caption'+i).not('.colourdenoted').addClass("lakeblue-bg"); break
      case "purple": $('span.caption'+i).not('.colourdenoted').addClass("purple-bg"); break
      case "gray": $('span.caption'+i).not('.colourdenoted').addClass("gray-bg"); break
      case "plum": $('span.caption'+i).not('.colourdenoted').addClass("plum-bg"); break
      case "redrock": $('span.caption'+i).not('.colourdenoted').addClass("redrock-bg"); break
      case "firered": $('span.caption'+i).not('.colourdenoted').addClass("firered-bg"); break
      case "orangetree": $('span.caption'+i).not('.colourdenoted').addClass("orangetree-bg"); break
      case "sunflower": $('span.caption'+i).not('.colourdenoted').addClass("sunflower-bg"); break
      case "saguaro": $('span.caption'+i).not('.colourdenoted').addClass("saguaro-bg"); break
      case "paloverde": $('span.caption'+i).not('.colourdenoted').addClass("paloverde-bg"); break
      case "royalblue": $('span.caption'+i).not('.colourdenoted').addClass("royalblue-bg"); break
      case "white": $('span.caption'+i).not('.colourdenoted').addClass("white-bg"); break
      case "lightgray": $('span.caption'+i).not('.colourdenoted').addClass("lightgray-bg"); break
      }
   });
} // caption

// tertiary navigation, aka Quick Links below

function tertiarynavload() {
   var list  = $('.quicklinks-list');
   var links = list.find('a');
   if (list.length <= 0 || links.length <= 0) {
      // No need to continue.
      list.remove();
      return null;
   }
   
   var nav      = $('<div class="quicklinks"><h5>Quick Links</h5></div>'),
       navlinks = $('<div class="navlinks">');
   links.each(function(){
      $('<p class="subnav-4">').append(this).appendTo(navlinks);
   });
   nav.append(navlinks);
   nav.append(
      '<div class="helpmessage"><p>Click "More Contents" to expand this box.' +
      ' Click the button at the top right to minimize or maximize the ' +
      'navigation.</p></div>' +
      '<a href="#" class="helpbutton"><img ' +
      'src="/webcms/groups/internet/@inter/@pio/documents/web_asset/quicklinks_helpbutton.png" alt="Help" /></a><a href="#" ' +
      'class="expandcollapsebutton"><img ' +
      'src="/webcms/groups/internet/@inter/@pio/documents/web_asset/quicklinks_expandcollapse.png" alt="Expand/Collapse" /></a>'
   );

   if (links.length > 9) {
      nav.append(
         '<a href="#" class="moreless">More Contents &raquo;</a>'
      );
      navlinks.children('p').eq('8').nextAll('p').hide().addClass('belowthefold');
   } // if > 9 links

   list.remove();
   nav.appendTo('#col1');
   navlinks.find('.belowthefold').hide();

   var is_mobile = false;
   is_mobile     = (
      navigator && navigator.platform && 
      navigator.platform.match(/^(iPad|iPod|iPhone|Android)$/)
   );

   if (is_mobile) { 
      nav.find('.helpbutton').toggle(function(event){
         event.preventDefault();
         nav.find('.helpmessage').show(200);
      }, function(){
         event.preventDefault();
         nav.find('.helpmessage').hide(200);
      }); //hover
   } else {
      nav.find('.helpbutton').click(function(event){
         event.preventDefault();
      });
      nav.find('.helpbutton').hover(function(){
         nav.find('.helpmessage').show(200);
      }, function(){
         nav.find('.helpmessage').hide(200);
      }); //hover
   }; //mobile

   tertiarynavinteraction();
}; //tertiarynavload



function tertiarynavinteraction(){
   var nav = $('#col1 .quicklinks');
   nav.find('.expandcollapsebutton').toggle(function(event){
      event.preventDefault();
      nav.find('.navlinks').slideUp(500);
      nav.find('.moreless').slideUp(500);
      nav.find('.expandcollapsebutton').addClass('plus');
   }, function(event){
      event.preventDefault();
      nav.find('.navlinks').slideDown(500);
      nav.find('.moreless').slideDown(500);
      nav.find('.expandcollapsebutton').removeClass('plus');
   }); //toggle expandcollapse

   nav.find('.moreless').toggle(function(event){
      event.preventDefault();
      nav.find('.belowthefold').show(500);
      nav.find('a.moreless').html('Less Contents &raquo;');
   }, function(event){
      event.preventDefault();
      nav.find('.belowthefold').hide(500);
      nav.find('a.moreless').html('More Contents &raquo;');
   }); //toggle moreless
}; //tertiarynavinteraction

