esyndicat.common = function()
{
	return {
		init: function()
		{
			var searchInput = $("#search_input").val(esyndicat.lang.search);

			$("a[@id^='lnk'], a[@id^='ctg']").each(function()
			{
				var info = $(this).attr('id').split('_');

				$(this).click(function()
				{
					i = new Image();
					h = Math.random().toString().split('.')[1];

					i.src = esyndicat.conf.base + esyndicat.conf.dir + 'counter.php?id='+ info[1] +'&item='+ info[0] +'&h='+h;
				});
			});

			searchInput.focus(function() 
			{
				if ($(this).val() == esyndicat.lang.search) 
				{
					$(this).val('');
				}
			}).blur(function() 
			{
				if ($(this).val() == '') 
				{
					$(this).val(esyndicat.lang.search);
				}
			});
		},
		/**
		* Opens Report Broken Listing window
		*
		* @param int id listing id
		*/
		reportBrokenListing: function(id, msg)
		{
			var answer = confirm(msg);

			if(answer)
			{
				$.get("report-listing.php", {id: id, report: 1}, function(data)
				{
					$("#tdlisting" + id).after(data);
				});
			}
		},
		actionFavorites: function(listing_id, account_id, action, refresh, remove_phrase, add_phrase)
		{
			var msg = ('add' == action) ? add_phrase : remove_phrase;
			var conf = confirm(msg);
			
			if (conf)
			{
				$.post("favorites.php", {account_id: account_id, listing_id: listing_id, action: action, refresh: refresh}, function(data)
				{
					$("#af_" + listing_id).text();
					$("#af_" + listing_id).html(data);
				});
			}

			return false;
		},
		moveListing: function(id)
		{
			var html = '';
			var idLink = id;

			if (!confirm(esyndicat.lang.listing_move_confirmation))
			{
				return false;
			}

			$(".moveto").hide().empty();
	
			html += '<fieldset style="collapsible"><legend><span id="change_category_text">'+ esyndicat.lang.move_listing +'</span></legend>';	
			html += '<div id="tree_'+ idLink +'" class="tree"></div>';
			html += '</fieldset>';

			$('#moveTo_' + idLink).append(html).show();
			
			var moveTree = new esyndicat.tree({
				id: 'tree_' + idLink,
				type: 'radio',
				state: '',
				hideRoot: false,
				callback: function()
				{
					if(!confirm(esyndicat.lang.listing_fin_move_confirmation))
					{
						return false;
					}
					
					var idCat = $(this).attr('id').split('_')[2];

					$.get('move-listings.php', {action: 'moving', idcat: idCat, idlink: idLink}, function(data)
					{
						$('#moveTo_' + idLink).html(data).show();
					});

					if($('#tdlisting' + idLink).attr('class') != 'listing approval')
					{
						$('#tdlisting' + idLink).attr('class', 'listing approval');
					}
				}
			});	

			moveTree.init();
		},
		removeListing: function(id, account)
		{
			if (!confirm('Do you really want to remove this listing?'))
			{
				return false;
			}

			$.post('remove-listings.php', {id: id, account: account}, function(data)
			{

				alert(data);

				window.location = window.location.href;
			});
		}
	}
}();
