#!/usr/bin/perl -w
use CGI qw(:all);

#open categories file for reading
open(CATG,"anim_categories.csv")|| die "cannot open categories list";

 
#read and discard first TOC line, that's just header/descriptors
$line = <CATG>;
#for each category file line
$count = 0;
while($line=<CATG>){
    #parse categories line
    @fld = split(/","/,$line);
        #field              field 2 indexin
        #field 0 category   field 3 indexout
        #field 1 directory  field 4 title
    $fld[0] =~ s/"//;
    $fld[4] =~ s/"//;
    $count=$count+1;
    $cats[$count]=$fld[0];
    $dirs[$count]=$fld[1];
    $inis[$count]=$fld[2];
    $outs[$count]=$fld[3];
    $tlts[$count]=$fld[4];
    $TOClink[$count] = $fld[1]."/".$fld[3];
    $TOCdata[$count] = $fld[1]."/".$fld[2];
}
#close categories file
close(CATG);
#write credits file
credits();

for($i=1 ; $i<=$count ; $i++ ){
       #Process each TOC category file SUB EACHCAT 
       #GOSUB EACHCAT(indexin, indexout,directory,title)
        eachcat($TOCdata[$i],$TOClink[$i],$dirs[$i],$tlts[$i]);
        print "writing index file ".$i." of ".$count."\n";
}



#SUB process list of directories and export to TOC html file
#EACHCAT(indexin, indexout,directory,title)
sub eachcat{  
    $infile=$_[0];
    $outfile=$_[1];
    $dir=$_[2];
    $title=$_[3];
    #open subcategory data file
    open(IND_IN,$infile)|| die "cannot open $_[0] for reading";
    #read 'n drop first line, its just column headers
    $iline=<IND_IN>;

    #open subcategory html file for writing
    open(IND_OUT,">".$outfile)|| die "cannot open TOC file for writing";

    #write top of subcategory html file
    print IND_OUT <<TOP_O_THIS_IND;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>
  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8">
  <title>Physics Animations</title>
  <meta name="CHANGEDBY" content="Michael Gallis">
  <meta name="Keywords" content="physics,animation,astronomy,mechanics,electricity,magnetism,optics,vectors,waves"> 
  <style>
  
body {margin:0;
 padding:0 10px 10px 10px;
 border:0;
 height:100%;
 overflow-y:auto;
 background:#66A;
 color:#000;
}
body {  font-family: Verdana, Arial, Geneva, Helvetica, sans-serif;
        font-size: 11px;
}
#page {
 margin:48px 0px 10px 7em;
 display:block;
 padding:5px;
  top:5em;
}
#page a:hover {background:#44A; color:#FFF;}
#page a:visited, #page a , #page em{
 text-align:center;
 text-decoration:none;
 color:#005;
}
#titler {
 top:0px;
 left:0px;
 right:0px;
 background:#44A; 
 color:#FFF;
 font-size: 18px;
 font-weight: bold;
 display:block;
 width:100%;
 height:40x;
 position:fixed;
 padding:1px;

 text-align:center;
}
#titler a:visited,#titler a{
 color:#AFA;
} 
* html #titler {position:absolute;}

#menu {
 background:#DDD; 
 color:#000; 
 display:block;
 top:6em;
 left:2px;
 width:7em;
 position:fixed;
 border:4px solid #aaa;
 padding:1px;
 text-align:center;
}
* html #menu {position:absolute;}
#menu a:visited, #menu a , #menu em{
 display:block;
 width:6em;
 margin:0 auto;
 border-top:1px solid #000;
 border-bottom:1px solid #000;
 text-align:center;
 text-decoration:none;
 line-height:20px;
 color:#005;
}
#menu em{ color:#ccddff;
}
#menu em:first-child { border-top: none }
#menu a:hover {background:#fff; color:#000099;}
.clear {clear:both;}

</style>
    <!--[if IE 6]>
        <style type="text/css">/*<![CDATA[*/ 
        html {overflow-x:auto;overflow-y:hidden;}/*]]>*/</style>
    <![endif]-->
</head>

<body>
<div id="titler">
$title
<font size=-2 FACE="sans-serif, Arial, Helvetica, Geneva">
<br>
These animations are available for use under a
<a rel="license" href="http://creativecommons.org/licenses/by-nc/2.0/">
Creative Commons License</a>.
<br>
</font>
</div>
<div id="page">
    <TABLE  WIDTH=95% BORDER=1 CELLPADDING=0 CELLSPACING=1 bgcolor="#FFFFFF"  >
