﻿/// <reference path="../../Libraries/Prototype/prototype.js" />

// GalleryCommon.js

var listElementId;

function loadImageGalleriesDropDown(sourceElement, targetListElementId, collectionName) {
    listElementId = targetListElementId;
    
    showLoader();
    
    if ($(sourceElement).selectedIndex > 0) {
        // Get the gallery list for selected collection
        GalleryAjax.GetImageGalleriesForCollection(_siteId, collectionName, getImageGalleriesForCollectionResult)
    }
    else {
        clearDropDown(targetListElementId);
    }
}

function loadVehicleImageGalleriesDropDown(sourceElement, targetListElementId, collectionName) {
    listElementId = targetListElementId;
    
    showLoader();
    
    if ($(sourceElement).selectedIndex > 0) {
        // Get the gallery list for selected collection
        GalleryAjax.GetImageGalleriesForVehicleCollection(_siteId, collectionName, getImageGalleriesForCollectionResult)
    }
    else {
        clearDropDown(targetListElementId);
    }
}

function showLoader() {
    clearDropDown();
    
    new Insertion.Bottom(listElementId, '<option value="">Loading...</option>');    
}

function getImageGalleriesForCollectionResult(response) {
    if (response.error == null) {
        clearDropDown();

        // add the new items
        response.value.each( function(gallery) {
            new Insertion.Bottom(listElementId, '<option value="' + gallery.GalleryUrl + '">' + gallery.GalleryDisplayName + '</option>');    
        });
        
        $(listElementId).selectedIndex = 0;
    }
    else {
        clearDropDown();

        alert('Galleries drop down failed to load.  Please try again later.');
    }
}

function clearDropDown() {
    // remove any current items
    for (i = $(listElementId).options.length - 1; i > 0; i--)
        $(listElementId).remove(i);
}

function goToGallery(galleryList) {
    if ($(galleryList).selectedIndex > 0) {
        window.location.href = $(galleryList).getValue(); //String.format(_galleryPageUrl, $(galleryList).getValue());
    }
    else {
        alert("Please select a gallery from the drop down list.");
    }
}