﻿// JScript File
var PageImages = new Images(-99,-99,-99);
var PageSets = new Sets(-99, -99, -99);
var PageSet;

function Images(current, last, total)
{
   
this.currentPage = current;
    this.lastPage = last;
    this.totalImages = total;
    this.firstTime = true;
    
    //alert(this.currentPage);
    this.HandleGetNextPage = GetNextImages;
    this.HandleGetPreviousPage = GetPreviousImages;
}

function GetNextImages()
{
       
var nextPage = this.currentPage + 1;
    
    if (this.firstTime == true)
    {
        //alert('one');
        this.firstTime = false;
        PageMethods.GetImages(DISPLAYID, COLOURCODE, nextPage, GetNextImages_OnComplete);
    }
    else if (this.totalImages != 0 && nextPage <= this.lastPage)
    {
        //alert('two');
        this.currentPage = nextPage;
        PageMethods.GetImages(DISPLAYID, COLOURCODE, nextPage, GetNextImages_OnComplete);
    }
    else
    {
        // Reached last page already
    }
}

function GetPreviousImages()
{
    var previousPage = this.currentPage - 1;
    
    if (this.totalImages != 0 && previousPage > 0)
    {
        this.currentPage = previousPage;
        PageMethods.GetImages(DISPLAYID, COLOURCODE, previousPage, GetNextImages_OnComplete);
    }
    else
    {
        // Reached first page already
    }
}

function GetNextImages_OnComplete(result)
{

    if (result.length == 2)
    {       
        var info = result[0];
        
        var indexOfComma = info.indexOf(",");
        var newImagesCount = info.substring(0, indexOfComma);
        
        info = info.substring(indexOfComma + 1);
        indexOfComma = info.indexOf(",");
        var paginationInfo = info.substring(0, indexOfComma);
        
        paginationInfo = paginationInfo.split(':');
        var currentPage = parseInt(paginationInfo[0]);
        var totalPages = parseInt(paginationInfo[1]);
        var totalImages = parseInt(paginationInfo[2]);
        
        PageImages = new Images(currentPage, totalPages, totalImages);
        PageImages.firstTime = false;
        
        info = info.substring(indexOfComma + 1);
        var images = info.split(',');
        
        if (newImagesCount > 0)
        {
            ClearChildren('imageTainer');
            for (var i = 0; i < newImagesCount; i++)
            {
                var imageinfo = images[i].split(';');
                var link = imageinfo[0].split('>')[1];
                var path = imageinfo[1].split('>')[1];
                var uname = imageinfo[1].split('>')[1];
                
                var image = CreateImage('', path, '');
                image.width = 75;
                image.height = 75;
                
                var link = CreateImageLink('', link, '', image);           
                
                var div = CreateDiv('', "vp-scroll-thumb");
                div.appendChild(link);
 
                var imageTainer = getObj('imageTainer');
                imageTainer.appendChild(div);
                
            }
        }
        
    }
    else if (result.length == 1)
    {
        alert(result);
    }
}

function Sets(current, last, items)
{
    this.currentPage = current;
    this.lastPage = last;
    this.totalSets = items;
    this.firstTime = true;
    
    this.HandleGetNextPage = GetNextPageSets;
    this.HandleGetPreviousPage = GetPreviousPageSets;
}

function GetNextPageSets()
{
    var nextPage = this.currentPage + 1;
    
    if (this.firstTime)
    {
        this.firstTime = false;
        this.currentPage = 1;
        PageMethods.GetSets(DISPLAYID, 1, GetNextPageSets_OnComplete);
    }
    else if (this.totalSets != 0 && nextPage <= this.lastPage)
    {
        this.currentPage = nextPage;
        PageMethods.GetSets(DISPLAYID, nextPage, GetNextPageSets_OnComplete);
    }
    else
    {
        // Reached last page already
    }
}

function GetNextPageSets_OnComplete(result)
{
    if (result.length == 2)
    {       
        var info = result[0];
        
        var indexOfComma = info.indexOf(",");
        var newSetsCount = info.substring(0, indexOfComma);
        
        info = info.substring(indexOfComma + 1);
        
        var sets = info.split('|');
        
        if (newSetsCount > 0)
        {
            ClearChildren('setsTainer');
            for (var i = 0; i < newSetsCount; i++)
            {
                var setinfo = sets[i].split(';');
                var onclick = setinfo[0].split('>')[1];
                var path = setinfo[1].split('>')[1];
                var setname = setinfo[2].split('>')[1];
                
                var image = CreateImage('', path, '');
                image.width = 75;
                image.height = 75;
                
                var link = CreateImageLink('', onclick, '', image);           
                
                var div = CreateDiv('', "s-photo");
                div.appendChild(link);
                
                var setnamediv = CreateDiv('', "s-meta-scroll");
                var setTextNode = document.createTextNode(setname);
                setnamediv.appendChild(setTextNode);
                
                var containingdiv = CreateDiv('', "s-chunk-scroll");
                containingdiv.appendChild(div);
                containingdiv.appendChild(setnamediv);
                
                var setsTainer = getObj('setsTainer');
                setsTainer.appendChild(containingdiv);
            }
        }
        
    }
    else if (result.length == 1)
    {
        alert(result);
    }
}

