
$(document).ready(function()
{
  var root = deriveRoot();
  
  fixButtonRolloversInIE();
  
  if ( $('div.contentSwitch').length )
  {
    prepareTabs( root );
    highlightTabs( root );
  }
  
  if ( $('form.postComment').length )
  {
    prepareCommentForm();
  }
  
  if ( $('img#primaryImage').length )
  {
    prepareImageSelector();
  }
  
  if ( $('ul.pagination').length )
  {
    centerPagination();
  }
  
  if ( $('div.popup form').length )
  {
    prepareEmailAFriendForm();
  }
  
  $('a#print').click(function(){ window.print(); return false; });
  
  if ( $('ul.shareArticle').length )
  {
    $('a.socialBookmark').click(function()
    {
      window.open($(this).attr('href'),$(this).attr('id'),'toolbar=0,status=0,height=550,width=700,scrollbars=yes,resizable=no');
      
      return false;
    });
  }
  
  if ( $('#imageCarousel').length )
  {
    if ( $('#imageCarousel li').length > 7 )
    {
      $('button#prevImage').show();
      $('button#nextImage').show();
      
      $('#imageCarousel').jCarouselLite();
    }
    
    if ( $('a#nextGalleryImage').length )
    {
      $('a#nextGalleryImage').click(function()
      {
        current = $('#imageSelector a.current').parent().attr('class').substring(5);
        
        count = 1;
        
        $('#imageSelector li').each(function()
        {
          imageNo = parseInt( $(this).attr('class').substring(5) );
          
          if ( imageNo > count )
          {
            count = imageNo;
          }
        });
        
        if ( current >= count )
        {
          gotoGalleryImage(1);
        }
        else
        {
          gotoGalleryImage(parseInt(current)+1);
        }
        
        return false;
      });
    }
    
    if ( $('a#prevGalleryImage').length )
    {
      $('a#prevGalleryImage').click(function()
      {
        current = $('#imageSelector a.current').parent().attr('class').substring(5);
        
        count = 1;
        
        $('#imageSelector li').each(function()
        {
          imageNo = parseInt( $(this).attr('class').substring(5) );
          
          if ( imageNo > count )
          {
            count = imageNo;
          }
        });
        
        if ( current <= 1 )
        {
          gotoGalleryImage(count);
        }
        else
        {
          gotoGalleryImage(parseInt(current)-1);
        }
        
        return false;
      });
    }
  }
});

function deriveRoot()
{
  var root = window.location.protocol+'//'+window.location.hostname;
  
  if( window.location.port != 80 )
  {
    root += ':' + window.location.port;
  }
  
  root += '/';
  
  if ( ( pos = window.location.pathname.search('public_html') ) != -1 )
  {
    root += window.location.pathname.substring(1,pos+12);
  }
  
  //root = 'http://gulf.furtheronline.co.uk/mags/Notes/';
  
  return root;
}


function fixButtonRolloversInIE()
{
  $('button').mouseover(function(){ $(this).css('background-color','#818077') });
  $('button').mouseout( function(){ $(this).css('background-color','#7DA700') });
}

function prepareTabs( root )
{
  // Content Switch
  $('div.contentSwitch ul').css('display','block');
  $('div.contentSwitch').tabs({
    fxFade: true,
    fxSpeed: 'fast',
    onClick: function(clicked){ removeTabGlyphs( clicked ); },
    onHide: function(){ highlightTabs( root ); }
  });
}

function removeTabGlyphs( clicked )
{
  var target = $(clicked).parent().parent().attr('id');
  
  $('ul#'+target+' li.tabs-selected img').remove();
}

function highlightTabs( root )
{
  $('ul.channelSelector li.tabs-selected').each(function()
  {
    var id = $(this).parent().attr('id');
    
    id = id.substring(0,id.indexOf('_'));
    
    switch( id )
    {
      case 'multimedia' :
      case 'infocus'    : $(this).append('<img src="'+root+'media/img/notes/current_panel.gif" alt="current" />'); break;
      case 'GNTV'       :
      case 'channel'    : $(this).append('<img src="'+root+'media/img/notes/current_channel_2.gif" alt="current" />'); break;
      case 'regional'   : $(this).append('<img src="'+root+'media/img/notes/current_channel.gif" alt="current" />');
    }
  });
}

