
$(document).ready(function() {
	$(document).mousemove(function(e) {
		CustomShadowbox.mousemove(e);
	});
	
	var resizeTimeout = false;
	var onResize = function() {
		if (resizeTimeout !== false) {
			window.clearTimeout(resizeTimeout);
		}
		resizeTimeout = window.setTimeout(function() {
			CustomShadowbox.deriveImageDimensions();
		}, 200);
	};
	$(window).resize(onResize);
	$(window).scroll(function() {
		if (CustomShadowbox.leftRightLink !== null) {
			CustomShadowbox.leftRightLink.hide();
		}
		onResize();
	});
});

/** this object can be customized further in a document ready event */
var CustomShadowbox = {
				
	/** last known mouse position */
	mousepos: null,
	
	/** the current image */
	image: null,
	imageDimensions: null,
	
	/** position of the mouse inside the image */
	isRight: null,
	
	/** the link that is placed over the image */
	leftRightLink: null,
	leftImage: 'includes/EzFileFrontends/Core/Viewers/ShadowBox/previous_large.png',
	rightImage: 'includes/EzFileFrontends/Core/Viewers/ShadowBox/next_large.png',
	leftRightImageWidth: 35,
	
	mousemove: function(e) {
		this.mousepos = e;
		
		if (this.image !== null && this.imageDimensions === null) {
			this.deriveImageDimensions();
		}
		
		if (this.imageDimensions !== null) {
			// check whether mouse is over image
			if (e.pageX >= this.imageDimensions.left
					&& e.pageX <= this.imageDimensions.right
					&& e.pageY >= this.imageDimensions.top
					&& e.pageY <= this.imageDimensions.bottom) {
				this.mousemoveOverImage(e.pageX - this.imageDimensions.left);
			} else if (this.isRight !== null) {
				// remove link on mouse leave
				if (this.isRight) {
					this.onLeaveRight();
				} else {
					this.onLeaveLeft();
				}
				this.isRight = null;
			}
		}
	},
	
	deriveImageDimensions: function() {
		var offset = this.image.offset();
		this.imageDimensions = {
			width: this.image.width() + 2 * this.leftRightImageWidth,
			height: this.image.height(),
			left: offset.left - this.leftRightImageWidth,
			top: offset.top
		};
		this.imageDimensions.right = this.imageDimensions.left + this.imageDimensions.width;
		this.imageDimensions.bottom = this.imageDimensions.top + this.imageDimensions.height;
		this.imageDimensions.middle = this.imageDimensions.width / 2;
	},
	
	mousemoveOverImage: function(x) {
		var middle = this.imageDimensions.middle;
		var newIsRight;
		if (x >= middle) {
			// mouse over right half
			newIsRight = true;
		} else {
			// mouse over left half
			newIsRight = false;
		}
		// events for entering and changing the side
		if (newIsRight !== this.isRight) {
			if (this.isRight === true) {
				CustomShadowbox.onLeaveRight();
			} else if (this.isRight === false) {
				CustomShadowbox.onLeaveLeft();
			}
			if (newIsRight) {
				CustomShadowbox.onEnterRight();
			} else {
				CustomShadowbox.onEnterLeft();
			}
			this.isRight = newIsRight;
		}
	},
	
	/** finish event for shadowbox */
	onFinish: function(galleryElement) {
		if (galleryElement.player != "img") {
			return;
		}
		this.image = $('img#sb-player');
		if (this.mousepos !== null) {
			this.mousemove(this.mousepos);
		}
	},
	
	reset: function() {
		this.image = null;
		this.imageDimensions = null;
		this.isRight = null;
		if (this.leftRightLink !== null) {
			this.leftRightLink.hide();
		}
	},
	
	/** change event for shadowbox, fired when next/prev is clicked */
	onChange: function(galleryElement) {
		this.reset();
	},
	
	/** close event for shadow box */
	onClose: function(galleryElement) {
		this.reset();
	},
	
	/** initialize and manipulate the link */
	getLeftRightLink: function(width, height, isRight, x, y) {
		if (this.leftRightLink === null) {
			this.leftRightLink = $(document.createElement('a'))
				.attr({href: '#', id: 'sb-nav-leftright'})
				.css({position:'absolute', zIndex:1010})
				.hide();
			
			this.leftRightLink.click(function() {
				if ($(this).blur().hide().hasClass('sb-left')) {
					Shadowbox.previous();
					return false;
				}
				Shadowbox.next();
				return false;
			});
			
			$('body').prepend(this.leftRightLink);
		}
		
		this.leftRightLink.css({
			left: x+'px',
			top: y+'px',
			background: 'url('+(isRight ? this.rightImage : this.leftImage)+') '
				+ 'no-repeat ' + (isRight ? 'right' : 'left') + ' center'
		})
		.width(width).height(height)
		.attr({'class': isRight ? 'sb-right' : 'sb-left'});
		
		return this.leftRightLink;
	},
	
	onEnterLeft: function() {
		if ($('#sb-nav-previous:visible').size() == 0) {
			return;
		}
		
		var width = Math.round(this.imageDimensions.width / 2);
		this.getLeftRightLink(width, this.imageDimensions.height, false,
				this.imageDimensions.left, this.imageDimensions.top).show();
	},
	
	onLeaveLeft: function() {
		if (this.leftRightLink !== null) {
			this.leftRightLink.hide();
		}
	},
	
	onEnterRight: function() {
		if ($('#sb-nav-next:visible').size() == 0) {
			return;
		}
		
		var linkWidth = Math.round(this.imageDimensions.width / 2);
		var linkOffset = this.imageDimensions.left + this.imageDimensions.width - linkWidth;
		this.getLeftRightLink(linkWidth, this.imageDimensions.height, true, 
				linkOffset, this.imageDimensions.top).show();
	},
	
	onLeaveRight: function() {
		if (this.leftRightLink !== null) {
			this.leftRightLink.hide();
		}
	}
	
};
