﻿/*
    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" onmouseover="OverLink('PreprintLink')" onmouseout="OutLink('PreprintLink')" 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" onmouseover="" onmouseout="" 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.AppendBook = function()
{
    this.journalCount++;
    
    var content = document.getElementById("id_content");
    var e = document.getElementById("t_book");
    var node = e.cloneNode(true);
    node.id = "t_book" + 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 e1 = getElementById(node, "t_entry_td1");
    var e2 = getElementById(node, "t_entry_td2");

    if (this.entryCount % 2 == 1)
    {
        e1.className = "dark" ;
        e2.className = "dark" ;
    }
    else
    {
        e1.className = "light" ;
        e2.className = "light" ;
    }
    
    e = getElementById(node, "t_entry_no");
    if(totalLen != -1)
        e.innerHTML = (totalLen - this.entryCount + 1);
    else        
        e.innerHTML = "";
           
    var link = getElementById(node, "t_url");
    var url = entry.getProperty("url");
    if (url == null)
    {
        link.style.display = "none";
        e = getElementById(node, "t_seperator1");
        e.style.display = "none";
    }
    link.href = url;
    link.onmouseover = function()
    {
        link.style.color = "#D26115"; 		
        link.style.textDecoration = "underline";
    }
    link.onmouseout = function()
    {
        link.style.color = "#D26115"; 		 
        link.style.textDecoration = "none"; 		 
    }

    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_seperator2");
        e.style.display = "none";
    }
    var url = entry.getProperty("link");
    link.href = url;
    link.onmouseover = function()
    {
        link.style.color = "#D26115"; 		
        link.style.textDecoration = "underline";
    }
    link.onmouseout = function()
    {
        link.style.color = "#D26115"; 		 
        link.style.textDecoration = "none"; 		 
    }

    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.onmouseover = function()
    {
        link2.style.color = "#D26115"; 		
        link2.style.textDecoration = "underline";
    }
    link2.onmouseout = function()
    {
        link2.style.color = "#D26115"; 		 
        link2.style.textDecoration = "none"; 		 
    }
    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);
}

/*
    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 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" || bibtex.Entries[j].Type == "misc")
                        {
                            if (first) 
                            {
                                output.AppendTechRep();
                                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 == "book" || bibtex.Entries[j].Type == "incollection")
                        {
                            if (first) 
                            {
                                output.AppendBook();
                                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 InitView(selectedMenu)
{
    var content = document.getElementById("id_content2");   
    content.style.display = "no" + "ne";
    
    /*var menu = document.getElementById(selectedMenu);
    menu.className = "MenuSelected";   
    
    var a = menu.getElementsByTagName("a");
    a = a[0];           //Expecting only one <a> element
    
    a.onmouseover = function(){};
    a.onmouseout = function(){};*/
}

