function editToy(toy_id) {
  data = {toy_id: toy_id}
  $.getJSON('/toys/edit_toy', data, function(data) {
    changeContent(data.content, data.keywords);
  })
}

var alreadyRated = [];
function rateToy(toy_id, rating) {
  if (alreadyRated.indexOf(toy_id) != -1) {/*console.log("already rated js");*/return;}
  if (typeof rating == "number") {
    $.postJSON('/toys/rate_toy', {toy_id: toy_id, rating: rating}, function (data) {
      alreadyRated.push(toy_id);
      if (data.success) {
        rating = Number(data.content[0]);
        total_votes = Number(data.content[1]);
        width = Math.round(((rating/5.0) * 150)*1000)/1000;
        $('#ratingblock_'+toy_id+' .current-rating').css('width', String(width) + "px");
        $('#ratingblock_'+toy_id+' .current-rating').text('Currently ' + String((Math.round(rating*100)/100)) + '/5');
        $('#ratingblock_'+toy_id+' p').html('Rating: <strong>'+String((Math.round(rating*10)/10)) + '</strong>/5 ('+total_votes+' votes cast)');
      } else {
        // console.log(data.error);
      }
    })
  }
}

function reloadThumbPreview() {
  url = $('#toy_thumb_url').val();
  $('#toy_thumb_preview').attr('src', url);
}

function reloadThumbPreview20() {
  url = $('#toy_thumb_url20').val();
  $('#toy_thumb_preview20').attr('src', url);
}

function deleteCategory(cat_id) {
  el = $('#cat_'+cat_id);
  el.attr('delete','true');
  el.attr('style', 'color:red;');
  el.find('a').replaceWith("<a href='#' onclick='undeleteCategory("+cat_id+");return false;'>(undo)</a>");
}

function undeleteCategory(cat_id) {
  el = $('#cat_'+cat_id);
  el.attr('delete','true');
  el.attr('style', '');
  el.find('a').replaceWith("<a href='#' onclick='deleteCategory("+cat_id+");return false;'>(remove)</a>");
}

function showExistingCategories() {
  $("#_existing_categories").show();
  $("#_add_another_link").hide();
}

function saveExistingCategory() {
  cat_id = $('#_existing_categories select').val();
  cat_name = $('#_existing_categories select option[value='+cat_id+']').text();
  if ($('#cat_'+cat_id).length == 0) {
      $("#categories_to_add").append("<div id='cat_"+cat_id+"' cat_id="+cat_id+" style='color:green;'><b>"+cat_name+" <a href='#' onclick='$(this).parent().remove(); return false;'>(remove)</a></div>");
  }
  $("#_existing_categories").hide();
  $("#_add_another_link").show();
}

function cancelExistingCategory() {
  $("#_existing_categories").hide();
  $("#_add_another_link").show();
}

