<?php

	require_once("session.php");
	
	require_once("classes/class.user.php");
	require_once("classes/class.url.php");
	require_once("classes/class.taxon.php");
	
        $authUser = new USER();
        $taxon = new Taxon();
	
        if( isset($_REQUEST['taxonType']) && is_string($_REQUEST['taxonType']) && isset($_REQUEST['taxonId']) && is_numeric($_REQUEST['taxonId']) ) {
          $taxonType=(string) $_REQUEST['taxonType'];
          $taxonId=(int) $_REQUEST['taxonId'];
        } elseif(isset($_REQUEST['regnum']) && is_string($_REQUEST['regnum'])) {
	  $taxonType='regnum';
	  $taxonId=(int) $taxon->getId('regnum',$_REQUEST['regnum']);
	} else {
	  $taxonType='regnum';
	  $taxonId=1;
	}
        $taxon->init($taxonType, $taxonId);
	if(isset($taxon->tree['regnum']['name'])) {
	  $regnum=$taxon->tree['regnum']['name'];
	} else {
	  $regnum="";
	}
	//echo "<pre>".var_dump($taxon->tree['regnum']['name'])."</pre>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<!-- Include common HTML META tags -->
	<?php require_once("meta.html"); ?>
	<?php require_once("default-css.html"); ?>
	<link href="css/treeview.css" rel="stylesheet" type="text/css" integrity="sha512-2CrY3icf/LpzGYkGH6iwb9yCKK9KOQqWRLRk6UYalj9SyzBKtSIcZ4WSx6L3nHXVZ3SOuWpw7uAkTubuq9F9+g==" crossorigin="anonymous">
	<title>welcome - <?php print($authUser->userRow['userEmail']); ?></title>
</head>
<body>
<?php require_once("navbar-fixed-top.php"); ?>

<div class="clearfix"></div>
    
<div class="container-fluid" style="margin-top:7px;">
  <div class="container">
    
<!-- Display -->
<div class="panel panel-success">
  <div class="panel-heading">
    <div class="input-group">
      <ol class="breadcrumb">
	<?php
          $taxon->displayTaxonList($taxonType, "display");
        ?>
      </ol>
    </div>
  </div>
  <div class="panel-body">
    <form class="form-inline" method="post">
      <input type="hidden" name="taxonType" value="<?php echo $taxon->getRealName($regnum,$taxonType); ?>" />
      <input type="hidden" name="taxonId" value="<?php echo $taxonId; ?>" />
      <div class="input-group has-success">
<?php if ( $session->is_loggedin() ) { ?>
        <span class="input-group-addon" id="basic-addon1">
          <?php echo ucfirst($taxonType).": ".$taxon->getDisplayName($taxonType); ?>
	</span>
        <div class="input-group-btn">
          <!-- Buttons -->
          <button type="submit" class="btn btn-default" formaction="edit.php" name="action" value="edit">Edit</button>
          <button type="submit" class="btn btn-default" formaction="edit.php" name="action" value="addSimilar">Add similar</button>
          <button type="submit" class="btn btn-default" formaction="edit.php" name="action" value="addChild">Add child</button>
        </div>
<?php } else { ?>
	<span class="label label-success">
	<?php
          echo ucfirst($taxonType).": ".$taxon->getDisplayName($taxonType);
          $collection=$taxon->getGeoJSONCollection($taxonType,$taxonId);
	  if( $collection !== "null" && $collection!=="[]" ){ 
        ?>
	  <form class="form-inline" method="post">
	  <input type="hidden" name="taxonType" value="<?php echo $taxonType; ?>">
	    <input type="hidden" name="taxonId" value="<?php echo $taxonId; ?>">
	    <div class="input-group-btn">
	      <button type="submit" class="btn btn-info btn-sm" formaction="map_leaflet.php" name="action" value="edit">
		<span class="nav-link glyphicon glyphicon-globe" />
	      </button>
	    </div>
          </form>
    <?php } ?>
	</span>
<?php } ?>
      </div>
    </form>
    <div class="tree">
      <?php
        if ($taxon->getChilds($taxonType,$taxonId)) {
	  echo "<ul>\n";
          $taxon->displayChilds($taxonType,$taxonId,'',1,$regnum,1);
	  echo "</ul>\n";
	}
      ?>
    </div>
  </div>
  <div class="panel-body">
    [Picture]
    <hr />
    Description:
    <hr />
    Locations:
    <hr />
    <div class="input-group has-success">
      <div class="input-group-addon">Wiki article inline</div>
      <button class="btn btn-<?php echo url_exists("https://en.wikipedia.org/wiki/".$taxon->getWikiName($taxonType))?"success":"warning"; ?>" id="wikiToggle"><?php echo $taxon->getDisplayName($taxonType); ?></button>
    </div>
    <br />
    <div class="service-block service-block-grey hidden" id="wikiContent" data-wikititle="<?php echo $taxon->getDisplayName($taxonType); ?>">
       Loading wikiContent ...
    </div>
    <hr />
    <div class="form-group">
      <form class="form-inline hidden" id="wikiExternal" action="https://en.wikipedia.org/wiki/<?php echo $taxon->getWikiName($taxonType); ?>">
        <div class="input-group has-success">
          <div class="input-group-addon">Full Wiki article for further reading</div>
          <button type="submit" class="btn btn-<?php echo url_exists("https://en.wikipedia.org/wiki/".$taxon->getWikiName($taxonType))?"success":"warning"; ?>"><?php echo $taxon->getDisplayName($taxonType); ?></button>
        </div>
      </form>
    </div>
  </div>

