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); 
                } 
            }); 
        }, 
        // Create new class for exstars functionality 
        voting: function(id, rating) 
        { 
            $.post(root+"exstars.php", {action: "vote", id: id, rating: rating}, function(result) { 
                if(result == 'already') 
                { 
                    //alert('{$lang.error_already_voted}'); 
                } 
                else 
                { 
                    $("#stars" + id).html(result); 
                } 
            }); 
        }, 
        /** 
        * 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;
			
			$("#dark").css("height", $("div.page").height());
			$(".move").hide().empty();	
			// Dialog Link
			html += '<div id="dialog_'+ idLink +'" title="' + esyndicat.lang.move_listing_title + '" class="myDialog">' + esyndicat.lang.listing_move_confirmation + '</div>';
			$('#move_' + idLink).append(html);

			$("#dark").fadeIn("slow");
			$('#dialog_' + idLink).dialog({
				resizable: false,
				width: 300,
				buttons: {
					"Da" : function() {
						
						$("#dark").fadeOut("slow");
						$(this).dialog("close");

						$(".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, 
							expandableLocked: true, 
							callback: function() 
							{
								var call = this;
								html += '<div id="dialog_fin_'+ idLink +'" title="' + esyndicat.lang.move_listing_title_confirmation + '" class="myDialog">' + esyndicat.lang.listing_fin_move_confirmation + '</div>';
								$('#move_fin_' + idLink).append(html);
								$("#dark").fadeIn("slow");	
								$('#dialog_fin_' + idLink).dialog({
									resizable: false,
									width: 300,
									buttons: {
										"Da": function() {

											$("#dark").fadeOut("slow");
											$(this).dialog("close");
												 
											var idCat = $(call).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'); 
											} 
										},
									   "Odustani": function() { 
											$(this).dialog("close");
											$("#dark").fadeOut("slow");
										//	return false;
										   	
										}	
									}
															
								});
								$("a.ui-dialog-titlebar-close").click(function() {
									$(this).dialog("close");
									$("#dark").fadeOut("slow");
								});
							}

							});
						moveTree.init();	
						},
					"Odustani": function() { 
						$(this).dialog("close");
						$("#dark").fadeOut("slow");
					//	return false;
				   }	
			
				}
        
			});
			$("a.ui-dialog-titlebar-close").click(function() {
				$(this).dialog("close");
				$("#dark").fadeOut("slow");
			});
		   	
		}
    } 
}(); 


