﻿
    function showModal(source)
    {
        this._source = source;
        var objFullImg = document.getElementById("ctl00_cphMain_imgFull");
        var headerLabel = document.getElementById("ctl00_cphMain_lblImgTitle");
        this._popup = $find('mdlImageDisplay');
        this._popup.show();
        
        var sourcePath = this._source.src;
        var fullImgPath = sourcePath.replace("/Thumbnails", "");
        
        objFullImg.title = this._source.alt
        objFullImg.src = fullImgPath;
        objFullImg.dataSrc = this._source.name;
        
        headerLabel.innerHTML = this._source.alt;
        
        __doPostBack('ctl00_cphMain_upnlImgModal', 'customPostBack');
    }

    function btnClose_click()
    {
        var objFullImg = document.getElementById("ctl00_cphMain_imgFull");
        var headerLabel = document.getElementById("ctl00_cphMain_lblImgTitle");
        objFullImg.title = "";
        objFullImg.src = "";
        headerLabel.innerHTML = "";
        
        this._popup.hide();
        this._source = null;
        this._popup = null;
    }       

function mycarousel_itemLoadCallback(carousel, state)
{
    // Since we get all URLs in one file, we simply add all items
    // at once and set the size accordingly.
    if (state != 'init')
        return;

    var providerID = GetProviderIDFromHiddenField();
    this._carousel = carousel;
    
    infostation.web.WebServices.Images_Service.CreateImagesFile(providerID, CreateImagesFileCallBack);
};

function CreateImagesFileCallBack(result)
{
    var divImages = document.getElementById("ctl00_cphMain_divImages");
    var deleteInstruction = document.getElementById("deleteInstruction");
    
    if (result != 0)
    {
        var providerID = GetProviderIDFromHiddenField();
        var carousel = this._carousel;

        jQuery.get('../../Images/Uploaded/' + providerID + '/' + result + '.txt', function(data) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
        });
    }
    else
    {
        divImages.style.display = "none";
        // some pages does not contain the "deleteInstruction" div (like the provider detail page), so don't try and reference it
        if (deleteInstruction != null)
        {
            deleteInstruction.style.display = "none";
        }
    }
}

function mycarousel_itemAddCallback(carousel, first, last, data)
{
    // Simply add all items at once and set the size accordingly.0.
    var items = data.split('|');

    for (i = 0; i < items.length; i++) 
    {
        var caption = items[i].split('*'); 
        var images = caption[1].split('$');
        carousel.add(i+1, mycarousel_getItemHTML(caption[0], images[0], images[1]));
    }

    carousel.size(items.length);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url, title, imgID)
{
    return '<img src="' + url + '" alt="' + title + '" width="75" height="75" onclick="showModal(this);"' + 'name="' + imgID + '" />';
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        scroll: 5,
        itemLoadCallback: mycarousel_itemLoadCallback  
    });     
});

function deleteImage()
{
    var objFullImg = document.getElementById("ctl00_cphMain_imgFull");
    var providerID = GetProviderIDFromHiddenField();
    infostation.web.WebServices.Images_Service.DeleteImage(providerID, objFullImg.dataSrc, doPostBackImage);
}  
function doPostBackImage()
{
    this._popup.hide();
    this._source = null;
    this._popup = null;

    __doPostBack();
}

function GetProviderIDFromHiddenField() {
    if (window.location.search.substring(1).match("providerID") != null) {
        // catering for the image pages that still use a provider ID
        var providerID = window.location.search.substring(1).replace("providerID=", "");
        var restOfQueryStringStart = providerID.indexOf("&");

        if (restOfQueryStringStart != -1) {
            providerID = providerID.substr(0, restOfQueryStringStart);
        }

        return providerID;
    }
    else {
        // for rewritten pages that get the provider ID from a hidden input field
        return document.getElementById("ctl00_cphMain_hdnProviderID").value;
    }
}

