﻿var Slideshow = new function () {

    var me = this;
    this.Holder;
    this.PageCount;
    this.PageWidth;
    this.CurrentPage;
    this.Seconds;

    this.CurrentLeft;
    this.TargetLeft;
    this.StartTime;
    this.TimeOutID;
    this.Paused;
    this.CallBackFunction;
    this.Shift = 1;
    this.FractionDone = 1;

    this.Setup = function (sHolder, iPageCount, iPageWidth, iSeconds) {
        this.Holder = f.GetObject(sHolder);
        this.PageCount = iPageCount;
        this.PageWidth = iPageWidth;
        this.Seconds = iSeconds;
        this.CurrentPage = 1;
        this.CurrentLeft = 0;
        this.TimeOutID = -1;
        this.Paused = false;
    }

    this.Forward = function () {

        if (me.Paused) return true;
        clearInterval(me.TimeOutID);

        if (me.CurrentPage + me.Shift > me.PageCount) {
            me.TargetLeft = 0;
            me.CurrentPage = 1;
        } else {
            me.TargetLeft = me.CurrentLeft - (me.Shift * me.PageWidth);
            me.CurrentPage += me.Shift;
        }

        me.Shift = 1;
        this.StartTime = new Date();
        Slideshow.Slide();
       }

     this.SlideBackward = function () {

       	if (me.Paused) return true;
       	clearInterval(me.TimeOutID);

       	if (me.CurrentPage > 1) {
       		me.TargetLeft = me.CurrentLeft + me.PageWidth;
       		me.CurrentPage -= 1;
       	} else {
       		me.TargetLeft = -((me.PageCount - 1) * me.PageWidth);
       		me.CurrentPage = me.PageCount;
       	}

       	me.Shift = -1;
       	this.StartTime = new Date();
       	Slideshow.Slide();
       }

    this.Backward = function () {
        if (me.CurrentPage > 1) {
            me.TargetLeft = me.CurrentLeft + me.PageWidth;
            me.CurrentPage -= 1;
        } else {
            me.TargetLeft = -((me.PageCount - 1) * me.PageWidth);
            me.CurrentPage = me.PageCount;
        }
        this.StartTime = new Date();

        for (var i = 0; i < 10; i++) {
            setTimeout('Slideshow.Slide()', i * 50);
        }

    }


    this.Slide = function () {

        me.FractionDone = Math.abs((new Date() - me.StartTime) / me.Seconds / 1000);
        if (me.FractionDone < 1) {
            me.Holder.style.left = me.CurrentLeft + (me.TargetLeft - me.CurrentLeft) * Math.sin(Math.PI / 2 * me.FractionDone) + 'px';
            me.TimeOutID = setTimeout('Slideshow.Slide();', 20);
        } else {
            me.Holder.style.left = me.TargetLeft + 'px';
            me.CurrentLeft = me.TargetLeft;
            me.TimeOutID = setInterval('Slideshow.Forward();', 4000);
        }
    }


    this.Pause = function () {
        this.Paused = true;
        clearTimeout(me.TimeOutID);
    }

    this.HoldShow = function (sSlideShowThumbName) {
        me.Paused = true;
        f.Show(sSlideShowThumbName);
    }

    this.Resume = function (sViewPortName, sSlideShowThumbName) {
        var bInDiv = this.CursorWithinObject(sViewPortName);

        if (bInDiv == false) {
            me.Paused = false;
            f.Hide(sSlideShowThumbName);
        }
    }

    this.ThumbSelectShowImage = function (iThumb) {
        me.Paused = false;
        me.Shift = iThumb - me.CurrentPage;
        //if we have completed the slide then just reset shift time out
        if (me.FractionDone >= 1) Slideshow.Forward();
        me.Paused = true;
    }


    this.GetCursorPosition = function (e) {
        e = e || window.event;
        var oCursor = { x: 0, y: 0 };
        if (e.pageX || e.pageY) {
            oCursor.x = e.pageX;
            oCursor.y = e.pageY;
        }
        else {
            var oDocument = document.documentElement;
            var oBody = document.body;
            oCursor.x = e.clientX +
            (oDocument.scrollLeft || oBody.scrollLeft) - (oDocument.clientLeft || 0);
            oCursor.y = e.clientY +
            (oDocument.scrollTop || oBody.scrollTop) - (oDocument.clientTop || 0);
        }
        return oCursor;
    }

    this.CursorWithinObject = function (sDivName) {
        var oCursor = me.GetCursorPosition();
        var oDivPosition = me.GetObjectPosition(sDivName)

        return oCursor.x > oDivPosition.left && oCursor.x < oDivPosition.right && oCursor.y > oDivPosition.top && oCursor.y < oDivPosition.bottom
    }

    this.GetObjectPosition = function (sObject) {
        var oObjectPosition = { left: 0, top: 0, right: 0, bottom: 0 };
        var oObject = f.SafeObject(sObject)

        if (oObject) {
            var oObjectTemp = f.SafeObject(oObject)

            while (oObjectTemp) {
                oObjectPosition.left += oObjectTemp.offsetLeft;
                oObjectPosition.top += oObjectTemp.offsetTop;
                oObjectTemp = oObjectTemp.offsetParent;
            }

            oObjectPosition.right = oObjectPosition.left + oObject.offsetWidth;
            oObjectPosition.bottom = oObjectPosition.top + oObject.offsetHeight;
        }

        return oObjectPosition;

    }
}
