$(document).ready(function() {
	$("#nav_bar_bottom a")
		.not(".prev_link_inactive, .next_link_inactive")
		.mouseover( function(){																										
			institute.doPreview(this);
		})
		.each(function() {
			var this_id = $(this).text().toLowerCase();
			if(this.id!="current_page") {
				this.id = "link"+this_id;
			}
			var this_title, img_name;
			img_name = "none"
			if( $(this).is("#current_page") ) {
				this_title = "Current Page";
				$(this).css("cursor","default");
				this.href="javascript:institute.goNowhere()";
			} else if ( $(this).is(".prev_link") ) {
				this_title = "Next Page";
			} else if ( $(this).is(".next_link") ) {
				this_title = "Previous Page";
			} else {
				this_id = this.id;
				this_title = this.title;
				img_name = $(this).text().toLowerCase();
			}
			$(this)
				.after('<div id="'+this_id+'_thumb" class="pageThumb offscreen_thumb"><div class="bubble_top"></div><div class="bubble_content"><div class="thumb_title">'+this_title+'</div><img src="thumbs/pg'+img_name+'.png" alt="" width="230" onerror="institute.setPosition(this,true)" onload="institute.setPosition(this)" /></div><div class="bubble_bottom"></div></div>');
		})
		.mouseout(function(){
			institute.removePreview(this);
		});
});

var institute = {
	doPreview:function(el) {
		$("#"+el.id+"_thumb").removeClass("offscreen_thumb");
	},
	removePreview:function(el) {
		$("#"+el.id+"_thumb").addClass("offscreen_thumb");
	},
	imgError:function(source) {
		//console.log("imgError");
		source.src = "/graphics/spacer.gif";
		source.onerror = "";
		return true;
	},
	setPosition:function(img,err) {
		var thumb_div = $(img).parents(".pageThumb:first");
		var a = $(thumb_div).prev();
		var next_a = $(thumb_div).next();
		if(err) {
			img.src = "/_graphics/spacer.gif";
			img.height = 1;
			$(img).css("border","0px");
		}
		var position = $(a).position();
		var next_position = $(next_a).position();
		if(!(next_position)) { next_position = $(a).position(); }
		var half_bubble_width = ($(thumb_div).width()) * 0.5;
		if(next_position.left===position.left) {
				next_position.left += 10;
		}
		var half_link_width = (next_position.left - position.left) * 0.5;
		var the_left = position.left - half_bubble_width + half_link_width;
		var the_top = position.top-($(thumb_div).height())-2;
		institute.top = the_top;
		$(thumb_div)
			.css("left",the_left+"px")
			.css("top",the_top+"px");
	},
	goNowhere:function(el) {  },
	setAllPos: function() {
		$(".pageThumb img").each(function() {
			institute.setPosition(this);						  
		});	
	}
}

setInterval(function() { institute.setAllPos(); }, 2500);