function GetPreviousPageSets()
{
    var previousPage = this.currentPage - 1;
    
    if (this.totalSets != 0 && previousPage > 0)
    {
        this.currentPage = previousPage;
        PageMethods.GetSets(DISPLAYID, previousPage, GetNextPageSets_OnComplete);
    }
    else
    {
        // Reached first page already
    }
}

function SetInitContinum(uidsetid, page)
{
    var info = uidsetid.split(':');
    PageMethods.GetSet(info[0], info[1], page, SetInit_OnComplete);
}

function SetInit(uidsetid)
{
    var info = uidsetid.split(':');
    PageMethods.GetSet(info[0], info[1], 1, SetInit_OnComplete);
}

function SetInit_OnComplete(result)
{
    if (result.length == 3)
    {       
        var info = result[0];
        var setId = result[1];
        
        var indexOfComma = info.indexOf(",");
        var newSetsCount = info.substring(0, indexOfComma);
        
        info = info.substring(indexOfComma + 1);
        indexOfComma = info.indexOf(",");
        var paginationInfo = info.substring(0, indexOfComma);
        
        paginationInfo = paginationInfo.split(':');
        var currentPage = parseInt(paginationInfo[0]);
        var totalPages = parseInt(paginationInfo[1]);
        var totalImages = parseInt(paginationInfo[2]);
        var setName = paginationInfo[3];
        
        PageSet = new SetPhotos(setId, currentPage, 1, totalPages);
        
        info = info.substring(indexOfComma + 1);
        var sets = info.split(',');
        
        ToggleSet();
        
        if (newSetsCount > 0)
        {
            ClearChildren('setTainer');
            for (var i = 0; i < newSetsCount; i++)
            {
                var setinfo = sets[i].split(';');
                var link = setinfo[0].split('>')[1];
                var path = setinfo[1].split('>')[1];
                var uname = setinfo[1].split('>')[1];
                
                var image = CreateImage('', path, '');
                image.width = 75;
                image.height = 75;
                
                var link = CreateImageLink('', link, '', image);           
                
                var div = CreateDiv('', "vp-scroll-thumb");
                div.appendChild(link);
 
                var setsTainer = getObj('setTainer');
                setsTainer.appendChild(div);
                
                ClearChildren('set_setname');
                var setTextNode = document.createTextNode(" / " + setName);
                
                var set_setname = getObj('set_setname');
                var backtosets = CreateHyperLink_Clickable('', OpenSets, "All of " + DISPLAYNAME + "'s public sets", '');
                set_setname.appendChild(backtosets);
                set_setname.appendChild(setTextNode);
                set_setname.appendChild(document.createTextNode(" "));
                
                //var someimage = CreateImage('', '../images/goto.gif', '');
                //var somelink = CreateImageLink('', '', '', someimage);
                //set_setname.appendChild(somelink);
            }
        }
        
    }
    else if (result.length == 1)
    {
        alert(result);
    }
}

function SetPhotos(id, current, first, last)
{
    this.setId = id;
    this.currentPage = current;
    this.firstPage = first;
    this.lastPage = last;
    this.firstTime = true;
    this.setName = '';
    
    this.HandleGetNextPage = GetNextPageSet;
    this.HandleGetPreviousPage = GetPreviousPageSet;
}

function GetNextPageSet()
{
    var nextPage = this.currentPage + 1;
    
    if (nextPage <= this.lastPage)
    {
        this.currentPage = nextPage;
        PageMethods.GetSet(DISPLAYID, this.setId, nextPage, SetInit_OnComplete);
    }
    else
    {
        // Reached last page already
    }
}

function GetPreviousPageSet()
{
    var previousPage = this.currentPage - 1;
    
    if (previousPage > 0 && previousPage <= this.lastPage)
    {
        this.currentPage = previousPage;
        PageMethods.GetSet(DISPLAYID, this.setId, previousPage, SetInit_OnComplete);
    }
    else if (previousPage <= 0)
    {
        this.currentPage = 1;
        PageMethods.GetSet(DISPLAYID, this.setId, 1, SetInit_OnComplete);
    }
    else
    {
        // Reached last page already
    }
}

function ClearChildren(id)
{
    var obj = getObj(id);
    
    if ( obj.hasChildNodes() )
    {
        while ( obj.childNodes.length >= 1 )
        {
            obj.removeChild( obj.firstChild );       
        } 
    }
}

