﻿function getScrollHeight()
{
    var content = $(".page-content");
    return (content.attr("scrollHeight") - content.innerHeight()) / 100;
}

function slider_change(e, ui)
{
    scroll(ui.value);
}

function slider_slide(e, ui)
{
    scroll(ui.value);
}

function scroll(value)
{
    var scrollHeight = getScrollHeight();
    var scrollPosition = 100 - value;
    $(".page-content").attr({ scrollTop: (scrollHeight * scrollPosition) });
}


function hookEvent(element, eventName, callback)
{
    if (typeof (element) == "string")
    {
        element = $(element).get(0);
    }

    if (element == null)
    {
        return;
    }

    if (element.addEventListener)
    {
        if (eventName == 'mousewheel')
        {
            element.addEventListener('DOMMouseScroll', callback, false);
        }

        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent)
    {
        element.attachEvent("on" + eventName, callback);
    }
}

function unhookEvent(element, eventName, callback)
{
    if (typeof (element) == "string")
        element = document.getElementById(element);

    if (element == null)
        return;

    if (element.removeEventListener)
    {
        if (eventName == 'mousewheel')
            element.removeEventListener('DOMMouseScroll', callback, false);
        element.removeEventListener(eventName, callback, false);
    }
    else if (element.detachEvent)
        element.detachEvent("on" + eventName, callback);
}

function cancelEvent(e)
{
    e = e ? e : window.event;
    if (e.stopPropagation)
        e.stopPropagation();
    if (e.preventDefault)
        e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
}

function scrollPageContent(e)
{
    e = e ? e : window.event;
    var wheelData = e.detail ? e.detail : e.wheelDelta;
    var wheelValue = e.detail ? e.detail * -1 : e.wheelDelta / 40;

    var sliderValue = $('#content-slider').slider('option', 'value');

    var value = sliderValue + wheelValue;

    $('#content-slider').slider('option', 'value', value);
    scroll(value)

    cancelEvent(e);
}

function initSlider()
{
    var slider = $("#content-slider");
    
    if (slider.length > 0)
    {
        $("#content-slider").slider({
            orientation: "vertical",
            animate: true,
            change: slider_change,
            slide: slider_slide,
            value: 100
        });

        hookEvent('.page-content', 'mousewheel', scrollPageContent);
    }
}




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, function() { initSlider(); });
            });

        });
    }, 1200);

    _isPageLoaded = true;
}

function minimizePageContent()
{
    var wrap = $('.content-wrap');
    var originalWidth = wrap.width();

    wrap.find('.close').addClass('open-x').removeClass('close-x')

    wrap.animate({ width: "25px" }, 500, function()
    {
        wrap.mouseover(function()
        {
            $(this).animate({ width: originalWidth }, 350)
                .unbind('mouseover')
                .find('.close').removeClass('open-x').addClass('close-x');
        });
    });
}

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 = .66;

        if (wrap.is("[size='grande']"))
        {
            widthAdjustment = .88;
        }
        else if (wrap.is("[size='full']"))
        {
            widthAdjustment = 1;
        }
        
        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);


(function($)
{
    $.loadImages = function(list)
    {
        var bucket = document.createElement('div');
        $(bucket).css('display', 'none').addClass('bg-images-bucket');

        var count = list.length;

        for (var i = count; i--; )
        {
            var image = document.createElement('img');
            image.src = list[i];

            setTimeout(function()
            {
                bucket.appendChild(image);
            }, 250);
        }

        $('body').append(bucket);
    }
})(jQuery)
