function Nav(elem){
	this.debug = true,
	this.links = elem.find('a'),
	this.margin = 5,
	this.bounding = {
		'top'    : 10,
		'left'   : 10,
		'right'  : 10,
		'bottom' : 10
	},
	this.visible = true,

	//-------------------------------------------
	//construction
	this.init = function(){
		var collid = {
			'top' : 0,
			'bottom' : 0,
			'right' : 0,
			'left' : 0
		};
		this.links.each(function(){
			$(this).data('collid',collid);
		})
	},
	this.render = function(){
		this.refresh();
	},

	//-------------------------------------------
	//position
	this.refresh = function(){
		var self = this;
		self.links.each(function(){
			self.testCollid($(this));
			self.setCollid($(this));
			var position = self.position($(this));
			$(this).css({
				left : 	position.left + 'px',
				top : 	position.top  + 'px'
			});
		});
	},

	//-------------------------------------------
	//position setting
	this.position = function(link){
		var target = $( link.attr('href') ); // $("#"+ link.attr("data-link") );
		var collid = link.data('collid');
		return {
			'left' : Math.max(
						parseInt(this.bounding.left + collid.left),
						Math.min(
							parseInt(scroller.wrapper.width() - link.outerWidth() - this.bounding.right - collid.right),
							parseInt(target.offset().left + target.outerWidth()*.5 - link.outerWidth()*.5)
						)
					),
			'top'  : Math.max(
						parseInt(this.bounding.top + collid.top),
						Math.min(
							parseInt(scroller.wrapper.height()  - link.outerHeight()  - this.bounding.bottom - collid.bottom),
							parseInt(target.offset().top + target.outerHeight()*.5 - link.outerHeight()*.5)
						)
					)
		};
	},

	//-------------------------------------------
	//collisions testing
	this.testCollid = function(link){
		var side = null;
		var corner = null;
		var collid = link.data('collid');
		var position = {
			top : parseInt(link.css('top')),
			left : parseInt(link.css('left'))
		};

		if(position.top <= this.bounding.top){ //top
			side = 'top';
			if(position.left <= this.bounding.left + collid.left) corner = 'left';
			else if(position.left + link.outerWidth() + 1 >= scroller.wrapper.width() - this.bounding.right - collid.right) corner = 'right';
		}else if(position.top + link.outerHeight() + 1 >= scroller.wrapper.height() - this.bounding.bottom){ //bottom
			side = 'bottom';
			if(position.left <= this.bounding.left + collid.left) corner = 'left';
			else if(position.left + link.outerWidth() + 1 >= scroller.wrapper.width() - this.bounding.right - collid.right) corner = 'right';
		}else{
			if(position.left <= this.bounding.left) side = 'left';
			else if(position.left + link.outerWidth() + 1 >= scroller.wrapper.width() - this.bounding.right) side = 'right';
		}

		link.attr({
			'data-side' : side,
			'data-corner' : corner
		});
	},
	this.setCollid = function(link){
		var self = this;
		var position = {
			top : parseInt(link.css('top')),
			left : parseInt(link.css('left'))
		};
		var side = link.attr('data-side');
		var corner = link.attr('data-corner');
		var collid = {
			'top' 	: 0,
			'bottom': 0,
			'left' 	: 0,
			'right' : 0
		};
		if(side){
			var topStart = 0;
			var bottomStart = 0;
			self.links.not(link).each(function(){
				t = $(this);
				if((side == 'top' || side == 'bottom') && side == t.attr('data-side')){
					if(position.left > t.position().left) collid.left += t.outerWidth() + self.margin;
					else collid.right += t.outerWidth() + self.margin;
				}else if(side=='left' || side == 'right'){
					if(side == t.attr('data-corner')){
						if(position.top < scroller.wrapper.height()*.5 && t.attr('data-side') == 'top'){
							topStart = t.outerHeight() + self.margin;
						}else if(t.attr('data-side') == 'bottom'){
							bottomStart = t.outerHeight() + self.margin;
						}
					}
					if(side == t.attr('data-side')){
						if(position.top > t.position().top){
							collid.top += t.outerHeight() + self.margin;
						}else{
							collid.bottom += t.outerHeight() + self.margin;
						}
					}
				}
			});
			collid.top += topStart;
			collid.bottom += bottomStart;
		}
		link.data('collid', collid);
	},

	//-------------------------------------------
	//display
	this.show = function(){
		this.visible = true;
		elem.stop().fadeIn(500);
	},
	this.hide = function(){
		this.visible = false;
		elem.stop().fadeOut(200);
	},
	this.toggle = function(){
		if(this.visible) this.hide(); else this.show();
	}
}
