﻿// TODO: Refactor these kilometers of javascript
function swapHeroPicture(fileName)
{

    var pictureUrl = "/images/mediums/" + fileName;
    if ($('#imgProductHeroShot').attr('src') != pictureUrl)
    {
    
        // Swap image itself
        $('#imgProductHeroShot').css({visibility: 'hidden'});
        $('#imgProductHeroShot').attr('src',pictureUrl);
        $('#imgProductHeroShot').load(positionHeroPicture);
        
        // Swap onclick popup
        $('#heroPictureWrapper').bind("click", {fileName:fileName}, popupBigPicture);
        
    }
}
function positionHeroPicture()
{
    
    var maxHeight = 250;
    var newHeight = $('#imgProductHeroShot').attr('height');
    if (newHeight < maxHeight)
        $('#imgProductHeroShot').css({position: 'relative', top: (maxHeight-newHeight)/2+'px'});
    else
        $('#imgProductHeroShot').css({position: 'relative', top: '0px'});
        $('#imgProductHeroShot').css({visibility: 'visible'});
}
function swapSlidePicture(fileName)
{

    var pictureUrl = "/images/larges/" + fileName;
    if ($('#currentSlide').attr('src') != pictureUrl)
    {
    
        // Swap image itself
        $('#currentSlide').hide();
        $('#loadingSlide').show();
        $('#currentSlide').attr('src',pictureUrl);
        $('#currentSlide').load(positionSlidePicture);
      
    }
}

var repositionAttempts = 0;
function positionSlidePicture()
{

    if ($('#currentSlide').height() != 0)
    {
        repositionAttempts = 0;
        var maxHeight = 500;
        var newHeight = $('#currentSlide').height();    
        if (newHeight < maxHeight)
            $('#currentSlide').css({position: 'relative', top: (maxHeight-newHeight)/2+'px'});
        else
            $('#currentSlide').css({position: 'relative', top: '0px'});  
        $('#loadingSlide').hide();
        $('#currentSlide').show();
    }
    else
    {
        if (repositionAttempts < 5)
        {
            setTimeout(positionSlidePicture, 300);
            repositionAttempts++;
        }
    }
    
}
function popupBigPicture(e)
{
    swapSlidePicture(e.data.fileName);
}
