function externalLinks() {
 if (!document.getElementsByTagName)
 {
   return;
 }
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
   {
     anchor.target = "_blank";
   }
 }
}

$("#nav ul li").hover(
    function()
    {
      $(this).children("div").slideDown('fast').show(); //Drop down the subnav on click
    },
    function()
    {
      $(this).children("div").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
    }
  );
  
$(document).ready(function()
{
 	$("#nav ul li").hover(
    function()
    {
      $(this).children("div").slideDown('fast').show(); //Drop down the subnav on click
    },
    function()
    {
      $(this).children("div").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
    }
  );
  
  jQuery.each($('#news ul li div.slide_copy'), function(){ 
  if($(this).height() > 60)
  {
    //$(this + ' div.summary').hide();
    $('.slide_copy div.summary').hide();
  }
  });
  
    
  $('.setlist-search').click(function()
  {
    $.dimScreen(500, 0.7, function() {
    });	
    $('#setlist-search').vCenter();
    $('#setlist-search').slideDown('slow'); 
    return false;
  });		

  $('.back-to-top').click(function()
  {
    $.scrollTo('#site-wrapper', 500);
    return false;
  });	
   
  if($('.chart-links').length)
  {
    $("#years").addClass("active"); 
  }
  
  /*if($('#content').height() > $('#sidebar').height())
  {
    $('#sidebar').height($('#content').height());
  }
  else
  {
    $('#content').height($('#sidebar').height());
  }*/
    
  externalLinks();

  if($('#radio_tlg').val())
  {
    $.timer(60000, function (timer) {
      $.ajax({
      type: "POST",
      url: '/custom/ajax/radio_tlg.php',
      cache: false,
      success: function(html){
        $('#radio_tlg').replaceWith(html);    
      },
      error: function(){
          $("#radio_tlg").html("Error loading Radio TLG");
        },
      beforeSend: function(){
          $("#radio_tlg").html('<div style="height:' + $('#radio_tlg').height() + 'px;"><img src="/images/loading.gif" alt="Loading..." style="width:16px;margin-top:4px;" /></div>'); 
        }
      });  
    });
  }  
  
});

function show_info(show, type)
{
  type_orig = type;
  
  if(type == 'upcoming-shows' || type == 'ushow')
  {
    type = 'u';    
  }
  else if(type == 'lshow' || type == 'local')
  {
    type = 'l';
  }
  else
  {
    type = 's';
  }
  
  $.ajax({
    type: "POST",
    url: '/show_info.php',
    data: "id=" + show + "&type=" + type,
    //cache: false,
    success: function(html){
      $('#' + type + 'show_' + show).html(html);    
    },
    error: function(){
      $('#' + type + 'show_' + show).html("<em>Error loading show details.</em> ");
    },
  });
  
  if(type_orig == 'sshow' || type_orig == 'ushow' || type_orig == 'lshow')
  {
   
  }
  else
  {
    $('#' + type + 'show_' + show).toggle();
  }
}

function show_info_all(type)
{
  if($('#' + type.slice(0, 1) + 'details').html() == 'Show Details')
  {
    action = 'show';
  }
  else
  {
    action = 'hide';
  }  
  
  $("div[id^='" + type + "_']").each(function(i) {
    show_info($(this).attr('id').substr(6), type);
  });   
  
  $("div[id^='" + type + "_']").each(function(i) {
    
    if(action == 'show')
    {
      $('#' + $(this).attr('id')).show();
    }
    else
    {
      $('#' + $(this).attr('id')).hide();
    }
  });  
  
  if($('#' + type.slice(0, 1) + 'details').html() == 'Show Details')
  {
    $('#' + type.slice(0, 1) + 'details').html('Hide Details');
  }
  else
  {
    $('#' + type.slice(0, 1) + 'details').html('Show Details');
  }
}

