<?php

include_once 'header.php';
if ($_GET['kid']) {
    $object_kid = $_GET['kid'];
    $fields = array('Image', 'Creator', 'Title', 'City', 'Country', 'Date Original', 'Description', 'Description French', 'Resource Identifier', 'Current Box', 'Original Format', 'Genre');
    $clause = new KORA_Clause('KID', '=', $object_kid);
    $results = KORA_Search(token, projectID, Object, $clause, $fields, array());
    echo "<div class='row'>";
    foreach ($results as $result) {
        $localName = $result['Image']['localName'];
        $full_image = getFullURLFromFileName($localName);
        $Thumb_image = getThumbURLFromFileName($localName);
        $detail_author = $result['Creator'];
        $detail_author = explode(', ', $detail_author[0]);
        $detail_title = $result['Title'];
        $detail_country = $result['Country'];
        $detail_city = $result['City'];
        $detail_date_original = "";
        $date_ = $result['Date Original'];
        if ($result['Date Original']['day']) {
            $detail_date_original .= $date_['day'] . " ";
        }
        if ($result['Date Original']['month']) {
            $detail_date_original .= getChMonth($date_['month']) . " ";
        }
        if ($result['Date Original']['year']) {
            $detail_date_original .= $date_['year'];
        }
        $detail_description = $result['Description'];
        $detail_description_french = $result['Description French'];
                <img src="' . getThumbURLFromFileName($value['Thumbnail']['localName']) . '" alt="' . htmlChars($value['Title']) . '" alt="Highslide JS" title="Click to enlarge" />
                </a>';
            echo '<div class="highslide-heading">' . $value['Title'] . '</div>';
            echo '<div class="highslide-caption">
                                   <a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/b">View Full Record</a>
                                </div></div>';
            $galleryTotal = $galleryTotal + 1;
        }
        if (!empty($value['Image'])) {
            $thumbWidth = 100;
            $thumbHeight = 120;
            //echo '<a href="'.baseURL.pfolder.'object/'.$value['kid'].'/"><img class="essayImg" width="'.$thumbWidth.'" height="'.$thumbHeight.'" src="'.getThumbURLFromFileName($value['Image']['localName']).'" alt="'.htmlChars($value['Title']).'" /></a>';
            //echo "<br />";
            echo '<div class="highslide-gallery">';
            echo '<a id = ' . $value['kid'] . 'single href="' . getFullURLFromFileName($value['Image']['localName']) . '" class="highslide" onclick="return hs.expand(this,	{ slideshowGroup: ' . $galleryTotal . '} )">
                <img src="' . getThumbURLFromFileName($value['Image']['localName']) . '" alt="' . htmlChars($value['Title']) . '" alt="Highslide JS" title="Click to enlarge" />
                </a>';
            echo '<div class="highslide-heading">' . $value['Title'] . '</div>';
            echo '<div class="highslide-caption">
                                   <a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/b">View Full Record</a>
                                </div></div>';
            $galleryTotal = $galleryTotal + 1;
        }
        //echo "<br />";
        ?>
</div><?php 
        $i++;
        if (!empty($value['Object Associator'])) {
            //collect object associators
            $obj_assocs = $value['Object Associator'];
        }
        } else {
            if (!empty($value['Thumbnail']['localName'])) {
                $thumbWidth = 100;
                $thumbHeight = 120;
                // Get thumb from KORA
                echo '<a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/b"><img   width="' . $thumbWidth . '" height="' . $thumbHeight . '" src="' . getThumbURLFromFileName($value['Thumbnail']['localName']) . '" alt="' . htmlChars($value['Title']) . '" /></a><br />';
            } else {
                if (!empty($value['Multi-Part Associator'])) {
                    $fields = array('Title', 'Image');
                    $query = new KORA_Clause("KID", "=", $value['Multi-Part Associator'][0]);
                    $sort = array('fields' => $fields, 'direction' => SORT_ASC, 'byProject' => false);
                    $m = KORA_Search(token, $projIDs, $multPartIDs, $query, $fields, $sort);
                    //var_dump($m);
                    $thumbWidth = 100;
                    $thumbHeight = 120;
                    echo '<a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/b"><img width="' . $thumbWidth . '" height="' . $thumbHeight . '" src="' . getThumbURLFromFileName($m[$value['Multi-Part Associator'][0]]['Image']['localName']) . '" alt="' . htmlChars($m[$value['Multi-Part Associator'][0]]['Title']) . '" /></a>';
                    echo "<br />";
                }
            }
        }
        echo "<br />";
        ?>
</div><?php 
        /*if($currType == "Document") {
              echo "</div>";
          }*/
        $i++;
        if (!empty($value['Object Associator'])) {
            //collect object associators
            $obj_assocs = $value['Object Associator'];
        }
            echo "<h3>" . $currFilter . "</h3>";
            echo "<h4>" . $currFilterDesc . "</h4>";
        }
        ?>
