﻿var FPLength = 0;
var ContainerWidth = 125;
var ULLeft = 0;
var DivLeft = 0;
var DivNumber = 0;
var PhotographLoadingControl = new Image();
var PhotographPath = null;
var FPSN = 0;
var NextPrevLoadControl = null;
var PhotographID = 0;
var LoadingInfoText = null;
var Type = null;
var AnimateType = null;
var NextPrevNumber = 0;
var DivNumber = 0;
var MinNumber = 0;
var BSNumber = 0;
var BSContainerLeft = 0;
var BannerSlidePhoto = 0;
var BannerSlidePhotoID = 0;
var BannerAnimateTime;
var BAStopStartTime;
var LinkControl = null;
var PhotoNameUrl = null;

$(document).ready(function () {
    FPLength = $('.FPListContainer ul li').length;
    $('.FPTotalNumber').text(FPLength);
    var ULLength = FPLength * ContainerWidth;
    $('.FPListContainer ul').css('width', '' + ULLength + 'px');
});

$(window).load(function () {
    BannerSlideOpen();
});

function BannerSlideOpen() {
    $('#BAControl').val('True');
    BSNumber = $('#BSNumber').val();
    if (BSNumber > 3) {
        BSNumber = 0;
    }
    BSContainerLeft = BSNumber * 245;
    BSNumber++;
    $('#BSNumber').val(BSNumber);
    BannerSlidePhoto = $('#BSPhotoNumber').val();
    if ($('.LoadingPhotoRecord').find('.LLargePhotoControl' + BannerSlidePhoto + '').text() == '') {
        $('.SCCPhotoBoxContent').html($('#FPLoadingText').val());
    }
    else {
        $('.SCCPhotoBoxContent').html($('.LoadingPhotoRecord').find('.LLargePhotoControl' + BannerSlidePhoto + '').html());
    }
    if ($('#BASControl').val() == 'True') {
        BannerSlideOpenClose('Open', BannerSlidePhoto);
    }
}

function BannerSlideClose(AnimateType, ActionNumber) {
    BannerSlideOpenClose('Close', '', AnimateType, ActionNumber);

}

function BannerSlideOpenClose(Type, ID, AnimateType, ActiveNumber) {
    clearTimeout(BannerAnimateTime);

    if (Type == 'Open') {
        $('.SCCPhotoBox').css('left', BSContainerLeft).animate({ height: '573px' }, 500).animate({ height: '550px' }, 200).animate({ height: '573px' }, 400, function () {
            if ($('.LoadingPhotoRecord').find('.LLargePhotoControl' + ID + '').text() == '') {
                BannerSlidePhotoLoading();
            }
            else {
                BannerSlideTimeControl();
            }
        });
    }
    else if (Type == 'Close') {
        if (AnimateType == 'Stop') {
            goFavoritePhotograph(ActiveNumber);
        }
        else {
            $('#CAControl').val('True');
            LinkControl = $('.SCCPhotoBoxContent > .SCCPhotoBoxCPhoto a').attr('href');
            $('.SCCPhotoBoxContent > .SCCPhotoBoxCPhoto a').attr('href', 'javascript:void();');
            $('.SCCPhotoBox').animate({ height: '507px' }, 400).animate({ height: '573px' }, 200).animate({ height: '0px' }, 500, function () {
                $('.SCCPhotoBoxContent > .SCCPhotoBoxCPhoto a').attr('href', LinkControl);
                $('#CAControl').val('False');
                if ($('#BAControl').val() == 'False') {
                    $('#BSPhotoNumber').val('1');
                    goFavoritePhotograph(ActiveNumber);
                }
                else {
                    BannerSlideOpen();
                }
            });
        }
    }
}

function BannerSlidePhotoLoading() {
    BannerSlidePhotoID = $('#FPLFileName' + BannerSlidePhoto + '').attr('name');
    JQAjaxStart('AjaxPages/FavoritePhotographInfo.ashx', 'PID=' + BannerSlidePhotoID + '&RT=Large&N=' + BannerSlidePhoto + '', BannerSlidePhotoLoadingResult);
}

function BannerSlidePhotoLoadingResult(AjaxResult) {
    $('.SCCPhotoBoxContent').html('').html(AjaxResult);
    LoadingRecord('LargePhoto', BannerSlidePhoto, AjaxResult);
    BannerSlideTimeControl();
}

function BannerSlideTimeControl() {
    clearTimeout(BannerAnimateTime);
    BannerSlidePhoto++;
    if (BannerSlidePhoto > FPLength) {
        BannerSlidePhoto = 1;
    }
    $('#BSPhotoNumber').val(BannerSlidePhoto);
    BannerAnimateTime = setTimeout(function () { BannerSlideClose(); }, 7000);
}