TOP_O_THIS_IND

    #dummy first subcat
    $thissubcat="zzz";
    #for each subcategory file line
    while($iline=<IND_IN>){
        #parse item line
        #$itemfld[0 through 5] = ID  subcategory    media   thumb   comment html
        @itemfld = split(/"?,"?/,$iline);
        #if its a new category, write the category header
        if($itemfld[1] ne $thissubcat){
            $thissubcat=$itemfld[1];
            print IND_OUT <<SUBCAT_THIS_IND;
            <TR>
                <TD COLSPAN=2 WIDTH=100%>
                    <P ALIGN=CENTER><font size=+1>$thissubcat</font></P>
                </TD>
            </TR>
SUBCAT_THIS_IND
        }
        #write the component table row
            $em1="embeder".$itemfld[0].".html";
            $em2="embederQ".$itemfld[0].".html";
            print IND_OUT <<ITEM_ROW_THIS_IND1;        
            <TR>
                <TD WIDTH=29%>
                    <P ALIGN=CENTER>
                    <a name=$itemfld[0] >
                    <IMG SRC="$itemfld[3]"  ALIGN=BOTTOM  BORDER=8>
                    </a>
                    </P>
            </TD>
                <TD WIDTH=71% VALIGN=TOP>
                    <P> $itemfld[4] </P>
                    <P><font size=-2>
ITEM_ROW_THIS_IND1

            print IND_OUT <<ITEM_ROW_THIS_IND2;        
                    |<A HREF="$itemfld[2]" TARGET="_new">
                    <SPAN TITLE="Direct link to the media file for download or default player.">
                    Media</SPAN> </A> |
                    <A HREF="$em1" TARGET="_new">
                    <SPAN TITLE="Video embedded in a web page using default player.  This only works with Internet Explorer!">
                    Old Embedded</SPAN> </A> |
                    <A HREF="$em2" TARGET="_new">
                    <SPAN TITLE="Video embedded in a web page using QuickTime player.  (preferred, with FireFox)">
                    QT Embedded</SPAN> </A> | 
ITEM_ROW_THIS_IND2

            if($itemfld[5] ){ 
                chomp($itemfld[5]);
                $itemfld[5] =~ s/"//;
                if($itemfld[5] =~ /htm/){
                    print IND_OUT <<ITEM_ROW_THIS_IND_HTML;
                    <A HREF="$itemfld[5]" TARGET="_new">
                    <SPAN TITLE="Video embedded in customized web page.  This may not work with all browsers!">
                    HTML</SPAN> </A> | 
ITEM_ROW_THIS_IND_HTML
                    
                }
            }


            #finish up this row demarcation
            print IND_OUT <<ITEM_ROW_THIS_IND3;        
                    </font></P>
                </TD>
            </TR>
ITEM_ROW_THIS_IND3
        
        
        #write the various html files for the mediafiles
        #write embedder1(directory,embedding html file,media file, comment)
         embedder1($dir,$em1,$itemfld[2],$itemfld[4]);
         embedder2($dir,$em2,$itemfld[2],$itemfld[4]);
        
     }

    #finish off and close this  indexer file
    print IND_OUT <<END_O_THIS_IND;
    </TABLE>
    </div><div id="menu">
    <DL>
    <DT>
    <strong>Categories</strong>
END_O_THIS_IND
    for($j=1 ; $j<=$count ; $j++ ){
        print IND_OUT "<dt>";
        $thislink="../".$TOClink[$j];
        print IND_OUT a({-href=>$thislink},$cats[$j]);
        print IND_OUT "\n";
    }
    print IND_OUT <<BOT_O_IND_OUT;
    <dt><A HREF="../student" >Student</A>
    <dt><A HREF="../Phys_anim.htm" >HOME</A>
    </DL>
    </div>
    </body>
    </html>
BOT_O_IND_OUT

    close(IND_OUT);
    
    #close the subcategory data file 
    close(IND_IN);
}

#embedder1(directory,embedding html file,media file, comment)
sub embedder1{
    $dir=$_[0];
    $outfile=$_[1];
    $media=$_[2];
    $comment=$_[3];
    #print "$dir $outfile  \n";
    #open embedder html file
    $fname=$_[0]."/".$_[1];
    open(EMBD,">".$fname)|| die "cannot open $fname for writing";
    #write embedder html file
    print EMBD <<ALL_OF_EMBD;
        <html>
        <head>
        <title> embedded media file </title>
        </head>
        <body link="#EEEEFF" alink="#ccffcc" vlink="#ffffcc" bgcolor="#000099" text="#FFFFEE">
            <center><table>
            <tr><td>
                <center><embed src="$media" loop=-1 ></center>
            </td></tr>
            <tr><td><center>$comment </center></td></tr>
            </table></center>
        </body>
        </html>
ALL_OF_EMBD

    #close embedder html file
   close(EMBD);
}

