
$(function()
{
	/* <loginbox> */

	var pos = $('.loginbox-control').offset();

	$('.loginbox').css(
	{
		top: pos.top + 60,
		left: pos.left - 140
	});

	$('.loginbox-control').click(function()
	{
		if ($(this).html() != 'Login') return;

		$('.loginbox').toggle();
	});

	$('.loginbox .close').click(function()
	{
		$('.loginbox').hide();
	});

	$('.loginbox button').click(function()
	{

		var val_username = $('.loginbox #username').val();
		var val_password = $('.loginbox #password').val();

		if (!(val_username.length && val_password.length))
		{
			alert('Morate popuniti oba polja.');
			return;
		}

		$.ajax(
		{
			type: 'POST',
			url: '/korisnici/login?output=json',
			dataType: 'json',
			cache: false,
			data:
			{
				username: val_username,
				password: val_password,
				remember: $('.loginbox-remember-box').val()
			},

			success: function(data)
			{
				if (data.result)
				{
					var reloads = [ function(){window.location.reload();}, function(){history.go(0);}, function(){window.location.href=window.location.href;} ];

					for (var i = 0; i < reloads.length; i++)
					{
						try
						{
							reloads[i]();
							break;
						}
						catch (o)
						{
							if (i == reloads.length - 1)
							{
								alert('Vaš browser ne podržava automatsko osvežavanje strane.\nMolimo Vas osvežite stranu ručno da biste bili ulogovani.');
							}
						}
					}
				}
				else
				{
					alert('Pogrešno korisničko ime i/ili šifra.');
				}
			},

			error: function()
			{
				alert('Došlo je do greške prilikom kontaktiranja servera.\nServer je možda privremeno nedostupan.\n\nPokušajte malo kasnije.');
			}
		});
	});

	/* </loginbox> */


	/* <regmebox> */

	$('.regmebox').css(
	{
		top: pos.top + 60,
		left: pos.left - 440
	});

	$('.register-control').click(function()
	{
		if ($('.regmebox').is(':hidden'))
		{
			$('#reg_user').css('font-style', 'italic').val('Provjeri dostupnost');
		}

		$('.regmebox').toggle();
	});

	$('.regmebox .close').click(function()
	{
		$('.regmebox').hide();
	});

	$('#reg_user').blur(function()
	{
		if ($(this).val().length)
		{
			$.getJSON('/korisnici/new?output=json', { username: $(this).val() }, function(data)
			{
				if (data.result)
				{
					$('.regmebox-icon').attr('class', 'regmebox-icon avaialable');
				}
				else
				{
					$('.regmebox-icon').attr('class', 'regmebox-icon unavaialable');
				}
			});
		}
		else
		{
			$(this).css('font-style', 'italic').val('Provjeri dostupnost');
			$('.regmebox-icon').attr('class', 'regmebox-icon');
		}
	})
	.focus(function()
	{
		if ($(this).val() == 'Provjeri dostupnost')
		{
			$(this).css('font-style', 'normal').val('');
		}
	});

	$('.regmebox button').click(function()
	{
		var doSubmit = true;

		$('input', $(this).parent()).each(function()
		{
			if (!$(this).val().length)
			{
				alert('Morate popuniti sva polja.');
				$(this).focus();
				return doSubmit = false;
			}
		});

		if (doSubmit)
		{
			if ($('.regmebox-icon').hasClass('unavaialable'))
			{
				alert('Izabrano korisničko ime je zauzeto. Pokušajte neko drugo.');
				return $('#reg_user').focus();
			}

			if ($('#reg_pass').val() != $('#reg_pass2').val())
			{
				alert('Ponovljena lozinka se ne poklapa sa unetom lozinkom.');
				return $('#reg_pass').focus();
			}

			if (!$('#reg_email').val().match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i))
			{
				alert('E-mail adresa koju ste uneli nije u odgovarajućem formatu.');
				return $('#reg_email').focus();
			}

			$.ajax(
			{
				type: 'POST',
				url: '/korisnici/new?output=json',
				dataType: 'json',
				cache: false,
				data:
				{
					username: $('#reg_user').val(),
					password: $('#reg_pass').val(),
					password_confirm: $('#reg_pass2').val(),
					email: $('#reg_email').val()
				},

				success: function(data)
				{
					if (data.success)
					{
						alert('Registracija je uspešno poslata. Poslali smo Vam e-mail sa uputstvom kako da završite proces registracije.\n\nHvala!');
						$('.regmebox').hide();
					}
					else
					{
						switch (data.error)
						{
							case 1:
								alert('Izabrano korisničko ime je zauzeto. Pokušajte neko drugo.');
								return $('#reg_user').focus();

							case 2:
								alert('Ponovljena lozinka se ne poklapa sa unetom lozinkom.');
								return $('#reg_pass').focus();

							case 3:
								alert('E-mail adresa koju ste uneli nije u odgovarajućem formatu.');
								return $('#reg_email').focus();

							default:
								alert('Vaša registracija nažalost nije bila uspešna. Pokušajte ponovo.');
						}
					}
				},

				error: function()
				{
					alert('Došlo je do greške prilikom kontaktiranja servera.\nMoguće je da server nije tenutno dostupan. Molimo Vas pokušajte kasnije.');
				}
			});
		}
	});

	/* </regmebox> */



	/* <editbox> */

	$('.editbox').css(
	{
		top: pos.top + 60,
		left: pos.left - 770
	})
	.each(function() { this.current = null; });

	if ($.browser.msie) $('.editbox select').height(18);

	$('table.bet-table td.edit a').click(function()
	{
		var editbox = $('.editbox')[0];

		if (editbox.current && editbox.current == this)
		{
			$(editbox).hide();

			editbox.current = null;
		}
		else
		{
			var pos = $(this).offset();

			$(editbox).css('top', pos.top + 30);

			$('.editbox-label', editbox).text($('.ltd', $(this).parent().parent()).text());

			var td_value = $('.value', $(this).parent().parent());
			var $value = td_value.text();
			var data = {};

			if (td_value[0].visibleTo)
			{
				data.visible = td_value[0].visibleTo;
			}
			else
			{
				data = td_value.metadata();
			}

			if (data && data.visible)
			{
				$('.option-' + data.visible, editbox).attr('checked', 'checked');
			}
			else
			{
				$('.option-all', editbox).attr('checked', 'checked');
			}


			if ($(this).is('.select-option'))
			{
				var option = $(this).metadata();
				var selectHtml = '';

				for (value in option)
				{
					selectHtml += '<option value="' + value + '">' + option[value] + '</option>';
				}

				$('.editbox-edit', editbox).hide();
				$('select', editbox).html(selectHtml).val($value).show();
			}
			else
			{
				$('select', editbox).hide();
				$('.editbox-edit', editbox).show().val($value);
			}

			if ($(this).is('.visibility'))
			{
				$('p, .option-all, .label-all, .option-friends, .label-friends', editbox).show();
			}
			else
			{
				$('p, .option-all, .label-all, .option-friends, .label-friends', editbox).hide();
			}

			$(editbox).show();

			editbox.current = this;
		}
	});

	$('.editbox button').click(function()
	{
		var editbox = $('.editbox');

		if (editbox[0].current)
		{
			var newval;
			var sendval;
			var visible = $(':radio:checked', editbox).metadata().value;
			var fieldname = $('.editbox-label', editbox).html();

			if ($(editbox[0].current).is('.select-option'))
			{
				newval = $('option:selected', editbox).html();
				sendval = $('option:selected', editbox).attr('value');
			}
			else
			{
				newval = $('.editbox-edit', editbox).val();

				/*if (fieldname.match(/datum|date/i)) {

					if (!newval.match(/^(\d{2}\s*[\.\\\/-]\s*){2}\d{4}\.?\s*$/)) {

						alert('Datum mora biti u formatu DD.MM.YYYY');
						return;
					}
				}*/

				sendval = makeURLClickable(newval);
			}

			if (!newval.length) newval = '&nbsp;';

			$('.value', $(editbox[0].current).parent().parent()).html(newval)[0].visibleTo = visible;

			$.ajax(
			{
				type: 'POST',
				url: '/korisnici/' + $('#korisnicko_ime').html() + '/edit?output=json',
				dataType: 'json',
				cache: false,
				data:
				{
					field: fieldname,
					value: sendval,
					visible: visible
				},

				error: function()
				{
					alert('Došlo je do greške prilikom kontaktiranja servera.\nMoguće je da server nije tenutno dostupan. Molimo Vas pokušajte kasnije.');
				}
			});

			editbox.hide();
		}
	});

	$('.editbox .close').click(function()
	{
		$('.editbox').hide()[0].current = null;
	});

	function makeURLClickable(input)
	{
		if (input.match(/(http:\/\/|ftp:\/\/)([^\s,]*)/i))
		{
			return input.replace(/(http:\/\/|ftp:\/\/)([^\s,]*)/i, '<a href="$1$2" target="_blank">$1$2</a>');
		}
		else
		{
			return input.replace(/(www)([^\s,]*)/i, '<a href="http://$1$2" target="_blank">$1$2</a>');
		}
	}

	/* </editbox> */


	/* <commentbox> */

	if ($('.post-comment').length)
	{
		var pos = $('.post-comment').offset();
	}

	$('.commentbox').css(
	{
		top: pos.top + 60,
		left: pos.left - 300
	});

	$('.post-comment').click(function()
	{
		if ($('.post-comment').is('.login-needed'))
		{
			alert('Da mi poslali komentar morate biti ulogovani.');
			return;
		}

		$('.commentbox').toggle();
	});

	$('.commentbox .close').click(function()
	{
		$('.commentbox').hide();
	});

	$('textarea', $('.commentbox')).each(function()
	{
		$(this).attr('hint', $(this).val())

		.focus(function()
		{
			if ($(this).val() == $(this).attr('hint'))
			{
				$(this).val('');
			}
		})

		.blur(function()
		{
			if (!$(this).val().length)
			{
				$(this).val($(this).attr('hint'));
			}
		});
	});

	$('button', $('.commentbox')).click(function()
	{
		var doSubmit = true;

		$('textarea', $('.commentbox')).each(function()
		{
			if ($(this).val().length == 0 || $(this).val() == $(this).attr('hint'))
			{
				alert('Morate uneti ' + $(this).attr('hint'));
				$(this).focus();
				return doSubmit = false;
			}
		});

		if (doSubmit) $(this).parent().submit();
	});

	/* </commentbox> */
});
