(function(jQuery) {
  
	jQuery.miniNav = function(id, options) {
		this.element = jQuery(id);
		this.element.bind("mouseenter", function(e){
	        var o = jQuery(this);
	        if (!o.hasClass('on')) o.addClass('on');
	    }).bind("mouseleave", function(e) {
	        jQuery(this).removeClass('on');
	    });
	    

		this.options = jQuery.extend({postSelector:'div.post', offset:20, scrolloffset: 60, bottomoffset:100, easing:'swing', limit:'lastpost', reposition:true, duration:1000, position:'inside',hasposts:true,prevurl:"",nexturl:""},options);
		//this.buttons = jQuery(id+' a');
		var self = this;
		if (this.options.reposition) jQuery(window).resize(function() {self.position()});
		
		jQuery(window).scroll(function() {self.scroll()});
		
		this.top = jQuery(id+' a.top').click(function() {self.goTo('top');this.blur();return false;});
		if (this.options.hasposts) this.prev = jQuery(id+' a.prev').click(function() {self.goTo('prev');this.blur();return false;});
		if (this.options.hasposts) this.next = jQuery(id+' a.next').click(function() {self.goTo('next');this.blur();return false;});
		this.bottom = jQuery(id+' a.bottom').click(function() {self.goTo('bottom');this.blur();return false;});
		var c="top-off";
		if (!this.options.hasposts) c += " prev-off next-off";
		else if (this.options.hasposts && this.options.prevurl == "") c+= " prev-off";
		this.element.addClass(c);
		if (this.options.prevurl != "") this.options.prevurl += "#last-post";
		
		this.posts= jQuery(this.options.postSelector);
		//var posts = this.posts;
		this.positions = new Array();
		
		this.currentPost = 0;
		this.minY = -1;
		this.maxY = -1;
		this.position();
	    this.element.css('visibility', 'inherit');
	    this.setCurrPost = true;
	}
	jQuery.miniNav.prototype = {
		goTo : function(where) {
			//alert(where)
			var y=0, elm = false;
			switch(where) {
				case 'bottom':
					if (this.options.limit == 'lastpost') {
						elm = this.posts[this.posts.length-1];
					} else {
						this.currentPost = this.posts.length;
						this.setCurrPost = false;
						elm = this.options.limit;
					}
					if (this.options.hasposts) {
						this.element.removeClass('top-off').removeClass('prev-off').addClass('bottom-off');
						if (this.options.nexturl == "") this.element.addClass('next-off');
					} else {
						this.element.removeClass('top-off').addClass('bottom-off');
					}
					break;
				case 'prev':
					if (this.currentPost == 0) {
						if (this.options.prevurl != "") window.location.replace(this.options.prevurl);
						return;
					}
					this.currentPost--;
					if (this.currentPost == 0) {
						this.element.addClass('top-off');
						if (this.options.prevurl == "") this.element.addClass('prev-off');
					}
					this.element.removeClass('bottom-off').removeClass('next-off');
					//console.log('prev checking cond = '+this.currentPost+' '+this.posts.length+' '+this.nexturl)
					if (this.currentPost == this.posts.length-1 && this.options.nexturl == "") this.element.addClass('next-off');
					elm = this.posts[this.currentPost];
					break;
				case 'next':
					//console.log(this.currentPost);
					this.currentPost++;
					//alert(this.currentPost+' '+this.posts.length)
					if (this.currentPost >= this.posts.length) {
						//alert(this.optioins.nexturl)
						if (this.options.nexturl != "") window.location.replace(this.options.nexturl);
						this.element.addClass('next-off');
						return;
					}
					this.element.removeClass('top-off').removeClass('prev-off');
					elm = this.posts[this.currentPost];
					break;
				case 'top':
					if (this.options.hasposts) {
						this.element.removeClass('bottom-off').removeClass('next-off').addClass('top-off');
						if (this.options.prevurl == "") this.element.addClass('prev-off');
					} else {
						this.element.removeClass('bottom-off').addClass('top-off');
					}
					break;
			}
			//alert(elm+' '+where)
			this.setMaxY();
			if (elm) {
				y = jQuery(elm).offset().top;
				if (where == 'bottom' && this.options.limit == 'lastpost') {
					y = this.maxY
				}
			}
			var self = this;
			jQuery('html, body').animate({scrollTop: y+this.options.offset}, this.options.duration, this.options.easing);
			return false;
		},
		scrollToLast : function() {
			jQuery('html, body').animate({scrollTop: jQuery(this.posts[this.posts.length-1]).offset().top+this.options.offset}, this.options.duration, this.options.easing);
			this.element.removeClass('top-off').removeClass('prev-off');
		},
		setMaxY : function() {
			if (this.options.limit == 'lastpost') {
				var elm = jQuery(this.posts[this.posts.length-1]);
				this.maxY = elm.offset().top+elm.height()-this.options.bottomoffset;
			} else {
				this.maxY = jQuery(this.options.limit).offset().top + this.options.offset;
			}
		},
		position : function() {
			var post = jQuery(this.posts[this.currentPost]);
			var offset = post.offset();
			var width = post.width();
			var x = offset.left + width;
			if (this.options.position == 'inside') x = x-this.element.width();
			if (this.minY < 0) this.minY = offset.top;
			var scrolloffset = jQuery.isFunction(this.options.scrolloffset)?this.options.scrolloffset(this.posts[this.currentPost]):this.options.scrolloffset;
			if (this.options.reposition) this.element.css({left:x,top:offset.top+scrolloffset});
			else this.element.css({top:offset.top+scrolloffset});
		},
		scroll : function() {
			if (!this.element) return;
			
			this.element.stop();
			this.setMaxY();
			var y = jQuery(window).scrollTop();
			if (y<this.minY) y = this.minY;
			if (y > this.maxY) y = this.maxY;
			var self = this;
			var offset = jQuery.isFunction(this.options.scrolloffset)?this.options.scrolloffset(this.posts[this.currentPost]):this.options.scrolloffset;
			opt = {top: y+offset};
			var right = jQuery(window).scrollLeft()
			if (right >= 0) opt.right = -right;
			this.element.animate(opt,'slow',this.options.easing,function() {self.setCurrentPost()});
		},
		setCurrentPost : function() {
			if (!this.setCurrPost) {
				this.setCurrPost = true;
				return;
			}
			//alert('got here')
			var currPost = this.currentPost;
			this.currentPost = 0;
			var y = jQuery(window).scrollTop();
			for(var i=this.posts.length-1;i>=0;i--) {
				if (y >= jQuery(this.posts[i]).offset().top) {
					//alert(jQuery(this.posts[i]).offset().top+' '+i)
					this.currentPost = i;
					//alert(this.currentPost)
					break;
				}
			}
			if (this.currentPost > currPost) {
				this.element.removeClass('top-off').removeClass('prev-off');
			}
			if (this.currentPost < currPost) {
				this.element.removeClass('bottom-off').removeClass('next-off');
			}
			//console.log(y+' '+this.minY);
			if (y <= this.minY) this.element.addClass('top-off');
			else this.element.removeClass('top-off');
			
			if (this.currentPost == 0 && this.options.prevurl == "") this.element.addClass('prev-off');
			
			if (this.currentPost >= this.posts.length -1 && this.options.nexturl == "") {
				this.element.addClass('next-off');
			}
			//console.log(y+this.options.offset+this.element.height()+' '+jQuery(this.options.limit).offset().top);
			
			/*
			add this as well var w=jQuery(window);
w.height()+w.scrollTop()+' '+jQuery(document).height();
			*/
			if (y+this.options.offset+this.element.height() >= jQuery(this.options.limit).offset().top || (this.options.limit == 'lastpost' && this.currentPost >= this.posts.length -1)) {
				this.element.addClass('bottom-off');
			} else {
				this.element.removeClass('bottom-off');
			}
		}
	}
})(jQuery);
