// AJB 28/10/2001 Better Archive Listings
// Last Update 3/8/2002

// My Type 'BlogArchiveEntry'
function BlogArchiveEntry( theLink, theName  )
{
    this.Link = theLink;
    this.Name = theName;
    
    // Extract Date from Name, Yuk!
    // 09/30/2001 - 10/06/2001
    this.StartDate = new Date( this.Name.slice(6,10), 
        this.Name.slice(0,2)-1, this.Name.slice(3,5) );
    this.EndDate = new Date( this.Name.slice(13+6,13+10),
        this.Name.slice(13+0,13+2)-1, this.Name.slice(13+3,13+5) );
}

BlogArchiveEntry.prototype.Compare = function( rhs ){
    if ( this.StartDate == rhs.StartDate )
        return 0;
    else if ( this.StartDate > rhs.StartDate )
        return 1;
    else
        return -1;
}

BlogArchiveEntry.prototype.MakeLink_UKShort = function(){
    var newName = this.StartDate.getDate()
        + "/"+(this.StartDate.getMonth()+1) 
        + "/"+this.StartDate.getFullYear();
    var outString = "";

    if ( -1 != location.href.indexOf( this.Link ) ){
        // No Link, this is our page!
        outString = "<span class='selected'>" + newName + "</span>";
    } else     {
        // Link
        outString = "<a href='" +
            this.Link + "'>" +
            newName + "</a>";
    }
    return outString;
}

BlogArchiveEntry.prototype.MakeLink_Normal = function(){
    var outString = "";
    
    if ( -1 != location.href.indexOf( this.Link ) ) {
        // No Link, this is our page!
        outString = this.Name;
    } else     {
        // Link
        outString = "<a href='" +
            this.Link + "'>" +
            this.Name + "</a>";
    }
    return outString;
}

// Short/Long listing selection
function ShowAll(){
    var cookie = new obiCookie( "LimitArchive", 365 );
    cookie.SetCookie( 0 );
    location.href = location.href;
}

function ShowSubset( HowMany ){
    var cookie = new obiCookie( "LimitArchive", 365 );
    cookie.SetCookie( HowMany );
    location.href = location.href;
}

// Locate URL in list of archives
function FindIdx( myURL ){
    for ( var n=0; n<BlogInfo.length; n++ )    {
        if ( -1 != myURL.indexOf( BlogInfo[n].Link ) )
            return n;
    }
    return -1;
}

// Make Archive Link Functions
function MakePreviousArchiveLink( myURL, AnchorText )
{
    var idx;
    if ( -1 == ( idx = FindIdx( myURL )) ) return null;
    if ( 0 == idx ) return null;
    
    if (( null == AnchorText ) || ( "" == AnchorText ))
        return BlogInfo[idx-1].MakeLink_UKShort();
    else
        return "<a href='" + BlogInfo[idx-1].Link 
            + "'>" + AnchorText + "</a>";
}

function MakeNextArchiveLink( myURL, AnchorText )
{
    var idx;
    if ( -1 == ( idx = FindIdx( myURL )) ) return null;
    if ( (BlogInfo.length-1) == idx ) return null;

    if (( null == AnchorText ) || ( "" == AnchorText ))
        return BlogInfo[idx+1].MakeLink_UKShort();
    else
        return "<a href='" + BlogInfo[idx+1].Link 
            + "'>" + AnchorText + "</a>";
}

function MakeFirstArchiveLink( AnchorText )
{
    if (( null == AnchorText ) || ( "" == AnchorText ))
        return BlogInfo[0].MakeLink_UKShort();
    else
        return "<a href='" + BlogInfo[0].Link 
            + "'>" + AnchorText + "</a>";
}

function WriteArchiveSection()
{
    document.write( "<ul>" );
    
    var isIndexPage = ( -1 != location.href.indexOf( "index.html" ) );
    var isArchivePage = ( -1 != location.href.indexOf( "" ) );
    var thisIndex = FindIdx( location.href );
    
    // Show Link for Home Page, if this is an archive Page
    if ( !isIndexPage )
        document.write( "<li><a href=\"./\">" 
            + "Última semana</a></li></ul><ul>" );

    //    Next/Previous Links
    var navlink="";
    var wrotelink=false;
    if ( navlink = MakeNextArchiveLink( location.href, "ANTERIORES" )){
        document.write( "<li>" + navlink + "</li>" );
        wrotelink=true;
    }
    if ( navlink = MakePreviousArchiveLink( location.href, "POSTERIORES" )){
        document.write( "<li>" + navlink + "</li>" );
        wrotelink=true;
    }
    if ( true == wrotelink ) document.write( "</ul><ul>" );
    
    // Work out extents of listing
    var startindex = 0;
    var limit = 0;
    
    var limitArchiveCookie = new obiCookie( "LimitArchive", 365 );
    if ( null == limitArchiveCookie.GetCookie() )
        limitArchiveCookie.SetCookie( defaultlimit );
    limit = Number(limitArchiveCookie.GetValue());

    if ( 0 != limit ){    // Showing a subset around the current archive file
        if ( -1 == thisIndex )
            startindex = 0;    // but this is not an archive page, so don't!
        else
            startindex = Math.round( thisIndex - (limit/2) );
        limit += startindex;
    }
    else    // Showing whole lisiting
        limit = BlogInfo.length;
        
    // Clamp selected region to ends
    if ( startindex < 0 ){
        limit -= startindex;
        startindex = 0;
    }
    if ( limit > BlogInfo.length ){
        startindex -= (limit-BlogInfo.length);
        limit = BlogInfo.length;
    }

    // The Archives
    for ( var n=startindex; n<limit; n++ )
    {
        document.write( "<li>" 
            + BlogInfo[n].MakeLink_UKShort()
            +"</li>" );
    }
    
    // Archive Length Options
    if ( 0 == limitArchiveCookie.GetCookie() )
        document.write( "<li><a href=\"javascript:ShowSubset(defaultlimit);\">"
            +"[ALGUNS]</a></li>" );
    else
        document.write( "<li><a href=\"javascript:ShowAll();\">"
            +"[TODOS]</a></li>" );
    document.write( "</ul>" );
}

function WriteArchiveBottom()
{
    //    Next/Previous Links
    var navlink="";
    var wrotelink=false;
    if ( navlink = MakeNextArchiveLink( location.href, 
	     "[<< ANTERIORES >>]" ))
    {
        document.write( navlink );
        wrotelink=true;
    }
    if ( navlink = MakePreviousArchiveLink( location.href, 
        "[<< POSTERIORES >>]" ))
    {
        document.write( (wrotelink?"  ":"") + navlink );
        wrotelink=true;
    }
    if ( !wrotelink )
    {
        document.write( MakeFirstArchiveLink( "[ARQUIVOS SEMANAIS]" ) );
    }
}
