$(document).ready(function(){

  //the sliding content containers (leadership and P.C. Desktop Clients pages)
  //$('div#drawer_wrapper> div').hide();
  //$('div#drawer_wrapper> h3').click(function() {
  //	$(this).next('div').slideToggle('slow');
  //  $(this).toggleClass('open');
  //});

	

  //setting up the share hover on news artice page
  $('.sharelogo').hover(
    function()  {   
      $('.wrpr', this).fadeIn('fast')    
    },
    function()  {
      $('.wrpr', this).fadeOut('fast')
      }
  )

  //set up mail page custom radios
  $('.customradio').css('display','block').siblings('input').css('visibility','hidden');
  $('.customradio').click(
    function(){ 
      $(this).addClass('checked').next().attr('value','yes');
      $(this).parent().siblings().find('input').attr('value','no').prev().removeClass('checked');
    }
  )

  //setting up the nav hovers
  $('#nav ul li').hover(
    function()  { 
        
      //$('.main', this).addClass('hover');
      //$('.wrpr:not(.disabled)', this).fadeIn('fast');

      $('.main', this).addClass('hover');
      $('.wrpr:not(.disabled)', this).css('visibility','visible');
            
      // make dropdowns at least as wide as their li parent
      if ($('.wrpr ul', this).width() <= $(this).width()    ){
        $('.wrpr ul', this).css('width', ($(this).width() - 6) + 'px'); 
      }
    },
    
    function()  {
      //$('a.main', this).removeClass('hover');
      //$('.wrpr', this).fadeOut('fast')
      
      $('a.main', this).removeClass('hover');
      $('.wrpr', this).css('visibility','hidden');
      
      }
  )
  //detect a click anywhere on the document to close the nav dropdowns

  $('body').click(
    function()  {
      $('li .wrpr').css('visibility','hidden')
    }   
  )

  //share links on news page
  if($('.sharelogo .wrpr').length > 0){
	//document.title = $('#story_wrapper strong#newstitle').text();
	var u=encodeURIComponent(location.href);
    var t=escape($('#col_wrapper strong#newstitle').text());
	var d=escape($('#col_wrapper em').text());
	//$("meta[name=title]").attr("content",t);
	//$("meta[name=description]").attr("content",t);
    $('.sharelogo .wrpr li a').each(function() {

      var url = $(this).attr('href');
      var new_url = url.replace('temp_url',u).replace('temp_title',t);
      $(this).attr('href',new_url);

    });
	//$("title").attr("content",t); 
  }

  if($('#linkBar #facebook').length > 0){
	var u=encodeURIComponent(location.href);
    var t=escape($('#col_wrapper strong#newstitle').text());
	var d=escape($('#col_wrapper em').text());
    $('#linkBar #facebook a').each(function() {

      var url = $(this).attr('href');
      var new_url = url.replace('temp_url',u).replace('temp_title',t);
      $(this).attr('href',new_url);

    });
  }
  if($('#linkBar #linkedin').length > 0){
	var u=encodeURIComponent(location.href);
    var t=escape($('#col_wrapper strong#newstitle').text());
	var d=escape($('#col_wrapper em').text());
    $('#linkBar #linkedin a').each(function() {

      var url = $(this).attr('href');
      var new_url = url.replace('temp_url',u).replace('temp_title',t);
      $(this).attr('href',new_url);

    });
  }
  if($('#linkBar #twitter').length > 0){
	var u=encodeURIComponent(location.href);
    var t=escape($('#col_wrapper strong#newstitle').text());
	var d=escape($('#col_wrapper em').text());
    $('#linkBar #twitter a').each(function() {

      var url = $(this).attr('href');
      var new_url = url.replace('temp_url',u).replace('temp_title',t);
      $(this).attr('href',new_url);

    });
  }



  //set up the news carousel
  if($('#news.info').length > 0){

    $('#news .content').cycle({ 
    speed:       200, 
    timeout:     0,
    fx:         'scrollUp',
    next:        '#news .top .next',
    prev:    '#news .top .prev',
    pager:      '#news .top .control div', 
    pagerEvent: 'click'
 
      });
  }

  //set up the news carousel
  if($('#events.sml').length > 0){

    $('#events.sml .content').cycle({ 
    speed:       200, 
    timeout:     0,
    fx:         'scrollUp',
    next:        '#events.sml .next',
    prev:    '#events.sml .prev',
    pager:      '#events.sml .top .control div', 
    pagerEvent: 'click'
 
      });
  }
    
  //add the year to the back link on the news story page, 
  //to return to the same year of the press carousel
  if($('.back.storypage').length > 0){
    var year = (jQuery.url.param("y"))?jQuery.url.param("y"):'2011';
    var backHref = $('.back.storypage a').attr('href');
    $('.back.storypage a').attr('href',backHref + '?y=' + year);
  }

  //set up the events carousel
  if($('#events.lrg').length > 0){
      init_Carousel($('#events.lrg'));
  }
    
  //set up the events carousel
  //if($('#pr').length > 0){
  //    init_Carousel($('#pr'));
  //}

  
  if ($('#archive').length > 0 ){
  
    //pressreleasepage load 2008 archive into carousel unless a year is passed in the url
    var year = (jQuery.url.param("y"))?jQuery.url.param("y"):'2011'; 
    doLoadContent(year); 
    $('#archive ul li a').click(
      function()  { 
      //get the year from the clicked link
      var year = $(this).text();
      if(year.length > 0){
        doLoadContent(year);
      } 
      return false;
      }
    ) 
  }

  //stories on news.php  ...adds green linkout icon  to links 
  if ($('#story_wrapper').length > 0 ){  
    $('#story_wrapper .stories_links').wrap("<span class=\"stories_links\"></span>");
  }
    
});


