/**
 * TLA Tip - TLA Video product tooltip
 * @author Charles Gravolet
 * @created 8/27/2007
 * @modified 12/11/2008 (dimensions plugin deprecated, jquery changes from 1.2.1 to 1.2.6 )
 * @requires jQuery 1.2.6, jQuery timers plugin
 */

(function($){
	
	
	$.extend({
		
		tlatipStatus: false,
		tlatipCache: new Object()
		
	});
	
	
	$.fn.extend({
		
		tlatip: function() {
			
			if (!$.tlatipStatus) return;
			
			var tooltip = $("#tlatip");
			var anchor = this;
			var productID = this.attr("tooltip");
			var gv = (typeof globalView == "undefined")?1:globalView;
			
			if (!productID.length) return;
			
			if (!tooltip.length) {
				$("<div id=\"tlatip\"></div>").appendTo("body");
				tooltip = $("#tlatip");
				tooltip.hover(function(){
					$.tlatipStatus = true;
					$(this).fadeIn();
				}, function() {
					$.tlatipStatus = false;
					$(this).tlatipHide();
				});
			}
			
			tooltip.fadeOut();
			
			if (!$.tlatipCache["product_" + productID]) {
				$.ajax({
					type: "GET",
					url: "../ajax/ajax_getTooltipResult.cfm",
					data: {id:productID,v:gv},
					dataType: "xml",
					complete: function(xml) {
						$.tlatipCache["product_" + productID] = xml.responseXML;
						if (!$.tlatipStatus) return;
						handleResponse(xml.responseXML);
					}
				});
			} else if ($.tlatipStatus) {
				handleResponse($.tlatipCache["product_" + productID]);
			}
			
			function handleResponse(xml) {
				var data = $("product",xml);
				if (tooltip.attr("product") == productID) {
					positionTip();
					return;
				}
				var blurb = data.find("blurb").text();
				if (blurb.length && /\$br\$/.test(blurb)) {
					while (/\$br\$/.test(blurb)) {
						// here we are replacing the line break code we get back from the server '$br$' with actual html line breaks
						blurb = blurb.replace(/\$br\$/,"<br />");
					}
				}
				//assign variables from xml return data
				var dataTitle = data.find('title').text();
				var dataYear = data.find('year').text();
				var dataStar = data.find('star').text();
				var dataDirector = data.find('director').text();
				var dataAuthor = data.find('author').text();
				
				//create original tooltip html element
				var tooltipHTML = "<h3> "+dataTitle;
				
				//test for variables and concat tooltipHTML
				if(dataYear.length) { tooltipHTML += "<span class='year'> "+dataYear+"</span></h3>"; }
					else { tooltipHTML += "</h3>"; }
				if(blurb.length) { tooltipHTML += "<p>"+blurb+"</p>";}
				if(dataStar.length) { tooltipHTML +="<p class='stars'><b>Starring: </b>"+dataStar+"</p>";}
				if(dataDirector.length) { tooltipHTML +="<p class='director'><b>Directed by: </b>"+dataDirector+"</p>";}
				if(dataAuthor.length) { tooltipHTML+="<p class='author'><b>Author: </b>"+dataAuthor+"</p>";}
				
				//add arrow for tooltip
				tooltipHTML += "<b id='tlatip-arrow'>&nbsp;</b>";
				
				//assign html to tooltip
				tooltip.html(tooltipHTML);
				tooltip.attr("product",productID);
				positionTip();
			}
			
			function positionTip() {
				var anchorProperties = {
					width: anchor.width(),
					height: anchor.height()
				}

				var anchorPosition = $(anchor).offset();
				anchorProperties.top = anchorPosition.top;
				anchorProperties.left = anchorPosition.left;

				var tipProperties = {
					width: tooltip.width(),
					height: tooltip.height(),
					top: 0,
					left: anchorProperties.left + anchorProperties.width + 10
				}

				tipProperties.top = Math.round((anchorProperties.top + (anchorProperties.height*0.5)) - (tipProperties.height * 0.5));

				var winProperties = {
					width: $("body").innerWidth(),
					height: $("body").innerHeight(),
					scrollTop: $(document).scrollTop(),
					scrollLeft:$(document).scrollLeft()
				}
				
				var arrowProperties = {
					top: tipProperties.height * 0.5 - 8,
					left: -8,
					bg: "url(../skins/graphics/tooltip_arrow_left.gif)"
				}

				// Is the bottom of the tooltip getting cut off?
				if ((tipProperties.top + tipProperties.height) > (winProperties.height + winProperties.scrollTop)) {
					arrowProperties.top = arrowProperties.top + tipProperties.top - (winProperties.height + winProperties.scrollTop - tipProperties.height - 10);
					tipProperties.top = (winProperties.height + winProperties.scrollTop) - tipProperties.height - 10;
				}
				// Is the top of the tooltip getting cut off?
				if (tipProperties.top < winProperties.scrollTop) {
					arrowProperties.top = arrowProperties.top - ((winProperties.scrollTop + 10) - tipProperties.top);
					tipProperties.top = winProperties.scrollTop + 10;
				}
				// Is the right side of the tooltip getting cut off?
				if ((tipProperties.left + tipProperties.width + 10) > (winProperties.width + winProperties.scrollLeft)) {
					tipProperties.left = anchorProperties.left - tipProperties.width - 10;
					arrowProperties.left = tipProperties.width - 0;
					arrowProperties.bg = "url(../skins/graphics/tooltip_arrow_right.gif)";
				}
				// After all is said and done, is the left side now cut off? /facepalm
				if (tipProperties.left < 0) {
					tipProperties.left = 10;
				}
				if (arrowProperties.top < 0) arrowProperties.top = 5;
				if ((arrowProperties.top+16) > tipProperties.height) arrowProperties.top = tipProperties.height - 22;
							
				tooltip.css("top",tipProperties.top+"px");
				tooltip.css("left",tipProperties.left+"px");
				
				$("#tlatip-arrow").css("top",arrowProperties.top+"px")
				$("#tlatip-arrow").css("left",arrowProperties.left+"px")
				$("#tlatip-arrow").css("background-image",arrowProperties.bg);
				
				tooltip.fadeIn();
			}
			
		},
		
		
		tlatipHide: function() {
			$("#tlatip").hide();
		}
		
		
	});

})(jQuery);


