﻿var contentflow;
var myNewFlow;

$(document).ready(function () {	
    contentflow = $("#imageFlow");
	
    if (!contentflow.is("*")) return;

    var rotating = true;
    var rotateTimer = null;
    startRotate();

    function startRotate() {
        rotateTimer = window.setTimeout(rotateNext, 8000);
    }

    function rotateNext() {
        if (!rotating) { return; }
        if (!myNewFlow) { return; }
        myNewFlow.moveTo("next");
        window.clearTimeout(rotateTimer);
        startRotate();
    }

    contentflow.hover(
        function () {
            rotating = false;
            window.clearTimeout(rotateTimer);
        },
        function () {
            rotating = true;
            startRotate();
        }
    );

    contentflow.find(".e-caption").click(function () {
        var url = $(this).parent().attr("href");
        if (url) document.location = url;
    });	
});

function initImageContentFlow(flowType)
{
    $(document).ready(function () {
        $("#imageFlow").addClass(flowType);
    });
    var options = {
        reflectionHeight: 0.0,
        startItem: "first",
        scrollInFrom: "none",
        endOpacity: 0,

        onMakeInactive: function (item) {


            $(".globalLabel div", contentflow).fadeIn().text($(item.label).text());
        },

        onMakeActive: function (item) {
            $(".e-caption", contentflow).clearQueue().hide();
            $(item.caption).fadeIn();
            $(".globalLabel div", contentflow).fadeIn().text($(item.label).text());
        },

        onclickActiveItem: function (item) {
            var url, target;
            if (url = item.element.getAttribute('href')) {
                target = item.element.getAttribute('target');
            }
            else if (url = item.content.getAttribute('href')) {
                target = item.content.getAttribute('target');
            }
            if (url) {
                if (target)
                    window.open(url, target).focus();
                else
                    window.location.href = url;
            }
        }
    }

    if (flowType === 'zoom') {
        $.extend(options, {
            scaleFactor: 1.52,
            flowSpeedFactor: 0.5,
            relativeItemPosition: "center",
            visibleItems: 3,
            scaleFactorLandscape: 1.0,

            calcSize: function (item) {
                var rP = item.relativePosition;
                var rPN = item.relativePositionNormed;
                var h = (rP <= -1) ? 0 : (rP < 0 ? 1 - rP : 1 - rPN);
                var w = h;
                return { width: w, height: h };
            },

            calcCoordinates: function (item) {
                var rP = item.relativePosition;
                var rPN = item.relativePositionNormed;
                var x = 0;
                var y = -0.75 - (rP >= 0 ? rPN / 2 : -rP * 0.75);
                return { x: x, y: y };
            },

            calcRelativeItemPosition: function (item) {
                var x = 0;
                var y = 1;
                return { x: x, y: y };
            },

            calcZIndex: function (item) {
                return -item.relativePositionNormed;
            },

            calcOpacity: function (item) {
                var rP = item.relativePosition;
                var rPN = item.relativePositionNormed;
                var outOpacity = 1 + (rP * 4);
                outOpacity = outOpacity < 0 ? 0 : (outOpacity > 1 ? 1 : outOpacity);
                return rP <= -1 ? 0 : (rP < 0 ? outOpacity : 1 - rPN * 0.85);
            }
        });
    }
    else {
        $.extend(options, {
            visibleItems: 3,
            scaleFactor: 1.5,
            flowSpeedFactor: 1.5,
            calcCoordinates: function (item) {
                var rP = item.relativePosition;
                var x = rP * 2.5 * (item.size.width / 10) * this.conf.scaleFactor;
                var y = 0.1 - (Math.abs(rP) * 0.1);
                return { x: x, y: y };
            },

            calcSize: function (item) {
                var rP = item.relativePosition;
                var h = 4 / (Math.abs(rP) + 4);
                var w = h;
                return { width: w, height: h };
            },

            calcOpacity: function (item) {
                var opa = 1.3 - Math.abs(item.relativePositionNormed);
                return opa < 1 ? opa : 1;
            }
        });
    }

    myNewFlow = new ContentFlow("imageFlow", options);
}
