jQuery.fn.indicate = function () {
    this.attr('oldbg',this.css('background-color'));
    this.animate({backgroundColor:'#fbe3e4',
		borderLeftColor: 'red',
		borderRightColor: 'red',
		borderBottomColor: 'red',
		borderTopColor: 'red'
		}, 750);
    return false;
}
jQuery.fn.unindicate = function () {
    var color = this.attr('oldbg');
    this.animate({backgroundColor:color,
		borderLeftColor:color,
		borderRightColor:color,
		borderBottomColor:color,
		borderTopColor:color
		}, 750);
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function() {
  $('#feedback-tab, #feedback-cancel').click(function() {
    var div = $('#feedback');
    var form = $('#feedback-form');
    var tab = $('#feedback-tab');    
    tab.css('display') == 'none'? tab.css('display','inline'):tab.css('display','none');
    div.animate({
	    left: parseInt(div.css('left'),10) == 0 ? 0-(form.outerWidth()):0
    });
    
    return false;
  });

  $('.draggable').draggable({ containment: '#player-list', 
	                      scroll: false });
  $('.team-side').droppable({
	  drop: function(event, ui) {
	      var top = parseInt(ui.position.top);
	      var side = $(this).attr('id');
	      var side_one_height = $('#side-one').height();
	      var half_person = ui.helper.height()/2;
		  
	      if (top < 0 - half_person - 2)
		  top = side_one_height + top;
	      else if (side == 'side-two' && (top + ui.helper.height()/2+2) > side_one_height)
		  top = top - side_one_height;
	      
	      var url = ui.helper.attr('id') + 
		  '/' + side +
		  '/' + ui.position.left + 
		  '/' + top;
	      var response = $.post(url);
	  },
	  over: function(event, ui) {
	      $(this).attr('id') == 'side-one' ? 
		  ui.helper.attr('class', 'reds') : 
		  ui.helper.attr('class', 'blues');
	      
	  }
	      });

  $('#show-reminder').click(function() {
	  $('#next-reminder').fadeIn('slow');
	  
	  return false;
      });
  $('#leave-squad').click(function() {
	  var leave = window.confirm("Are you sure you want to leave this squad?");
	  if (leave)
	      return true;
	  else
	      return false;
      });

 
  $('#invite').click(function() {
	  $('#invite-form').fadeIn('slow');
  });
  $('#invite-form-cancel').click(function() {
	  $('#invite-form').fadeOut('slow');
	  $('#id_invitees').val('');
	  $('#invite-form form').submit();
  });

  $('.indicator').click(function() {
	  var id = this.id;
	  id = '#' + id.replace('show-', '');
	  var scrollto = $(id).offset().top;
	  $("html:not(:animated),body:not(:animated)")
	      .animate({ scrollTop:scrollto-500}, 1000 );	      
	  $(id).indicate();
	  $(id).unindicate();	  
	  return false;
      });
  $('.next').click(function() {
	  var id = this.id;
	  id = '#' + id.replace('show-', '');
	  $(this).parent().parent().hide('slow');
	  $(id).show('slow');
	  for (n=1; n<4; n++)
	      $(id).children('.indicator').click()
	  return false;
      });

  $('#help-tour').css('display', 'block');
  $('#slideshow').click(function() {
	  var src = $('#slide').attr('src');
	  var count = parseInt($('#slide').attr('count'));
	  if (count == 6) {
	      document.location.href = "/account/register/";
	  } else if (count == 5) {
	      $('#slide').parent().css('cursor','default');
	      $('#slide').parent().css('outline','none');
	  }
		  
	  $('#slide').attr('count', count+1);
	  var newsrc = src.replace(count, parseInt(count)+1);
	  $('#slide').attr('src', newsrc);
	  $('#slide-text-' + parseInt(count-1)).css('display', 'none');
	  $('#slide-text-' + parseInt(count)).css('display', 'block');	  
	  return false;
  });

});


