﻿/*
    Output Class
    Used to output bibtex entries to the HTML page. 
    The methods require presence of certain hidden HTML entires in the host page and 
    look for certain HTML IDs to clone the interface.
    
    Here is what needs to be located in the HTML page:
    <div style="display:none;">
        <table id="t_year" cellpadding="6" cellspacing="0" width="538">
            <tbody>
                <tr></td><td height="13"></td></tr>
                <tr>
                    <td width="10"></td>
                    <td>
                        <font class="MainBoldSmall" id="t_year_text">2007</font>
                    </td>
                </tr>
            </tbody>
        </table>
        <table id="t_journal" cellpadding="6" cellspacing="0" width="538">
            <tbody>
                <tr><td height="5"</td></tr>
                <tr>
                    <td width="20"></td>
                    <td>
                        <font class="MainBoldSmall">J</font><font class="MainBoldSmalls">OURNALS AND </font><font class="MainBoldSmall">M</font><font class="MainBoldSmalls">AGAZINES:</font>
                    </td>
                </tr>
            </tbody>
        </table>
        <table id="t_conference" cellpadding="6" cellspacing="0" width="538">
            <tbody>
                <tr><td height="5"</td></tr>
                <tr>
                    <td width="20"></td>
                    <td>
                        <font class="MainBoldSmall">C</font><font class="MainBoldSmalls">ONFERENCES:</font>
                    </td>
                </tr>
            </tbody>
        </table>
        <table id="t_entry" cellpadding="6" cellspacing="0" width="538">
            <tbody>
                <tr><td height="5"></td></tr>
                <tr>
                    <td width="20"></td>
                    <td id="t_entry_td1" bgcolor="#c1c1c1" width="25" align="center" valign="top"><font class="Main" id="t_entry_no">1.</font></td>
                    <td id="t_entry_td2" bgcolor="#c1c1c1">
                        <font class="Main" id="t_entry_text"></font>&nbsp;
                        <a style="color: rgb(210, 97, 21); text-decoration: none;" id="t_link" class="Links" href="">PDF</a>
                        <font class="Main" id="t_seperator">|</font>
                        <a style="color: rgb(210, 97, 21); text-decoration: none;" id="t_bibtex_link" class="Links" href="">bibtex</a>
                    </td>
                </tr>
                <tr></tr>
                <tr id="t_bibtex_tr" style="display:none;">
                    <td width="20"></td>
                    <td width="25"></td>
                    <td class="bibtex" id="t_bibtex_text">
                        the bibtex entry goes here
                    </td>
                </tr>
            </tbody>
        </table>
    </div>        
*/
function Output()
{
    this.entryCount = 0;             //Number of entries
    this.yearCount = 0;
    this.conferenceCount = 0;
    this.journalCount = 0;
}

function getElementById(node, id)
{
    if (node.id == id)
        return node;
    for (var i = 0; i < node.childNodes.length; i++)
    {
        var ret = getElementById(node.childNodes[i], id);
        if (ret != null)
            return ret;
    }
    
    return null;
}
        
Output.prototype.AppendYear = function(year)
{
    this.yearCount++;

    var content = document.getElementById("id_content");
    var e = document.getElementById("t_year");
    var node = e.cloneNode(true);
    node.id = "t_year" + this.yearCount;
    //FireFox does not support node.all[id_text], so we have to take the long path, this is in accordance with W3C standard
    var e = getElementById(node, "t_year_text");
    e.id = "t_year_text" + this.yearCount;
    e.innerHTML = year;            

    content.appendChild(node);
}

Output.prototype.AppendJournal = function()
{
    this.journalCount++;

    var content = document.getElementById("id_content");
    var e = document.getElementById("t_journal");
    var node = e.cloneNode(true);
    node.id = "t_journal" + this.journalCount;

    content.appendChild(node);
}

Output.prototype.AppendConference = function()
{
    this.conferenceCount++;
    
    var content = document.getElementById("id_content");
    var e = document.getElementById("t_conference");
    var node = e.cloneNode(true);
    node.id = "t_conference" + this.conferenceCount;

    content.appendChild(node);
}

Output.prototype.AppendTechRep = function()
{
    this.conferenceCount++;
    
    var content = document.getElementById("id_content");
    var e = document.getElementById("t_techrep");
    var node = e.cloneNode(true);
    node.id = "t_techrep" + this.conferenceCount;

    content.appendChild(node);
}