<div class="browse_item" style = "height: 150px;"><?php 
        if (!empty($value['Thumbnail']['localName'])) {
            echo '<div class="thumb">';
        } else {
            echo '<div class="no_thumb">';
        }
        $m;
        if (!empty($value['Thumbnail']['localName'])) {
            $thumbWidth = 100;
            $thumbHeight = 120;
            // Get thumb from KORA
            echo '<a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/b"><img class="essayImg"  width="' . $thumbWidth . '" height="' . $thumbHeight . '" src="' . getThumbURLFromFileName($value['Thumbnail']['localName']) . '" alt="' . htmlChars($value['Title']) . '" /></a><br />';
        }
        //echo "<br />";
        echo "</div>";
        $i++;
        if (!empty($value['Object Associator'])) {
            //collect object associators
            $obj_assocs = $value['Object Associator'];
        }
        if (!empty($value['Title'])) {
            echo '<h4><a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/b/">' . $value['Title'] . '</a></h4>';
        }
        if (is_array($value['Date Original'])) {
            echo '<strong>Date: </strong>' . formatDate($value['Date Original']['month'], $value['Date Original']['day'], $value['Date Original']['year']) . '<br />';
        } else {
            if (is_array($value['Date Range'])) {
                                //var_dump(baseURL);
                                $images[] = $thumbDiv . '<a href="' . baseURL . pfolder . 'object/' . $match['kid'] . '/e/' . $kid . '/">
								<img class="essayImg" src="' . getThumbURLFromFileName($match['Image']['localName'], $thumbWidth, $thumbHeight) . '" title="' . htmlescape(htmlChars($match['Title'])) . '" alt="' . htmlescape(htmlChars($match['Title'])) . '" />
				     			<div class="clearboth"><strong>' . $match['Title'] . '</strong><br />' . '
								</div></a></div>';
                            } else {
                                $fields = array('Title', 'Image');
                                $query = new KORA_Clause("KID", "!=", " ");
                                $multiObj = KORA_Search(token, $projID, $multPartID, $query, $fields);
                                $m[] = array();
                                foreach ($multiObj as $mpo) {
                                    $m[] = $mpo['kid'];
                                }
                                $assocObj = array_intersect($match['Multi-Part Associator'], $m);
                                $images[] = $thumbDiv . '<a href="' . baseURL . pfolder . 'object/' . $assocObj[0] . '/y/' . $kid . '/">
								<img class="essayImg" src="' . getThumbURLFromFileName($multiObj[$assocObj[0]]['Image']['localName'], $thumbWidth, $thumbHeight) . '" title="' . htmlescape(htmlChars($multiObj[$assocObj[0]]['Title'])) . '" alt="' . htmlescape(htmlChars($multiObj[$assocObj[0]]['Title'])) . '" />
				     			<div class="clearboth"><strong>' . $match['Title'] . '</strong><br />' . '
								</div></a></div>';
                            }
                            $i++;
                            $groupNo++;
                        }
                        echo "";
                    }
                }
                echo "<div>";
                echo str_replace($totalReplacements, $images, $value['Description']) . '<br />';
                echo "</div>";
            } else {
                echo "<div>";
                echo fixTags($value['Description']) . '<br />';
                                    if (!empty($value['Multi-Part Associator'])) {
                                        //  var_dump($value['Multi-Part Associator']);
                                        foreach ($value['Multi-Part Associator'] as $m) {
                                            $mult_assocs[] = $m;
                                        }
                                        $mult_fields = array('Part Number', 'Title', 'Image', 'Total Parts');
                                        $mult_order[] = array('field' => 'Part Number', 'direction' => SORT_ASC);
                                        $mult_query = new KORA_Clause("KID", "IN", $mult_assocs);
                                        $mult_obj = KORA_Search(token, $projID, $multPartID, $mult_query, $mult_fields, $mult_order);
                                        //  var_dump($mult_assocs);
                                        foreach ($mult_obj as $mo) {
                                            if (!empty($mo['Image'])) {
                                                // var_dump(getThumbnailKORA($mo['Image']['localName'], 700, 2000));
                                                //  var_dump($mo['Title']);
                                                //highslide display for GALLERY of images associated with object
                                                echo '<li><a class="highslide" onclick="return hs.expand(this, { slideshowGroup: \'docGalgroup-' . $docGalleryGroupNo . '\' })" href="' . getThumbURLFromFileName($mo['Image']['localName'], 700, 2000) . '">';
                                                //only print title for gallery group once
                                                if (!in_array($mo['Title'], $docTitleArray)) {
                                                    echo $value['Title'];
                                                }
                                                //KID in link must be for full record, not multi-part
                                                echo '</a>
			  	 				<div class="highslide-heading">' . $value['Title'] . '</div>
			  	    			<div class="highslide-caption"><a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/">View Full Record</a><br />
			   	  	 			<br />Total Pages: ' . $mo['Total Parts'] . '</div>
			     				<div class="clearboth"></div></li>';
                                            }
                                            $docTitleArray[] = $mo['Title'];
                                        }
                                        $docGalleryGroupNo++;
                                    } else {
                //OBJECT Assoc
                $obj_fields = array('Image', 'Title', 'Date Original', 'Description', 'Creditline');
                $obj_order[] = array('field' => 'kid', 'direction' => SORT_ASC);
                //sort in order associated
                $obj_query = new KORA_Clause("KID", "IN", $obj_assocs);
                $obj_obj = KORA_Search(token, $projID, $objID, $obj_query, $obj_fields, $obj_order);
                if (!empty($obj_obj)) {
                    echo "<strong>Related Objects</strong>";
                    echo "<br /><br />";
                }
                foreach ($obj_obj as $o) {
                    if (!empty($o['Image'])) {
                        $thumbWidth = 100;
                        $thumbHeight = 300;
                        echo '<a href="' . baseURL . pfolder . 'object/' . $o['kid'] . '/">
					      <img src="' . getThumbURLFromFileName($o['Image']['localName'], $thumbWidth, $thumbHeight) . '" title="' . htmlChars($o['Title']) . '" alt="' . htmlChars($o['Title']) . '" /></a>';
                        echo '<br />';
                    }
                    if (!empty($o['Title'])) {
                        if (!empty($o['Date Original'])) {
                            echo '<a href="' . baseURL . pfolder . 'object/' . $o['kid'] . '/">' . $o['Title'] . '</a> (' . formatDate($o['Date Original']['month'], $o['Date Original']['day'], $o['Date Original']['year']) . ')' . '<br />';
                        } else {
                            echo '<a href="' . baseURL . pfolder . 'object/' . $o['kid'] . '/">' . $o['Title'] . '</a><br />';
                        }
                    }
                    if (!empty($o['Creditline'])) {
                        echo '<div class="desc">' . $o['Creditline'] . "</div>";
                    }
                    echo "<br />";
                }
            }
if ($_GET['title']) {
    $fields = array('Thumbnail', 'Photographer', 'Title', 'Date', 'Location');
    $clause = new KORA_Clause('Section', '=', 'HighlightPhotographer');
    $results = KORA_Search(token, projectID, Section, $clause, $fields, array(array('field' => 'Order', 'direction' => SORT_ASC)));
    $pics_kid = array();
    $file = array();
    $image = array();
    $pic_photographer = array();
    $pic_title = array();
    $pic_date = array();
    $pic_location = array();
    foreach ($results as $result) {
        array_push($pics_kid, $result['kid']);
        $localName = $result['Thumbnail']['localName'];
        array_push($file, getFullURLFromFileName($localName));
        array_push($image, getThumbURLFromFileName($localName));
        array_push($pic_photographer, $result['Photographer']);
        array_push($pic_title, $result['Title']);
        array_push($pic_date, $result['Date']);
        array_push($pic_location, $result['Location']);
    }
    $pic_date_string = array();
    foreach ($pic_date as $value) {
        $str_date = "";
        if ($value['day']) {
            $str_date .= $value['day'] . " ";
        }
        if ($value['month']) {
            $str_date .= getChMonth($value['month']) . " ";
        }
        if ($value['year']) {
            if (!empty($value['Resource Type'])) {
                echo '<div class="desc"><strong>Format:</strong> ';
                echo $value['Resource Type'];
                //echo parseResourceType($value['Resource Type']);
                if (!empty($value['Format'])) {
                    echo '/';
                }
            }
            if (!empty($value['Format'])) {
                echo $value['Format'];
            }
            echo "</div>";
        }
        if (!empty($value['Image'])) {
            //link to full record, 100px wide
            echo '<a href="' . baseURL . pfolder . 'object/' . $value['kid'] . '/"><img src="' . getThumbURLFromFileName($value['Image']['localName']) . '" alt="" /></a>';
        }
        ?>
</div><?php 
        if (!empty($value['Description'])) {
            echo "<span class = 'descContent'>";
            if (strlen($value['Description']) > 370) {
                $descSub = substr($value['Description'], 0, 370);
                $link = baseURL . pfolder . 'object/' . $value['kid'] . "/b/";
                echo fixTags($descSub) . "&nbsp;&nbsp;<a href='" . $link . "'>SHOW MORE</a>" . '<br />';
            } else {
                echo fixTags($value['Description']) . '<br />';
            }
            echo "</span><br>";
        }
        echo '<div class="clearboth"></div><div class="clearboth"></div><div style="border-bottom: 1px solid #ccc; margin: 20px 0px 20px 0px;"></div>';