$(function() {
  $('.header-nav a img').each(function() {
    $('<img/>').attr('src', $(this).attr('src').replace('.jpg', '-over.jpg'));
  });
  $('.header-nav a img').hover(function() {
    $(this).attr('src', $(this).attr('src').replace('.jpg', '-over.jpg'))
  }, function() {
    $(this).attr('src', $(this).attr('src').replace('-over', ''))
  })
  $('.members-profile-pic-thumbs img').click(function() {
    var clickedPic = $(this);
    if($(clickedPic).attr('alt') && $('#main-profile-pic').attr('src') != $(clickedPic).attr('alt')) {
      $('#main-profile-pic').fadeOut('slow', function() {
        $('#main-profile-pic').attr('src', $(clickedPic).attr('alt')).fadeIn('slow', function() {

        });
      });
    }
  });
  $('.btn-add-girl-fav').live('click', function() {
	  var tmp = $(this);
	  var tid = $(this).attr('id').replace('btn-add-fav-', '');
    $.get('/members/addFav', {id: $(this).attr('id').replace('btn-add-fav-', '')}, function(response) {
      if(response == 'success') {
		if(tmp.children('img:first').attr('src') != '/images/btn-favorite-me.png'){
			tmp.html('<img alt="Remove from Favorites" src="/images/icon-rmv-favorite.jpg">');
		}else{
			tmp.html('<img alt="Remove From Favorites" src="/images/btn-favorite-rmv.png">');
		}
		tmp.attr('id','btn-del-fav-'+tid);
		tmp.attr('class','btn-del-girl-fav');
		tmp.attr('title','Remove from Favorites');
        alert('Successfully added to your favorites');
        return;
      }
      if(response == 'failed') {
        alert('we failed to add to favorites');
        return;
      }
      if(response == 'dupe') {
        alert('Already in your favorites');
        return;
      }
      alert('You must be logged in if you want to add to your favorites');
    });
  });
  $('.btn-del-girl-fav').live('click', function() {
	  var tmp = $(this);
	  var tid = $(this).attr('id').replace('btn-del-fav-', '');
    $.get('/members/delFav', {id: $(this).attr('id').replace('btn-del-fav-', '')}, function(response) {
      if(response == 'success') {
		if(tmp.children('img:first').attr('src') != '/images/btn-favorite-rmv.png'){
			tmp.html('<img alt="Add as Favorite" src="/images/icon-add-favorite.jpg">');
		}else{
			tmp.html('<img alt="Favorite Me" src="/images/btn-favorite-me.png">');
		}
		
		tmp.attr('id','btn-add-fav-'+tid);
		tmp.attr('class','btn-add-girl-fav');
		tmp.attr('title','Add as Favorite');
        alert('Successfully removed from your favorites');
        return;
      }
      if(response == 'failed') {
        alert('we failed to remover from favorites');
        return;
      }
    });
  });
  $('.btn-add-member-fav').live('click', function() {
    $.get('/girls/addFav', {id: $(this).attr('id').replace('btn-add-fav-', '')}, function(response) {
      if(response == 'success') {
        alert('Successfully added to your favorites');
        return;
      }
      if(response == 'failed') {
        alert('we failed to add to favorites');
        return;
      }
      if(response == 'dupe') {
        alert('Already in your favorites');
        return;
      }
      alert('You must be logged in if you want to add to your favorites');
    });
  });
  $('.btn-del-member-fav').live('click', function() {
    $.get('/girls/delFav', {id: $(this).attr('id').replace('btn-del-fav-', '')}, function(response) {
      if(response == 'success') {
        alert('Successfully removed from your favorites');
        return;
      }
      if(response == 'failed') {
        alert('we failed to remover from favorites');
        return;
      }
    });
  });
  $('.btn-preview-dialog').click(function() {
    $('#preview-dialog').html('<img style="margin:0 auto; display:block;" src="'+$(this).attr('href')+'" />');
    $('#preview-dialog').dialog({modal: true, title: 'Preview Picture'});
    return false;
  });
  $('#btn-toggle-search').click(function() {
	if($("#girls-search-form").css("display") == "none"){
		$("#btn-toggle-search").text("Hide Advanced Search Options");
	}else{
		$("#btn-toggle-search").text("Advanced Search Options");	
	}
    $('#girls-search-form').slideToggle();
  });
  $('#btn-reset-pass').click(function() {
    $('.login-form').fadeOut(function() {
      $('.reset-form').fadeIn();
    });
  });
  $('#btn-login-now').click(function() {
    $('.reset-form').fadeOut(function() {
      $('.login-form').fadeIn();
    });
  });
  $('#member-signup-form').submit(function() {
    if(!$('#member_username').val()) {
      $('#member_username').focus();
      alert('Username is required');
      return false;
    }
    if(!$('#member_email').val()) {
      $('#member_email').focus();
      alert('Email is required');
      return false;
    }
    if(!$('#member_new_password').val()) {
      $('#member_new_password').focus();
      alert('Password is required');
      return false;
    }
    if($('#member_new_password').val() != $('#member_new_password_confirm').val()) {
      alert('Your passwords do not match');
      return false;
    }
    return true;
  });
});
