﻿/************************************************************************  
VARS
**************************************************************************/
var _errorManager = new ErrorManagerJQ($(LayoutElementsJQ.JQDivError));

var _photoGallery = null;
var _photos = null;

var _selectedOrganizationId = 4;
var _selectedPhotoGalleryYear = '2009';
var _selectedPhotoGallery = '';
var _current_currentPhotoGalleryYearDirectories = null;
var _currentPhotoGalleries = null;
var _currentPhotos = null;
var _currentPhoto = '';
var _currentPhotoIndex = 0;
var _year = "year";
var _gallery = "photoGallery"

/************************************************************************ 
                        Page Element Enums
**************************************************************************/
var PhotoGalleryElements =
{
    DivSecondIINonePhotoGallery: 'divSecondIINonePhotoGallery',
    DivSecondIINonePhoto: 'photo',
    JQDivSecondIINonePhotoGallery: '#divSecondIINonePhotoGallery',
    DdlChapters: 'ddlChapters'
}

/************************************************************************ 
                        Functions  
**************************************************************************/

function pageLoad() {
    getOrganizationsUsingService();
    getSecondIINonePhotoGallerys();
}

function getOrganizationsUsingService() {
    SecondIINoneUIService.GetOrganizations(onGetOrganizationsComplete, onSecondIINonePhotoGalleryError);
}

function onGetOrganizationsComplete(organizations) {
    var element = $get(PhotoGalleryElements.DdlChapters);

    populateSecondIINoneOrganizationsWithSelectedOrganizationId(element, organizations, _selectedOrganizationId);
}

function getSecondIINonePhotoGalleryBySelectedOrganizationId() {
    _selectedOrganizationId = getSelectedValue($get(PhotoGalleryElements.DdlChapters));

    showWaitDialog(InformationMessages.PhotoGalleryRetrieval);

    getSecondIINonePhotoGalleryYearsForAnOrganizationUsingService();
} 

function getSecondIINonePhotoGallerys() {
    showWaitDialog(InformationMessages.PhotoGalleryRetrieval);

    getSecondIINonePhotoGallerysUsingService();
}

function getSecondIINonePhotoGallerysUsingService() {
    SecondIINoneUIService.GetPhotoYearDirectories(onGetSecondIINonePhotoGalleryYearsForAnOrganizationComplete, onSecondIINonePhotoGalleryError);
}

function getSecondIINonePhotoGalleryYearsForAnOrganizationUsingService() {
    SecondIINoneUIService.GetPhotoYearDirectoriesByOrganizationId(_selectedOrganizationId, onGetSecondIINonePhotoGalleryYearsForAnOrganizationComplete, onSecondIINonePhotoGalleryError);
}

function onGetSecondIINonePhotoGalleryYearsForAnOrganizationComplete(photoGalleryYearDirectories) {
    _currentPhotoGalleryYearDirectories = photoGalleryYearDirectories;
    
    populateSecondIINonePhotoGalleryYearDirectories();

    performPostLoadOperations();
}

