/*
* BOTTIPS
* - Creates a popup window when the element is rolled over
* REQUIRES
* - jquery 1.3.2
* 
* Jay Richardson
* Werkbot Studios - www.werkbot.com
*/
$(document).ready(function() {	
	$(".bottips").each( function() {
		//CHECK CHILDREN FOR IMAGES AND SET SIZE IF WIDTH OR HEIGHT IS EQUAL TO ZERO
			var image_flag = false;
			var image_height = 0;
			var image_width = 0;
			$(this).find("img").each( function() {
				image_flag = true;
				image_height = $(this).attr("height");
				image_width = $(this).attr("width");
			});
			if(image_flag){
				$(this).height(image_height);
				$(this).width(image_width);
			}
		//ADD OUR HOVER EVENT
			$(this).mouseenter(function () {
				$("#bottips_"+this.rel).css("visibility", "visible");
			});
			$(this).mouseleave(function () {
				$("#bottips_"+this.rel).css("visibility", "hidden");
			});
		//CREATE OUR TIP CONTAINER
			var container = $("<div>").attr({
				'class': "bottips_container",
				'id': "bottips_"+this.rel
			});
			container.css("visibility", "hidden");
		//CREATE THE PARTS OF OUR TIP
			//ARROW
				var arrow = $("<div>").attr({
					'class': "bottips_arrow_left"
				});
			//TOP
				var top = $("<div>").attr({
					'class': "bottips_top"
				});
			//CLOSE
				var close_but = $("<div>").attr({
					'class': "bottips_close"
				});
				close_but.css("top", "5px");
				close_but.css("right", "5px");
				//ADD OUR CLICK EVENT TO THE CLOSE BUTTON
					close_but.click(function () {
						container.css("visibility", "hidden");	
					});
			//CONTENT
				var content = $("<div>").attr({
					'class': "bottips_content"
				});
				//GRAB OUR CONTENT AND SET IT IN OUR CONTENT
					content.html($("#"+this.rel).html());
			//BOTTOM
				var bottom = $("<div>").attr({
					'class': "bottips_bottom"
				});
			//ADD ALL THE PARTS TO OUR CONTAINER
				container.append(top, close_but, content, bottom, arrow);
		//ADD THE TIP TO THE DOCUMENT
			$(this).append(container);
		//GRAB OUR SCREEN DIMENSIONS
			var screen_height = screen.height;
			var screen_width = screen.width;
		//GRAB THE SIZES
			var e_size = new Object();
			if(image_flag){
				e_size.x = image_width;
				e_size.y = image_height;
			}else{
				e_size.x = $(this).width();
				e_size.y = $(this).height();
			}
			var e_pos = $(this).offset();
			var container_size = new Object();
			container_size.x = container.width();
			container_size.y = container.height();
			var arrow_size = new Object();
			arrow_size.x = arrow.width();
			arrow_size.y = arrow.height();
		//TEST TO SEE WHERE WE SHOULD POSITION THE CONTAINER - RIGHT OR LEFT
			var container_position = "";
			var container_x = container_size.x + arrow_size.x;
			var test_container_pos = container.offset().left - container_x;
			if(test_container_pos>0){
				container_position = "Left";
			}else{
				container_position = "Right";
			}
		//TEST TO SEE WHERE WE SHOULD POSITION THE CONTAINER - Y COORD
			var container_y = (container_size.y/2) - (e_size.y/2);
			var test_y_container_pos = container.offset().top - container_y;
			var arrow_y = 0;
			if(test_y_container_pos<0){
				//WE NEED TO MOVE IT DOWN 
					container_y = container_y+(test_y_container_pos - (e_size.y));
				//
					arrow_y = e_pos.top;
			}else{
				if(image_flag){
					arrow_y = (container_size.y/2) - (e_size.y/2);
				}else{
					arrow_y = (container_size.y/2) - (e_size.y/2);
				}
			}
		//POSITION OUR CONTAINER
			if(container_position == "Left"){
				arrow.attr("class", "bottips_arrow_left");
				var container_x = container_size.x + arrow_size.x -2;
				var arrow_x = container_size.x-1;
				arrow.css({
					'top':arrow_y+'px',
					'left':arrow_x+'px'				
				});
				container.css({
					'top':-container_y+'px',
					'left':-container_x+'px'
				});
			}else if(container_position == "Right"){
				arrow.attr("class", "bottips_arrow_right");
				var container_x = e_size.x + arrow_size.x -2;
				var arrow_x = arrow_size.x - 1;
				arrow.css({
					'top':arrow_y+'px',
					'left':-arrow_x+'px'
				});
				container.css({
					'top':-container_y+'px',
					'left':container_x+'px'
				});
			}
		//REMOVE OUR DUMMY HTML
			$("#"+this.rel).remove();
	});
});