var favorites_working = false;
var history_working = false;
var comments_working = false;
var account_working = false;
var avatar_working = false;
var toy_working = false;

window.addEvent('domready', function() {
	myzamSetup();
});

function verifyEmail() {
	if (account_working == false) {
		account_working = true;
		$('verify_error').innerHTML = 'Working...';
		new Ajax.Request('/user/sendvemail', {
			parameters: {
				really: 'yap'
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					$('verify_error').innerHTML = res[1];
				} else {
					$('verify_error').innerHTML = res[1];
				}
				account_working = false;
			},
			onFailure: function(t) {
				account_working = false;
			}
		});
	}
}

function changeName() {
	if (account_working == false) {
		account_working = true;
		new Ajax.Request('/user/changename', {
			parameters: {
				newname: $('new_name').value
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					this.window.location.href = this.window.location.href;
				} else {
					$('name_error').innerHTML = res[1];
				}
				account_working = false;
			},
			onFailure: function(t) {
				account_working = false;
			}
		})
	}
}

function changeEmail() {
	if ($('new_email').value != $('new_email_v').value) {
		$('email_error').innerHTML = "Emails must match.";
		return;
	}

	if (account_working == false) {
		account_working = true;

		new Ajax.Request('/user/changeemail', {
			parameters: {
				newemail: $('new_email').value
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					this.window.location.href = this.window.location.href;
				} else {
					$('email_error').innerHTML = res[1];
				}
				account_working = false;
			},
			onFailure: function(t) {
				account_working = false;
			}
		})
		
	}
}

function updateEmailSetting(type) {
	if (account_working == false) {
		account_working = true;
		$(type+'_cb').disabled = true;
		$(type+'_error').innerHTML = 'Working...';

		var on = true;
		if ($(type+'_cb').checked) {
			on = '1';
		} else {
			on = '0';
		}

		new Ajax.Request('/user/changeemailprefs', {
			parameters: {
				type: type,
				on: on
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					$(type+'_error').innerHTML = 'Success.';
				} else {
					$(type+'_error').innerHTML = res[1];
				}
				$(type+'_cb').disabled = false;
				account_working = false;
			},
			onFailure: function(t) {
				account_working = false;
				$(type+'_cb').disabled = false;
			}
		})

	}
	
}

function clearHistory() {
	if (history_working == false) {
		history_working = true;
		new Ajax.Request('/history/clear', {
			parameters: {
				really: 'yap'
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					$('history_chunk').innerHTML = 'Your grab history has been cleared!';
				}
				history_working = false;
			},
			onFailure: function(t) {
				history_working = false;
			}
		})
	}
}

function switchAvatar(aid) {
	if (avatar_working == false) {
		avatar_working = true;
		new Ajax.Request('/avatar/switchavatar', {
			parameters: {
				avatar_id: aid
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					window.location.href = '/my';
				}
				avatar_working = false;
			},
			onFailure: function(t) {
				avatar_working = false;
			}
		})
	}
}

function useAvatar(a) {
	if (avatar_working == false) {
		avatar_working = true;
		new Ajax.Request('/my/useavatar', {
			parameters: {
				avatar: a
			},
			onSuccess: function(t) {
				var res = t.responseText.split(':');
				if (res[0] == '+') {
					window.location.href = '/my';
				}
				avatar_working = false;
			},
			onFailure: function(t) {
				avatar_working = false;
			}
		})
	}
}

function myzamSetup() {
	var com_del_buttons = $$('.com_del');
	for (var i = com_del_buttons.length - 1; i>= 0; i--) {
		com_del_buttons[i].addEvent('click', function() {
			if (comments_working == false) {
				comments_working = true;

				var cid = this.id.split('_')[1];
				var current_com = $('com_'+cid);

				new Request({
					url: '/comments/delete',
					onSuccess: function(t) {
						var res = t.split(':');
						if (res[0] == '+') {
							current_com.style.display = 'none';
						}
						comments_working = false;
					},
					onFailure: function(t) {
						comments_working = false;
					}
				}).send('id='+cid);
			}
		});
	}

	var fav_del_buttons = $$('.fav_del');
	for (var i = fav_del_buttons.length - 1; i>= 0; i--) {
		fav_del_buttons[i].addEvent('click', function() {
			if (favorites_working == false) {
				favorites_working = true;

				var cid = this.id.split('_')[1];
				var current_fav = $('fav_'+cid);

				new Request({
					url: '/favorites/delete',
					onSuccess: function(t) {
						var res = t.split(':');
						if (res[0] == '+') {
							current_fav.style.display = 'none';
						}
						favorites_working = false;
					},
					onFailure: function(t) {
						favorites_working = false;
					}
				}).send('id='+cid);
			}
		});
	}

	var his_del_buttons = $$('.his_del');
	for (var i = his_del_buttons.length - 1; i>= 0; i--) {
		his_del_buttons[i].addEvent('click', function() {
			if (history_working == false) {
				history_working = true;

				var cid = this.id.split('_')[1];
				var current_his = $('his_'+cid);

				new Request({
					url: '/history/delete',
					onSuccess: function(t) {
						var res = t.split(':');
						if (res[0] == '+') {
							current_his.style.display = 'none';
						}
						history_working = false;
					},
					onFailure: function(t) {
						history_working = false;
					}
				}).send('id='+cid);
			}
		});
	}
	
	var toy_del_buttons = $$('.toy_del');
	for (var i = toy_del_buttons.length - 1; i>= 0; i--) {
		toy_del_buttons[i].addEvent('click', function() {
			if (toy_working == false) {
				if (confirm('This will only remove the toy from this list. If you want to delete the toy from your profile, you must remove the code manually. Are you sure you want to remove the toy from this list?')) {
					toy_working = true;

					var cid = this.id.split('_')[1];
					var current_toy = $('toy_'+cid);

					new Request({
						url: '/toys/delete',
						onSuccess: function(t) {
							var res = t.split(':');
							if (res[0] == '+') {
								current_toy.style.display = 'none';
							}
							toy_working = false;
						},
						onFailure: function(t) {
							toy_working = false;
						}
					}).send('id='+cid);
				}
			}
		});
	}
}