#embedder2(directory,embedding html file,media file, comment)
sub embedder2{
    $dir=$_[0];
    $outfile=$_[1];
    $media=$_[2];
    $comment=$_[3];
    #print "$dir $outfile  \n";
    #open embedder html file
    $fname=$_[0]."/".$_[1];
    open(EMBD2,">".$fname)|| die "cannot open $fname for writing";
    #write embedder html file
    print EMBD2 <<ALL_OF_EMBD2;
<html>
<head>
<title> quicktime embedded media file </title>
<style type="text/css">
<!--
/* object stuff here */
/*  mrg note: adapted from page example at http://realdev1.realise.com/rossa/rendertest/quicktime.html */

/* hides the second object from all versions of IE */
* html object.avi {
    display: none;
}

/* displays the second object in all versions of IE apart from 5 on PC */
* html object.avi/**/ {
    display: inline;
}

/* hides the second object from all versions of IE >= 5.5 */
* html object.avi {
    display/**/: none;
}
-->
</style>
</head>
<body LANG="en-US" link="#EEEEFF" alink="#ccffcc" vlink="#ffffcc" bgcolor="#000099" text="#FFFFEE">
<center><table BORDER=0 CELLPADDING=0 CELLSPACING=0>
<tr><td><center> 
            <p><object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" 
                codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="512" height="400">
                <param name="src" value="$media" />
                <param name="controller" value="true" />
                <param name="LOOP" value="TRUE" />
                <object type="video/quicktime" data="$media" width="512" height="400" class="avi">
                    <param name="controller" value="true" />
                    <param name="LOOP" value="TRUE" />
                    Error text.
                </object>
            </object></p></td></tr><tr><td><center>$comment </td></tr>
</table></center>
</body>
</html>
ALL_OF_EMBD2

    #close embedder html file
   close(EMBD2);
}

############################################################################
# Sub name:     credits
# Input:        none
# Output:       none
# Description:  writes credits file
#
############################################################################
sub credits {
#open TOC html file for writing/and write the top stuff
open(TOC,">Phys_anim.htm")|| die "Phys_anim2.htm";
$myemail="mrg3@"."psu.edu";
print TOC <<TOP_O_TOC;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>
  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8">
  <title>Physics Animations</title>
  <meta name="CHANGEDBY" content="Michael Gallis">
  <meta name="Keywords" content="physics,animation,astronomy,mechanics,electricity,magnetism,optics,vectors,waves"> 
  <style>
  
body {margin:0;
 padding:0 0px 0px 0px;
 border:0;
 height:100%;
 overflow-y:auto;
 background:#66A;
 color:#000;
}
body {  font-family: Verdana, Arial, Geneva, Helvetica, sans-serif;
        font-size: 11px;
}
#page {
 margin:69px 20px 10px 9em;
 display:block;
 padding:5px;
     background:#FFF;
    color:#000;
 top:9em;

}
#page a:hover {background:#44A; color:#FFF;}
#page a:visited, #page a , #page em{
 text-align:center;
 color:#502;
}
#titler {
 top:0px;
 left:0px;
 right:0px;
 background:#44A; 
 color:#FFF;
 font-size: 18px;
 font-weight: bold;
 display:block;
 width:100%;
 height:59px;
 position:fixed;
 padding:0px;

 text-align:center;
}
* html #titler {position:absolute;}

#menu {
 background:#DDD; 
 color:#000; 
 display:block;
 top:9em;
 left:2px;
 width:7em;
 position:fixed;
 border:4px solid #aaa;
 padding:1px;
 text-align:center;
}
* html #menu {position:absolute;}
#menu a:visited, #menu a , #menu em{
 display:block;
 width:6em;
 margin:0 auto;
 border-top:1px solid #000;
 border-bottom:1px solid #000;
 text-align:center;
 text-decoration:none;
 line-height:20px;
 color:#005;
}
#menu em{ color:#ccddff;
}
#menu em:first-child { border-top: none }
#menu a:hover {background:#fff; color:#000099;}
.clear {clear:both;}