function PhotographLoading(ActiveNumber) {
    $('.FPActiveNumber').text(ActiveNumber);
    if ($('#FPControl').val() == 'True') {
        $('#FavoritePhotograph').fadeOut(function () {
            InfoTextControl('Loading', ActiveNumber);
            PhotographPreloader(ActiveNumber);
        });
    }
    else {
        $('.PhotoShowOverlay').css({ opacity: '0.7' }).fadeIn(function () {
            $('.FPControlBox').fadeIn();
            $('.LargePhotoContainer').fadeIn(function () {
                InfoTextControl('Loading', ActiveNumber);
                $('#FPControl').val('True');
                PhotographPreloader(ActiveNumber);
            });
        });
    }
}

function InfoTextControl(Type, ActiveNumber, InfoResult) {
    LoadingInfoText = $('#FPLoadingText').val();
    if (Type == 'Loading') {
        if ($('.LoadingPhotoRecord').find('.LPhotoControl' + ActiveNumber + '').text() == '') {
            $('.FPLoading').show();
        }
        $('.FPInfo').fadeOut(function () {
            if ($('.LoadingPhotoRecord').find('.LInfoControl' + ActiveNumber + '').text() == '') {
                $(this).text(LoadingInfoText).fadeIn(function () {
                    FavoritePhotographInfo(ActiveNumber);
                });
            }
            else {
                $(this).html($('.LoadingPhotoRecord').find('.LInfoControl' + ActiveNumber + '').html()).fadeIn();
            }
        });
    }
    else if (Type == 'InfoResult') {
        $('.FPInfo').fadeOut(function () {
            $(this).html(InfoResult).fadeIn();
            LoadingRecord('Info', ActiveNumber, InfoResult);
        });
    }
}

function PhotographPreloader(ActiveNumber) {
    PhotographPath = $('#URL').val() + '/PhotographDepot/FavoriteLarge/' + $('#FPLFileName' + ActiveNumber + '').val();
    PhotographLoadingControl.onload = function () {
        $('#FavoritePhotograph').attr('src', PhotographPath);
        PhotographLoadingControl.onload = function () { };
        PhotographBox_resize(ActiveNumber, PhotographLoadingControl.width, PhotographLoadingControl.height);
    }
    PhotographLoadingControl.src = PhotographPath;
}

function PhotographBox_resize(ActiveNumber, imgWidth, imgHeight) {
    $('.FPLoading').hide();
    $('.PhotographBox').animate({ width: imgWidth, height: imgHeight }, 337, function () { PhotographShowHide(ActiveNumber); });
    if ($('.LoadingPhotoRecord').find('.LPhotoControl' + ActiveNumber + '').text() == '') {
        LoadingRecord('Photo', ActiveNumber, 'True');
    }
}

function LoadingRecord(Type, ActiveNumber, Data) {
    $('.LoadingPhotoRecord').append('<div class="L' + Type + 'Control' + ActiveNumber + '">' + Data + '</div>');
}

function PhotographShowHide(ActiveNumber) {
    $('#FavoritePhotograph').fadeIn(function () {
        $('#PhotoLoadControl').val('True');
        PhotoNameUrl = $('#FPLFileName' + ActiveNumber + '').val();
        $('#FavoritePhotographLink').attr('href', '' + $('#URL').val() + '/' + $('#URLPhotograph').val() + '/' + PhotoNameUrl.replace('-template02.jpg', '') + '.html');
    });
}

function FavoritePhotographInfo(ActiveNumber) {
    $('.FPInfo').ajaxStop(function () { $(this).fadeOut(); });
    $('#ActiveNumber').val(ActiveNumber);
    PhotographID = $('#FPLFileName' + ActiveNumber + '').attr('name');
    JQAjaxStart('AjaxPages/FavoritePhotographInfo.ashx', 'PID=' + PhotographID + '&RT=Small', FavoritePhotographInfoResult);
}

function FavoritePhotographInfoResult(AjaxResult) {
    InfoTextControl('InfoResult', $('#ActiveNumber').val(), AjaxResult);
}

function goFavoritePhotograph(ActiveNumber, Type) {
    clearTimeout(BannerAnimateTime);
    clearTimeout(BAStopStartTime);
    $('.FPNextPrev a').css('display', 'block');
    $('#FPSN').val(ActiveNumber);
    if (Type == 'LargePhoto') {
        $('#BAControl').val('False');
        BannerSlideClose('', ActiveNumber);
    }
    else if (Type == 'SmallPhoto' && $('#BAControl').val() == 'True') {
        $('#BASControl').val('False');
        $('#BAControl').val('False');
        if ($('#CAControl').val() == 'False') {
            BannerSlideClose('', ActiveNumber);
        }
        else {
            BannerSlideClose('Stop', ActiveNumber);
        }
    }
    else {
        if ($('#PhotoLoadControl').val() == 'True') {
            $('#BAControl').val('False');
            $('#PhotoLoadControl').val('False');
            goFPAnimate(ActiveNumber);
        }
    }
}