function addSong()
{
  var id = 'song' + $('select#song_ids').val();
  
  if($('select#song_ids').val() && $("#"+id).length === 0)
  {	
	$.ajax({
    type: "POST",
	  url: '/custom/ajax/addsong.php',
    data: "song_id=" + $('select#song_ids').val(),
	  cache: false,
    success: function(html){
      $('select#song_ids').val("");
      $("#song-loader").html('');
      $('#songs').append(html);
      $('#songs').show();
      $("#" + id).effect("highlight", {}, 2500);
    },
	  error: function(){
        $("#songs").html("Error");
      },
	  beforeSend: function(){
        $("#song-loader").html('<img src="/images/loading.gif" alt="Loading..." style="width:16px;margin-top:4px;" />'); 
      }
    });
  }
  else
  {
    $('select#song_ids').val("");
  }
}

function removeSong(song)
{
  $("#" + song).remove();
  if($("#songs").children().size() === 0)
  {
    $("#songs").html("");
  }  
}

// My TLG.

function mytlg(action, show)
{
	var url = "/" + action + "_show.php";
	var loading_message;
	if(action == "add")
  {
    loading_message = '<img src="/images/loading.gif" alt="Loading..." style="width:22px;height:21px;" />';
  }
	else 
  {
    loading_message = '<img src="/images/loading.gif" alt="Loading..." style="width:22px;height:21px;" />';
  }
	var failure_message;
	if(action == "add")
  {
    failure_message = "Failed to add show";
  }
	else
  {
    failure_message = "Failed to remove show";
  }
	
  $.ajax({
    type: "POST",
	  url: url,
    data: "show=" + show,
	  cache: false,
	  success: function(html){
        $("#show_" + show).html(html);
        $("#gmap_show_" + show).html(html);
      },
	  error: function(){
        $("#show_" + show).html(failure_message);        
        $("#gmap_show_" + show).html(failure_message);
      },
	  beforeSend: function(){
        $("#show_" + show).html(loading_message);
        $("#gmap_show_" + show).html(loading_message);
      }
    }); 
}

// Chart.

function chart(chart, args, mytlg)
{
  $(".chart-links a").each(function(i){
    $(this).removeClass("active");
  });

  if(chart != 'sets')
  {
    $("#chart-links-sets").slideUp(400);
  }
  else
  {
    $("#chart-links-sets").slideDown(400);
  }
  if(chart != 'openers-closers')
  {
    $("#chart-links-openers-closers").slideUp(400);
  }
  else
  {
    $("#chart-links-openers-closers").slideDown(400);
  }
  
  var active;
  if(args)
  {
    active = "#" + args;
    $(active).addClass("active");
  }
  active = "#" + chart;
  $(active).addClass("active");
    
  var url = "/chart.php";
  var loading_message = '<br /><img src="/images/loading.gif" alt="" />';
  var failure_message = "<p>Error loading chart.</p>";
	
  $.ajax({
    type: "POST",
	  url: url,
    data: "chart=" + chart + "&args=" + args + "&mytlg=" + mytlg,
	  cache: true,
    success: function(html)
    {
      $('#content').removeAttr('height');
      $("#chart").html(html);
    },
	  error: function(){
        $("#chart").html(failure_message);
      },
	  beforeSend: function(){
        $("#chart").html(loading_message);
      }
  }); 
}

function headlineScroll()
{
	var headlines_wrapper = $('#news-wrapper'); 
  var headlines = $('#news');

  headlines_wrapper.css('overflow', 'hidden');						

  // height of area at the top at bottom, that don't respond to mousemove
  var inactiveMargin = 99;			
  
  // Cache for performance
  //var wrapperWidth = headlines_wrapper.width();
  var wrapperHeight = headlines_wrapper.height();
  
  // Using outer height to include padding too
  var scrollableHeight = headlines.outerHeight() + 2*inactiveMargin;
  
  headlines_wrapper.mousemove(function(e)
  {
    var wrapperOffset = headlines_wrapper.offset();
          
    // Scroll menu
    var headlines_top = (e.pageY -  wrapperOffset.top) * 
      (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
    if (headlines_top < 0)
    {
      headlines_top = 0;
    }			
    
    headlines_wrapper.scrollTop(headlines_top);     
  });		
  
  headlines_wrapper.bind("mouseleave", function(e)
  {
    headlines_wrapper.scrollTop($('#news-selected').position().top + headlines_wrapper.scrollTop());     
  });		
}