function populateSecondIINonePhotoGalleryYearDirectories() {
    var divError = $get(LayoutElements.DivError);
    hideContent(divError);

    var divSecondIINonePhotoGallery = $get(PhotoGalleryElements.DivSecondIINonePhotoGallery);

    if (_currentPhotoGalleryYearDirectories.length > 0) {

        var secondIInonePhotoGalleryString = ''

        for (var secondIInonePhotoYearDirectoryRowCount = 0; secondIInonePhotoYearDirectoryRowCount < _currentPhotoGalleryYearDirectories.length; secondIInonePhotoYearDirectoryRowCount++) {

            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="0" class="secondIInonePreviewTable">';
            for (var i = 0; i < 2; i++) {
                secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr>';

                if (secondIInonePhotoYearDirectoryRowCount < (_currentPhotoGalleryYearDirectories.length)) {

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="0" class="secondIInonePhotosPreviewTable">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td style="width: 110px;">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<img class="photoGalleryPicture" height="' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].Height + '" width="' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].Width + '" src="' + _applicationPhotoGalleryWebPath + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].Organization.Prefix + '/' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].Year + '/' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].PhotoGallery + '/' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].FileName + '" />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td style="width: auto;">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<a href="#" onclick="javascript:getSecondIINonePhotoGalleries(' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].EntityName + ', ' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].Organization.Id + ');return false;">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString +  _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].Organization.Name + ': ' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].EntityName;
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</a>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<br />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<span style="font-size: 12px;">' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].NumberOfGalleries + ' galleries with ' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].NumberOfPhotos + ' photos</span>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<br />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<span style="font-size: 8px;">Updated: ' + _currentPhotoGalleryYearDirectories[secondIInonePhotoYearDirectoryRowCount].LastUpdated + '</span>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table>';

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                }
                else {

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<p>&nbsp;</p>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                }

                secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
                secondIInonePhotoYearDirectoryRowCount++
            }

            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table>';

            secondIInonePhotoYearDirectoryRowCount--
        }

        showContent(divSecondIINonePhotoGallery);

        divSecondIINonePhotoGallery.innerHTML = secondIInonePhotoGalleryString;

        window.scrollTo(0, 250);

        hideWaitDialog();
    }
    else {
        showContent(divSecondIINonePhotoGallery);

        divSecondIINonePhotoGallery.innerHTML = '<p style="color: white;">There are no photo galleries to display.  Please select another chapter.</p>';

        window.scrollTo(0, 250);

        hideWaitDialog();
    }
}

function getSecondIINonePhotoGalleries(year, organizationId) {
    showWaitDialog(InformationMessages.PhotoGalleryRetrieval);
    _selectedPhotoGalleryYear = year;
    _selectedOrganizationId = organizationId;

    getSecondIINonePhotoGalleriesUsingService();
}

function getSecondIINonePhotoGalleriesUsingService() {
    SecondIINoneUIService.GetPhotoGalleriesByOrganizationIdAndYear(_selectedOrganizationId, _selectedPhotoGalleryYear, onGetSecondIINonePhotoGalleriesComplete, onSecondIINonePhotoGalleryError);
}

function onGetSecondIINonePhotoGalleriesComplete(photoGalleries) {
    _currentPhotoGalleries = photoGalleries;
    populateSecondIINonePhotoGalleries();
}

function populateSecondIINonePhotoGalleries() {
    var divError = $get(LayoutElements.DivError);
    hideContent(divError);

    var divSecondIINonePhotoGallery = $get(PhotoGalleryElements.DivSecondIINonePhotoGallery);

    if (_currentPhotoGalleries != null) {

        var secondIInonePhotoGalleryString = ''

        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="0" align="center">';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr class="photoGalleryBreadCrumb">';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + getBreadCrumb(_year);
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table>';

        for (var secondIInonePhotoGalleryDirectoryRowCount = 0; secondIInonePhotoGalleryDirectoryRowCount < _currentPhotoGalleries.length; secondIInonePhotoGalleryDirectoryRowCount++) {

            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="0" class="secondIInonePreviewTable">';
            
            for (var i = 0; i < 2; i++) {
                if (secondIInonePhotoGalleryDirectoryRowCount < (_currentPhotoGalleries.length)) {

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="0" class="secondIInonePhotosPreviewTable">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td style="width: 110px;">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<img class="photoGalleryPicture" height="' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].Height + '" width="' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].Width + '" src="' + _applicationPhotoGalleryWebPath + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].Organization.Prefix + '/' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].Year + '/' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].PhotoGallery + '/' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].FileName + '" />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td style="width: auto;">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<a href="#" onclick="javascript:getSecondIINonePhotos(\'' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].EntityName + '\');return false;">';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].EntityName;
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</a>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<br />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<span style="font-size: 12px;">' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].NumberOfPhotos + ' photos</span>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<br />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<span style="font-size: 8px;">Updated: ' + _currentPhotoGalleries[secondIInonePhotoGalleryDirectoryRowCount].LastUpdated + '</span>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table>';
                    
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                }
                else {

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                }

                secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
                secondIInonePhotoGalleryDirectoryRowCount++
            }

            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table>';

            secondIInonePhotoGalleryDirectoryRowCount--
        }

        divSecondIINonePhotoGallery.innerHTML = secondIInonePhotoGalleryString;

        window.scrollTo(0, 250);

        hideWaitDialog();
    }
}

