<?php
    /*
    solarTelescopes.php
    author: Sydney D'Silva
    
    Usage: 
    To generate html files of each page
    solarTelescopes.php?generate=true
    To get page n
    solarTelescopes.php?page_num=n

    Provides the client with images and links to a
    subset of images of Solar Telescopes from all 
    around the world.

    A brief description of each image is provided as a
    popup on hover. Best viewed with Firefox, Mozilla, Netscape
    IE does not show this description.

    */

    require_once ("xmlParser.php");
    $dir_name = dirname(realpath($PHP_SELF))."/";

    $generate=$_GET["generate"];
    if($generate = "")$generate=false;
    if($generate)
    {
        $current_sun="solar_telescopes";
        $page_num=$argv[1];
        ob_start();
    }
    //Matrix parameters
    $n_col_img=6; // number of columns
    $n_row_img=2; // number of rows
    $n_page_img=$n_col_img*$n_row_img; // number of images in page
    $cell_width=130;
    $cell_padding=2;
    $cell_spacing=0;
    $table_border=0;
    $table_width=($cell_width+2.*$cell_padding+2.*$cell_spacing)*$n_col_img;

    $xml = new xmlParser("solarTelescopes.xml");
    $xml->getArray("thumb");
    $keys=array("thumb","hires","caption","copywrite","description","thumbURL","hiresURL");
    $n_keys=count($keys);
    for($i_col=0; $i_col<$n_keys; $i_col++){
        $xml->getArray($keys[$i_col]);
        $img_arr[$keys[$i_col]] = $xml->returnArray;
    }

    //Read data into array $img_arr
    /*
    $lines=file($dir_name."telescope_data");
    $i_row=0;
    $i_col=0;
    $keys=array("thmb","hires","caption","info_title","info_text","url_thmb","url_hires");
    $n_keys=count($keys);
    foreach($lines as $line)
    {
        $line=rtrim($line);
        $img_arr[$i_row][$keys[$i_col]]=$line;
        $i_col++;
        if($i_col % $n_keys == 0)
        {
            $i_col=0;
            $i_row++;
        }
    }
    */
    $n_img = count($img_arr["thumb"]); // Total number of images

    //css data string
    $header_string = "<head>
                     <style type='text/css'>
                     h1
                     {
                         color:green;
                         text-align:center;
                     }
                     div.turn_page
                     {
                         align:right;width:{$table_width}px;color:green;text-align:right;
                     }
                     div.caption
                     {   
                         align:left; font-face:Tahoma; color:#ffffff; font-size:12px; font-style=normal
                     }

                     div#img a span {display: none;}
                     div#img a {text-decoration:none; color:white;}
                     div#img a.img:link {color: #0000ff}
                     div#img a.img:visited {color: #00ff00} 
                     div#img a.img:hover {color: #ffcc00}
                     div#img a:hover span 
                     {
                         display: block;
                         position: fixed; /*absolute; top: 0px; left: 0*/; 
                         width: 225px;
                         padding: 5px; margin: 10px; z-index: 100;
                         color: #ff0000; background:rgb(200,200,200);
                         font: 12px Verdana, sans-serif; text-align: left;
                     }
                     td.img
                     {
                         background-color:#000000;
                     }
                         a.img:link {color: #ff0000}
                         a.img:visited {color: #0000ff} 
                         a.img:hover {color: #ffcc00}
                     .popup
                     {
                         COLOR: #ffffff;
                         CURSOR: help;
                         TEXT-DECORATION: none
                     }
                     </style>
                     </head>
                     ";



    //js string for popup
    $script= <<<END
             <script type="text/javascript" language="javascript">
             <!--
             var newWindow='';
             function PopUp(mylink, windowname)
             {
                 if (! window.focus)return true;
                 var href;

                 if (typeof(mylink) == 'string')
                 href=mylink;
                 else
                 href=mylink.href;

                 newWindow = window.open(href, windowname, 'width=500,height=500,scrollbars=yes,left=0,top=100,resizable=yes');
                 if (window.focus) {newWindow.focus()}
                 return false;
             }
                 //--> 
                 </script>
END;
    $open_html = "<html>";
    $open_table=<<<END
                <table cellpadding="{$cell_padding}" cellspacing="{$cell_spacing}"
                       border="{$table_border}" style="text-align: left; width: {$table_width}px; 
                       margin-left: auto; margin-right: auto;">
                <tbody>
                <tr>
END;
    $open_td=<<<END
             <td class="img" width="{$cell_width}" valign="middle" align="center" height="7">;
END;
    $title = "<h1>Solar Telescopes Around the World</h1>";

    // If calling sequence is current_sun.php?display=hires&ref_string=http://... then open popup window with 
    // the given url
    $display = $_GET['display'];
    $ref_string = $_GET['ref_string'];
    $page_num = $_GET['page_num'];
    if($display == hires)
    {
        $xtract_xtension = explode(".",$ref_string);
        $file_type=$xtract_xtension[count($xtract_xtension)-1];
        if($file_type == "html")
        {
            header("Location:{$ref_string}");
        }
        else
        {
            print <<<END
                  <a href="javascript:self.close()"><img src="{$ref_string}"/></a>
END;
        }
        if($ref_string == '')print "Calling sequence is 'current_sun.php?display=hires&ref_string=http://...'";
    }
    else
    {

        // The main page with image matrix
        // Start HTML
        print $open_html;
        print $header_string;
        print "<body>";
        print $script;
        print $title;
        $page_num = !empty($page_num) ? $page_num : 0;
        $next_page=$page_num+1;
        $prev_page=$page_num-1;
        $img_indx = $page_num*$n_page_img;
        $n_page  = (int)($n_img/$n_page_img);    //Total number of pages
        $n_this_page_img = ($img_indx+$n_page_img > $n_img)? $n_img : $n_page_img*(1.+$page_num);
        if($img_indx > $n_page_img-1)
        {
            if($generate)
            {
                $turn_page = "<div class=\"turn_page\">
                       <a href=\"$current_sun$prev_page.html\" style=\"text-decoration:none\">Prev page |</a>";
            }
            else
            {
                $turn_page = "<div class=\"turn_page\">
                       <a href=\"{$GLOBALS['PHP_SELF']}?page_num=$prev_page\" style=\"text-decoration:none\">Prev page |</a>";
            }
        }
        else
        {
            $turn_page = "<div class=\"turn_page\">Prev page |";
        }

        if($n_img > $n_this_page_img)
        {
            if($generate)
            {
                $turn_page .= "
                       <a href=\"{$current_sun}$next_page.html\" style=\"text-decoration:none\"> Next page</a></div>";
            }
            else
            {
                $turn_page .= "
                       <a href=\"{$GLOBALS['PHP_SELF']}?page_num=$next_page\" style=\"text-decoration:none\"> Next page</a></div>";
            }
        }
        else
        {
            $turn_page .= " Next page</div>";
        }
        print $open_table;

        $value=array();
        $i_cnt = 0;
        for($i_img=$img_indx; $i_img<$n_this_page_img; $i_img++)
        {
            $url_thmb  = "http://".$img_arr['thumbURL'][$i_img];
            $url_hires = "http://".$img_arr['hiresURL'][$i_img];
            $thmb  = $img_arr['thumb'][$i_img];
            $hires = $img_arr['hires'][$i_img];
            $src        = $url_thmb.$thmb ;
            $ref_string = $url_hires.$hires ;

            $caption = "<span title=\"".$img_arr['copywrite'][$i_img]."\" class='popup'>".$img_arr['caption'][$i_img]."</span>";
            $span = "<span><center>".$img_arr['copywrite'][$i_img]."</center>".$img_arr['description'][$i_img]."</span>";
            $caption = "<a href=\" \">".$img_arr['caption'][$i_img].$span."</a>";

            print $open_td;
            if($generate)
            {
                print <<<END
            <div id="img">
               <a class="img" href="{$ref_string}" onclick="return PopUp(this,$i_img)">
              <img src="{$src}" width="100" height="100"/>{$span}
               </a>
               <br />
               {$caption}
               <br />
            </div>
            </td>
END;
            }
            else
            {
                print <<<END
            <div id="img">
               <a class="img" href="{$GLOBALS['PHP_SELF']}?display=hires&ref_string={$ref_string}" onclick="return PopUp(this,$i_img)">
              <img src="{$src}" width="100" height="100"/>{$span}
               </a>
               <br />
               {$caption}
               <br />
            </div>
            </td>
END;
            }
            if (($i_cnt+1) % $n_col_img == 0)
            {
                print "</tr><tr>";
            }
            $i_cnt++;
        }
        // Adds " " to prevent netscape from complaining
            if (($i_cnt+1) % $n_col_img != 1)
            {
        for($i_img=0;$i_img<($n_col_img - $n_this_page_img % $n_col_img); $i_img++)
        {
            print $open_td;
            print "</td>";
        }
    }
        print "</tr>
           <th colspan=\"{$n_col_img}\" frame=\"above\">{$turn_page}</th>
                   </tbody>
           </table>
           </body>
           </html>
           ";
    }// if statement


    function img_dimensions($url)
    {
        list($img_w,$img_h,$type,$attr)=getImageSize($url);
        if($img_w != $img_h)
        {
            $img_side = ($img_w < $img_h) ? $img_w : $img_h;
        }
        return $img_side;
    }
    //clip:rect(0px 100px 200px 0px)


    if($generate)
    {
        $temp_file="$dir_name$current_sun$page_num.html";
        $fp = fopen($temp_file, "w");
        fwrite($fp, ob_get_contents());
        fclose($fp);
        ob_end_flush();

         `rsync -avz --stats --rsh=ssh  $temp_file sdsilva@polaris.umuc.edu:www/Sun/`;
    }
?>