// Attach tooltip hover event
$(function() {

	$("img").filter("[tooltip]").removeAttr("alt").removeAttr("title").parent().removeAttr("title").end()
		.hover(function(e) {
			$.tlatipStatus = true;
			$(this).oneTime(200,"tlatipOn",function() {
				$(this).tlatip();
			});
		},function(e) {
			$.tlatipStatus = false;
			$(this).stopTime("tlatipOn");
			$(this).tlatipHide();
		});

	// this is to handle "more info" tooltips on details pages.
	// MSB, 10.31.03	
	
	$(".moreinfo a").click( 
		function() {
			var moreInfoDiv = $(this).attr("href");
			var $moreInfoTr = $(this).parent('div').parent('td').parent('tr');
			
			// close all open tooltips that aren't the current tooltip
			$(".moreinfo-tooltip").each( 
				function() {
					if ($("a", this.parentNode).attr("href") != moreInfoDiv) {
						$(this).hide();
					}
				});
				
			if ($(moreInfoDiv).css("display") == "none") {
				$('table.pricing tr').css('z-index', 1);
				$moreInfoTr.css('z-index', 2);
				$(moreInfoDiv).show();
			} else {
				$(moreInfoDiv).hide();
			}
			return false;
		});
	
	$(".moreinfo-tooltip").click( 
		function() {
			var $moreInfoTr = $(this).parent('div').parent('td').parent('tr');
			$('table.pricing tr').css('z-index', 1);
			$(this).hide();
		});
	$(".moreinfo-tooltip a").unbind();
	
	// tooltip for forms
	
	$('p.formHelper').hide();
	$('input[name=dob]').focus(function(){
		$('p.formHelper').fadeIn();
	});
	$('input[name=dob]').blur(function(){
		$('p.formHelper').fadeOut();
	});
	
});