//object types
var TABLE_CELL = "[object HTMLTableCellElement]";
var DIV = "[object HTMLDivElement]";

//Ids
var FIRST_NO_PHOTO = "first_no_photo";
var LAST_NO_PHOTO = "last_no_photo";

var _photos_activePhoto = 1; 

function _showPreviousPicture(prev)
{
    var cell = _findNextActivePicture(prev);

    if (cell.previousSibling.id != FIRST_NO_PHOTO)
    {
        cell = cell.previousSibling;
        var id = cell.id.substr(cell.id.lastIndexOf("_") + 1);
        _showPhoto(id);
        return;
    }
 
    while (cell.nextSibling.id != LAST_NO_PHOTO)
        cell = cell.nextSibling;

    var id = cell.id.substr(cell.id.lastIndexOf("_") + 1);
    _showPhoto(id);    
}

function _showNextPicture(next, large)
{
    var cell = _findPreviousActivePicture(next);
    
    if (cell.nextSibling.id != LAST_NO_PHOTO)
    {
        cell = cell.nextSibling;
        var id = cell.id.substr(cell.id.lastIndexOf("_") + 1);
        _showPhoto(id);
        return;
    }
 
    while (cell.previousSibling.id != FIRST_NO_PHOTO)
        cell = cell.previousSibling;
    
    var id = cell.id.substr(cell.id.lastIndexOf("_") + 1);
    _showPhoto(id);    
}

function _findNextActivePicture(cell)
{
    while (cell.nextSibling.style.display == "none")    
        cell = cell.nextSibling;

    return cell.nextSibling;
}

function _findPreviousActivePicture(cell)
{
    while (cell.previousSibling.style.display == "none")    
        cell = cell.previousSibling;

    return cell.previousSibling;
}

function _showPhoto(id)
{
    var lrgActive = document.getElementById("lrg_img_" + _photos_activePhoto);
    var thmActive = document.getElementById("thm_img_" + _photos_activePhoto);
    
    var lrgNext = document.getElementById("lrg_img_" + id);
    var thmNext = document.getElementById("thm_img_" + id);

    if (!lrgActive || lrgActive.style.display == "none" || thmActive.style.borderColor == "#CCC") 
    {
        lrgActive = document.getElementById("lrg_img_" + 1);
        thmActive = document.getElementById("thm_img_" + 1);
    }

    
    _setDisplay(lrgActive, false);
    _setDisplay(lrgNext, true);
    
    thmActive.style.borderColor = "#CCC";
    thmNext.style.borderColor = "#666";
    
    _photos_activePhoto = id;
    
    // Ensure the containing div is displaying the thumbnail
    _ensureVisible(thmNext.parentNode, thmNext);    
}

function _ensureVisible(container, element)
{
    _checkVisibility(container, element, true);
}

function _checkVisibility(div, element, width)
{
    var dPos, ePos, dDim, eDim;
    
    if (width)
    {
        pos = "Left";
        dDim = _getElementDimension(div, true);
        eDim = _getElementDimension(element, true);
    }
    else
    {
        pos = "Right";
        dDim = _getElementDimension(div, false);
        eDim = _getElementDimension(element, false);
    }
    
    if (dDim + (div["scroll" + pos]) < (eDim + element["offset" + pos]))
    {
        div["scroll" + pos] = ((element["offset" + pos] + eDim) - dDim);
    }
    else if (div["scroll" + pos] > element["offset" + pos])
    {
        div["scroll" + pos] = element["offset" + pos];
    }
}

function _getElementDimension(element, width)
{
    var width = 0;
    var style = element.style;
    width = parseInt(style.width.substr(0, style.width.length - 2));
    if (style.borderLeftWidth)
    {
        width = width + parseInt(style.borderLeftWidth.substr(0, style.borderLeftWidth.length - 2));
    }
    if (style.borderRightWidth)
    {
    width = width + parseInt(style.borderRightWidth.substr(0, style.borderRightWidth.length - 2));
    }
    if (style.marginLeftWidth)
    {
    width = width + parseInt(style.marginLeftWidth.substr(0, style.marginLeftWidth.length - 2));
    }
    if (style.marginRightWidth)
    {
    width = width + parseInt(style.marginRightWidth.substr(0, style.marginRightWidth.length - 2));
    }
    return width;
}

function _setDisplay(element, show)
{
    element.style.display = (show ? "" : "none");
}

function _findParentElement(e, valueOf)
{
    while(e.parentNode != null && e.parentNode.valueOf() != valueOf)
        e = e.parentNode;

    return e.parentNode;
}

function clickPreviousLargePhoto(cell)
{
    _showPreviousPicture(prev, large)
}

function clickNextLargePhoto(cell)
{
    _showNextPicture(prev, large)
}

function clickThumbnail(thm)
{
    var id = thm.id.substr(thm.id.lastIndexOf("_") + 1);
    _showPhoto(id);  
}
