function parsePicData( inputStr )
{
    var aHash = {};
    var aField, xTime, oPhoto;

    var aRow = inputStr.split( ',' );

    for ( var i = 0; i < aRow.length; i++ )
    {
        if ( '' == aRow[i] ) continue;

        var aField = aRow[i].split('|');

        var xTime = 'x' + Math.floor( aField[1] / 86400 );

        if ( 'undefined' == typeof aHash[ xTime ] )
        {
            aHash[ xTime ] = [];
        }

        oPhoto = {
            id:   aField[0],
            time: aField[1],
            view: aField[2],
            path: aField[3]
        };

        aHash[ xTime ].push( oPhoto );
    }

    return aHash;
}

function loadSelector( oData )
{
    var iTime, oTime, sContent, oOption;

    firstDate = jsUrlHash.get('AL');

    for( var sKey in oData )
    {
        iTime = parseInt( sKey.substring( 1 ) );
        if (!firstDate)
            firstDate = iTime;
        oTime = new Date();
        oTime.setTime( iTime * 86400000 );
        sDate = formatDate( oTime, 'dd NNN yyyy' );
        sCount = ' (' + oData[ sKey ].length + ')';

        if (typeof archiveSelect != "undefined" && archiveSelect != null){

            // DropDown append
            oOption = document.createElement('option');
            oOption.appendChild( document.createTextNode( sDate + sCount ) );
            oOption.value = iTime;

            if ( 'function' == typeof archiveSelect.add )
                archiveSelect.add( oOption, null );
            else
                archiveSelect.appendChild( oOption );
        }

        // dataList append
        if (typeof archiveList != "undefined" && archiveList != null){

            oSP = document.createElement('SPAN');
            oSP.appendChild ( document.createTextNode ( sCount ) );
            oDD = document.createElement('li');
            oDD.appendChild( new getListLink( sDate, iTime ) );
            oDD.appendChild( oSP );
            archiveList.appendChild( oDD );
        }
    }

    return firstDate;
}

function markSelector( val )
{
    for ( var i = 0; i < archiveSelect.options.length; i++ )
    {
        var item = archiveSelect.options[ i ];

        if ( item.value == val )
        {
            archiveSelect.selectedIndex = i;
            break;
        }
    }
}

// returns object
function getListLink( sDate, stamp )
{
    var self = this;

    this.date = stamp;

    var a = document.createElement('A');

    a.appendChild( document.createTextNode( sDate ) );
    a.id = 'AL_'+stamp;
    a.href = jsUrlHash.set( 'AL', stamp, true );

    a.onclick = function()
    {
        showArchive( self.date );
        if ( 'object' == typeof archPage && archPage != null &&
             'number' == typeof archPage.scrollTop )
            archPage.scrollTop = 0;
        this.blur();
        return false;
    }

    return a;
}

// returns object
function getPhotoCell( bAuthorized, oPhoto, sLocation )
{
    var self = this;

    var imgSpacer  = '/com/img/spacer.gif';
    if (typeof overlayURL != "undefined" && overlayURL != null){
        var imgOverlay = overlayURL;
    } else {
        var imgOverlay = '/com/img/overlay.gif';
    }

    var li  = document.createElement('LI');
    var a   = document.createElement('A');
    var img = document.createElement('IMG');
    var span= document.createElement('SPAN');

    this.bigPic = bAuthorized ? picPrefixBig + oPhoto.path : null;
    var liPath  = bAuthorized ? picPrefix : picPrefixOverlay;

    li.style.backgroundImage = 'url(' + liPath + oPhoto.path + ')';

    var time = new Date();
    time.setTime( oPhoto.time * 1000);
    span.innerHTML = formatDate( time, 'E d NNN yyyy HH:mm' );

    img.src = bAuthorized ? imgSpacer : imgOverlay;
    img.height = 120;
    img.width = 160;
    img.alt = '';

    a.href = sLocation;

    a.onmouseout = function() {
        window.status = '';
        return true;
    }

    if ( !bAuthorized )
    {
        a.onmouseover = function() {
            window.status = 'Click on picture to log in';
            return true;
        }
    }
    else
    {
        a.onmouseover = function() {
            window.status = 'Click on picture to enlarge';
            return true;
        }

        a.onclick = function() {
            var width = (typeof popupWidth == 'undefined' ? 340 : popupWidth);
            var height = (typeof popupHeight == 'undefined' ? 275 : popupHeight);
            var r = popupWin ('ArchPhoto', '/showpic.html?PicFilename=' + self.bigPic, '',
                              width, height, (screen.width - 400), 20, false, false, true);
            return false;
        }
    }

    a.appendChild( img );
    li.appendChild( a );
    li.appendChild( span );

    return li;
}

function showArchive( archDate )
{
    if ( 'object' == typeof archDate && archDate == archiveSelect && archDate != null )
    {
        archDate = archDate.options[ archDate.selectedIndex ].value;
    }

    var oPhoto, li;

    while ( picContent.hasChildNodes() )
    {
        picContent.removeChild( picContent.lastChild );
    }

    if (typeof archiveSelect != "undefined" && archiveSelect != null){
        markSelector( archDate );
    }

    updateArchiveHistory( archDate );

    for ( var i = 0; i < oData['x'+archDate].length; i++ )
    {
        oPhoto = oData['x'+archDate][ i ];

        if ( hasPicAccess || !oPhoto.view )
        {
            li = new getPhotoCell( true, oPhoto, '#' );
        }
        else
        {
            li = new getPhotoCell( false, oPhoto, linkLogon + urlencode( jsUrlHash.set('AL', archDate, true ) ) );
        }

        picContent.appendChild( li );
    }

    if (typeof archCurrentDate != 'undefined' &&
        archCurrentDate != null){
        oTime = new Date();
        oTime.setTime( archDate * 86400000 );
        sDate = formatDate( oTime, 'dd NNN yyyy' );
        while (archCurrentDate.hasChildNodes()){
            archCurrentDate.removeChild(archCurrentDate.lastChild);
        }
        archCurrentDate.appendChild(document.createTextNode( sDate ));
    }
}

function updateArchiveHistory( albumIndex )
{
    if ( chathostID != jsCookie.get( 'chID' ) )
    {
        jsCookie.set( 'chID', chathostID );
        jsCookie.set( 'chVA', null );
    }

    var aViewedAlbum = jsCookie.get( 'chVA' );

    if ( null == aViewedAlbum )
    {
        aViewedAlbum = [];
    }
    else
    {
        aViewedAlbum = aViewedAlbum.split(',');

        for ( var i = 0; i < aViewedAlbum.length; i++ )
        {
            document.getElementById( 'AL_' + aViewedAlbum[ i ] ).className = 'visited';
        }
    }

    if ( null != albumIndex && document.getElementById( 'AL_' + albumIndex ) )
    {
        jsUrlHash.set( 'AL', albumIndex );

        document.getElementById( 'AL_' + albumIndex ).className = 'selected';

        aViewedAlbum[ aViewedAlbum.length ] = albumIndex;

        jsCookie.set( 'chVA', aViewedAlbum.unique() );
    }

    return false;
}