</style>
    <!--[if IE 6]>
        <style type="text/css">/*<![CDATA[*/ 
        html {overflow-x:auto;overflow-y:hidden;}/*]]>*/</style>
    <![endif]-->

</head>

<body>
<div id="titler">
<img src="new_title_bar.png"  align="top" border="0">
</div>

<div id="page">
<br>
Simple Animations of Basic Concepts in Physics and Astronomy<br>
by Dr. Michael R. Gallis<br>
<a href="mailto:$myemail ">$myemail </a><br>

<blockquote>
Select a category from the menu at left to browse the catalogue of animations by category.
Most animations are 512 by 384 pixel avi files and are generally several megabytes long. 
Small screenshots are provided.</blockquote>
<blockquote>
    <span style="font-style:bold;">NEW:</span>
    <a href="http://phys23p.sl.psu.edu/CWIS/index.php">CWIS Portal </a>
    to the Animations.  The portal allows searching as well as 
    <a href="http://phys23p.sl.psu.edu/CWIS/SPT--BrowseResources.php">browsing</a> 
    of animation topics. Users are invited to use the portal to provide feedback 
    and suggestions. <br>
</blockquote>
<blockquote>
    POV-Ray, etc. Source Files <a href="povray">directory</a>
</blockquote>
<blockquote>-=Papers and Presentations=- <br><hr></blockquote>
<blockquote>
    <i>Animations for Introductory Courses</i>, Dr. Michael R. Gallis 
    and Dr. Ping Wang presented at the 2004 
    <a href="http://www.asee.org/">ASEE</a>
    Annual Conference and Exposition: &nbsp; 
    <a href="papers/asee/Start.html" target="_new2">-Presentation</a>&nbsp; 
    <a href="papers/asee/asee04_paper_final_a.pdf">-Paper</a>
</blockquote>
<blockquote>
    <span style="font-style: italic;">
    3-D Animations for Introductory Electromagnetism</span>, MichaelR. Gallis 
    presented at CPS-AAPT 2005 Spring Meeting &nbsp;
    <a href="papers/cps-ppt_2_26/Start.html" target="_new2">-Presentation</a><br>

    <a href="papers/geodesic_pov/povray_geodesics.pdf">
    <i>Elementary Animated Illustrations of Geodesics using POV-Ray</i> </a>
    , Michael R. Gallis (unpublished)
</blockquote>
<blockquote>See also 
    <a href="http://phys23p.sl.psu.edu/%7Emrg3/mathanim/vmath.html" target="_top"> 
    Visual Mathematics</a> by Dr. Michael R. Gallis and Dr. Ping Wang
</blockquote>

<!-- Creative Commons License -->
<a rel="license" href="http://creativecommons.org/licenses/by-nc/2.0/"><img
 alt="Creative Commons License"
 src="http://creativecommons.org/images/public/somerights20.gif"
 border="0"></a><br>
These works are licensed under a <a rel="license"
 href="http://creativecommons.org/licenses/by-nc/2.0/">Creative Commons
License</a>.
<!-- /Creative Commons License --><!--
<rdf:RDF xmlns="http://web.resource.org/cc/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Work rdf:about="">
   <dc:type rdf:resource="http://purl.org/dc/dcmitype/MovingImage" />
   <license rdf:resource="http://creativecommons.org/licenses/by-nc/2.0/" />
</Work>
<License rdf:about="http://creativecommons.org/licenses/by-nc/2.0/">
   <permits rdf:resource="http://web.resource.org/cc/Reproduction" />
   <permits rdf:resource="http://web.resource.org/cc/Distribution" />
   <requires rdf:resource="http://web.resource.org/cc/Notice" />
   <requires rdf:resource="http://web.resource.org/cc/Attribution" />
   <prohibits rdf:resource="http://web.resource.org/cc/CommercialUse" />
   <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
</License>
</rdf:RDF>
-->
</div>
<div id="menu">
<DL>
<DT>
<strong>Categories</strong>
TOP_O_TOC
for($i=1 ; $i<=$count ; $i++ ){
    print TOC "<dt>";
    print TOC a({-href=>$TOClink[$i]},$cats[$i]);
    print TOC "\n";
}
print TOC <<BOT_O_TOC;
<dt><A HREF="student" >Student</A>
<dt><A HREF="Phys_anim.htm" >HOME</A>
</DL>
</div>
</body>
</html>
BOT_O_TOC
close(TOC);


    
}