function doLoadContent(year) {
  // hiding the content pane so it can be faded in...
  $("#col_wrapper .l").fadeOut("fast");
  $.ajax({
	   type: "GET",
	   url: "./news_carousel_lrg.php?y="  + year,
	   success: function(msg){
	      // ...then inserting the html and fading back in the  div
		   $("#col_wrapper .l").html(msg);
		  //init_Carousel($('#pr'));
		   $("#col_wrapper .l").fadeIn("fast");
		  //give that year the selected class
		  $('#archive ul li a').removeClass('current');
      $('#archive ul li a:contains('+ year + ')').addClass('current');

      $('h1.year').text(year);
      $('#pr .inner').each(function(){
        //move the date to before the title
        $('strong',$(this)).before($('.date',$(this)));
      });
      
	   },
	   error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('error throw');
        console.log(errorThrown);
        var errorMsg = "<h2>Sorry, the page you requested cannot be loaded</h2>";
        $("#col_wrapper .l").html(errorMsg);
        $("#col_wrapper .l").fadeIn("fast");
      } 
	 });
}


//carousels on pressrelease and events pages
function init_Carousel(carousel) {

    this.carousel = carousel;
    //get the number of li in the carousel
    this.num_pages = $('.content li',this.carousel).length;
    this.control = $('.control',this.carousel);

    //if there are more than 1 page
    if(this.num_pages > 1){ 
    
      //write the "previous" link 
      this.control.append("<a class=\"prev inactive \" href=\"#\">Previous</a>")
    
      //for each li (2 stories) write a link in the nav
      for (var i=1; i <= num_pages; i++){
        this.control.append("<a class=\"index\" href=\"#\">"  + i +  "</a>")      
      }
      
      $('a.index:first',this.control).addClass('inactive');
      
      //write the "next" link 
      this.control.append("<a class=\"next\" href=\"#\">Next</a>")
      
      // bind the click events to the nav links
      $('a.index',this.control).bind('click', function() {
          this.carousel = $(this).parent().parent().parent();
          this.control = $('.control',this.carousel);
          //fade out the present li and fade in the clicked index
          $('li.item:visible',this.carousel).hide();
          $('li.item',this.carousel).eq($('a.index',this.control).index(this)).fadeIn('slow');
          //update button styles
          $('a',this.control).removeClass('inactive');
          $(this).addClass('inactive').next(".next").addClass('inactive');
          $(this).prev('.prev').addClass('inactive');
        
          return false;
      });
    
      $('.next',this.control).bind('click', function() {
          this.carousel = $(this).parent().parent().parent();
          this.control = $('.control',this.carousel);
          //show the next slide
          if (!$('li.item:visible',this.carousel).is(":last-child")){
            $('li.item:visible',this.carousel).hide().next().fadeIn('slow');
          }
          //update the button style
          if(!$(this).hasClass("inactive")){
            $('a.index.inactive',this.control).removeClass('inactive').next().addClass('inactive');
            $('a.prev',this.control).removeClass('inactive');
            //if the inactive link is also the last one, inactivate the 'next' link also
            $('a.index.inactive',this.control).next('.next').addClass('inactive');
          }
        return false;
      });
    
      $('.prev',this.control).bind('click', function() {
          this.carousel = $(this).parent().parent().parent();
          this.control = $('.control',this.carousel);
        if (!$('li.item:visible',this.carousel).is(":first-child")){
                  $('li.item:visible',this.carousel).hide().prev().fadeIn('slow');
        }

        if(!$(this).hasClass("inactive")){
          $(' a.index.inactive',this.control).removeClass('inactive').prev().addClass('inactive');
          $(' a.next',this.control).removeClass('inactive');
          //if the inactive link is also the last one, inactivate the previous link also
          $(' a.index.inactive',this.control).prev('.prev').addClass('inactive');
        }
        return false;
      });

    }
    
};





  


