$(function()
{
	if ($.browser.msie)
	{
		try { document.execCommand('BackgroundImageCache', false, true); } catch (o){}
	}
        try
        {
                $('.registrujse input[name=username]').val('Upiši željeni nick').focus(function()
                {
                        if ($(this).val() == 'Upiši željeni nick') $(this).val('');
                })
                .blur(function()
                {
                        if ($(this).val() == '') $(this).val('Upiši željeni nick');
                });
        }
        catch (e){}

	$('a.need-login').each(function()
	{
		$(this).click(function()
		{
			alert('Morate biti ulogovani.');
		})
		.attr('href', 'javascript:void(null);');
	});

	$('#menubar>ul>li>a').each(function()
	{
		$(this).parent().prepend('<div class="ml"></div>');
		$(this).parent().append('<div class="mr" style="left:' + ($(this).width() + 8) + 'px"></div>');
		$(this).css({'padding-left':8, 'padding-right':8});

		$(this).hover
		(
			function()
			{
				$('.ml', $(this).parent()).addClass('hl');
				$('.mr', $(this).parent()).addClass('hr');
			},
			function()
			{
				$('.ml', $(this).parent()).removeClass('hl');
				$('.mr', $(this).parent()).removeClass('hr');
			}
		);
	});

	$('#menubar span').each(function()
	{
		$(this).parent().prepend('<div class="cl"></div>');
		$(this).parent().append('<div class="cr" style="left:' + ($(this).width() + 8) + 'px"></div>');
		$(this).css({'padding-left':8, 'padding-right':8});
	});

	if ($.browser.msie && $.browser.version < 7)
	{
		$('#menubar>ul>li>ul').each(function()
		{
			$(this).parent().hover
			(
				function() { $(this).addClass('iefix'); },
				function() { $(this).removeClass('iefix'); }
			)
		});
	}

	$('table.hover tr').hover
	(
		function() { $('td', this).addClass('hovered'); },
		function() { $('td', this).removeClass('hovered'); }
	);

	$('.breadcrumbs a').hover
	(
		function() { $(this).parent().parent().addClass('hovered'); $(this).parent().addClass('hovered'); },
		function() { $(this).parent().parent().removeClass('hovered'); $(this).parent().removeClass('hovered'); }
	);

	try
	{
		$('.file-upload').click(function()
		{
			$('input', $(this)).click();
			$('.file-upload-text', $(this).parent()).val($('input', $(this)).val());
		});

		$('.file-upload input').click(function()
		{
			$('.file-upload-text', $(this).parent().parent()).val($(this).val());
		});

		$('.file-upload-text').blur(function()
		{
			$('.file-upload input', $(this).parent()).val($(this).val());
		});

		$('.user-avatar .izbrisi').click(function()
		{
			$('.form-action').val('delete').parent().submit();
		});

		$('.user-avatar .sacuvaj').click(function()
		{
			if ($('#picture', $(this).parent().parent()).val().length)
			{
				$('#form-action').val('file').parent().submit();
			}
			else if ($('#link', $(this).parent().parent()).val().length)
			{
				$('#form-action').val('url').parent().submit();
			}
			else
			{
				alert('Morate izabrati fajl sa Vašeg računara, ili uneti direktan link do slike.');
			}
		});

		$('#frm_listici .sacuvaj').click(function()
		{
			if ($('#frm_listici .picture').val().length == 0 || $('#frm_listici #title').val().length == 0)
			{
				alert('Morate izabrati fajl sa Vašeg računara i uneti naziv slike.');
			}
			else
			{
				$('#frm_listici').submit();
			}
		});

		$('#frm_slike .sacuvaj').click(function()
		{
			if ($('#frm_slike .picture').val().length == 0 || $('#frm_slike #title').val().length == 0)
			{
				alert('Morate izabrati fajl sa Vašeg računara i uneti naziv slike.');
			}
			else
			{
				$('#frm_slike').submit();
			}
		});
	}
	catch (o) {}

	try
	{
		$('.blog-table .delete-button a').click(function()
		{
			if (confirm('Da li ste sigurni da želite da izbrišete ovaj blog?'))
			{
				var $tr = $(this).parent().parent();

				$.ajax(
				{
					type: 'GET',
					url: '/korisnici/' + $('#korisnicko_ime').html() + '/blog/' + $(this).parent().attr('id').substr(5) + '/delete',
					dataType: 'json',
					cache: false,
					data: { o: 0 },

					success: function()
					{
						$tr.remove();
					},

					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.');
					}
				});
			}
		});
	}
	catch (o) {}

	try
	{
		$('.tickets-table .delete-button a').click(function()
		{
			if (confirm('Da li ste sigurni da želite da izbrišete ovaj listić?'))
			{
				var $tr = $(this).parent().parent();

				$.ajax(
				{
					type: 'GET',
					url: '/korisnici/' + $('#korisnicko_ime').html() + '/tiketi/' + $(this).parent().attr('id').substr(5) + '/delete',
					dataType: 'json',
					cache: false,
					data: { o: 0 },

					success: function(data)
					{
						if (data.result == 0)
						{
							alert('Došlo je do greške prilikom brisanja listića. Pokušajte ponovo.');
						}
						else if (data.result == 2)
						{
							alert('Ovaj listić sadrži tipove koji su prošli. Nije moguće obrisati listić');
						}
						else
						{
							$tr.remove();
						}
					},

					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.');
					}
				});
			}
		});
	}
	catch (o) {}

	try
	{
		var pos = $('.findbox-control').offset();

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

		$('.findbox-control').click(function()
		{
			$('.findbox').toggle();
		});

		$('.findbox button').click(function()
		{
			// do something...
		});

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

	}
	catch (o){}

	try
	{
		$('.infobar').hover
		(
			function() { $(this).addClass('infobar-hovered'); },
			function() { $(this).removeClass('infobar-hovered'); }
		)
		.slideDown('slow')
		.click(function()
		{
			window.location.href = $('.infobar .target-url').attr('href');
		});

		$('.close-icon a').click(function(e)
		{
			e.stopPropagation();
			$('.infobar').hide();
		});
	}
	catch (e){}


	try
	{
		$('.dark-block .favorites').click(function()
		{
			addBookmark('SuperDobitak', 'http://www.superdobitak.com');
		});

		$('.dark-block .home').click(function()
		{
			setHomePage('http://www.superdobitak.com');
		});
	}
	catch (e){}

	try
	{
		$('.deleteable .friend').each(function()
		{
			$('<a href="javascript:void(null)" title="Izbriši" class="delete-pic"></a>').css(
			{
				width: '19px',
				height: '18px',
				background: 'url(img/delete.gif) 0 -18px no-repeat',
				position: 'absolute',
				left: '40px',
				top: '40px',
				display: 'none'
			})

			.prependTo($(this).css('position', 'relative'))

			.click(function()
			{
				if (!confirm('Da li ste sigurni da želite da izbrišete fajl?')) return;

				$(this).parent().addClass('active-picbox');

				$.ajax(
				{
					type: 'POST',
					url: this.requestUrl,
					dataType: 'json',
					cache: false,
					data: { o: 0 },

					success: function(data)
					{
						if (data.result)
						{
							$('.active-picbox').remove();
						}
						else
						{
							alert('Došslo je do greške prilikom brisanja fajla.\nPokušajte kasnije.');
						}
					},

					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.');
					}
				});
			})

			[0].requestUrl = $('a', $(this)).not('.delete-pic').attr('href') + '/delete?output=json';


			$(this).hover
			(
				function() { $('.delete-pic', this).show(); },
				function() { $('.delete-pic', this).hide(); }
			);
		});
	}
	catch (e){}

});

