<style type="text/css">
div.yp_middle {
	margin: 10px 2px;
	border: 1px solid #cccccc;
}
</style>

<?php
$microdata_domain = str_replace("www.", "", $ultra_domainname);
$microdata_host = $_SERVER['HTTP_HOST'];
if(str_replace('www.', '', $microdata_host) != str_replace('www.', '', $ultra_domainname)) $microdata_host .= "/" . $ultra_addonname;
$have_microdata = (isset($microdata_domains) && in_array($microdata_domain, $microdata_domains));


$optres['css'][] = "$path" . "css/jqModal.css";
$optres['js'][] = "$path" . "js/jqModal.js";
if(isset($module_params['module_yp_categs']) && is_array($module_params['module_yp_categs']) && count($module_params['module_yp_categs']) > 0) {
	
	require_once(dirname(__FILE__) . "/functions/yp_list_entries.php");
	$yp_limit = 10;
	if(!isset($_REQUEST['list']) || intval($_REQUEST['list'])<1) {
		$yp_page = 1;
	}
	else {
		$yp_page = intval($_REQUEST['list']);
	}
	$yp_from = 0;
	if($yp_page > 1) {
		$yp_from = $yp_page * $yp_limit;
	}
	
	
	$module_yp_categs = $module_params['module_yp_categs'];
	
	$categs_uids = array();
	foreach (array_keys($module_yp_categs) as $categ_uid) {
		$categs_uids [] = "'" . $categ_uid . "'";
	}
	$categs_uids_str = implode(",", $categs_uids);
	$yp_limit = 10;
	$yp_data_search = list_all_entries($categs_uids_str, '', $yp_page, $yp_limit);
			
	//echo '<pre>', print_r($yp_data_search), '</pre>';
	
	$yp_total_records = $yp_data_search['total_records'];
	$yp_data = $yp_data_search['data'];
	
	if($yp_total_records > 0) {
		?>
		<div id="yp_business_dialog">
			<div id="yp_jqm" class="jqmWindow article_jqm yp_business_jqm" style="display:none;">
				<img alt="Close" title="Close Window" class="jqmClose" src="<?php echo $path;?>images/jqmodal/close_pop.png">
				<div id="jqm_header">
					
				</div>
				<div id="yp_jqm_target"></div>
			</div>
		</div>
		<div class="yp_middle">
			<div id="yp_records">
				<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<?php
				
				//echo '<pre>',print_r($yp_data), '</pre>';
				if($yp_data && count($yp_data) > 0) {
					foreach ($yp_data as $b_uid=>$b_data) {
						$locations = $b_data['locations'];
						$num_locs = count($locations);
						?>
						<tr class="itr" <?php echo ($have_microdata) ? 'itemscope itemtype="' . $microdata_types[$microdata_domain]['adverts']['type'] . '"' : '';?>>
								
								
								<?php
								$td_locs = array(
									"district"	=> array(),
									"tel"		=> array(),
									"fax"		=> array()
								);
								$cnt = 0;
								foreach ($locations as $i=>$loc) {
									$have_loc_data = false;
								
									$td_locs_tmp = array(
										"district"	=> $loc['district'],
										"tel"		=> "&nbsp;",
										"fax"		=> "&nbsp;"
									);
									
									$tels = array();
									if($loc['tel1'] != "") $tels [] = $loc['tel1'];
									if($loc['tel2'] != "") $tels [] = $loc['tel2'];
									
									$faxes = array();
									if($loc['fax1'] != "") $faxes [] = $loc['fax1'];
									if($loc['fax2'] != "") $faxes [] = $loc['fax2'];
									
									
									if(count($tels) > 0) {
										$have_loc_data = true;
										$td_locs_tmp['tel'] = implode(", ", $tels) . "";
									}
									
									if(count($faxes) > 0) {
										$have_loc_data = true;
										$td_locs_tmp['fax'] =  implode(", ", $faxes). "";
									}
									
									if($have_loc_data) {
										$td_locs ['district'][] = $td_locs_tmp['district'];
										$td_locs ['tel'][] = $td_locs_tmp['tel'];
										$td_locs ['fax'][] = $td_locs_tmp['fax'];
									}
									
									
								}
								
								?>
								<td align="left" valign="top" width="40%">
									<?php
									$b_data['name'] = str_replace(".", ". ", $b_data['name']);
									echo '<a href="'. $protocol.$server_host.'/directory/name/'.$b_data['friendly_name'].'" class="yp_link">';
									echo ($have_microdata) ? '<span itemprop="name">':'';
									echo ucwords(strtolower($b_data['name']));
									echo ($have_microdata) ? '</span>':'';
									echo '<a/><br/>';
									?>
								</td>
								<td align="left" width="60%" class="yp_locs">
									<table cellpadding="0" cellspacing="0" border="0" width="100%">
										<?php
										$cnt = 1;
										for($i=0; $i<count($td_locs['district']); $i++) {
											?>
											<tr <?php echo ($have_microdata) ? 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"' : '';?>>
												<?php
												foreach ($td_locs as $td_type=>$td_array) {
													$yp_val = trim($td_array[$i]);
													$yp_tmp = "";
													
													if($td_type == "district") {
														if($have_microdata) {
															$yp_tmp = '<meta itemprop="addressCountry" content="CY">';
															$yp_tmp .= '<span itemprop="addressRegion">';
														}
														//$yp_val = $yp_val;
														$yp_tmp .= "<strong>$yp_val</strong>";
														if($have_microdata) {
															$yp_tmp .= '</span>';
														}
													}
													
													if($td_type == "tel" && $yp_val != "&nbsp;") {
														
														$yp_tmp .= "<strong>Tel: </strong>";
														if($have_microdata) {
															$yp_tmp = '<span itemprop="telephone">' . $yp_val;
														}
														else {
															$yp_tmp .= $yp_val;
														}
														if($have_microdata) {
															$yp_tmp .= '</span>';
														}
													}
													
													if($td_type == "fax" && $yp_val !="&nbsp;") {
														
														$yp_tmp .= "<strong>Fax: </strong>";
														if($have_microdata) {
															$yp_tmp = '<span itemprop="faxNumber">' . $yp_val;
														}
														else {
															$yp_tmp .= $yp_val;
														}
														if($have_microdata) {
															$yp_tmp .= '</span>';
														}
													}
													?>
													<td align="left" class="yp_lb" width="33%"><?php echo $yp_tmp;?></td>
													<?php
												}
												?>
											</tr>
											<?php
										}
										?>
									</table>
								</td>
									
							</tr>
						<?php
						
					}
				}
				
				if($yp_total_records > $yp_limit) {
					$nrpages = intval(ceil($yp_total_records/$yp_limit));
					$yp_from = ($yp_page == 1) ? 0 : $yp_page * $yp_limit;
					$page = $yp_limit;
					?>
					<tr>
						<td class="yp_pagination" colspan="2" align="left">
							<?php
							$pages_to_show = 5;
							$prev_text = "&laquo;";
							$next_text = "&raquo;";
							
							$P = $yp_page;
							
							$plinks = array(
								"prev" => array(),
								"left" => array(),
								"middle" => array(),
								"right"	=> array(),
								"next"	=> array()
							);
							//echo '<pre>', print_r($plinks), '</pre>';
							$link_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
							
							if($P + 2 > $pages_to_show) {
								for($i=2; $i>=1; $i--) {
									$cur_p = $P - $i;
									$cur_from = ($cur_p-1) * $page;
									$plink['left'][] = array(
										"href" 	=> $link_url . "?list=$cur_p",
										"rel"	=> $cur_from,
										"text"	=> $cur_p,
										"page"	=> $cur_p
									);
								}
								
								$cur_p = $P;
								$cur_from = ($cur_p-1) * $page;
								$plink['middle'][] = array(
										"href" 	=> $link_url . "?list=$cur_p",
										"rel"	=> $cur_from,
										"text"	=> $cur_p,
										"page"	=> $cur_p
									);
								if($nrpages > $P) {	
									
									for($i=1; $i<=($P+2 <= $nrpages ? 2 : $nrpages-$P); $i++) {
										$cur_p = $P + $i;
										$cur_from = ($cur_p-1) * $page;
										$plink['right'][] = array(
											"href" 	=> $link_url . "?list=$cur_p",
											"rel"	=> $cur_from,
											"text"	=> $cur_p,
											"page"	=> $cur_p
										);
									}
								}
							}
							else {
								for($i=1; $i<=($nrpages < 5 ? $nrpages : 5); $i++) {
									$cur_p = $i;
									$cur_from = 0;
									$plink['left'][] = array(
										"href" 	=> $link_url . "?list=$cur_p",
										"rel"	=> $cur_from,
										"text"	=> $cur_p,
										"page"	=> $cur_p
									);
								}
							}
							
							if($P>1) {
								$cur_p = $P - 1;
								$cur_from = ($cur_p-1) * $page;
								$plink['prev'][] = array(
									"href" 	=> $link_url . "?list=$cur_p",
									"rel"	=> $cur_from,
									"text"	=> '&laquo; ' . 'Prev',
									"page"	=> $cur_p
								);
							}
							
							if($nrpages > $P) {
								$cur_p = $P + 1;
								$cur_from = ($cur_p-1) * $page;
								$plink['next'][] = array(
									"href" 	=> $link_url . "?list=$cur_p",
									"rel"	=> $cur_from,
									"text"	=> 'Next' . ' &raquo;',
									"page"	=> $cur_p
								);
							}
							
							ob_start();
							echo '<div class="pagination">';
								//echo '<textarea cols=80 rows=10><pre>', print_r($plink),'</pre></textarea>';
								//echo '<div id="ui-paginator" style="padding: 0px 3px;">';
								if(isset($plink['prev']) && count($plink['prev']) > 0) {
									$p_class = "pag_num lnext";
									$ld = $plink['prev'][0];
									echo '<a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a>';
								}
								else {
									echo '<span>&laquo; ' . 'Prev' . '&nbsp;</span>';
								}
								
								$margins = array(
									"left" => array(),
									"right" => array(),
								);
								if($P>=5) {
									
									for($i=1; $i<=1; $i++) {
										$cur_p = $i;
										$cur_from = ($cur_p-1) * $page;
										$margins['left'][] = array(
											"href" 	=> $link_url . "?list=" . $cur_p,
											"rel"	=> $cur_from,
											"text"	=> $cur_p,
											"page"	=> $cur_p
										);
									}
									
									foreach ($margins['left'] as $i=>$ld) {
										$p_class = "pag_num";
										echo '<a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a>';
									}
									echo '<span>..</span>';
								}
								
								foreach ($plink as $pos_label => $pos) {
									foreach ($pos as $i=>$ld) {
										$p_class = "pag_num";
										if($pos_label != "prev" && $pos_label != "next") {
											if($ld['page'] == $P) {
												echo '<span class="current">' . $ld['text'] . '</span>';
											}
											else {
												echo '<a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a>';
											}
										}
									}
								}
								
								if($P+3 < $nrpages) {
									
									for($i=1; $i<=1; $i++) {
										$cur_p = $nrpages;
										$cur_from = ($cur_p-1) * $page;
										$margins['right'][] = array(
											"href" 	=> $link_url . "?list=" . $cur_p,
											"rel"	=> $cur_from,
											"text"	=> $cur_p,
											"page"	=> $cur_p
										);
									}
									echo '<span>..</span>';
									foreach ($margins['right'] as $i=>$ld) {
										$p_class = "pag_num";
										echo '<a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a>';
									}
									
								}
								
								if(isset($plink['next']) && count($plink['next']) > 0) {
									$p_class = "pag_num lnext";
									$ld = $plink['next'][0];
									echo '<a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a>';
								}
								else {
									echo '<span>&nbsp;' . 'Next' . ' &raquo;</span>';
								}
								//echo '</div>';
							echo '</div>';
							
							$pagination_code = ob_get_contents();
							ob_end_clean();
							echo $pagination_code;
				
							?>
						</td>
					</tr>
					<?php
				}
				
				?>
			</table>
			<?php
			/*
			<script type="text/javascript">
				$(document).ready(function(){
					$('a.yp_link').live('click', function(ev){
						ev.preventDefault();
						var url = $(this).attr('href') + '?page=1';
						$('#yp_jqm').jqm({ajax: url , modal: false, target : "#yp_jqm_target", toTop: true});
						$('#yp_jqm').jqmShow();	
						return false;
					});
					
				});
			</script>
			*/
			?>
			</div>
		</div>
		<?php
		
	}
}
?>