var is_featured = 0;
var dir_id = '';
var sort_by_change = 'pdl.location';
var g_keyword = '';
var firstcall=1;
var sort_by='pdl.location';
function dirlist(dir_id,page,sort_by,keyword)
{
    sort_by_change = sort_by;
	if(keyword == undefined)
	{
	    g_keyword = '';
	}
	else
	{
		g_keyword=keyword;
	}
    $.ajax({
		   type: 'POST',
		   url: root+"dir_list_detail/featuredBusinessPost",
		   data: "method=dir_list&dir_id="+dir_id+"&page="+page+"&sort_by="+sort_by+"&keyword="+g_keyword,
		   dataType: 'json',
	   success: function(data){


var result ='';
		if(firstcall==1)
		{
			initPagination(data.total);
			firstcall=2;
		}
		else
		{
			
			   if(data.rows != '')
			   {
               $.each(
					data.rows,
				   function(i,row)
				   { 
			   
				   if(i==0 )
					{
					      if(is_featured == 1)
					      {
                                result+='<div id="body_content" style="padding:10px 0 30px 5px;border:none"><div class="author" style="margin:0px 0px 10px 0px">Featured Business</div>';
					      }
					      else
					      {	
							if(g_keyword == '')
                                result+='<div id="body_content" style="padding:10px 0 30px 5px;border:none"><div class="author" style="margin:0px 0px 10px 0px">Listing of '+row.cell[13]+' <span>( '+data.total+' Listings found )</span></div>';
							else
								result+='<div id="body_content" style="padding:10px 0 30px 5px;border:none"><div class="author" style="margin:0px 0px 10px 0px">Search results for "'+g_keyword+'" <span>( '+data.total+' Listings found )</span></div>';
					      }
					      result+='<div style="float:left;width:355px;margin-top:10px"><div style="float:left;width:auto;"><input type="text" name="keyword" id="keyword"></div><div style="height:25px; float:left; padding-right:3px;width:100px;"><div style="float:none;"><div id="btnLeft"></div><div class="button" style="float:left"><input name="Button" id="contact_agent" class="button" type="submit" value="Search" onclick="search_dirlist('+dir_id+')" style="margin-top:-4px" /></div><div id="btnRight"></div></div></div></div>';
						  result+='<div style="float:left;margin-top:10px">Sort By: ';
						  result+='<select onchange=dirlist("'+dir_id+'",1,this.value)><option value="all">All</option>';
						  if(sort_by == 'users.username') result+='<option value="users.username" selected>Name</option>';
						  else result+='<option value="users.username" >Name</option>';

						  if(sort_by == 'state.state') result+='<option value="state.state" selected>Location</option>';
						  else result+='<option value="state.state" >Location</option>';
						  result+='</select></div>';
					}
				   });
			   }
				else
				{
					result+='<div id="body_content" style="padding:10px 0 30px 5px;border:none"><div class="author" style="margin:0px 0px 10px 0px">No listings found in this category.</div>';
					result+='<div style="float:left;width:355px;margin-top:10px"><div style="float:left;width:auto;"><input type="text" name="keyword" id="keyword"></div><div style="height:25px; float:left; padding-right:3px;width:100px;"><div style="float:none;"><div id="btnLeft"></div><div class="button" style="float:left"><input name="Button" id="contact_agent" class="button" type="submit" value="Search" onclick="search_dirlist('+dir_id+')" style="margin-top:-4px" /></div><div id="btnRight"></div></div></div></div>';
						  result+='<div style="float:left;margin-top:10px">Sort By: ';
						  result+='<select onchange=sort_dirlist("'+dir_id+'",1,this.value)><option value="all">All</option>';
						  if(sort_by == 'users.username') result+='<option value="users.username" selected>Name</option>';
						  else result+='<option value="users.username" >Name</option>';

						  if(sort_by == 'state.state') result+='<option value="state.state" selected>Location</option>';
						  else result+='<option value="state.state" >Location</option>';
						  result+='</select></div>';
				}
				   

				$.each(
				data.rows,
			   function(i,row)
			   {     
							
                             //result += '<div class="listingstyle"> <div id="imagestyle"><img src="'+root+row.cell[5]+'" alt="pic1" height="76px" width="82px"/></div><div id="content">'+row.cell[0]+','+row.cell[1]+','+row.cell[2]+','+row.cell[3]+'<br>'+row.cell[4]+'<div class="price">N '+row.cell[6]+'</div></div><div id="morepart"><img src="'+root+'css/default/images/arrowformore.jpg" alt="arrow" />&nbsp;<a href="'+root+'property/view/'+row.id+'">More</a></div></div>';  
	if(row.cell[1] != 1)
	{
		result += '<div class="listingstyle"><div id="content" style="width:25%">'+row.cell[2]+'<br>';
		if(row.cell[0] == 1 || row.cell[0] == 0)
		{
			result += '<div id="morepart" style="float:none;"><a href="'+row.cell[3]+'" target="_blank">'+row.cell[3]+'</a></div><br>';
		}
		else
		{
			result += row.cell[3]+'<br>';
		}
		if(row.cell[4] != '')
		{
			result += row.cell[4];
			if(row.cell[5] != '')
			{
				result +=', '+row.cell[5];
			}
		}
		else if(row.cell[5] != '')
		{
			result += row.cell[5];			
		}

		result +='<br>'+row.cell[6]+'<br/><strong>Our Services:</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+row.cell[15].replace(/,/g,"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")+'<br/><a style="color:grey;font-family:tahoma;font-size:10px ;" href="#" onclick="change_form(\'contactagent_form\',\''+row.cell[6]+'\');centerPopup();loadPopup();"><img src="'+root+'css/default/images/contact_icon.png" width="20" height="14" alt="" border="0" /> CONTACT </a></div>';
		if(row.cell[0] == 1 || row.cell[0] == 0)
		{
			result +='<div id="content" style="width:40%;float:left;text-align:justify;"><span style="font-size:14px;color:#666;font-weight:bold">About Company</span><br />'+row.cell[7]+'</div>';
			result +='<div id="content" style="width:28%;float:right;"><img width="100px" height="80" style="margin-left:25px" src="'+root+row.cell[8]+'">';
			result +='<br><div id="morepart"><img src="'+root+'css/default/images/arrowformore.jpg" alt="arrow" />&nbsp;<a href="'+root+'myprofile/view/'+row.cell[14]+'">More Information</a></div></div>';
		}
	}
	else
	{
		result += '<div class="listingstyle"><div id="content" style="width:25%">'+row.cell[9]+'<br>';
		if(row.cell[0] == 1 || row.cell[0] == 0)
		{
			result += '<div id="morepart" style="float:none;"><a href="'+row.cell[3]+'" target="_blank">'+row.cell[3]+'</a></div><br>';
		}
		else
		{
			result += row.cell[3]+'<br>';
		}

//		result += row.cell[10]+", "+row.cell[11]+", "+row.cell[12];

		if(row.cell[10] != '')		
		{
			result += row.cell[10];	
			if(row.cell[11] != '')
			{
				result += ", "+row.cell[11];
				if(row.cell[12] != '')
				{
					result += ", "+row.cell[12];		
				}
			}
			else if(row.cell[12] != '')
			{
				result += ", "+row.cell[12];		
			}
		}
		else if(row.cell[11] != '')
		{
			result += row.cell[11];
			if(row.cell[12] != '')
			{
				result += ", "+row.cell[12];		
			}
		}
		else if(row.cell[12] != '')
		{
			result += ", "+row.cell[12];		
		}
		else
		{
			result += row.cell[12];		
		}
	
		result +='<br>'+row.cell[6]+'<br/><strong>Our Services:</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+row.cell[15].replace(/,/g,"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")+'<br/><a style="color:grey;font-family:tahoma;font-size:10px ;" href="#" onclick="change_form(\'contactagent_form\',\''+row.cell[6]+'\');centerPopup();loadPopup();"><img src="'+root+'css/default/images/contact_icon.png" width="20" height="14" alt="" border="0" /> CONTACT </a></div>';
		if(row.cell[0] == 1 || row.cell[0] == 0)
		{
			result +='<div id="content" style="width:40%;float:left;text-align:justify;"><span style="font-size:14px;color:#666;font-weight:bold">About Company</span><br />'+row.cell[7]+'</div>';
			result +='<div id="content" style="width:28%;float:right;"><img width="100px" height="80" style="margin-left:25px" src="'+root+row.cell[8]+'">';
			result +='<br><div id="morepart"><img src="'+root+'css/default/images/arrowformore.jpg" alt="arrow" />&nbsp;<a href="'+root+'myprofile/view/'+row.cell[14]+'">More Information</a></div></div>';
		}
	}
	
	result +='</div>';  
                               
                           });
                                                                 
                       /* result += '<div class="pagination" style="float:left;">';
                      
                       for( var i = 1; i<= Math.ceil((data.total)/10); i++ )
                       {
                         result += '<a style="cursor:pointer" id='+i+' onclick=dirlist("'+dir_id+'",'+i+',"'+sort_by_change+'")>'+i+'</a> &nbsp;&nbsp;';    
                       } 
                       result += '</div></div>';  */
					   result += '</div>'; 
                       $("#result").html(result);
		}



	   }
	 });
}
function search_dirlist(dir_id)
{	
	firstcall=1;
    dirlist(dir_id,'1','',$('#keyword').val());
}
function sort_dirlist(dir_id,page,sort_by)
{	
	firstcall=1;
	g_keyword='';
	dirlist(dir_id,page,sort_by);
}
