/*
///////////////////////////////////////////////////////
Archant
Project : Regional Paper Templates
File    : Main Javascript File
///////////////////////////////////////////////////////
*/
var t;

$(document).ready(function(){

  // IE detection, if its ie then
  // we cant use the nice slide effects
  var slide = true;
  var showMap = false;
  
  // Detect if we should use the slide animation (IE does not like it so non IE only)
  if($.browser.msie)
  {
    slide = false;
  }

  // Hide anything that should be hidden if JS is enabled
  $('.js-hide').hide();     
    
  // Detect rollovers on tabbed news callouts
  $('.tabbed-news-callouts .tabs li').hover(function() {
    $(this).addClass('hover');                          
  },
  function() {
    $(this).removeClass('hover');                         
  });
  
  // Add shadows to li elements either side of the selected nav item
  $('#navigation li.selected').prev('li').addClass('prev');
  $('#navigation li.selected').next('li').addClass('next');
  
  
  // Start Tabbed News Callouts
  // --------------------------------------------------------------------
  
  // Hide items in tabbed news list when page loads so the tabs can show and hide them.
  $('.tabbed-news-callouts h2').addClass('inactive');
  $('.tabbed-news-callouts .articles').each(function() {
    if(!$(this).hasClass('active'))
    {
      $(this).addClass('inactive'); 
    }
  });
  
  // functionality to show hide sections on click of tabbed news callouts tabs.
  $('.tabbed-news-callouts .tabs li a').click(function() {
    $('.tabbed-news-callouts .tabs li').each(function() {
      $(this).removeClass('selected');
    });
    var rel = $(this).attr('rel');
    $('.tabbed-news-callouts .active').fadeOut('fast',function() {
        $(this).removeClass('active');
        $("#"+rel).fadeIn('fast');
        $("#"+rel).addClass('active');
    });
    $(this).parents('li').addClass('selected');
    $(this).blur();
    return false;
  });
  // --------------------------------------------------------------------
  // End Tabbed News Callouts


  // Start Calendar type a + b
  // --------------------------------------------------------------------
  // Add the calendar key and show calendar if JS is active
  $('.calendar-type-a .calendar,.calendar-type-b .calendar').append("<p>Click on the calendar to see that day's events</p>");
  $('.calendar-type-a .calendar,.calendar-type-b .calendar').css({display:'block'});
  $('.calendar-type-a ul').addClass('active');
  
  // Highlight cal cells when you rollover a event relating to that calendar item.
  $('.cal-event-listing li a').hover(function(){
    var id = $(this).attr('rel');
    $("."+id).addClass('hover');
  },
  function() {
    var id = $(this).attr('rel');
    $("."+id).removeClass('hover');
  });
  
  $("input[value='dd/mm/yyyy']").css({color:'#666'});
  $("input[value='dd/mm/yyyy']").click(
    function(){
      $(this).css({color:'#000'});
    }
  );
  // --------------------------------------------------------------------
  // End Calendar type a + b
  
  
  // Start Form Actions
  // -------------------------------------------------------------------- 
  //Checkbox tree
  $(".checkbox-tree input").click(function(){
      var itemClass = $(this).attr('name');
      var checked_status = $(this).attr('checked');
       $("."+itemClass).each(function()
       { if (checked_status) {$(this).attr('checked','checked');}
        else {$(this).attr('checked','');} });
    });
    $(".checkbox-tree a").click(function(){
      var itemClass = $(this).attr('name');
       $("."+itemClass).toggle();
      return false;
    });
  
  
  // Close
  $('.login .close a').click(function() {
    if(slide) {
      $('.login').slideUp('fast');
    } else {
      $('.login').hide();
    }
    return false;
  });
  
  // Open
  if($('form.login').attr('action'))
  {
    $('li.log-in a').click(function() {
      if(slide) {
        $('form.login').slideDown();              
      } else {
        $('form.login').show();             
      }
      return false;
    }); 
  }
  
  // Close
  $('.register-form .close a').click(function() {
    if(slide) {
      $('.register-form').slideUp('fast');
    } else {
      $('.register-form').hide();
    }
    return false;
  });
  
  // Open
  if($(".register-form").attr('action'))
  {
    $('li.register a').click(function() {
                    
      if(slide) {
        $('.register-form').slideDown();              
      } else {
        $('.register-form').show();             
      }
      return false;
    }); 
  }
  
  $('.message-panel .close a').click(function() {
    if(slide) {
      $('.message-panel').slideUp('fast');
    } else {
      $('.message-panel').hide();
    }
    return false;
  });
  // -------------------------------------------------------------------- 
  // End Form Close Button Actions
    
  
  // Start Save Search
  // -------------------------------------------------------------------- 
  $('.save-search a').click(function(){
    if(slide) {
      $('.frm-save-search').slideToggle('fast');
    } else {
      $('.frm-save-search').toggle();
    }
    return false;                 
  });
  
  $('.saved-searches .actions a').click(function() {
    $(this).parents('li').fadeOut('fast');                      
  });
  
  // -------------------------------------------------------------------- 
  // End Save Search
  
  

  
  // -------------------------------------------------------------------- 
  // Start Profile
  $(".profile button[name='new-email-address'],.profile button[name='new-password'],.profile button[name='new-screen-name']").click(function(){
    var itemClass = $(this).attr('name');
    if($('fieldset.'+itemClass).data('visible') || !$('fieldset.'+itemClass).hasClass('hide'))
    {
      $(this).text("Change");
      $('fieldset.'+itemClass).removeData('visible');
      $('fieldset.'+itemClass).hide();
      $('fieldset.'+itemClass).addClass('hide');
      $(this).removeClass('btn-changeable');
      return false;
    }
    else
    {
      $(this).text("Cancel");
      $('fieldset.'+itemClass).data('visible','1');
      $('fieldset.'+itemClass).show();
      $(this).addClass('btn-changeable');
      return false;
    }
  });
  // End Profile
  // -------------------------------------------------------------------- 
  
  
  // -------------------------------------------------------------------- 
  // Start Rating
  $('.rating ul').hover(function() {
    $(this).children('li').each(function() {
      if($(this).hasClass('active'))
      {
        $(this).removeClass('active');
        $(this).data('wasActive','1');
      }
    });
  }
  ,function() {
    $(this).children('li').each(function() {
      if($(this).data('wasActive'))
      {
        $(this).addClass('active');
      }
    });
  });
  // -------------------------------------------------------------------- 
  // End Rating
  

  
  
  // -------------------------------------------------------------------- 
  // Start Misc
  $('.password-reminder a').click(function() {
    $('.forgotten-password').toggle();
    return false;
  });
  
  $('button.btn').hover(function() {
    $(this).addClass('active');             
  }
  ,function() {
    $(this).removeClass('active');              
  });
  
  $('.print a, a.print').click(function() {
    window.print();
    return false;
  });
  
  $('.close-window a, a.close-window').click(function() {
    window.close();
    return false;
  });
  
  $('.share').click(function() {
    $('.sharing').toggle();
  });
  
    $('.send-to-friend').click(function() {
    $('#send-to-friend-email').toggle();
  });
  
  // End Misc
  // -------------------------------------------------------------------- 
    
  $('#sub-navigation').data('defaultNav','');
  // append datya object with sub nav items
  $('#navigation > li').each(function() {
    
    var secondaryNavigation = $(this).children('ul');
    secondaryNavigation.addClass('secondary-repositioned');
    $(this).data('subNav',secondaryNavigation);
    $(this).children('ul').remove();
    
    if($(this).hasClass('selected'))
    {
      $(this).data('selected',1);                 
      $('#sub-navigation').html($(this).data('subNav'));
      $('#sub-navigation').data('defaultNav',$(this).data('subNav'));
    }
              
    $(this).mouseover(function() {
      clearTimeout(t);
      $('#navigation .highlight').removeClass('highlight');
      $(this).addClass('highlight');
      $('#sub-navigation').html($(this).data('subNav'));
      rebindRolloverActions(secondaryNavigation);
    });
        
    $(this).mouseout(function() {
      t = setTimeout(detectMouseOut,1500);
    });
            
  });
  
  $('#navigation, #sub-navigation').addClass('repositioned'); 
    
  $(".secondary-news-headline .thumb img[align='left'],.feature-content .thumb img[align='left'],.news-headline-c .thumb img[align='left'],.category-detail-b .thumb img[align='left']").addClass('left');
  $(".secondary-news-headline .thumb img[align='right'],.feature-content .thumb img[align='right'],.news-headline-c .thumb img[align='right'],.category-detail-b .thumb img[align='right']").addClass('right');
  
});

function detectMouseOut() {
  $('#sub-navigation').html($('#sub-navigation').data('defaultNav'));
  $('#navigation .highlight').removeClass('highlight');
}

function rebindRolloverActions(navigationalItem)
{
  navigationalItem.mouseover(function() {
    clearTimeout(t);
  });
  
  navigationalItem.mouseout(function() {
    t = setTimeout(detectMouseOut,1500);
  });
}

function animatePollAnswers()
{
  $('.results .yes em').each(function() {
    var amount = $(this).css('border-right-width');
    $(this).css('border-right-width','0');
    $(this).animate({
        borderRightWidth: amount
      }, 1500, "linear");
  });
  
  $('.results .no em').each(function() {
    var amount = $(this).css('border-right-width');
    $(this).css('border-right-width','0');
    $(this).animate({
        borderRightWidth: amount
      }, 1500, "linear");
  });
}

function highlightEvent(id)
{
  $('.cal-event-listing li').each(function() {
    if($(this).children('a').attr('rel') == id)
    {
      $(this).addClass('highlight');
    }
  }); 
}

function removeHighlights()
{
  $('.cal-event-listing li').each(function() {
    $(this).removeClass('highlight');
  }); 
}

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}

