/*
---
DESCRIPTION: For GrossmütterRevolution Teasers.

LICENSE: Closed license

AUTHORS:
- Rajeev J. Sebastian

COPYRIGHT:
- QUERIDODESIGN, Switzerland - http://www.queridodesign.net/

DEPENDENCIES:
- MooTools-Core 1.2.4

- MooTools-More 1.2.4
...
*/

window.addEvent('domready', function(){

	var Directions = new Class({
		Implements: [Options, Events],
		
		initialize: function(){
			this.findDirection = this.findDirection.bindWithEvent(this);
			document.addEvent('mousemove', this.findDirection);
			var dir = $A([
				"n",
				"ne",
				"e",
				"se",
				"s",
				"sw",
				"w",
				"nw"
			]);

			this.dir = dir;
			this.xold = 0;
			this.yold = 0;
			this.flag = 0;
		},
		
		setDirection: function(direction) {
		 	this.direction = this.dir[direction];
			// console.log('set direction', this.direction);
		},
		
		findDirection: function(event) {
			this.flag += 1;
			if(this.flag > 0 && this.flag < 4) return;
			this.flag = 0;
			x = event.page.x;
			y = event.page.y;
			xdiff = x - this.xold;
			ydiff = y - this.yold
			// if ((xdiff <  2) && (ydiff < -2)) this.setDirection(0);
			// if ((xdiff <  2) && (ydiff >  2)) this.setDirection(4);
			// if ((xdiff >  2) && (ydiff <  2)) this.setDirection(2);
			// if ((xdiff < -2) && (ydiff <  2)) this.setDirection(6);
			// if ((xdiff >  2) && (ydiff >  2)) this.setDirection(3);
			// if ((xdiff >  2) && (ydiff < -2)) this.setDirection(1);
			// if ((xdiff < -2) && (ydiff >  2)) this.setDirection(5);
			// if ((xdiff < -2) && (ydiff < -2)) this.setDirection(7);
			this.xold = x;
			this.yold = y;
		}
		
	});
	Directions.instance = new Directions();
	
	$$('.teaser').each(function(teaser){
		var info = teaser.getElement('.info');
		var teaserFx = new Fx.Tween(info, {
			link: 'cancel',
			duration: 150
		});

		var co = teaser.getCoordinates(document.body);

		function findEdge(x1, y1, x2, y2){
			var edge = false;
			if((x1 <= co.right && co.right <= x2) || (x2 <= co.right && co.right <= x1)) {
				edge = 'e';
			} else if((x1 <= co.left && co.left <= x2) || (x2 <= co.left && co.left <= x1)){
				edge = 'w';
			} else if((y1 <= co.bottom && co.bottom <= y2) || (y2 <= co.bottom && co.bottom <= y1)){
				edge = 's';
			} else if((y1 <= co.top && co.top <= y2) || (y2 <= co.top && co.top <= y1)){
				edge = 'n';
			}
			return edge;
		}

		teaser.addEvents({
			'mouseenter': function(ev){
				start = [ev.page.x, ev.page.y];
				old = [Directions.instance.xold, Directions.instance.yold];

				switch(findEdge(old[0], old[1], start[0], start[1])) {
					case 's':
						info.setStyles({'top': 186, 'left': 0});
						teaserFx.start('top', 0);
						break;
					case 'n':
						info.setStyles({'top': -186, 'left': 0});
						teaserFx.start('top', 0);
						break;
					case 'w':
						info.setStyles({'top': 0, 'left': -280});
						teaserFx.start('left', 0);
						break;
					case 'e':
						info.setStyles({'top': 0, 'left': 280});
						teaserFx.start('left', 0);
						break;

				}
			},
			'mouseleave': function(ev){
					start = [ev.page.x, ev.page.y];
					old = [Directions.instance.xold, Directions.instance.yold];

					switch(findEdge(old[0], old[1], start[0], start[1])) {
						case 'n':
							info.setStyles({'top': 0, 'left': 0});
							teaserFx.start('top', -186);
							break;
						case 's':
							info.setStyles({'top': 0, 'left': 0});
							teaserFx.start('top', 186);
							break;
						case 'e':
							info.setStyles({'top': 0, 'left': 0});
							teaserFx.start('left', 280);
							break;
						case 'w':
							info.setStyles({'top': 0, 'left': 0});
							teaserFx.start('left', -280);
							break;

					}
			}
		});

	});

});