function saveEditToy() {
  toy_name = $('#toy_name').val();
  toy_id = $('#toy_id').text();
  toy_thumb_url = $('#toy_thumb_url').val();
  toy_thumb_url20 = $('#toy_thumb_url20').val();
	toy_type = $('#toy_type').val();
	toy_css = $('#toy_css').val();
	toy_css20 = $('#toy_css20').val();
	toy_keywords_str = $('#toy_keywords').val();
	cats_to_delete_str = "";
	cats_to_delete = $('#toy_categories div[delete=true]');
	cats_to_delete.each(function(i, el) {
		cats_to_delete_str += $(el).attr('cat_id');
		if (i+1 < cats_to_delete.size()) {
			cats_to_delete_str += ",";
		}
	});
	cats_to_add_str = ""
	cats_to_add = $('#categories_to_add div');
	cats_to_add.each(function(i, el) {
		cats_to_add_str += $(el).attr('cat_id');
		if (i+1 < cats_to_add.size()) {
			cats_to_add_str += ",";
		}
	});
	if (toy_name != "" &&
			toy_id != "" &&
			(toy_thumb_url != "" || toy_thumb_url20 != "") &&
			toy_type != "" &&
			(toy_css != "" || toy_css20 != "") &&
			toy_keywords_str != "") {
		  data = { toy_name: toy_name,
						 toy_id: toy_id,
						 toy_thumb_url: toy_thumb_url,
						 toy_thumb_url20: toy_thumb_url20,
						 toy_type: toy_type,
						 toy_css: toy_css,
						 toy_css20: toy_css20,
						 toy_keywords_str: toy_keywords_str,
						 cats_to_delete_str: cats_to_delete_str,
						 cats_to_add_str: cats_to_add_str };
		$.postJSON("/toys/update_toy", data, function(data) {
			if (data.success) {
				$('#success').html('Toy saved!');
			} else {
				$('#error').html(formatErrors('Toy didn\'t save: ', data.error));
			}
		})
	} else {
		error_msg = "Toy didn't save: <br /><ul>";
		error_msg +=
			((toy_name == "") ? "<li>Name can't be blank</li>" : "") +
			((toy_id == "") ? "<li>ID can't be blank, why are you even messing with it?</li>" : "") +
			((toy_thumb_url == "" && toy_thumb_url20) ? "<li>Thumbnail Location and Thumbnail Location (2.0) can't both be blank</li>" : "") +
			((toy_type == "") ? "<li>Toy Type can't be blank</li>" : "") +
			((toy_css == "" && toy_css20 == "") ? "<li>CSS and CSS (2.0) can't both be blank</li>" : "") +
			((toy_keywords_str == "") ? "<li>Keywords can't be blank. If you want to be lazy, just put 'all' as the only keyword</li>" : "");
		error_msg += "</ul>";
		$('#error').html(error_msg);
	}
}

function saveNewToy() {
  toy_name = $('#toy_name').val();
  toy_thumb_url = $('#toy_thumb_url').val();
	toy_thumb_url20 = $('#toy_thumb_url20').val();
  toy_type = $('#toy_type').val();
	toy_css = $('#toy_css').val();
	toy_css20 = $('#toy_css20').val();
  toy_keywords_str = $('#toy_keywords').val();
	cats_to_add_str = ""
	cats_to_add = $('#categories_to_add div');
	cats_to_add.each(function(i, el) {
		cats_to_add_str += $(el).attr('cat_id');
		if (i+1 < cats_to_add.size()) {
			cats_to_add_str += ",";
		}
	});
	if (toy_name != "" &&
			(toy_thumb_url != "" || toy_thumb_url20 != "") &&
			toy_type != "" &&
			(toy_css != "" || toy_css20 != "") &&
			toy_keywords_str != "") {
		  data = { toy_name: toy_name,
						 toy_thumb_url: toy_thumb_url,
 						 toy_thumb_url20: toy_thumb_url20,
 						 toy_type: toy_type,
 						 toy_css: toy_css,
 						 toy_css20: toy_css20,
						 toy_keywords_str: toy_keywords_str,
						 cats_to_add_str: cats_to_add_str };
		$.postJSON("/toys/save_toy", data, function(data) {
			if (data.success) {
				$('#success').html('Toy saved!');
			} else {
				$('#error').html(formatErrors('Toy didn\'t save: ', data.error));
			}
		})
	} else {
		error_msg = "Toy didn't save: <br /><ul>";
		error_msg +=
			((toy_name == "") ? "<li>Name can't be blank</li>" : "") +
			((toy_thumb_url == "" && toy_thumb_url20 == "") ? "<li>Thumbnail Location and Thumbnail Location (2.0) can't both be blank</li>" : "") +
			((toy_type == "") ? "<li>Toy Type can't be blank</li>" : "") +
			((toy_css == "" && toy_css20 == "") ? "<li>CSS and CSS (2.0) can't both be blank</li>" : "") +
			((toy_keywords_str == "") ? "<li>Keywords can't be blank. If you want to be lazy, just put 'all' as the only keyword</li>" : "");
		error_msg += "</ul>";
		$('#error').html(error_msg);
	}
}