Output.prototype.AppendEntry = function(entry, totalLen)
{
    this.entryCount++;

    var content = document.getElementById("id_content");
    var e = document.getElementById("t_entry");
    var node = e.cloneNode(true);
    node.id = "t_entry" + this.entryCount;            

    //FireFox does not support node.all[id_text], so we have to take the long path
    var e = getElementById(node, "t_entry_text");
    e.innerHTML = entry.toString();
    
    var e = getElementById(node, "t_entry_icon");
    if (e != null)
    {
        url = entry.getProperty("icon");
        if (url != null)
            e.src = url;
        else
            e.style.display = "none";
    }
    
    var e1 = getElementById(node, "t_entry_td1");
    var e2 = getElementById(node, "t_entry_td2");
    if (this.entryCount % 2 == 1)
    {
        if (e1 != null) e1.className = "dark" ;
        if (e2 != null) e2.className = "dark" ;
    }
    else
    {
        if (e1 != null) e1.className = "light" ;
        if (e2 != null) e2.className = "light" ;
    }
    
    e = getElementById(node, "t_entry_no");
    if (e != null)
        e.innerHTML = (totalLen - this.entryCount + 1);
        
    var link = getElementById(node, "t_link");
    var linkname = entry.getProperty("linkname");
    if (linkname != null) link.innerHTML = linkname; 
    else
    {
        link.style.display = "none";
        e = getElementById(node, "t_seperator");
        e.style.display = "none";
    }
    var url = entry.getProperty("link");
    if (url != null)
    {
        link.href = url;
        // Add Google Analytics counter for the resource
        link.onclick = function()
        {
            urchinTracker(url.toString())
        }
    }

    e = getElementById(node, "t_bibtex_text");
    e.innerHTML = entry.bibtexHTML;

    var link2 = getElementById(node, "t_bibtex_link");
    link2.id = "t_bibtex_link" + this.entryCount;
    link2.onclick = function()          //toggle display of bibtex
    {
        e = getElementById(node, "t_bibtex_tr");
        if (e.style.display == "none")
            e.style.display = "";
        else
            e.style.display = "none";
        return false;
    }

    content.appendChild(node);
}

function InitView()
{
    var content = document.getElementById("id_content2");
    
    content.style.display = "no" + "ne";
}

/*
    Display functions, can be customized based on the page logic.
*/
function LoadRecentPublications(path, num)
{ 
    var req = null; 
    
    if(window.XMLHttpRequest)
        req = new XMLHttpRequest(); 
    else if (window.ActiveXObject)
        req  = new ActiveXObject("Microsoft.XMLHTTP");
        
    var content = document.getElementById("id_content");

    content.innerHTML = "Loading Publications...";
    req.onreadystatechange = function()
    { 
        if(req.readyState == 4)
        {
            if(req.status == 200)
            {
                var str = new String(req.responseText);
                var bibtex = new Bibtex(str);                        
                bibtex.Sort("date", false);
                
                content.innerHTML = "";                        
                
                output = new Output();
                var last_year = "";
                var confs = new Array();
                var jrnls = new Array();
                num = Math.min(bibtex.Entries.length,num);
                var i = 0;
                while (jrnls.length + confs.length < num)
                {
                    var year = bibtex.Entries[i].getProperty("year");
                    if (year != last_year)
                        last_year = year;
                    
                    var j = i;
                    while (jrnls.length + confs.length < num && j < bibtex.Entries.length && bibtex.Entries[j].getProperty("year") == year)
                    {
                        if ( bibtex.Entries[j].Type == "article")
                            jrnls[jrnls.length] = bibtex.Entries[j];
                        j++;
                    }
                    
                    k = i;
                    while (jrnls.length + confs.length < num && k < bibtex.Entries.length && bibtex.Entries[k].getProperty("year") == year)
                    {
                        if ( bibtex.Entries[k].Type == "inproceedings")
                            confs[confs.length] = bibtex.Entries[k];
                        k++;
                    }
                    
                    i = Math.max(k, j);                            
                }
                
                output.AppendJournal();
                for (i = 0; i < jrnls.length; i++)
                    output.AppendEntry(jrnls[i], -1);

                output.AppendConference();                        
                for (i = 0; i < confs.length; i++)
                    output.AppendEntry(confs[i], -1);
            }    
            else    
            {
                content.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;
            }    
        } 
    }; 
    req.open("GET", path, true); 
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    req.send(null);
}        

function LoadSelectedPublications(path)
{
    var req = null; 
    
    if(window.XMLHttpRequest)
        req = new XMLHttpRequest(); 
    else if (window.ActiveXObject)
        req  = new ActiveXObject("Microsoft.XMLHTTP"); 

    var content = document.getElementById("id_selected");
    content.innerHTML = "Loading Publications...";
    req.onreadystatechange = function()
    { 
        if(req.readyState == 4)
        {
            if(req.status == 200)
            {
                var str = new String(req.responseText);
                var bibtex = new Bibtex(str);                        
                bibtex.Sort("selected", true);
                
                content.innerHTML = "";                    
                
                for (var i = 0; i < bibtex.Entries.length; i++)
                {
                    var entry = bibtex.Entries[i];
                    if (entry.getProperty("selected") == null)
                        break;
                    content.innerHTML += "<div class='author'>" + entry.getProperty("author") + "</div>";
                    var link = entry.getProperty("link");
                    if (link != null)
                        content.innerHTML += sprintf("<a class='Links' href='%s' target=_blank onclick='javascript:urchinTracker(\"%s\")'><img border='0' src='images/pdf-48x48.png' align='left' /></a>", link, link);
                    content.innerHTML += sprintf("<div class='title'>%s</div>",entry.getProperty("title"));
                    content.innerHTML += sprintf("<div class='publication'>%s, %s</div>", entry.getProperty("publication"), entry.getProperty("year"));
                    content.innerHTML += "<br/>";
                }
            }    
            else    
            {
                content.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;
            }    
        } 
    }; 
    req.open("GET", path, true); 
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    req.send(null);
}

