﻿var Tools =
{
    // -------------------------------------------------------------------------------

	toolsPathToRoot: "",
	init: function (pathToRoot)
    {
		this.toolsPathToRoot = pathToRoot;

        this.collapsableSections.init();
        this.photoStripAnim.init();
    },

    // -------------------------------------------------------------------------------

    CollapsableSlideTime: 400,
    collapsableSections:
    {
        // -------------------------------------------------------------------------------

        init: function ()
        {
            $('div.collapsable.open').next().show();

            $('div.collapsable').live('click', function ()
            {
                var section = $(this);
                var sectionContent = section.next('div.collapsable-content');

                if (sectionContent.is(':visible'))
                {
                    Tools.collapsableSections.close(section, sectionContent);
                }
                else
                {
                    Tools.collapsableSections.open(section, sectionContent);
                }
            });
        },

        // -------------------------------------------------------------------------------

        open: function (section, sectionContent)
        {
            section.removeClass('closed');
            section.addClass('open');
            sectionContent.slideDown(Tools.CollapsableSlideTime);
        },

        // -------------------------------------------------------------------------------

        openWithDelay: function (section, sectionContent, delayInMs)
        {
            section.removeClass('closed');
            section.addClass('open');
            sectionContent.delay(delayInMs).slideDown(Tools.CollapsableSlideTime);
        },

        // -------------------------------------------------------------------------------

        close: function (section, sectionContent)
        {
            sectionContent.slideUp(Tools.CollapsableSlideTime,
                function ()
                {
                    section.removeClass('open');
                    section.addClass('closed');
                });
        },

        // -------------------------------------------------------------------------------

        closeWithDelay: function (section, sectionContent, delayInMs)
        {
            sectionContent.delay(delayInMs).slideUp(Tools.CollapsableSlideTime,
                function ()
                {
                    section.removeClass('open');
                    section.addClass('closed');
                });
        }

        // -------------------------------------------------------------------------------
    },

    PhotoReloadDelay: 20000,
    photoStripAnim:
    {
        // -------------------------------------------------------------------------------

        init: function ()
        {
            var section = $('#PhotoStripContainer');
            var sectionContent = section.next('div.collapsable-content')
            this.startPhotoReload(section, sectionContent, Tools.PhotoReloadDelay);
        },

        // -------------------------------------------------------------------------------

        startPhotoReload: function (section, sectionContent, delayInMs)
        {
            sectionContent.delay(delayInMs).slideUp(Tools.CollapsableSlideTime,
                function ()
                {
                    section.removeClass('open');
                    section.addClass('closed');

					var pageUrl = Tools.toolsPathToRoot + '/Master/PhotoStrip.aspx';

                    $('#PhotoStripContent').load(pageUrl + '?path=' + Tools.toolsPathToRoot + '&time=' + Date.now() + ' #PhotoStripContent', 
                        function()
                        {
                            section.removeClass('closed');
                            section.addClass('open');
                            sectionContent.slideDown(Tools.CollapsableSlideTime);
                            Tools.photoStripAnim.startPhotoReload(section, sectionContent, Tools.PhotoReloadDelay);
                        });
                });
        }

        // -------------------------------------------------------------------------------
    },

    // -------------------------------------------------------------------------------

    BlastReloadDelay: 10000,
	BlastFadeTime: 600,
	
    blastFromThePastAnim:
    {
        // -------------------------------------------------------------------------------

        init: function (linkOpensNewWindow)
        {
            var section = $('#BFTP_Reload');
            this.startBlastReload(section, Tools.BlastReloadDelay, linkOpensNewWindow);
        },

        // -------------------------------------------------------------------------------

        startBlastReload: function (section, delayInMs, linkOpensNewWindow)
        {
            section.delay(delayInMs).fadeTo(Tools.BlastFadeTime, 0, 
                function ()
                {
                    var pageUrl = Tools.toolsPathToRoot + '/HomeControls/BlastFromThePast.aspx';
					section.load(pageUrl + '?path=' + Tools.toolsPathToRoot + '&newWindow=' + linkOpensNewWindow + '&time=' + Date.now() + ' #BFTP_Frame', 
                        function()
                        {
                            section.fadeTo(Tools.BlastFadeTime, 1);
                            Tools.blastFromThePastAnim.startBlastReload(section, Tools.BlastReloadDelay, linkOpensNewWindow);
                        });
                });
        }

        // -------------------------------------------------------------------------------
    },

    // -------------------------------------------------------------------------------

    photoBlastFromThePastAnim:
    {
        // -------------------------------------------------------------------------------

    	paused: false,

        // -------------------------------------------------------------------------------

    	init: function (startIndex, numEvents)
        {
            var section = $('#BFTP_Reload');
    		Tools.photoBlastFromThePastAnim.paused = false;
            this.startBlastReload(section, Tools.BlastReloadDelay, startIndex, startIndex, numEvents);
        },

        // -------------------------------------------------------------------------------

        startBlastReload: function (section, delayInMs, currentIndex, startIndex, numEvents)
        {
            section.delay(delayInMs).fadeTo(Tools.BlastFadeTime, 0, 
                function ()
                {
                    if (Tools.photoBlastFromThePastAnim.paused == true) 
                    {
                    	// Try this index again after a delay.
						section.fadeTo(Tools.BlastFadeTime, 1);
                    	Tools.photoBlastFromThePastAnim.startBlastReload(section, Tools.BlastReloadDelay, currentIndex, startIndex, numEvents);
                    }
                    else 
                    {
                		// AJAX for the next event's details.
                    	var pageUrl = Tools.toolsPathToRoot + '/PartyPics/PhotoBlastFromThePast.aspx';
                		var nextIndex = Tools.photoBlastFromThePastAnim.getNextEventIndex(currentIndex, startIndex, numEvents);
                	
						section.load(pageUrl + '?path=' + Tools.toolsPathToRoot + '&index=' + nextIndex + '&time=' + Date.now() + ' #BFTP_Frame', 
							function()
							{
								section.fadeTo(Tools.BlastFadeTime, 1);
                        		CB_Init();
								Tools.photoBlastFromThePastAnim.startBlastReload(section, Tools.BlastReloadDelay, nextIndex, startIndex, numEvents);
							});
                    }
                });
        },
    	
        // -------------------------------------------------------------------------------

    	pauseBlast: function () 
    	{
    		Tools.photoBlastFromThePastAnim.paused = true;
    	},
    	
        // -------------------------------------------------------------------------------

    	restartBlast: function () 
    	{
    		Tools.photoBlastFromThePastAnim.paused = false;
    	},
    	
        // -------------------------------------------------------------------------------

    	getNextEventIndex: function (currentIndex, startIndex, numEvents)
		{
			var index = currentIndex + 1;
			if (index == numEvents) 
			{
				index = startIndex;
			}

    		return index;
		}

        // -------------------------------------------------------------------------------
    },

    // -------------------------------------------------------------------------------

    ScrollToDuration: 1000,
    handleErrorLabel: function (errorLabel, errorContainer, previousContainer)
    {
        var messageIsHidden = errorContainer.hasClass('hide');

		var yOffset = 0;
		var element = errorContainer;

		do 
		{
			yOffset += element.offset().top;
			element = element.parent();
		}
		while (!element.is('body'));

        if (errorLabel.text() == '') 
        {
            if (!messageIsHidden) 
            {
                previousContainer.addClass('text-block-lower');

				$.scrollTo(yOffset, { duration: Tools.ScrollToDuration });

                errorContainer.slideUp(Tools.CollapsableSlideTime,
                    function () 
                    {
                        errorContainer.addClass('hide');
                    });
            }
        }
        else 
        {
            if (messageIsHidden) 
            {
                previousContainer.removeClass('text-block-lower');

				$.scrollTo(yOffset, { duration: Tools.ScrollToDuration });

                errorContainer.slideDown(Tools.CollapsableSlideTime,
                    function () 
                    {
                        errorContainer.removeClass('hide');
                    });
            }
            else
            {
				$.scrollTo(yOffset, { duration: Tools.ScrollToDuration });
            }
        }
    }

    // -------------------------------------------------------------------------------
};
