(function($) {
	$(document).ready(function() {
		$('div.content a').each(function() {
			if ($(this).hasClass('none') || !this.href) {
				return;
			}
			
			var href = this.href.replace(/http:\/\//, ''), url;
			href = href.split('/');
			href = href[0];
			
			$(this).parent().append('<i />');
			if ($(this).hasClass('from-site')) {
				url = 'http://' + href + '/favicon.ico';
			}
			else {
				url = 'http://favicon.yandex.net/favicon/' + href;
			}
			
			$(this).parent().find('i').css('background-image', 'url(' + url + ')');
		});
		
		$('#add-site span.inside').click(function() {
			$('#add-site-form').toggle();
			$('#site').focus();
		});

		function checkAddSite() {
			var disabled = false,
				site = $('#site').val();
			
			if ($.trim(site) == '') {
				disabled = true;
			}

			$('#submit')[0].disabled = disabled;
		}

		$('#site').bind('keypress keyup blur', checkAddSite);
		
		checkAddSite();
		$('#site, #comment').val('');
		
		$('#submit').click(function() {
			$(this)[0].disabled = true;
			$('#add-site-form').hide();
			$('p.send-ok').show();
			$.post('add-site.php?action=save', {site: $('#site').val(), comment: $('#comment').val()}, null);
			$('#site, #comment').val('');
		});
	});
})(jQuery);