$('#fishStats input,#fishStats textarea').live('focus',togglePlaceholderText);
$('#fishStats input,#fishStats textarea').live('blur',togglePlaceholderText);
$('#fishStats input,#fishStats textarea').live('keyup',togglePlaceholderText);

function togglePlaceholderText(e)
{
  var inputJQElem = $(e.target);
  var bgPos = inputJQElem.css('background-position');
  var yOffset = '';
  if(bgPos.indexOf(' ') > -1)
    yOffset = bgPos.substr(bgPos.indexOf(' ')+1);
  if(inputJQElem.val().length > 0)
    inputJQElem.css('background-position','right '+yOffset);
  else
    inputJQElem.css('background-position','left '+yOffset);
  return true;
}

$(document).ready(function(){
  
  $('.fishingBottomFeeder').each(function(){
    $(this).find('.feedItem:first').show();
  });

  $('#weatherIcons td:not(.noBg)').css('background','#81d5e1');

  $('#weatherIcons td:even:not(.noBg)').css('background-color','#c5e1e5');
  $('#weatherIcons td:even:not(.noBg)').css('border-left-width','0px');
  $('#weatherIcons td:even:not(.noBg)').css('border-right-width','0px');

  $('#fishingNewsLinks .headline:last').css('border-bottom-width','0px');
});
function paginateFeed(section,page)
{
  if(typeof $(section) == 'object')
  {
    if(page == 1)
    {
      var oldItem = $(section).find('.feedItem:visible:first');
      if($(section).find('.feedItem:visible:last').next().length > 0)
      {
        var newItem = $(section).find('.feedItem:visible:last').next();
        oldItem.hide();
        newItem.show();
      }
      else
      {
        var newItem = $(section).find('.feedItem:first');
        oldItem.hide();
        newItem.show();
      }
    }
    else if(page == -1)
    {
      var oldItem = $(section).find('.feedItem:visible:last');
      if($(section).find('.feedItem:visible:first').prev().length > 0)
      {
        var newItem = $(section).find('.feedItem:visible:first').prev();
        oldItem.hide();
        newItem.show();
      }
      else
      {
        var newItem = $(section).find('.feedItem:last');
        oldItem.hide();
        newItem.show();
      }
    }
    else if(page >= 0)
    {
      page = Math.max(page-1,0);
      if($(section).find('.feedItem:eq('+page+')').length > 0)
      {
        var newItem = $(section).find('.feedItem:eq('+page+')');
        $(section).find('.feedItem').hide();
        newItem.show();
      }
    }
  }
}

function flagPhoto(id)
{
  if(typeof id == 'number')
  {
    $.post('/fishing/braggin_board/id.'+id,{'ajaxify':'yes','flag':'yes'});
  }
}



$('.legendBtn').hover(
  function(){ $(this).toggleClass('legendOver'); },
  function(){ $(this).toggleClass('legendOver'); }
);

function loadFishInfo(id)
{
  $('#mapLoading').show();
  $.post('/fishing/florida-fish-species',{'ajaxify':'true','fish':id},function(response){
    $('#mapLoading').hide();
    $.colorbox({html:response,innerWidth:600,innerHeight:500,onComplete:function(){$('#colorbox').css('display','block');}});
  });
  return false;
}

function chooseOptions(legend)
{
  if($('#legendOptionTab'+legend+':visible').length > 0)
  {
    $('#legendOptionTab'+legend).hide();
  }
  else
  {
    $('.legendOptionTab').hide();
    $('#legendOptionTab'+legend).show();
    $('#legendOptionTab'+legend).find('.legendOptionTabWrapper').isotope({itemSelector:'.mapOption'})
    resizeInnerContent();
  }
}

function addFilter(val)
{
  map.closeInfoWindow();
  if($('input#'+val).length > 0 && $('#activeFilters input#'+val).length == 0)
  {
    $('input#'+val).attr('checked','checked');
    setTimeout("resetOptions();",10);
    return false;
  }
}

function quickFilter(filterName)
{
  if($('#legendOptionTab'+filterName+' input:checkbox').length > 0)
  {
    $('.legendOptionTab input:checkbox').removeAttr('checked');
    $('#legendOptionTab'+filterName+' input:checkbox').attr('checked','checked');
    resetOptions();
  }
}

var jsonMarkers;

function resetOptions()
{
  $('#mapLoading').show();
  var aData = $('#legendOptionsForm').serialize();
  $('.mapOption input').attr('disabled','disabled');
  jQuery.post('/fishing/map',aData,function(data){
    markers = new Array();
    if(data.indexOf(':]}') == -1)
    {
      jsonMarkers = JSON.parse(data);
      jsonMarkers = jsonMarkers['markers'];
      for(i = 0; i < jsonMarkers.length; i++)
      {
        markers[i]= new GMarker(new GLatLng(jsonMarkers[i].latitude, jsonMarkers[i].longitude),{icon:window[jsonMarkers[i].icon],title:jsonMarkers[i].title});
        setTimeout('GEvent.addListener(markers['+i+'],"click",function(){ markers['+i+'].openInfoWindowHtml( \''+jsonMarkers[i].popup+'\', { onCloseFn: recenterMap } );});',10);
      }

    }
    $('#mapLoading').hide();
    setupMarkers();
    $('.mapOption input').removeAttr('disabled');
    resetActiveFilters();
  });
}

function resetActiveFilters()
{
  $('.activeFilter').hide();
  $('#activeFilters .mapOption').remove();
  $('.legendOptionTab').each(function(){
    if($(this).find('.mapOption:has(input:checked)').length > 0)
    {
      var activeFilter = '#'+$(this).attr('id')+'Active';
      $(activeFilter).html('');
      $(activeFilter).append($(this).find('.mapOption:has(input:checked)').clone());
      $(activeFilter).show();
    }
    $('#activeFilters .mapOption').css('position','static');
  });
  if($('#activeFilters input:checked').length > 0)
  {
    $('#quickFilters').hide();
    $('#activeFilters').show();
  }
  else
  {
    $('#activeFilters').hide();
    $('#quickFilters').show();
  }
  $('#activeFilters .mapOption').each(function(){$(this).removeAttr('style');});
}


$('#activeFilters input').live('click',function(){
  var id = '#'+$(this).attr('id');
  $(id).removeAttr('checked');
  resetOptions();
});

$('.resetFilters').live('click',function(){
 $('.legendOptionTab input:checkbox').removeAttr('checked');
 resetOptions();
});

resetActiveFilters();