function TogglePhotosSpecial()
{
    if (PageImages.currentPage != -99)
    {
        var latest_zone = getObj('latest_zone');
        var sets_zone = getObj('sets_zone');
        var set_zone = getObj('set_zone');
        
        var img_album = getObj('img_album');
        img_album.src = "../images/arrow2-up.gif";
        var img_photos = getObj('img_photos');
        img_photos.src = "../images/arrow2.gif";
        
        latest_zone.style.display = '';
        sets_zone.style.display = 'none';
        set_zone.style.display = 'none';
        ClearChildren('set_setname');
       
        var setTextNode = document.createTextNode("All of " + DISPLAYNAME + "'s public photos");
        var set_setname = getObj('set_setname');
        set_setname.appendChild(setTextNode);
        
        
        if (PageImages.firstTime)
        {
            PageImages.HandleGetNextPage();
        }
    }   
}

function TogglePhotos()
{
    //alert(PageImages.currentPage);
    if (PageImages.currentPage != -99)
    {
        var latest_zone = getObj('latest_zone');
        var sets_zone = getObj('sets_zone');
        var set_zone = getObj('set_zone');
        
        if (latest_zone.style.display == 'none')
        {
            var img_album = getObj('img_album');
            img_album.src = "../images/arrow2-up.gif";
            var img_photos = getObj('img_photos');
            img_photos.src = "../images/arrow2.gif";
            
            latest_zone.style.display = '';
            sets_zone.style.display = 'none';
            set_zone.style.display = 'none';
            ClearChildren('set_setname');
           
            var setTextNode = document.createTextNode("All of " + DISPLAYNAME + "'s public photos");
            var set_setname = getObj('set_setname');
            set_setname.appendChild(setTextNode);
        }
        else
        {
            var img_album = getObj('img_album');
            img_album.src = "../images/arrow2-up.gif";
            var img_photos = getObj('img_photos');
            img_photos.src = "../images/arrow2-up.gif";
            
            latest_zone.style.display = 'none';
            sets_zone.style.display = 'none';
            set_zone.style.display = 'none';
            ClearChildren('set_setname');
        }
        
        if (PageImages.firstTime)
        {
            PageImages.HandleGetNextPage();
        }
    }
}

function ToggleAlbums()
{
    //alert(PageSets.currentPage);
    if (PageSets.currentPage != -99)
    {
       
var latest_zone = getObj('latest_zone');
        var sets_zone = getObj('sets_zone');
        var set_zone = getObj('set_zone');
        
        if (sets_zone.style.display == 'none' && set_zone.style.display == 'none')
        {
            var img_album = getObj('img_album');
            img_album.src = "../images/arrow2.gif";
            var img_photos = getObj('img_photos');
            img_photos.src = "../images/arrow2-up.gif";
            
            latest_zone.style.display = 'none';
            sets_zone.style.display = '';
            set_zone.style.display = 'none';
            ClearChildren('set_setname');
            
            var setTextNode = document.createTextNode("All of " + DISPLAYNAME + "'s public sets");
            var set_setname = getObj('set_setname');
            set_setname.appendChild(setTextNode);
        }
        else
        {
            var img_album = getObj('img_album');
            img_album.src = "../images/arrow2-up.gif";
            var img_photos = getObj('img_photos');
            img_photos.src = "../images/arrow2-up.gif";
            
            latest_zone.style.display = 'none';
            sets_zone.style.display = 'none';
            set_zone.style.display = 'none';
            ClearChildren('set_setname');
        }
        
        if (PageSets.firstTime)
        {
            PageSets.HandleGetNextPage();
        }
    }
}

function ToggleSet()
{
    var latest_zone = getObj('latest_zone');
    var sets_zone = getObj('sets_zone');
    var set_zone = getObj('set_zone');
    
    if (set_zone.style.display == 'none')
    {
        var img_album = getObj('img_album');
        img_album.src = "../images/arrow2.gif";
        var img_photos = getObj('img_photos');
        img_photos.src = "../images/arrow2-up.gif";
        
        latest_zone.style.display = 'none';
        sets_zone.style.display = 'none';
        set_zone.style.display = '';
    }
}

function OpenSets()
{
    var latest_zone = getObj('latest_zone');
    var sets_zone = getObj('sets_zone');
    var set_zone = getObj('set_zone');
    
    var img_album = getObj('img_album');
    img_album.src = "../images/arrow2.gif";
    var img_photos = getObj('img_photos');
    img_photos.src = "../images/arrow2-up.gif";
    
    latest_zone.style.display = 'none';
    sets_zone.style.display = '';
    set_zone.style.display = 'none';
    ClearChildren('set_setname');
    
    var setTextNode = document.createTextNode("All of " + DISPLAYNAME + "'s public sets");
    var set_setname = getObj('set_setname');
    set_setname.appendChild(setTextNode);
    
    if (PageSets.firstTime)
    {
        PageSets.HandleGetNextPage();
    }
}