
$(document).ready(function() { 

  //------------------------------------------------------------------------------------------------
  // Get page URL.
  var sPageURL = '';
  sPageURL = document.location.toString();
  sPageURL = sPageURL.replace('http://www.britishcouncil.org/new/','')
  sPageURL = sPageURL.replace('http://britishcouncil.rufr.co.uk/new/','')

  // jQuery to control hiding / showing of panels.
  // Requires at least jQuery core (http://www.jquery.com) to be specified in the document HEAD.
  
  ePanels = $(".panels_question");
  if (ePanels != null) {
    //Start by hiding all the videos and all the panels apart from the 1st one (1st one's open by default). 
    $(".panels_answer").addClass("displayNone");
    $(".panels_vidCont").addClass("displayNone");
    $(".panels_vidCont:eq(0)").removeClass("displayNone");

    // Adds click event to panel handles.
    // First item shows by default - controled by not setting 'hiding' class in CMS.
    ePanels.click(function() {
      // Remove brdr from all other panels
      $(this).parents(".panels_questbox_wrap").siblings("div").children(".panels_questbox").addClass("brdr_on");
      // Alter the class of the parent div (for border)
      $(this).parent().removeClass("brdr_on");
      // Hide all of the other panels
      $('div.panels_answer').slideUp('medium');
      // Display the panel that was clicked.
      $(this).siblings("div").slideDown('medium');
      // Alter the src of the image handle.
      var iSrc = $(this).children("img").attr("src");
      // Reset all images.
      $(this).parents(".panels_questbox_wrap").siblings("div").find("img.imgToggle").attr("src","images_structure/button_show.gif");
      // Set the current item to show status
      $(this).children("img").attr("src","images_structure/button_hide.gif");
      // Hide all other video panels
      $(".panels_vidCont").addClass("displayNone");
      // Show the video for current panel selection.
      var elmVid = "#"+($(this).attr("id"))+"_vid";
      $(elmVid).toggleClass("displayNone");
      //return false; removed to stop the flash video playing in the background
    });
  }
  
  //------------------------------------------------------------------------------------------------
  //  Routines to control the image gaallery controls.
  
  // Find the gallery dom element.
  eGall = $('#gallery');
  // Var to keep track of current docused item.  This method means only 1 gallery per page can be used.
  var iCurrItem = 0;  // Zero mean it is the first item
  // If it exists continue.
  if (eGall != null) {
    var prevPos;
    var nextPos;
    
    // Find the gallery navigation element.
    eGallNav = eGall.find('.gallery_nav');
    //eGallNav.css('display','block');
    eGallNav.show();
    // Find the unordered list element for the gallery.
    eGallLst = eGall.find("ul");
    // Find the child list elements.
    eGallLstItms = eGallLst.children('li');
    // Remove unwanted css styles as we have JS enabled,
    eGallLstItms.css('display','none').css('border-bottom','none').css('margin-bottom','0');
    // Always show the first list element in the gallery
    eGallLst.children('li:eq(0)').css('display','block');
    var sTmpA = '';
    var sTmpB = '';
    var sTmpC = '';
    //-----------------------------------------------
    // Create the left arrow
    sTmpA = $('<a><img src="/new/images_structure/but_galle_prev.gif" alt="Previous image" /></a>').attr('href','#').addClass('gallery_nav_arrow').click(function() {
      // What is the current item index?
      if (iCurrItem == 0) {
        iCurrItem = eGallLstItms.length-1;
      } else {
        iCurrItem = (iCurrItem-1);
      }
      // Hide all galleri items
      eGallLstItms.hide();
      // Set the item nav position and take into account the zero-based dom count difference.
      iNavPos = (iCurrItem+1);
      // Show the current list item.
      eGallLst.children('li:eq(' + iCurrItem + ')').show();
      // Set the correct nav item to have the active class.
      eGallNav.children('a').removeClass('gallery_nav_active');
      // Set the active nav item
      eGallNav.children('a:eq(' + iNavPos + ')').addClass('gallery_nav_active');
//--------------WT tracking (when left arrow is used)
        // Dynamically grab the gallery item headline to send to webtrends.
        call_wt_multitrack('DCS.dcsuri', sPageURL+'?Headline='+eGallLst.children('li:eq(' + iCurrItem + ')').find('h3').text());
//--------------   
      // Don't follow the link.
      return false;
    });
    eGallNav.append(sTmpA);
    //-----------------------------------------------
    
    // Loop through all list elements to build up the gallery navigation.
    for (var i=0;i<=eGallLstItms.length-1;i++) {
      // Dynamically create the nav links and assign the click event to show the corresponding gallery list item.
      sTmpB = $("<a>"+(i+1)+"</a>").attr('href','#').click(function() {
        // Set the item to be active.
        eGallNav.children('a').removeClass('gallery_nav_active');
        // Set the active nav item
        $(this).addClass('gallery_nav_active');
        // Hide all of the list items.
        eGallLstItms.hide();
        // Show the current list item.
        iCurrItem = eval($(this).text())-1;
        // Set the item nav position and take into account the zero-based dom count difference.
        iNavPos = (iCurrItem+1);
        // Show the current list item - taking into account the zero based dom count.
        eGallLst.children('li:eq(' + iCurrItem + ')').show();
        
//--------------WT tracking (when a number is clicked)
        // Dynamically grab the gallery item headline to send to webtrends.
        call_wt_multitrack('DCS.dcsuri', sPageURL+'?Headline='+eGallLst.children('li:eq(' + iCurrItem + ')').find('h3').text());
//--------------        

        
        // Don't follow the link.
        return false;
      });
      // Append all nav links to the nav element.
      eGallNav.append(sTmpB);
    }
    //-----------------------------------------------
    // Set nav item to active.
    eGallNav.children('a:eq(' + (iCurrItem+1) + ')').addClass('gallery_nav_active');
    // Create the right arrow
    sTmpC = $('<a><img src="/new/images_structure/but_galle_next.gif" alt="Next image" /></a>').attr('href','#').addClass('gallery_nav_arrow').click(function() {
      // What is the current item index?
      if (iCurrItem == (eGallLstItms.length-1)) {
        iCurrItem = 0;
      } else {
        iCurrItem = (iCurrItem+1);
      }
      
      
      
      // Set the item nav position and take into account the zero-based dom count difference.
      iNavPos = (iCurrItem+1);
      // Hide all gallery items
      eGallLstItms.hide();
      // Show the current list item.
      eGallLst.children('li:eq(' + iCurrItem + ')').show();
      // Set the correct nav item to have the active class.
      eGallNav.children('a').removeClass('gallery_nav_active');
      // Set the active nav item
      eGallNav.children('a:eq(' + iNavPos + ')').addClass('gallery_nav_active');
//-------------- WT tracking (when right arrow is used)
        // Dynamically grab the gallery item headline to send to webtrends.
        call_wt_multitrack('DCS.dcsuri', sPageURL+'?Headline='+eGallLst.children('li:eq(' + iCurrItem + ')').find('h3').text());
//--------------   
      // Don't follow the link.
      return false;
    });
    eGallNav.append(sTmpC);
    //-----------------------------------------------
  }
  
  //------------------------------------------------------------------------------------------------
  
  //--------------
  //------------------------------------------------------------------------------------------------
  
  // Traverse anchor tags to attach WT tracking call.
  $('a').click(function() {
    var aFiletypes = ['.doc','.xls','.ppt','.pdf','.vsd','.pub','.docx','.xml','.jpg','.gif','.bmp','.tif','.tiff','.eps','.ai','.rss','.psd','.flv','.swf','.mp3'];
    var sfileURL = $(this).attr('href');
    for (i=0;i<=aFiletypes.length-1;i++) {
      if (sfileURL.indexOf(aFiletypes[i]) != -1) {
        call_wt_multitrack('DCS.dcsuri', sfileURL);
      }
    }
  });
  
  //------------------------------------------------------------------------------------------------
//-------------- 
  
});


//--------------
// Function to call the webtrends "dcsMultiTrack" function.
function call_wt_multitrack(trackType, trackURL) {
  if (trackType == '') {
    trackType = 'DCS.dcsuri';
  }
  if (trackURL != '') {
    dcsMultiTrack(trackType,trackURL);
  }
}
//--------------

//--------------