function FPClose() {
    clearTimeout(BAStopStartTime);
    $('#BSNumber').val('0');
    $('.FPNextPrev a').css('display', 'none');
    $('.PhotoShowOverlay, .LargePhotoContainer, .FPControlBox').fadeOut(function () {
        $('#FavoritePhotograph').css('display', 'none');
        $('.PhotographBox').css({ width: '73px', height: '73px' });
        $('.FPActiveNumber').text('');
        $('.FPInfo').html('');
    });
    $('#FPControl').val('False');
    $('#BASControl').val('True');
    goFPAnimate('1', 'Stop');
    BAStopStartTime = setTimeout(function () { BannerSlideOpen(); }, 1373);
}

function goFPAnimate(ActiveNumber, Type) {
    if (ActiveNumber > 4 && ActiveNumber < (FPLength - 2)) {
        DivLeft = (ContainerWidth * 4) - ContainerWidth;
        ULLeft = (ActiveNumber - 4) * ContainerWidth;
        AnimateType = 'All';
    }
    else if (ActiveNumber <= 4) {
        DivLeft = (ActiveNumber * ContainerWidth) - ContainerWidth;
        ULLeft = 0;
    }
    else {
        if (ActiveNumber == FPLength) {
            DivNumber = 7;
        }
        else if (ActiveNumber == (FPLength - 1)) {
            DivNumber = 6;
        }
        else if (ActiveNumber == (FPLength - 2)) {
            DivNumber = 5;
        }
        else if (ActiveNumber == (FPLength - 3)) {
            DivNumber = 4;
        }
        DivLeft = (DivNumber * ContainerWidth) - ContainerWidth;
        ULLeft = (FPLength - 7) * ContainerWidth;
    }
    $('.FPSelective').fadeIn(function () {
        if (AnimateType == 'All') {
            $('.FPSelective').animate({ left: '' + DivLeft + 'px' }, 377, function () { if (Type == 'Stop') { $('.FPSelective').fadeOut('slow'); } else { PhotographLoading(ActiveNumber); } });
            $('.FPListContainer ul').animate({ left: '-' + ULLeft + 'px' }, 377);
        }
        else {
            $('.FPSelective').animate({ left: '' + DivLeft + 'px' }, 377, function () { if (Type == 'Stop') { $('.FPSelective').fadeOut('slow'); } else { PhotographLoading(ActiveNumber); } });
            $('.FPListContainer ul').animate({ left: '-' + ULLeft + 'px' }, 377);
        }
    });
}

function goFPNextPrev(Type) {
    if ($('#PhotoLoadControl').val() == 'True') {
        FPSN = $('#FPSN').val();
        NextPrevLoadControl = false;
        if (Type == 'Next' && FPSN < FPLength) {
            FPSN++;
            NextPrevLoadControl = true;
        }
        else if (Type == 'Prev' && FPSN > 1) {
            FPSN--;
            NextPrevLoadControl = true;
        }

        if (NextPrevLoadControl == true) {
            $('#FPSN').val(FPSN);
            $('#PhotoLoadControl').val('False');
            FPLAnimate(FPSN, Type);
        }
    }
}

function FPLAnimate(ActiveNumber, Type) {
    if (Type == 'Next') {
        NextPrevNumber = FPLength - 2;
        MinNumber = 4;
    }
    else if (Type == 'Prev') {
        NextPrevNumber = FPLength - 3;
        MinNumber = 3;
    }

    if (ActiveNumber > MinNumber && ActiveNumber < NextPrevNumber) {
        ULLeft = (ActiveNumber - 4) * ContainerWidth;
        $('.FPListContainer ul').animate({ left: '-' + ULLeft + 'px' }, 377, function () { PhotographLoading(ActiveNumber); });
    }
    else {
        if (ActiveNumber == FPLength) {
            DivNumber = 7;
        }
        else if (ActiveNumber == (FPLength - 1)) {
            DivNumber = 6;
        }
        else if (ActiveNumber == (FPLength - 2)) {
            DivNumber = 5;
        }
        else if (ActiveNumber == (FPLength - 3)) {
            DivNumber = 4;
        }
        else {
            DivNumber = ActiveNumber;
        }
        DivLeft = (DivNumber * ContainerWidth) - ContainerWidth;
        $('.FPSelective').animate({ left: '' + DivLeft + 'px' }, 377, function () { PhotographLoading(ActiveNumber); });
    }
}