function LoadAllPublications(path)
{ 
    var req = null; 
    
    if(window.XMLHttpRequest)
        req = new XMLHttpRequest(); 
    else if (window.ActiveXObject)
        req  = new ActiveXObject("Microsoft.XMLHTTP"); 

    var content = document.getElementById("id_content");

    content.innerHTML = "Loading Publications...";
    req.onreadystatechange = function()
    { 
        if(req.readyState == 4)
        {
            if(req.status == 200)
            {
                var str = new String(req.responseText);
                var bibtex = new Bibtex(str);                        
                bibtex.Sort("date", false);
                
                content.innerHTML = "";                        
                
                output = new Output();
                var last_year = "";
                for (var i = 0; i < bibtex.Entries.length; i++)
                {
                    var year = bibtex.Entries[i].getProperty("year");
                    if (year != last_year)
                    {
                        last_year = year;
                        output.AppendYear(year);                                
                    }
                    
                    var j = i;
                    var first = true;
                    while (j < bibtex.Entries.length && bibtex.Entries[j].getProperty("year") == year)
                    {
                        if ( bibtex.Entries[j].Type == "article")
                        {
                            if (first) 
                            {
                                output.AppendJournal();
                                first = false;
                            }
                            output.AppendEntry(bibtex.Entries[j], bibtex.Entries.length);
                        }
                        j++;
                    }
                    var next_i = j - 1;
                    
                    j = i;
                    first = true;
                    while (j < bibtex.Entries.length && bibtex.Entries[j].getProperty("year") == year)
                    {
                        if ( bibtex.Entries[j].Type == "inproceedings")
                        {
                            if (first) 
                            {
                                output.AppendConference();
                                first = false;
                            }
                            output.AppendEntry(bibtex.Entries[j], bibtex.Entries.length);
                        }
                        j++;
                    }                    
                    next_i = Math.max(next_i, j -1);
                    
                    j = i;
                    first = true;
                    while (j < bibtex.Entries.length && bibtex.Entries[j].getProperty("year") == year)
                    {
                        if ( bibtex.Entries[j].Type == "techreport")
                        {
                            if (first) 
                            {
                                output.AppendTechRep();
                                first = false;
                            }
                            output.AppendEntry(bibtex.Entries[j], bibtex.Entries.length);
                        }
                        j++;
                    }                    
                    next_i = Math.max(next_i, j -1);

                    i = next_i;
                }
            }    
            else    
            {
                content.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;
            }    
        } 
    }; 
    req.open("GET", path, true); 
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    req.send(null);
}    

function Capitalize(text, class1, class2)
{
    var arr = text.split(" ");
    for (var i = 0; i < arr.length; i++)
    {
        var lower = arr[i].toLowerCase();
        if (lower == "and" || lower == "to" || lower == "of" || lower == "from")            //add more propositions and conjunctives here, as needed
        {
            document.write('<font class="' + class2 + '">' + arr[i].toUpperCase() + '</font>');            
        }
        else
        {
            document.write('<font class="' + class1 + '">' + arr[i].substr(0, 1).toUpperCase() + '</font>');
            document.write('<font class="' + class2 + '">' + arr[i].substr(1, arr[i].length - 1).toUpperCase() + '</font>');
        }
        document.write(' ');
    }
}

function InitMenu(selected)
{
    var arr = objMenu.text;  
    var link = objMenu.link;
    
    document.write('<table cellpadding="0" cellspacing="0" width="150" id="id_menu">');
    document.write('<tbody>');
    
    for (var i = 0; i < arr.length; i++)
    {
        document.write('<tr bgcolor="#aaaaaa" height="1"><td></td><td></td></tr>');
        if (arr[i] == selected)
        {
            document.write('<tr class="MenuSelected">');
            document.write('<td height="23" width="5"></td>');
            document.write('<td>');
            document.write('<script type="text/javascript">Capitalize("' + arr[i] + '", "Menu", "Menus");</script>');
            document.write('</td>');
            document.write('</tr>');
        }
        else
        {
            var menu_str = 'id_menu' + i;
            document.write('<tr class="Menu" id="' + menu_str + '">');
            document.write('<td height="23" width="5"></td>');
            document.write('<td>');
            document.write('<a href="' + link[i] + '" onmouseover="OverMenu(\'' + menu_str + '\')" onmouseout="OutMenu(\'' + menu_str + '\')">');
            document.write('<script type="text/javascript">Capitalize("' + arr[i] + '", "Menu", "Menus");</script>');
            document.write('</a>');
            document.write('</td>');
            document.write('</tr>');
        }
    }

    document.write('<tr bgcolor="#aaaaaa" height="1"><td></td><td></td></tr>');
    document.write('</tbody>');
    document.write('</table>');
}

function WriteEmail()
{
    document.write('<a class="Links" href="mailto:' + objContact.Email + '">' + objContact.Email + '</a>');
}