// Author: Pierre-Abraham Rochat - 11.2009



// given an element, adds two areas on the border that are clickable
// and bindable to functions (prev and next ideally)

// if you want to hide the arrows when there are no more next / previous elements, 
// you must transmit the options.counter argument that must implemenet isNext and isPrev (returning booleans)
jQuery.fn.navigation = function(options) {
	
	if (options.nextEl != null) options.nextEl.hide ();
	if (options.prevEl != null) options.prevEl.hide ();
	
	settings = jQuery.extend({
		margin: 100,
	  	nextFn: function () {},
	  	prevFn : function () {}
	}, options);
	
	var w;
	var x;
	var crtMode = 'none';
	// this refers here to the selected element
	var _this = this;
	
	_init ();	
		
	function _init () {
		// console.log ('INIT');
		
		w = _this.width ();
		_this.click (_click);
		_this.mousemove(_update);
		_this.mouseout (function (e) {
			// console.log ('MOUSE_OUT');
			_outLeft (e);
			_outRight (e);
			crtMode = 'none';
		})
		_this.mouseover (function (e) {
			// console.log ('MOUSE_OVER');
			_update (e);
		})
	}
	
	function _update (e) {
		x = e.pageX - _this.offset().left;
		
		
		if (x < settings.margin) {
			if (crtMode == 'none' || crtMode == 'right') {
				crtMode = 'left';
				_overLeft ();				
			}
		}
		else if (x > w - settings.margin) {
			if (crtMode == 'none' || crtMode == 'left') {
				crtMode = 'right';
				_overRight ();
			}
		}
		else {
			if 	(crtMode == 'left') {
				crtMode = 'none';
				_outLeft (e);
			} 	
			else if (crtMode == 'right') {
				crtMode = 'none';
				_outRight (e);
			} 	
		}
		
		// $("#title").html (x+" - "+w+" - "+crtMode);
	}
	
	function showFinger () {
		_this.css ("cursor", "pointer");
	}
	function hideFinger () {
		_this.css ('cursor', 'auto');
	}
	
	function _click () {
		if (crtMode == 'left') {
			if (_isPrev ()) {
				settings.prevFn ();
				return false;
			}
			return true;
		}
		else if (crtMode == 'right') {
			if (_isNext ()) {
				settings.nextFn ();
				return false;
			}
			else {
				if (settings.endUrl != "")
					window.location.href = settings.endUrl;
				
			}
			return true;
		}
		else {
			return true;
		}
	}
	
	function _isNext () {
		if (settings.counter == null) return true;
		return settings.counter.isNext ();
		
		
	//return true;
	}
	function _isPrev () {
		if (settings.counter == null) return true;
		return settings.counter.isPrev ();
		
		
	}
	
	function _overRight () {
		
		// console.log ('OVER RIGHT');
		if (settings.nextEl != null && _isNext ()) 
		{
			settings.nextEl.fadeIn (500);
				//settings.nextEl.fadeTo ("slow", 0.8);
		}
		showFinger ();
	}
	function _overLeft () {
		if (settings.prevEl != null && _isPrev ()) {
				settings.prevEl.fadeIn (500);
		}
		showFinger ();
	}
	function _outLeft (e) {
		x = e.pageX - _this.offset().left;
		y = e.pageY - _this.offset().top;
		
		if (x < 0 || x > settings.margin || y < 0 || y > 600) {
	 
			if (settings.prevEl != null) {
					settings.prevEl.fadeOut (300);
			
			}
			hideFinger ();
		}
	}
	function _outRight (e) {
		x = e.pageX - _this.offset().left;
		y = e.pageY - _this.offset().top;
		
		
		if (x < 960-settings.margin || x > 960 || y < 0 || y > 600) {
			if (settings.nextEl != null) {
					settings.nextEl.fadeOut (300);
			}
			hideFinger ();
		}
	}
	
	
	return this;
}