function prepareCommentForm()
{
  $('form.postComment:not(:has(div.alert))').hide();
  
  $('form.postComment:has(div.alert)').each(function()
  {
    $(this).prev('p').find('a.postComment').addClass('active');
  });
  
  $('a.postComment').click(function()
  {
    if ( $(this).parent().next('form.postComment').is(':visible') )
    {
      $(this).removeClass('active');
      
      $(this).parent().next('form.postComment').slideUp();
    }
    else
    {
      $(this).addClass('active');
      
      $(this).parent().next('form.postComment').slideDown();
    }
    
    return false;
  });
}

function prepareImageSelector()
{
  $('#imageSelector a').click(function()
  {
    $('#imageSelector a').removeClass('current');
    $(this).addClass('current');
    
    $('li#imageTitle').html($(this).attr('title'));
    $('img#primaryImage').attr('src',$(this).attr('href'));
    
    if ( $('dd#imageSelector').length ) // Article Image Selector
    {
      $('li.credit').html('Image Credit: '+$(this).attr('credit'));
      
      i = $('#imageSelector a').index(this)+1;
      count = $('#imageSelector a').length;
      
      $('#imageNumber').html('Image '+i+' of '+count);
    }
    else // Gallery Carousel Image Selector
    {
      $('li.credit em').html('Image Credit: '+$(this).attr('credit'));
      
      i = $('#imageSelector a.current').parent().attr('class').substring(5);
      
      count = 1;
      
      $('#imageSelector li').each(function()
      {
        imageNo = parseInt( $(this).attr('class').substring(5) );
        
        if ( imageNo > count )
        {
          count = imageNo;
        }
      });
      
      $('#imageNumber').html(i+'/'+count);
    }
    
    return false;
  });
}

function gotoGalleryImage(i)
{
  $('#imageSelector a').removeClass('current');
  
  $('#imageSelector li.image'+i+' a').addClass('current');
  
  count = 1;
  
  $('#imageSelector li').each(function()
  {
    imageNo = parseInt( $(this).attr('class').substring(5) );
    
    if ( imageNo > count )
    {
      count = imageNo;
    }
  });
  
  $('li#imageTitle').html($('#imageSelector li.image'+i+' a').attr('title'));
  $('img#primaryImage').attr('src',$('#imageSelector li.image'+i+' a').attr('href'));
  
  $('#imageNumber').html(i+'/'+count);
  $('li.credit em').html('Image Credit: '+$('#imageSelector li.image'+i+' a').attr('credit'));
}

function centerPagination()
{
  width = 0;
  
  $('ul.pagination li').each( function(){ width += $(this).width(); } );
  
  width += 4; // Mac Fix
  
  if ( width < 320 )
  {
    $('ul.pagination').css('width',width+'px');
  }
}

function prepareEmailAFriendForm()
{
  $('div.popup form').css({ 'display':'none','position':'absolute','top':'-300px','left':'-2px','z-index':'9999','border':'2px solid #CCCCCC' });
  
  // Position form within wrapper_twothirds differently...
  $('div.wrapper_twothirds div.popup form').css({ 'top':'-110px','left':'85px' });
  
  $('a#email').click(function()
  {
    $('div.popup form').fadeIn();
    
    $('div.popup form').append('<a class="closePopup" href="" title="Close Popup">Close Popup</a>');
    
    prepareEmailAFriendFormCloseBtn();
    
    return false;
  });
}

function prepareEmailAFriendFormCloseBtn()
{
  $('a.closePopup').click(function()
  {
    $('div.popup form').fadeOut();
    
    $('div.popup form a').remove();
    
    return false;
  });
}