﻿var imageProportion = 0.6;
var resizeCheckMs = 100;
var animateDurationMs = 400;
var defaultGalleryHeight = '180px'; // page specific
var largeGalleryHeight = '600px';
var homeGalleryHeight = '368px';
var resizeTimeout = null;
var galleryOpen = false;
var currentGalleryHeight = null;

$(window).resize(function() {
    if (resizeTimeout) {
        window.clearTimeout(resizeTimeout);
        resizeTimeout = null;
    }

    resizeTimeout = window.setTimeout(repositionGalleryContent, resizeCheckMs);
    var bg = $('#background');
    bg.width($(window).width()); bg.height($(document).height());
});

function dropLights() {
    var bg = $('#background');
    //workaround for IE7 bug
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
        var ieversion = new Number(RegExp.$1);
        if (ieversion >= 8) {
            bg.css('display', 'block').fadeTo('slow', 0.8);
        }
    } else {
        $('.gallery .galleryContent img').css('clip', 'auto');
    }
    window.setTimeout(function() { bg.width($(window).width()); bg.height($(document).height()); }, 1000);
}

function raiseLights() {
    $('#background').fadeTo('slow', 0).css('display', 'none');
}

$(document).ready(function() {
    $('.galleryCalloutBox').css('opacity', '0.7').hide();

    /*
    raiseLights();
    alert("create gallery");
    $('.gallery').gallery({
    height: 368,
    interval: 2000,
    ratio: 0.16,
    slideshow: false,
    toggleBar: false
    });
       
    $('.galleryBar,.galleryScreen').hide();

    var img = $('.gallery .galleryContent img');
    var offset = parseInt(($(window).width() * imageProportion) * percentOffsetY);

    img.css('top', (-1 * offset) + 'px');
    img.css('clip', 'rect(' + offset + 'px ' + $(window).width() + 'px ' + ($('.gallery').height() + offset) + 'px 0px)');
    img.width($(window).width());
    */
});

$('.togglegallery').toggle(
    function() {
        galleryOpen = true;
        dropLights();
        $('.galleryCalloutBox').hide();
        $('#learnMoreButton').hide();
        //workaround for IE7 bug for css clip:auto problem
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
            var ieversion = new Number(RegExp.$1);
            if (ieversion >= 8) {
                $('.gallery .galleryContent img').css('clip', 'auto');
            }
        } else {
            $('.gallery .galleryContent img').css('clip', 'auto');
        }
        $(this).addClass('closegallery');
        currentGalleryHeight = $('.gallery').height();
        $('.gallery,.galleryContent').animate(
        {
            height: largeGalleryHeight
        }, 'fast', null,
            function() {
                repositionGalleryContent();
                $('.galleryBar').css('top', '800px').fadeIn().animate(
                {
                    top: String(parseInt(largeGalleryHeight) - $('.galleryBar').height() - 3) + 'px'
                }, 'fast'
            );
                $('.galleryScreen').css('top', '800px').fadeIn().animate(
            {
                top: String(parseInt(largeGalleryHeight) - $('.galleryBar').height() - 15) + 'px',
                height: '80px'
            }, 'fast'
        );
            }
 );
    },
    function() {
        raiseLights();
        galleryOpen = false;
        $(this).removeClass('closegallery');
        $('.galleryBar,.galleryScreen').hide();
        var currentGalleryHeightPx = currentGalleryHeight + "px";
        if (currentGalleryHeightPx == homeGalleryHeight) {
            $('.galleryCalloutBox').show().css('opacity', '0.7');
            $('#learnMoreButton').show();
        } else {
            $('.galleryCalloutBox').hide();
            $('#learnMoreButton').hide();
        }
        $('.gallery,.galleryContent').animate(
            {
                height: currentGalleryHeight
            }, 'fast', null, repositionGalleryContent
        );
    }
);

function repositionGalleryContent() {
    var img = $('.gallery .galleryContent img');
    var re = /[\w_]+_([0-9]+)$/
    var matches = re.exec(img[0].id);
    var index = parseInt(matches[1]);
    var percentOffsetY = $('.galleryBar li a').eq(index).attr('image:yoffset');
        
    var gallery = $('.gallery');
    var offset = parseInt(($(window).width() * imageProportion) * percentOffsetY);
    var endWidth = $(window).width()

    var endHeight = endWidth * imageProportion;
    if (endHeight - offset < gallery.height()) {
        endWidth = parseInt((gallery.height() + offset) / imageProportion) + 1;
        endHeight = gallery.height() + offset;
    }

    img.stop().animate(
        {
            width: endWidth,
            height: endHeight,
            top: (-1 * offset) + 'px',
            clip: 'rect(' + offset + 'px ' + endWidth + 'px ' + (gallery.height() + offset) + 'px 0px)'
        }, animateDurationMs, 'easeOutQuad'
    );
}

function createGallery(windowHeight) {
    $('#background').css('display', 'none');

    if (windowHeight == '368px') {
        //$('.galleryCalloutBox').show();
        //$('#learnMoreButton').show();
        $('.gallery').gallery({
            height: 368,
            interval: 2000,
            ratio: 0.16,
            slideshow: false,
            toggleBar: false
        });
    } else {
        $('.galleryCalloutBox').hide();
        $('#learnMoreButton').hide();
        $('.gallery').gallery({
            height: 180,
            interval: 2000,
            ratio: 0.16,
            slideshow: false,
            toggleBar: false
        });
    }

    $('.galleryBar,.galleryScreen').hide();

    var img = $('.gallery .galleryContent img');
    var re = /[\w_]+_([0-9]+)$/
    var matches = re.exec(img[0].id);
    var index = parseInt(matches[1]);
    var percentOffsetY = $('.galleryBar li a').eq(index).attr('image:yoffset');

    var offset = parseInt(($(window).width() * imageProportion) * percentOffsetY);
    
    img.css('top', (-1 * offset) + 'px');
    img.css('clip', 'rect(' + offset + 'px ' + $(window).width() + 'px ' + ($('.gallery').height() + offset) + 'px 0px)');
    img.width($(window).width());
}

function growGalleryWindow() {
    createGallery(homeGalleryHeight); 
}  

function shrinkGalleryWindow() {
    if (document.referrer.indexOf('Default.aspx') != -1) {
        createGallery(homeGalleryHeight);
        $('div.gallery').animate({ height: defaultGalleryHeight }, 'slow');
    } else {
        createGallery(defaultGalleryHeight);
    }
}