<!-- ToDo
  <div class="panel-footer">
    Last edited by: <'userName'> <'date'>
  </div>
--> 

</div>
</div>
</div>

</body>
<?php require_once("default-js.html"); ?>
<script type="text/javascript">
function getWikiText(div_name, title) {
  //Get Leading paragraphs (section 0)
  $.getJSON("http://en.wikipedia.org/w/api.php?format=json&action=parse&page=" + title + "&prop=text&section=0&callback=?", function (data) {
    for (text in data.parse.text) {
        var text = data.parse.text[text].split("<p>");
        var pText = "";

        for (p in text) {
            //Remove html comment
            text[p] = text[p].split("<!--");
            if (text[p].length > 1) {
                text[p][0] = text[p][0].split(/\r\n|\r|\n/);
                text[p][0] = text[p][0][0];
                text[p][0] += "</p> ";
            }
            text[p] = text[p][0];

            //Construct a string from paragraphs
            if (text[p].indexOf("</p>") == text[p].length - 5) {
                var htmlStrip = text[p].replace(/<(?:.|\n)*?>/gm, '') //Remove HTML
                var splitNewline = htmlStrip.split(/\r\n|\r|\n/); //Split on newlines
                for (newline in splitNewline) {
                    if (splitNewline[newline].substring(0, 11) != "Cite error:") {
                        pText += splitNewline[newline];
                        pText += "\n";
                    }
                }
            }
        }
        pText = pText.substring(0, pText.length - 2); //Remove extra newline
        pText = pText.replace(/\[\d+\]/g, ""); //Remove reference tags (e.x. [1], [4], etc)
        document.getElementById(div_name).textContent = pText;
    }
  });
};

$("#wikiToggle").on('click', function (e) {
  var page ="";
  $("#wikiContent").toggleClass('hidden','show');
  $("#wikiContent").addClass('has-success');
  $("#wikiExternal").toggleClass('hidden','show');
  page=$("#wikiContent").attr("data-wikititle");

  $.ajax({
        type: "GET",
        url: "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=" + page + "&callback=?&redirects",
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
 
            var markup = data.parse.text["*"];
            var blurb = $('<div></div>').html(markup);
 
            // remove links as they will not work
            blurb.find('a').each(function() { $(this).replaceWith($(this).html()); });
 
            // remove any references
            blurb.find('sup').remove();
 
            // remove cite error
            blurb.find('.mw-ext-cite-error').remove();
            $('#wikiContent').html($(blurb).find('p'));
 
        },
        error: function (errorMessage) {
        }
    });
});
</script>
</html>
