<?php
include_once("formularios/data/connect.php");

drupal_add_css('modulos/css/investment_tooltip.css');
drupal_add_css('modulos/css/jquery.autocomplete.css');
drupal_add_js('modulos/js/investment_tooltip.js');
drupal_add_js('modulos/js/jquery.autocomplete.js');
drupal_add_js('modulos/js/jquery.bgiframe.min.js');
drupal_add_js('modulos/js/autosuggest.js');

$buscid = $GET['buscid'];

$buscArray = array(); //stores only businessCaseIds. we need this array to be simple to avoid complications
$title_upi_Array = array(); //this is used to store UPI and title, having the businessCaseId as the key

$query = "SELECT P.Codigo, P.Nombre, P.IdPrograma, D.Nombre AS Departamento, D.Abrev FROM programas P INNER JOIN departamentos D ON P.IdDepartamento = D.IdDepartamento AND P.IdDepartamento = (SELECT IdDepartamento FROM programas WHERE IdPrograma = $buscid) ORDER BY P.Nombre ASC";

$RS = mysql_query($query);

while($row=mysql_fetch_array($RS)){
	$buscArray[] = $row['IdPrograma'];
	$title_upi_Array[$row['IdPrograma']] = array($row['Codigo'],$row['Nombre'],$row['Departamento'],$row['Abrev']);
}

$key = array_keys($buscArray, $buscid); //gets the key for the specific businessCaseId

$maxKey = array_pop(array_keys($buscArray)); //this is used to find the last key of the array
$maxBuscid = $buscArray[$maxKey];  //get the businessCaseId at the last key of buscArray
$minBuscid = $buscArray[0];  //get the businessCaseId at the first key of buscArray

$prev_key = $key[0] == 0 ? 0 : $key[0]-1;
$next_key = $key[0] == $maxKey ? $maxKey : $key[0]+1;

if($prev_key < 0){ //we do not want prev_key to be negative
	$prev_key = 0;
}

if($next_key > $maxKey){ //we do not want next_key to be greater than the max key in buscArray
	$next_key = $maxKey;
}


if(count($buscArray) < 9){ //if there are less than 9 records for that agency, take $i = 0 and $j = number of records
	$i = 0;
	$j = count($buscArray)-1;
}else{
	$i = $key[0]-4;  //to make sure there are 9 links on the page (except the last few records) at any time
	if($i < 0){ //we don't want negative array indexes
		$i = 0;
	}
	$j = $key[0]+4;
	if($j < 8){                  //this part of the code is used to make sure that there are 9 links on the page (except the last few records) at any time
		$j = $j + (8-$j);
	}elseif($j > $maxKey){      //to revert to old code make $maxKey[0] in this else block
		$j = $maxKey;
	}
}

$link = "";
for($i; $i <= $j; $i++){
	$upi = $title_upi_Array[$buscArray[$i]][0];
	$title = $title_upi_Array[$buscArray[$i]][1];
	$agencyName = $title_upi_Array[$buscArray[$i]][2];
	$agencyAbbrev = $title_upi_Array[$buscArray[$i]][3];
	$information = '<b>ID:</b> '.$buscArray[$i].'<br/><b>Codigo Proyecto:</b> '.str_replace('"',"'",$upi).'<br/><b>Titulo:</b> '.str_replace('"',"'",$title).'<br/><b>Departamento:</b> '.str_replace('"',"'",$agencyName);
	$link .= ($i == $key[0]) ? '<li class = "selectedPage">'.($i+1).'</li>' : '<li><a class="investmentList_pagination" title="'.$information.'" href="/investment?buscid='.$buscArray[$i].'">'.($i+1).'</a></li>';
}


$firstLink = $businessCaseId == $minBuscid ? "<span class=\"hidden-offscreen\">Inicio de la Lista</span>" : "<a href=\"/investment?buscid=$minBuscid\"><span class=\"hidden-offscreen\">Primero</span></a>";
$prevLink = $businessCaseId == $minBuscid ? "Anterior" : "<a href=\"/investment?buscid=$buscArray[$prev_key]\">Ant.</a>";
$nextLink = $businessCaseId == $maxBuscid ? "Siguiente" : "<a href=\"/investment?buscid=$buscArray[$next_key]\">Sig.</a>";
$lastLink = $businessCaseId == $maxBuscid ? "<span class=\"hidden-offscreen\">Ultimo</span>" : "<a href=\"/investment?buscid=$maxBuscid\"><span class=\"hidden-offscreen\">Fin de la Lista</span></a>";

$text = $PREQUEST['text'];
?>

<div id="portfolioSearchContainer">
<form id="investmentOverallNavigation" method="GET" action="/investment/getBuscid_fromProjectname">
<div id="portfolioSearch" class="clearfix">
	<p id="projectName">Nombre del Programa:</p>
	<label class="hidden-offscreen" for="investmentName_autoSuggest">Buscar</label>
	<p id="investimentName"><input type="text" id="investmentName_autoSuggest" name="investmentName_autoSuggest" value = "<?php echo $text; ?>"></p>
	<input type="hidden" id="hidden_investmentName_autoSuggest" name="hidden_investmentName_autoSuggest" value="<?php echo $businessCaseId; ?>" />
	<p id="findinAgency"><input type="submit" value="Buscar en <?php echo $agencyAbbrev; ?>" name="Submit"></p>
</div>
<?php
if(isset($PREQUEST['error']) && !empty($text)){
	echo "<p id=\"investmentNameNotFound\" class=\"app_Error\">Programa <b>$text</b> no disponible.</p>";
}
?>
</form>
</div>

<div id="portfolioPaginationContainer">
<p id="ilcPaginationTitle">Buscar en <?php echo $agencyName; ?>: <b><?php echo count($buscArray); ?></b> Programas</p>
<div id="ilcPaginationLinks" class="clearfix">
	<div id="firstPrevious">
		<!--<p id="firstPage"><a href="#"><span class="hidden-offscreen">Primero</span></a></p>-->
		<p id="firstPage"><?php echo $firstLink; ?></p>
		<p id="previousPage"><?php echo $prevLink; ?></p>
	</div>
	<ul>
		<?php echo $link; ?>
	</ul>

	<div id="nextLast">
		<p id="nextPage"><?php echo $nextLink; ?></p>
		<!--<p id="lastPage"><span class="hidden-offscreen"><a href="#">Ultimo</a></span></p>-->
		<p id="lastPage"><?php echo $lastLink; ?></p>
	</div>
</div>
</div>