Type.registerNamespace("manathanWeb");

manathanWeb.Boxed = function(images, prev, next, doSlideshow, slideshowSpeed, bigImgId, imgId, txtId, showPrevNext) {
    this._index = 0;
    this._currIndex = null;
    this._doSlideshow = doSlideshow;
    this._slideshowSpeed = slideshowSpeed;
    this._slideshow = null;
    this._images = images;
    this._next = next;
    this._prev = prev;
    this._animation = null;
    this._lastAnimation = null;
    this._bigImgId = bigImgId;
    this._imgId = imgId;
    this._txtId = txtId;
    this._addToId = '';
    this._showPrevNext = showPrevNext;
};

manathanWeb.Boxed.prototype = {

    /** Private methods */
    __IndexHandler : function(index) {
        if (this._doSlideshow) 
            window.clearTimeout(this._slideshow);
        this._index = index;
        if (this._index >= this._images.length)
            this._index = 0;
        if (this._index < 0)
            this._index = _images.length;
        if (this._next) this._next.style.visibility = (this._index == this._images.length - 1 && this._showPrevNext) ? 'hidden' : 'visible';
        if (this._prev) this._prev.style.visibility = (this._index == 0 && this._showPrevNext) ? 'hidden' : 'visible';
        this._currIndex = this._images[this._index];
    },

    __AfterTasks : function() {
        if (this._doSlideshow)
            this._slideshow = window.setTimeout('DoSlideshow();', this._slideshowSpeed);
    },

    __GetCurrentAnimation : function() {
        var onclick = $find(this._currIndex + this._addToId); 
        if (!onclick) { 
            alert('manathanWeb.Slides error: reference to the animation ' + this._currIndex + ' was corrupt'); 
            return false; 
        }; 
        this._animation = onclick.get_OnClickBehavior().get_animation(); 
        return true;
    },
    __PlayAnimations : function() {
        this.__IndexHandler(this._index);
        if (!this.__GetCurrentAnimation())
            return false;
        this._animation.play();
        this.__AfterTasks();
        return false;
    },

    /** Public methods */
    ChangeImage : function(index, url, description, alt, dontHideContainer) {
        try {
            this.StopRunningAnimation();
            this.__IndexHandler(index);
            this.__GetCurrentAnimation();
            var gid = ((dontHideContainer) ? '' : this._bigImgId);
            ShowImage(gid, this._imgId, this._txtId, url, description, alt);
            this._lastAnimation = this._animation;
            this.__AfterTasks();
        } catch (err) {
            alert('manathanWeb.Boxed.ChangeImage(index, url, descr) error: ' + err);
        }
    },

    NextImage : function() {
        try {
            window.clearTimeout(this._slideshow);
            this._index += 1;
            this._addToId = "_next";
            this.__PlayAnimations();
        } catch (err) {
            alert('manathanWeb.Boxed.NextImage() error: ' + err);
        }
        this._addToId = '';
        return false;
    },

    PreviousImage : function() {
        try {
            window.clearTimeout(this._slideshow);
            this._index -= 1;
            this._addToId = "_prev";
            this.__PlayAnimations();
        } catch (err) {
            alert('manathanWeb.Boxed.PreviousImage() error: ' + err);
        }
        this._addToId = '';
        return false;
    },
       
    StopRunningAnimation : function() {
        try {
            if (this._lastAnimation && this._lastAnimation.get_isPlaying()) {
                this._lastAnimation.stop();
            }
            this._lastAnimation = null;
        } catch (err) {
            alert('manathanWeb.Boxed.StopRunningAnimation() error: ' + err);
        }
    },

    Close : function() {
        try {
            if (this._doSlideshow) window.clearTimeout(this._slideshow);
            _doSlideshow = false;
            if (this._next) this._next.style.visibility = 'hidden';
            if (this._prev) this._prev.style.visibility = 'hidden';
        } catch (err) {
            alert('manathanWeb.Boxed.Close() error: ' + err);
        }
        return false;
    },

    FirstImage : function() {
        try {
            window.clearTimeout(this._slideshow);
            return this.__PlayAnimations();
        } catch (err) {
            alert('manathanWeb.Boxed.FirstImage() error: ' + err);
        }
    },

    ContinueSlideshow : function() {
        try {
            this._lastAnimation = this._animation;
            if (this._doSlideshow)
                this.NextImage();
        } catch (err) {
            alert('manathanWeb.Boxed.ContinueSlideshow() error: ' + err);
        }
    },

    StartSlideshow : function() {
        try {
            window.clearTimeout(this._slideshow);
            return this.__PlayAnimations();
        } catch (err) {
            alert('manathanWeb.Boxed.StartSlideshow() error: ' + err);
        }
    },

    dispose: function() {
        try {
            this._doSlideshow = false;
            window.clearTimeout(this._slideshow);
        } catch (err) {
            alert('manathanWeb.Boxed.dispose() error: ' + err);
        }
    }

};
manathanWeb.Boxed.registerClass('manathanWeb.Boxed', null, Sys.IDisposable);

var boxed;
var horiMov = 0;
var vertMov = 0;
var calculateScrollBarIn = true;

function DoSlideshow() { boxed.ContinueSlideshow(); }

function GetHorizontalPosition(container, imgWidth) {
    try {
	    var positioning = new manathanWeb.Positioning();
        var posX = new manathanWeb.Positioning().findPositions(container).x;
	    if (!calculateScrollBarIn)
		    return posX;
	    var scrolled = positioning.scrollPosition();
        var width = container.offsetWidth;
        width = (posX + (width / 2));
        width = width - (imgWidth / 2);    
        width += (scrolled.x / 2);
        return width + horiMov;
    } catch (err) {
        alert('manathanWeb.GetHorizontalPosition() error: ' + err);
    }
}
function GetVerticalPosition(container) {
    try {
	    var positioning = new manathanWeb.Positioning();
	    var pos = positioning.findPositions(container).y + vertMov;
	    if (!calculateScrollBarIn)
		    return pos;
	    var windowSize = positioning.windowSize();
	    var scrolled = positioning.scrollPosition();
	    if (pos < scrolled.y) 
		    pos += scrolled.y;
	    // if too big, align with top
	    if (container.offsetHeight > windowSize.height)
		    pos = scrolled.y;		
	    // if too low, move up
	    else if (pos + container.offsetHeight > windowSize.height + scrolled.y) {
		    pos -= (pos + container.offsetHeight) - (windowSize.height + scrolled.y);
		    if (pos < scrolled.y)
			    pos = scrolled.y;
	    }
	    return pos;
    } catch (err) {
        alert('manathanWeb.GetVerticalPosition() error: ' + err);
    }
}

function GetBoxedBottomHorizontalPosition(container, imgWidth) {
    try {
        var posX = new manathanWeb.Positioning().findPositions(container).x;
        var width = container.offsetWidth;
        width = (posX + width) / 2;
        return width + 16;    
    } catch (err) {
        alert('manathanWeb.GetBoxedBottomHorizontalPosition() error: ' + err);
    }
}

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();