﻿function Size()
{
    this.height = 0;
    this.width = 0;
}

function Photo()
{
    this.height = 0;
    this.width = 0;
    this.fileName = '';
}

var PhotoUtil =
{
    current: function()
    {

        function _getFileName()
        {
            var fileName = $('image.mask').attr("src");
            return fileName.substring(fileName.lastIndexOf('/') + 1);
        }

        var photo = new Photo();
        photo.fileName = _getFileName();

        var size = { Height: 0, Width: 0 };


        window.ImageService.execute('GetSize', { fileName: photo.fileName }, function(response)
        {
            photo.height = response.result.Height;
            photo.width = response.result.Width;
        });

        return photo;
    }
}

window.currentPhoto = PhotoUtil.current;

var _isPageLoaded = false;
var MIN_MASK_WIDTH = 1500;

function isPageBeingEdited()
{
    return $('.sf_wrapper').length > 0;
}

function closePageContent()
{
    var wrap = $(".content-wrap");
    var tweenAmount = wrap.width() + 15;

    wrap.animate({ width: tweenAmount }, 250, function()
    {
        $(".content-wrap").hide('slide', { direction: 'right' }, 'fast');
    });
}

function showPageContent()
{
    if (_isPageLoaded)
    {
        return;
    }

    setTimeout(function()
    {
        var wrap = $('.content-wrap');

        wrap.show('slide', { direction: 'right' }, 'normal', function()
        {
            var content = $(".page-content");

            if (content.attr("scrollHeight") > content.innerHeight())
            {
                $("#content-slider").show();
            }

            var originalWidth = wrap.width();
            var tweenAmount = wrap.width() + 15;

            wrap.animate({ width: tweenAmount }, 10, function()
            {
                wrap.animate({ width: originalWidth }, 200);
            });

        });
    }, 1200);

    _isPageLoaded = true;
}

function minimizePageContent()
{
    var wrap = $('.content-wrap');
    var originalWidth = wrap.width();

    wrap.find('.close-x').html('&lt;');

    wrap.animate({ width: "25px" }, 500, function()
    {
        wrap.mouseover(function()
        {
            $(this).animate({ width: originalWidth }, 350)
                .unbind('mouseover')
                .find('.close-x').html('&gt;');
        });
    });
}

function setContentDimensions()
{
    var win = $(window);
    var page = $("#pageContent");
    var footer = $('#footer');
    var header = $('#header');
    
    var maskHeight = 623;
    var defaultBarHeight = 35;

    if (!isPageBeingEdited()) //resize footer
    {
        var adjustedHeight = win.height() - (maskHeight + defaultBarHeight);
        footer.height(Math.max(adjustedHeight, defaultBarHeight));
    }
    
    // adjust page viewport
    var totalHeight = page.offset().top + page.height() + footer.height();

    if (totalHeight != win.height())
    {
        var adjustedHeight = win.height() - (page.offset().top + footer.height());
        page.height(adjustedHeight);
    }

    //adjust page content panel
    
    var wrap = $(".content-wrap");

    if (wrap.length > 0)
    {
        var content = $(".page-content");

        var widthAdjustment = wrap.is("[size='full']") ? .88 : .66;
        
        var size = new Size();
        size.width = page.width() * widthAdjustment;
        size.height = page.height() * .94;

        wrap.width(size.width).height(size.height);

        var contentHeight = wrap.height() - 110;

        content.height(contentHeight);

        var slider = $("#content-slider");
        slider.height(contentHeight);
    }
    
}

function configureMask()
{
    var mask = $('div.mask');
    var imageMask = mask.find('img');
    var win = $(window);

    if (!mask.is(":visible")) {
        mask.fadeIn('slow');
    }

    if (win.width() >= MIN_MASK_WIDTH) {
        imageMask.width(win.width());
    }
    

}

function adjustScreenElements()
{
    configureMask();

    showPageContent();

    setContentDimensions();
    
}

function window_onResize()
{
    adjustScreenElements();
}


$(document).ready(function()
{
    adjustScreenElements();
});


$(window).bind('resize', window_onResize);
