<?php
$hc=new hc;
class hc {
	//线路名称
	function linename( $tmpline ) {
		if ( substr($tmpline,-3)=="行]") {
			$line=substr($tmpline,0,-6);//去除线路上下行、单行的后缀
		} else {
			$line=$tmpline;
		}
		return $line;
	}
	//线路url
	function lineurl($v){
		global $city,$rewrite;
		$url=curl($v);
		if($rewrite){
			return "/x_{$city}-{$url}.html";
		}else{
			return "x.php?{$city}-{$url}.html";
		}
	}
	//站点url
	function zdurl($v){
		global $city,$rewrite;
		$url=curl($v);
		if($rewrite){
			return "/z_{$city}-{$url}.html";
		}else{
			return "z.php?{$city}-{$url}.html";
		}
	}
	//换乘主程序
	function bus_hc(){
		global $city,$A,$B;
		//载入数据
		$a_all=loadcache("bus/{$city}/a_all.txt");
		$line=loadcache("bus/{$city}/line.txt");
		$zd=loadcache("bus/{$city}/zd.txt");
		$all=array();
		foreach($a_all as $k => $v){
			foreach($v as $kk=>$vv) {
				$all[trim($vv)][]=$k;
			}
		}
		if($A==$B) {
			return "<h4>起点和终点不能相同！</h4>";
		}
		//A B经过的线路
		$line1=&$all[$A];
		$line2=&$all[$B];
		if(empty($line1) || empty($line2)) {
			return "<h4>信息载入错误！</h4>";
		}
		//A B直达的站点
		$zdA=$zdB=array();
		foreach($line1 as $v){
			foreach($a_all[$v] as $vv) {
				$zdA[$vv]=$vv;
			}
		}
		foreach($line2 as $v){
			foreach($a_all[$v] as $vv) {
				$zdB[$vv]=$vv;
			}
		}
		//直达
		$line11=array_intersect($line1,$line2); //数组交集
		if($line11) {
			$line12=array();
			foreach($line11 as $v){
				$key1=array_search($A,$a_all[$v]);
				$key2=array_search($B,$a_all[$v]);
				$key3=$key2-$key1;
				if($key3>0){
					$line12[$v]=abs($key3);
				}
			}
			asort($line12);
			$cc=count($line12);
			if(!empty($line12)){
				$content="<h4>有{$cc}条直达线路供你选择</h4>";
				$i=0;
				foreach($line12 as $k=>$v){
					$i++;
					if($i>10) break;
					$line=$this->linename($k);
					$lineurl=$this->lineurl($line);
					$content.="<div>
					<h4>方案{$i}(约{$v}站)：</h4>
					<p>{$A} 乘 <a target=_blank href={$lineurl}>{$line}</a> 直达 {$B}</p>
					</div>";
				}
				return $content;
			}
		}
		//直达结束
		//一次换乘
		$zdC=array_intersect($zdA,$zdB); //数组交集
		if($zdC) {
			$zdD=$zdE=$zdF=array();
			foreach($zdC as $C){
				$tmpline=$all[$C];
				$line13=$line14=array();
				$tmpline1=array_intersect($line1,$tmpline);
				$tmpline2=array_intersect($line2,$tmpline);
				foreach($tmpline1 as $v){
					$key1=array_search($A,$a_all[$v]);
					$key2=array_search($C,$a_all[$v]);
					$key3=$key2-$key1;
					if($key3>0){
						$line13[$v]=abs($key3);
					}
				}
				foreach($tmpline2 as $v){
					$key1=array_search($C,$a_all[$v]);
					$key2=array_search($B,$a_all[$v]);
					$key3=$key2-$key1;
					if($key3>0){
						$line14[$v]=abs($key3);
					}
				}
				asort($line13);
				asort($line14);
				if(!empty($line13) && !empty($line14)){
					$key1=array_slice($line13,0,1);
					$key2=array_slice($line14,0,1);
					sort($key1);
					sort($key2);
					$key3=$key1[0]+$key2[0];
					$zdD[$C]=$key3;
					$zdE[$C]=$line13;
					$zdF[$C]=$line14;
				}
			}
			asort($zdD);
			$cc=count($zdD);
			if(!empty($zdD)){
				$content="<h4>有{$cc}条一次换乘线路供你选择</h4>";
				$i=0;
				foreach($zdD as $k=>$v){
					$i++;
					if($i>10) break;
					$zd=$k;
					$zdurl=$this->zdurl($zd);
					$zt1=$zt2=array();
					$tmp1=array_slice($zdE[$k],0,3);
					$tmp2=array_slice($zdF[$k],0,3);
					foreach($tmp1 as $kk=>$vv){
						$kk=$this->linename($kk);
						$zt1[]="<a target=_blank href=".$this->lineurl($kk).">{$kk}</a>";
					}
					foreach($tmp2 as $kk=>$vv){
						$kk=$this->linename($kk);
						$zt2[]="<a target=_blank href=".$this->lineurl($kk).">{$kk}</a>";
					}
					$tmp1=implode(" | ",$zt1);
					$tmp2=implode(" | ",$zt2);
					$content.="<div>
					<h4>方案{$i}(约{$v}站)：</h4>
					<p>{$A} 乘 {$tmp1} 到 【<a target=_blank href={$zdurl}>{$zd}</a>】 换乘 {$tmp2} 到达 {$B}</p>
					</div>";
				}
				return $content;
			}
		}
		//一次换乘结束
		return "<h4><a target=_blank class=red
		href=http://gj.jtxxol.com/sobuszz.php?city={$city}&buszd1={$A}&buszd2={$B}>
		查看 {$A} 到 {$B} 二次换乘方案
		</a></h4>";
	}
}
?>