function whoisonline()
{
	$.ajax(
	{
		type: 'POST',
		url: '/korisnici/online?output=json',
		dataType: 'json',
		cache: false,
		data: { o: 0 },

		success: function(data)
		{
			var html = '';

			for (i = 0; i < data.length; i++)
			{
				html += '<div class="friend">';
				html += "<img src=\"" + data[i].avatar + "\" alt=\"" + data[i].name + "\" height=\"56\" width=\"56\" />";
				html += "<a href=\"" + data[i].url + "\">" + data[i].name + "</a> ";
				html += '</div>';
			}

			$('.whosonline').html(html);
		}
	});
}

setInterval('whoisonline()', 60 * 1000);

function addBookmark(title, url)
{
	try
	{
		if ($.browser.mozilla)
		{
			window.sidebar.addPanel(title, url, url);
		}
		else if ($.browser.opera)
		{
			$('<a href="' + url + '" title="' + title + '" rel="sidebar"/>')
			.appendTo('body').hide().click().remove();
		}
		else if ($.browser.msie)
		{
			window.external.AddFavorite(url, title);
		}
	}
	catch (e) {}
}

function setHomePage(url)
{
	try
	{
		$('<a href="#" onclick="this.style.behavior = \'url(#default#homepage)\'; this.setHomePage(\'' + url + '\');"/>')
		.appendTo('body').hide().click().remove();
	}
	catch (e){}
}