function isBody(element){
	return (/^(?:body|html)$/i).test(element.tagName);
};
/*
Element.implement ({
	getPosition: function(relative){
		if (isBody(this)) return {x: 0, y: 0};
		var el = this, position = {x: 0, y: 0};
		while (el){
			position.x += el.offsetLeft;
			position.y += el.offsetTop;
			el = el.offsetParent;
		}
		var rpos = (relative) ? $(relative).getPosition() : {x: 0, y: 0};
		return {x: position.x - rpos.x, y: position.y - rpos.y};
	}
});
*/
// declaring the class
var flipbookGallery = {
	Implements: [Events, Options],
	options: {
		duration: 1000,
		effect: 'slide',
		offset: 490,
		clickableArrows: false,
		current: 1
	},
	initialize: function(element, options) {
		this.element = element;
		this.setOptions(options);
		this.touching = false;
		this.oX = 0;
		this.oY = 0;
		this.current = this.options.current;
		this.previous = this.current - 1;
		this.next = this.current + 1;
		this.divOut;
		this.switchNext.bind(this);
		this.switchPrevious.bind(this);
		this.element.addEventListener('touchstart', this.handleTouch.bind(this), false);
		this.element.addEventListener('touchend', this.handleTouch.bind(this), false);
		this.element.addEventListener('touchmove', this.handleTouch.bind(this), false);
		this.element.addEventListener('touchcancel', this.handleTouch.bind(this), false);
		this.element.addEventListener('mousedown', this.handleTouch.bind(this), false);
		this.element.addEventListener('mousemove', this.handleTouch.bind(this), false);
		this.element.addEventListener('mouseup', this.handleTouch.bind(this), false);
		this.addArrows();
		this.adjustHeight();
	},
	addArrows: function() {
		this.leftArrow = new Element('a').addClass('left').setStyle('display', 'none').injectInside(this.element);
		this.rightArrow = new Element('a').addClass('right').injectInside(this.element);
		if($('Div'+this.previous) == null) this.leftArrow.setStyle('display', 'none');
		else this.leftArrow.setStyle('display', 'block');
		if($('Div'+this.next) == null) this.rightArrow.setStyle('display', 'none');
		else this.rightArrow.setStyle('display', 'block');
		if(this.options.clickableArrows) 
		{
			this.leftArrow.addEvent(
				'click',
				this.prevItem.bind(this)
			);
			this.rightArrow.addEvent(
				'click',
				this.nextItem.bind(this)
			);
		}
	},
	prevItem: function() {
		this.switchPrevious(this.current,this.previous);
		this.next = this.current;
		this.current = this.previous;
		this.previous = this.previous-1;
		if($('Div'+this.previous) == null) this.leftArrow.setStyle('display', 'none');
		else this.leftArrow.setStyle('display', 'block');
		this.rightArrow.setStyle('display', 'block');
		this.adjustHeight();
	},
	nextItem: function() {
		this.switchNext(this.current,this.next);
		this.previous = this.current;
		this.current = this.next;
		this.next = this.next+1;
		this.adjustHeight();
		if($('Div'+this.next) == null) this.rightArrow.setStyle('display', 'none');
		else this.rightArrow.setStyle('display', 'block');
		this.leftArrow.setStyle('display', 'block');
	},
	handleTouch: function(e) {
		if (e.type == "touchstart" || e.type=="mousedown") {
			this.touching = true;

			if (e.touches && e.touches.length == 1) {
				var touch = e.touches[0];
				if(touch.target.onclick) { touch.target.onclick(); }
				this.oX = touch.pageX;
				this.oY = touch.pageY;
				nX = 0;
				nY = 0;
				scrollX = 0;
			}
			else 
			{
				this.oX = e.pageX;
				this.oY = e.pageY;
				nX = 0;
				nY = 0;
				scrollX = 0;
			}
		}

		else if (e.type == "touchmove" || (this.touching && e.type== "mousemove")) {
			if ((e.touches && e.touches.length == 1) || e.type=="mousemove") {
				if (e.touches && e.touches.length == 1){
					var touch = e.touches[0];
					var nX = touch.pageX;
					var nY = touch.pageY;
					var scrollY = nY - this.oY;
				}
				else {
					var nX = e.pageX;
					var nY = e.pageY;
					var scrollY = nY - this.oY;
				}

				if (scrollY < 0) 
					scrollY = this.oY - nY;

				if (this.oX > nX) {
					var scrollX = this.oX-nX;
					if(scrollX > scrollY)
						e.preventDefault();
					if (scrollX > 100) {
						if (document.getElementById('Div'+this.next)) {
							if (this.touching == true)
							{
								this.touching = false;
								this.nextItem();
							}
						}
					}
				} else {
			
					var scrollX = nX-this.oX;
					if(scrollX > scrollY)
						e.preventDefault();
					if (scrollX > 100) {
						if (this.previous != 0) {
							if (this.touching == true) {
								this.touching = false;
								this.prevItem();
							}
						}
					}
				}
	
			}
		}
		else if (e.type == "touchend" || e.type == "touchcancel" || e.type == "mouseup") {
			this.touching = false;
		}
	},
	
	switchNext: function(outIndex,inIndex) {
		var divIn = $('Div'+inIndex);
		var divOut = $('Div'+outIndex);
		if(this.options.effect == 'slide')
		{
			var morphOut = new Fx.Morph(divOut);
			morphOut.start({
				duration: this.options.duration, 
				left: -this.options.offset
			}).chain(function(){divOut.style.display='none'});
			var morphIn = new Fx.Morph(divIn);
			morphIn.set({
				duration: this.options.duration, 
				'left': this.offset,
				'display' : 'block'
			});
			divIn.morph({duration: this.options.duration, 'left': 0});
		}
	},	
	switchPrevious:function (outIndex,inIndex) {
		var divIn = $('Div'+inIndex);
		var divOut = $('Div'+outIndex);
		if(this.options.effect == 'slide') 
		{
			var morphIn = new Fx.Morph(divIn);
			morphIn.set({
				duration: this.options.duration, 
				'left': -this.offset,
				'display' : 'block'
			});
			divIn.morph({duration: this.options.duration, 'left': 0});

			var morphOut = new Fx.Morph(divOut);
			morphOut.start({
				duration: this.options.duration, 
				left:this.options.offset
			}).chain(function(){divOut.style.display='none'});
		}
	},
	adjustHeight: function() {
		// Adjust the height of the container to the height of the contents of the div.
		var currentdiv = $('Div'+this.current);
		//alert(currentdiv.id);
		this.element.morph({height: currentdiv.clientHeight});
	}
};
flipbookGallery = new Class(flipbookGallery);
