<?php
$title = "IMOS Gliders";
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . ini_get('include_path'));
include_once("include/header.php");
?>

    <!--
   **** start with layout component from http://getbootstrap.com/css/#grid
   Any relative links not in the same directory as the templated file should be prepended with
   <?php /* echo $BASEDIR */ ?>
-->
<?php
$dom = new DOMDocument;
$dom->loadHTMLFile('index.html');
$dom->formatOutput = true;

// Create the output document
$ndom = new DOMDocument;
$ndom->formatOutput = true;

// Extract the body
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('/html/body/*');

// Copy it to the output document
foreach ($nodes as $rec) {
	$node = $ndom->importNode($rec,true);
	$ndom->appendChild($node);
}

// Fix the table classes
foreach($ndom->getElementsByTagName('table') as $t ) {
	if ($t->hasAttribute('class')) {
		$t->setAttribute('class','table table-condensed');
	}
	else {
		$a = $ndom->createAttribute('class');
		$a->value = 'table table-condensed';
		$t->appendChild($a);
	}
}

// Fix the anchors
foreach($ndom->getElementsByTagName('a') as $a ) {
	if ($a->hasAttribute('href')) {
		$href = $a->getAttribute('href');

		if ($a->nodeValue === 'T-S' ) { // T-S in the table
			$a->setAttribute('href','missionts.php?link='.basename($href));
		}
		else if ($a->nodeValue === '4-day' ) { // 4-day in the table
			$a->setAttribute('href','4day.php?link='.$href);
		}
		else if ( substr($href,-15) === '_4d/latest.html' ) { // "_4d/latest.html" references
			$a->setAttribute('href','4day.php?link='.$href);
		}
		else if (substr($href,0,9) === 'yearmaps/' ) { // yearmaps
			$a->setAttribute('href','yearmaps.php?link='.substr($href,9));
		}
		else if ($href === 'missionTS/' ) { // missionsts
			$a->setAttribute('href','missionts_index.php');
		}
		else if ($href === 'glidernotes.htm' ) { // glider notes
			$a->setAttribute('href','glidernotes.php');
		}
	}
}

// Fix the &amp in the table header
foreach($ndom->getElementsByTagName('th') as $a ) {
	//echo "<code>$a->nodeValue</code>";
	if ($a->nodeValue === '&degS' ) $a->nodeValue = '&deg;S';
	else if ($a->nodeValue === '&degE' ) $a->nodeValue = '&deg;E';
}

// Do some cleaning so the output is reasonable

// Delete the first and last node
$ndom->removeChild($ndom->firstChild);	// imos header
$ndom->removeChild($ndom->lastChild);	// copyright footer

// Completely replace the <h2> header
$h2 = $ndom->getElementsByTagName('h2')->item(0);
$nh2 = $ndom->createElement('h2', 'Gliders' );
$ndom->replaceChild( $nh2, $h2 );

// Wrap the tables in a div with a table-responsive class
foreach( $ndom->getElementsByTagName('table') as $t ) {
	$d = $ndom->createElement('div' );
	$d->setAttribute('class','table-responsive');
	$t->parentNode->replaceChild( $d, $t );
	$d->appendChild( $t );
}

// Hide the tables just for fun...
$count = 0;
foreach( $ndom->getElementsByTagName('div') as $d ) {
	if ($d->hasAttribute('class')) {
		$a = $d->getAttribute('class');
		if ($a == 'table-responsive') {
			if ($count > 0) {
				// Shift preceeding p tag into div and hide.
				$p = $d->previousSibling;
				$d->parentNode->removeChild($p);
				$p = $d->insertBefore($p, $d->firstChild);
				$p->setAttribute('class','hidden');
				// Same for anchor...
				$a = $d->previousSibling;
				$d->parentNode->removeChild($a);
				$a = $d->insertBefore($a, $d->firstChild);
				// Create the navigation outside the div
				if ($count == 1) {
					$n = $ndom->createElement('p','testing navigation insert');
					$n2 = $p->cloneNode(true);
					$n2->removeAttribute('class');
					$b = $ndom->createElement('a',$n2->childNodes[1]->nodeValue);
					$n2->replaceChild($b,$n2->childNodes[1]);
					// Fix all anchors
					foreach( $n2->getElementsByTagName('a') as $aa ) {
						$aa->setAttribute('href','#');
						$aa->setAttribute('class','alink');
						$aa->setAttribute('data-id',$aa->nodeValue[0]);
					}
					$d->parentNode->insertBefore($n2,$d);
				}
				if ($count > 1) $d->setAttribute('class','table-responsive hidden');
				$d->setAttribute('class', $d->getAttribute('class').' atable');
				$d->setAttribute('data-id', $count);
			}
			$count++;
		}
	}
}


// print the beast.
echo $ndom->saveHTML();
?>

<script>
+function($) {
	$('.alink').click(function() {
		var id = $(this).attr('data-id');
		$('.atable').addClass('hidden');
		$('.atable[data-id=\"'+id+'\"]').toggleClass('hidden');
	});
}(jQuery);
</script>


<?php
include("include/footer.php");
?>