function getSecondIINonePhotos(photoGallery) {
    showWaitDialog(InformationMessages.PhotoGalleryRetrieval);
    _selectedPhotoGallery = photoGallery;
    getSecondIINonePhotosUsingService();
}

function getSecondIINonePhotosUsingService() {
    SecondIINoneUIService.GetPhotosByOrganizationYearGallery(_selectedOrganizationId, _selectedPhotoGalleryYear, _selectedPhotoGallery, onGetSecondIINonePhotosComplete, onSecondIINonePhotoGalleryError);
}

function onGetSecondIINonePhotosComplete(photos) {
    _currentPhotos = photos;
    populateSecondIINonePhotos();
}

function populateSecondIINonePhotos() {
    var divError = $get(LayoutElements.DivError);
    hideContent(divError);

    var divSecondIINonePhotoGallery = $get(PhotoGalleryElements.DivSecondIINonePhotoGallery);

    if (_currentPhotos != null) {

        var secondIInonePhotoGalleryString = ''

        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="0" align="center">';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr class="photoGalleryBreadCrumb">';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + getBreadCrumb(_photoGallery);
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
        secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table><br/>';

        for (var secondIInonePhotoGalleryRowCount = 0; secondIInonePhotoGalleryRowCount < _currentPhotos.length; secondIInonePhotoGalleryRowCount++) {

            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<table cellpadding="0" cellspacing="15" class="secondIInonePhotosTable">';
            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<tr>';

            for (var i = 0; i < 4; i++) {
                if (secondIInonePhotoGalleryRowCount < (_currentPhotos.length)) {

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';

                    var photoPath = _applicationPhotoGalleryWebPath + _currentPhotos[secondIInonePhotoGalleryRowCount].Organization.Prefix + "/" + _currentPhotos[secondIInonePhotoGalleryRowCount].Year + "/" + _currentPhotos[secondIInonePhotoGalleryRowCount].PhotoGallery + "/" + _currentPhotos[secondIInonePhotoGalleryRowCount].File.FileName;

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<a href="#" onclick="javascript:viewPhoto(\'' + secondIInonePhotoGalleryRowCount + '\');return false;"><img class="photoGalleryPicture" height="' + _currentPhotos[secondIInonePhotoGalleryRowCount].Height + '" width="' + _currentPhotos[secondIInonePhotoGalleryRowCount].Width + '" src="';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + photoPath;
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '" /></a>';

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<br />';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                }
                else {

                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<td>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '<p style="width: 107px;">&nbsp;</p>';
                    secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</td>';
                }

                secondIInonePhotoGalleryRowCount++
            }

            secondIInonePhotoGalleryRowCount--

            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</tr>';
            secondIInonePhotoGalleryString = secondIInonePhotoGalleryString + '</table>';
        }

        divSecondIINonePhotoGallery.innerHTML = secondIInonePhotoGalleryString;

        window.scrollTo(0, 450);

        hideWaitDialog();
    }
}

function viewPhoto(index) {
    var photoPath = _applicationPhotoGalleryWebPath + _currentPhotos[index].Organization.Prefix + "/" + _currentPhotos[index].Year + "/" + _currentPhotos[index].PhotoGallery + "/" + _currentPhotos[index].File.FileName;

    _currentPhoto = photoPath;
    _currentPhotoIndex = index * 1;

    var nextPageIndex = _currentPhotoIndex + 1
    var previousPageIndex = _currentPhotoIndex - 1
  
    var secondIInonePhoto = ''

    secondIInonePhoto = secondIInonePhoto + '<table cellpadding="0" cellspacing="0" align="center">';
    secondIInonePhoto = secondIInonePhoto + '<tr class="photoGalleryBreadCrumb">';

    if (previousPageIndex >= 0) {
        secondIInonePhoto = secondIInonePhoto + '<td>';
        secondIInonePhoto = secondIInonePhoto + '<a href="#" onclick="javascript:viewPhoto(\'' + previousPageIndex + '\');return false;"><img src="' + _applicationWebPath + 'App_Themes/Standard/Images/Icons/prevpage16.gif" /></a>';
        secondIInonePhoto = secondIInonePhoto + '</td>';
    }
    
    secondIInonePhoto = secondIInonePhoto + '<td>';
    secondIInonePhoto = secondIInonePhoto + getBreadCrumb(_photoGallery);
    secondIInonePhoto = secondIInonePhoto + '</td>';

    if (nextPageIndex < _currentPhotos.length) {
        secondIInonePhoto = secondIInonePhoto + '<td>';
        secondIInonePhoto = secondIInonePhoto + '<a href="#" onclick="javascript:viewPhoto(\'' + nextPageIndex + '\');return false;"><img src="' + _applicationWebPath + 'App_Themes/Standard/Images/Icons/nextpage16.gif" /></a>';
        secondIInonePhoto = secondIInonePhoto + '</td>';
    }
        
    secondIInonePhoto = secondIInonePhoto + '</tr>';
    secondIInonePhoto = secondIInonePhoto + '</table>';

    secondIInonePhoto = secondIInonePhoto + '<table align="center" cellspacing="0" class="photoImageTable" cellpadding="0">';
    secondIInonePhoto = secondIInonePhoto + '<tr>';
    secondIInonePhoto = secondIInonePhoto + '<td>';

    secondIInonePhoto = secondIInonePhoto + '<img class="photoImage" src="';
    secondIInonePhoto = secondIInonePhoto + _currentPhoto;
    secondIInonePhoto = secondIInonePhoto + '" />';

    secondIInonePhoto = secondIInonePhoto + '</td>';
    secondIInonePhoto = secondIInonePhoto + '</tr>';
    secondIInonePhoto = secondIInonePhoto + '</table>';

    var divSecondIINonePhoto = $get(PhotoGalleryElements.DivSecondIINonePhoto);
    showContent(divSecondIINonePhoto);

    divSecondIINonePhoto.innerHTML = secondIInonePhoto;

    globalBackgroundPopup = '#backgroundPhotoPopup';
    globalPopup = '#photoPopup';
    globalPopupClose = '#photoPopupClose'

    //centering with css
    centerPopup();
    //load popup
    loadJQueryPopup();
}

function getBreadCrumb(level) {
    var breadCrumb = '';

    breadCrumb = breadCrumb + '<a href="#" onclick="javascript:populateSecondIINonePhotoGalleryYearDirectories();return false;">Second II None Photo Gallery</a>';

    switch (level) {
        case _year:
            breadCrumb = breadCrumb + ' :: <a href="#" onclick="javascript:populateSecondIINonePhotoGalleries();return false;">' + _selectedPhotoGalleryYear + '</a>';
            break;
        case _photoGallery:
            breadCrumb = breadCrumb + ' :: <a href="#" onclick="javascript:populateSecondIINonePhotoGalleries();return false;">' + _selectedPhotoGalleryYear + '</a>';
            breadCrumb = breadCrumb + ' :: <a href="#" onclick="javascript:populateSecondIINonePhotos();return false;">' + _selectedPhotoGallery + '</a>';
            break;
    }

    return breadCrumb;
}

function onSecondIINonePhotoGalleryError(result) {
    var problems = new Array();
    problems[problems.length] = result.get_message();
    setErrorInformation($get(LayoutElements.DivError), problems);

    hideWaitDialog();
}
