﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

function ResetImageCount(imageorder) {
    $("#tbCount").val(imageorder);
    $('#ImageCounter').html('Viewing ' + (parseInt($("#tbCount").val()) + 1) + ' of ' + $("#tbTotalCount").val() + ' images.');
};

function LoadImage(imageOrder) {
    var divSelected = $('.imagesGallery img').eq(imageOrder)
    var imgpath = divSelected.attr('original');
    var imgTitle = divSelected.next(".link").text();
    var imgId = divSelected.attr('id');

    var strImage;
    if (imgpath.match(/AccessDenied.jpg$/)) {
        strImage = '<br><p>Sorry, your subscription does not include access to this part of Planet Retail.<br><br>If you would like to upgrade your subscription or find out more about our additional services, please call +44 (0)20 7728 5600 to speak to your account manager or a representative from sales.  Alternatively, you can email <a href="mailto:sales@planetretail.net">sales@planetretail.net</a></p>.'
        $('#download').hide();
    }
    else {
        dcsMultiTrack('DCS.dcsuri','MediaGallery_ImageClicked','WT.z_image','' + imgId + '');
        strImage = '<img src="' + imgpath + '" onLoad="ResizeImage();"/>'
        $('#download').show().attr('href', '/ImageGallery/Download?filename=' + imgpath);
    };

    $('#simplemodal-container').hide();

    $('#LargePhoto').html(strImage);
    $('#Desc').html($('<strong>').append(imgTitle));

    $.get("/ImageGallery/GetImageDate", { itemId: imgId }, function(markup) {
        $('#ImageDate').html(markup);
    });

    $.get("/ImageGallery/GetImageComment", { itemId: imgId }, function(markup) {
        $('#PhotoComment').html(markup);
    });

    $('#simplemodal-container').show();

    $.post("/ImageGallery/SaveRecentlyViewedImage", { mediaItemId: imgId }, function(data) { });
};

function ResizeImage() {

    var modalContainer = $('#simplemodal-container');
    var newHeight;
    var newWidth;
    //Checks image dimensions for modal
    if (modalContainer.height() > modalContainer.width()) { //portrait
        newHeight = 366;
        newWidth = 275;
    }
    else if (modalContainer.height() < 280) { //Logo
        newHeight = 46;
        newWidth = 196;
    }
    else { //landscape
        newHeight = 375;
        newWidth = 500;
    };
    if (modalContainer.height() != newHeight || modalContainer.width() != newWidth) {
        //$('#LargePhoto img').attr('height', newHeight).attr('width', newWidth);
        $('#LargePhoto img').animate({ height: newHeight, width: newWidth }, 'fast');
    };
};

function initialisePopup() {
    $('#Result .imagesGallery').live('click', function(e) {
        var target = $(e.target);
        if (target.is('a, img')) {
            var imageorder = target.is('img') ? target.attr('order') : target.prev('img').attr('order');

            $('#ImagePopupModal').modal({ onOpen: function(dialog) {
                dialog.overlay.fadeIn('fast', function() {
                    dialog.data.hide();

                    ResetImageCount(imageorder);
                    LoadImage(imageorder);

                    $('#simplemodal-container').css('height', 'auto');
                    $('#simplemodal-container').css('width', '600px');

                    dialog.data.show();

                    dialog.container.fadeIn('fast', function() {

                    });
                });
            }, onClose: function(dialog) {
                $.modal.close(); // must call this!
            }
            });
        };
        return false;
    });

    $('#btnPreviousImg').live('click', function() {
        var imageorder = parseInt($("#tbCount").val()) - 1;
        LoadImage(imageorder);

        if (imageorder < 0) {
            var imageorder = parseInt($("#tbTotalCount").val()) - 1;
        }
        ResetImageCount(imageorder);
    });

    $('#btnNextImg').live('click', function() {
        if (parseInt($("#tbCount").val()) >= (parseInt($("#tbTotalCount").val() - 1))) {
            var imageorder = 0;
        } else {
            var imageorder = parseInt($("#tbCount").val()) + 1;
        };
        LoadImage(imageorder);
        ResetImageCount(imageorder);
    });
}
$(function() {
    initialisePopup();
});
