/* START OF Title FLYOVER */
function TitleFlyoverType() { }

TitleFlyoverType.ID = "TITLE_FLYOVER";

TitleFlyoverType.HoverDelayTime = 350;
TitleFlyoverType.FadeInDelayTime = 200;
TitleFlyoverType.FadeOutDelayTime = 200;
TitleFlyoverType.GetContainer = function() { return document.getElementById('popoverContainer'); }
TitleFlyoverType.GetContentContainer = function() { return document.getElementById('popoverContainerContent'); }
TitleFlyoverType.MaxContainerWidth = 400;
TitleFlyoverType.ShowIsLoading = false;
TitleFlyoverType.IsAjax = true;
TitleFlyoverType.ShowTail = true;
TitleFlyoverType.ForcePosition = "";
TitleFlyoverType.CenterTail = false;
TitleFlyoverType.VerticalGap = 10;
TitleFlyoverType.HorizontalGap = -15;
TitleFlyoverType.TailOffsets = [15, 11, 24, 24];
TitleFlyoverType.TailPrefix = "popover";
TitleFlyoverType.TailMargin = 15;
TitleFlyoverType.AnchorBuffer = 0;

TitleFlyoverType.GetFlyoverContent = function(params, ajaxResult) {
	return ajaxResult;
}

TitleFlyoverType.GetFlyoverAjaxUrl = function(params) { 
	var ASIN = params[0];
	var refTag = params[1];

	return "/gp/video/ajax/unbox-product-snapshot.html/ref=" + refTag + "?ASIN=" + ASIN + "&PowerBar=0";	
}
/* END OF Title FLYOVER */

/* START OF Shovler FLYOVER */
function ShovlerFlyoverType() { }

ShovlerFlyoverType.ID = "SHOVELER_FLYOVER";

ShovlerFlyoverType.HoverDelayTime = 350;
ShovlerFlyoverType.FadeInDelayTime = 200;
ShovlerFlyoverType.FadeOutDelayTime = 200;
ShovlerFlyoverType.GetContainer = function() { return document.getElementById('popoverContainer'); }
ShovlerFlyoverType.GetContentContainer = function() { return document.getElementById('popoverContainerContent'); }
ShovlerFlyoverType.MaxContainerWidth = 320;
ShovlerFlyoverType.ShowIsLoading = false;
ShovlerFlyoverType.IsAjax = true;
ShovlerFlyoverType.ShowTail = true;
ShovlerFlyoverType.ForcePosition = "bottom";
ShovlerFlyoverType.CenterTail = false;
ShovlerFlyoverType.VerticalGap = 10;
ShovlerFlyoverType.HorizontalGap = -15;
ShovlerFlyoverType.TailOffsets = [15, 11, 24, 24];
ShovlerFlyoverType.TailPrefix = "popover";
ShovlerFlyoverType.TailMargin = 15;
ShovlerFlyoverType.AnchorBuffer = 0;

ShovlerFlyoverType.GetFlyoverContent = function(params, ajaxResult) {
	return ajaxResult;
}

ShovlerFlyoverType.GetFlyoverAjaxUrl = function(params) { 
	var anchorId = params[0];

	//strip refTag then ASIN out of url
	var ASIN = document.getElementById(anchorId).href;
	var refTagEnd = ASIN.lastIndexOf('/') - 1;
	ASIN = ASIN.substr(ASIN.lastIndexOf('/', refTagEnd) + 1);
	ASIN = ASIN.substr(0, ASIN.lastIndexOf('/'));

	if (ASIN == "") { return null; }

	return "/gp/video/ajax/unbox-episode-snapshot.html/ref=atv_dp_epshvl_hover?ASIN=" + ASIN + "&PowerBar=0";	
}
/* END OF Shovler FLYOVER */

/* START OF IMDB FLYOVER */
function IMDBFlyoverType() { }

IMDBFlyoverType.ID = "IMDB_FLYOVER";

IMDBFlyoverType.HoverDelayTime = 350;
IMDBFlyoverType.FadeInDelayTime = 200;
IMDBFlyoverType.FadeOutDelayTime = 200;
IMDBFlyoverType.GetContainer = function() { return document.getElementById('popoverContainer'); }
IMDBFlyoverType.GetContentContainer = function() { return document.getElementById('popoverContainerContent'); }
IMDBFlyoverType.MaxContainerWidth = 0;
IMDBFlyoverType.ShowIsLoading = false;
IMDBFlyoverType.IsAjax = true;
IMDBFlyoverType.ShowTail = false;
IMDBFlyoverType.ForcePosition = "";
IMDBFlyoverType.CenterTail = false;
IMDBFlyoverType.VerticalGap = 10;
IMDBFlyoverType.HorizontalGap = 0;
IMDBFlyoverType.TailOffsets = [15, 11, 24, 24];
IMDBFlyoverType.TailPrefix = "popover";
IMDBFlyoverType.TailMargin = 15;
IMDBFlyoverType.AnchorBuffer = 0;

IMDBFlyoverType.DefaultWidth = 230;
IMDBFlyoverType.TablePadding = 70;
IMDBFlyoverType.MinWidthWithContent = 325;
	

IMDBFlyoverType.GetFlyoverContent = function(params, ajaxResult) {
	var type = params[0];
	var title = params[2]
	var viewAllLink = params[3];

	title = "<div class=\"unbox_popup_header\" style=\"margin-bottom: 14px;\ font-weight: bold\">" + title + "</div>";
	if (viewAllLink != null && type != 'Genre') {
	  viewAllLink = "<div align=\"right\">" + viewAllLink + "</div>";
	}
	else { 
	  viewAllLink = "";
	}	

	if (ajaxResult.length > 0) {
		return title + ajaxResult + viewAllLink;
	}

	return "No other videos found.";
}

IMDBFlyoverType.GetFlyoverAjaxUrl = function(params) { 
	var type = params[0];
	var imdbID = params[1];
	var title = params[2]
	var viewAllLink = params[3];
	var refTag = params[4];

	return "/gp/video/ajax/unbox-product-lists.html/ref=" + refTag + "?queryType=" + type + "&imdb_id=" + imdbID + "&PowerBar=0";
}

IMDBFlyoverType.DetermineWidth = function () {
	var tableObject = jQuery('.ap_content > table');
	if (tableObject.length == 0) {
		return (IMDBFlyoverType.DefaultWidth);
	} else {
		var tableWidth = parseInt(tableObject.css('width').replace('px',''));
		return (Math.max(tableWidth + IMDBFlyoverType.TablePadding, IMDBFlyoverType.MinWidthWithContent));
	}
}
/* END OF IMDB FLYOVER */

/* START OF NO TAIL TEXT FLYOVER */
function NoTailTextFlyoverType() { }

NoTailTextFlyoverType.ID = "TEXT_FLYOVER";

NoTailTextFlyoverType.HoverDelayTime = 350;
NoTailTextFlyoverType.FadeInDelayTime = 200;
NoTailTextFlyoverType.FadeOutDelayTime = 200;
NoTailTextFlyoverType.GetContainer = function() { return document.getElementById('textPopoverContainer'); }
NoTailTextFlyoverType.GetContentContainer = function() { return document.getElementById('textPopoverContainerContent'); }
NoTailTextFlyoverType.MaxContainerWidth = 400;
NoTailTextFlyoverType.ShowIsLoading = false;
NoTailTextFlyoverType.IsAjax = false;
NoTailTextFlyoverType.ShowTail = false;
NoTailTextFlyoverType.ForcePosition = "";
NoTailTextFlyoverType.CenterTail = false;
NoTailTextFlyoverType.VerticalGap = 0;
NoTailTextFlyoverType.HorizontalGap = -15;
NoTailTextFlyoverType.TailOffsets = [11, 12, 26, 25];
NoTailTextFlyoverType.TailPrefix = "textPopover";
NoTailTextFlyoverType.TailMargin = 15;
NoTailTextFlyoverType.AnchorBuffer = 1;

NoTailTextFlyoverType.GetFlyoverContent = function(params) { 
	var textKey = params[0];
	var content = Flyovers.TextArray[textKey];

	return content;
}
/* END OF NO TAIL TEXT FLYOVER */

/* START OF TEXT FLYOVER */
function TextFlyoverType() { }

TextFlyoverType.ID = "TEXT_FLYOVER";

TextFlyoverType.HoverDelayTime = 350;
TextFlyoverType.FadeInDelayTime = 200;
TextFlyoverType.FadeOutDelayTime = 200;
TextFlyoverType.GetContainer = function() { return document.getElementById('textPopoverContainer'); }
TextFlyoverType.GetContentContainer = function() { return document.getElementById('textPopoverContainerContent'); }
TextFlyoverType.MaxContainerWidth = 400;
TextFlyoverType.ShowIsLoading = false;
TextFlyoverType.IsAjax = false;
TextFlyoverType.ShowTail = true;
TextFlyoverType.ForcePosition = "";
TextFlyoverType.CenterTail = false;
TextFlyoverType.VerticalGap = 0;
TextFlyoverType.HorizontalGap = -15;
TextFlyoverType.TailOffsets = [11, 12, 26, 25];
TextFlyoverType.TailPrefix = "textPopover";
TextFlyoverType.TailMargin = 15;
TextFlyoverType.AnchorBuffer = 1;

TextFlyoverType.GetFlyoverContent = function(params) { 
	var textKey = params[0];
	var content = Flyovers.TextArray[textKey];

	return content;
}
/* END OF TEXT FLYOVER */

/* START OF BOTTOM TEXT FLYOVER */
function BottomTextFlyoverType() { }

BottomTextFlyoverType.ID = "BOTTOM_TEXT_FLYOVER";

BottomTextFlyoverType.HoverDelayTime = 350;
BottomTextFlyoverType.FadeInDelayTime = 200;
BottomTextFlyoverType.FadeOutDelayTime = 200;
BottomTextFlyoverType.GetContainer = function() { return document.getElementById('textPopoverContainer'); }
BottomTextFlyoverType.GetContentContainer = function() { return document.getElementById('textPopoverContainerContent'); }
BottomTextFlyoverType.MaxContainerWidth = 400;
BottomTextFlyoverType.ShowIsLoading = false;
BottomTextFlyoverType.IsAjax = false;
BottomTextFlyoverType.ShowTail = true;
BottomTextFlyoverType.ForcePosition = "bottom";
BottomTextFlyoverType.CenterTail = false;
BottomTextFlyoverType.VerticalGap = 0;
BottomTextFlyoverType.HorizontalGap = -15;
BottomTextFlyoverType.TailOffsets = [11, 12, 26, 25];
BottomTextFlyoverType.TailPrefix = "textPopover";
BottomTextFlyoverType.TailMargin = 15;
BottomTextFlyoverType.AnchorBuffer = 1;

BottomTextFlyoverType.GetFlyoverContent = function(params) { 
	var textKey = params[0];
	var content = Flyovers.TextArray[textKey];

	return content;
}
/* END OF BOTTOM TEXT FLYOVER */

/* START OF CENTER TEXT FLYOVER */
function CenterTextFlyoverType() { }

CenterTextFlyoverType.ID = "TEXT_FLYOVER";

CenterTextFlyoverType.HoverDelayTime = 350;
CenterTextFlyoverType.FadeInDelayTime = 200;
CenterTextFlyoverType.FadeOutDelayTime = 200;
CenterTextFlyoverType.GetContainer = function() { return document.getElementById('textPopoverContainer'); }
CenterTextFlyoverType.GetContentContainer = function() { return document.getElementById('textPopoverContainerContent'); }
CenterTextFlyoverType.MaxContainerWidth = 400;
CenterTextFlyoverType.ShowIsLoading = false;
CenterTextFlyoverType.IsAjax = false;
CenterTextFlyoverType.ShowTail = true;
CenterTextFlyoverType.ForcePosition = "";
CenterTextFlyoverType.CenterTail = true;
CenterTextFlyoverType.CenterOffset = 25;
CenterTextFlyoverType.VerticalGap = 0;
CenterTextFlyoverType.HorizontalGap = -15;
CenterTextFlyoverType.TailOffsets = [11, 12, 26, 25];
CenterTextFlyoverType.TailPrefix = "textPopover";
CenterTextFlyoverType.TailMargin = 15;
CenterTextFlyoverType.AnchorBuffer = 1;

CenterTextFlyoverType.GetFlyoverContent = function(params) { 
	var textKey = params[0];
	var content = Flyovers.TextArray[textKey];

	return content;
}
/* END OF CENTER TEXT FLYOVER */

function Flyovers() { }

Flyovers.Initalize = function() {
	Flyovers.curContainer = null;
	Flyovers.curContent = null;
	Flyovers.curTail = null;
	Flyovers.curContainerCoords = null;
	Flyovers.curAnchorCoords = null;

	Flyovers.showCallback = new Array();
	Flyovers.hideCallback = new Array();
	Flyovers.TextArray = new Array();
	Flyovers.delayedHoversByAnchorID = new Array();

	Flyovers.ajaxRequestObj = new AjaxRequest();
}

Flyovers.RegisterTextFlyover = function(anchorID, textKey, showTail, centerTail, refTag, forcePosition, width, locationOffset)
{
	var flyoverType = TextFlyoverType;
	var forceAlignment = (forcePosition != undefined);
	var location = (forcePosition != undefined) ? [forcePosition] : ['right', 'top', 'auto'];
	if (locationOffset == undefined) {
		locationOffset = (forcePosition) ? [0, 0,] : [0, -30]; // helps prevent flyovers from forcing IE to scroll
	}

	//create necessary parameters
	var params = new Array();
	params.push(textKey);
	if (refTag) { params['refTag'] = refTag; }
	
	if ( width == undefined ) {
		width = flyoverType.MaxContainerWidth;
	}

	amznJQ.available('popover', function() {
		jQuery('#' + anchorID).amazonPopoverTrigger({
			showCloseButton: false,
			showOnHover: true,
			forceAlignment: forceAlignment,
			location: location,
			locationOffset: locationOffset,
			locationMargin: 5,
			width: width,
			literalContent: Flyovers.TextArray[textKey]
		});
	});
}

Flyovers.RegisterIMDBFlyover = function(anchorID, type, imdbID, title, viewAllLink, refTag)
{
	var flyoverType = IMDBFlyoverType;

	//create necessary parameters
	var params = new Array();
	params.push(type);
	params.push(imdbID);
	params.push(title);
	params.push(viewAllLink);
	params.push(refTag);

	amznJQ.available('popover', function() {
		jQuery('#' + anchorID).amazonPopoverTrigger({
			showCloseButton: false,
			showOnHover: true,
			location: ['bottom', 'top', 'auto'],
			locationMargin: 5,
			destination: IMDBFlyoverType.GetFlyoverAjaxUrl(params),
			width: IMDBFlyoverType.DefaultWidth,
			onShow: function (popover) {
				popover.width(IMDBFlyoverType.DetermineWidth());
			},
			onFilled: function (popover) {
				var contentObject = jQuery('.ap_content');
				var htmlContent = contentObject.html().replace(/^\s+|\s+$/, '');
				contentObject.html(IMDBFlyoverType.GetFlyoverContent(params, htmlContent));
				popover.width(IMDBFlyoverType.DetermineWidth());
			}
		});
	});
}

Flyovers.RegisterShovlerFlyover = function(cellID, anchorID)
{
	if (document.getElementById(cellID) == null)  { return; }
	
	var flyoverType = ShovlerFlyoverType;

	//create necessary parameters
	var params = new Array();
	params.push(anchorID);

	Flyovers.RegisterFlyover(cellID, flyoverType, params);
}

Flyovers.RegisterTitleFlyover = function(anchorID, ASIN, refTag)
{
	var flyoverType = TitleFlyoverType;

	//create necessary parameters
	var params = new Array();
	params.push(ASIN);
	params.push(refTag);

	Flyovers.RegisterFlyover(anchorID, flyoverType, params);
}

Flyovers.RegisterFlyover = function(anchorID, flyoverType, params)
{
  	var objAnchor = document.getElementById(anchorID);
	var obj = Flyovers;

	//create reference to show callback method
	var showReference = Flyovers.startFlyover;
	Flyovers.showCallback[anchorID] = function() { showReference.call(obj, anchorID, flyoverType, params); };

	//create reference to hide callback method
	var hideReference = Flyovers.hideFlyover;
	Flyovers.hideCallback[anchorID] = function(event) { hideReference.call(obj, event, anchorID, flyoverType); };

	//create reference to cancel callback method
	var cancelReference = Flyovers.cancelFlyover;
	Flyovers.cancelCallback = function() { cancelReference.call(obj, flyoverType); };

	//create delayed hover
	var delayedHover = new DelayHover(objAnchor, flyoverType.HoverDelayTime);
	delayedHover.HoverFiredCallback = createClosure(Flyovers, Flyovers.showCallback[anchorID]);
	delayedHover.onMouseOutCallback = createClosure(Flyovers, Flyovers.cancelCallback);
	addHandler(objAnchor, "onclick", Flyovers.cancelCallback);

	Flyovers.delayedHoversByAnchorID[anchorID] = delayedHover;
}

Flyovers.startFlyover = function(anchorID, flyoverType, params)
{
	Flyovers.isCancel = false;
	params.push(anchorID);
	params.push(flyoverType);

	if (flyoverType.IsAjax) {
		var url = flyoverType.GetFlyoverAjaxUrl(params);
		if (url != null) {
			Flyovers.ajaxRequestObj.URL = url;
			Flyovers.ajaxRequestObj.CompletedCallback = createClosure(Flyovers, Flyovers.startFlyoverDone, params);
			Flyovers.ajaxRequestObj.StartRequest();
		}
	}
	else {
		Flyovers.makeRefTagCall(params);
		Flyovers.startFlyoverDone(params);
	}
}

Flyovers.makeRefTagCall = function(params) {
	var refTag = params['refTag'];

	if (refTag != null) {
		var url = '/gp/video/tag/ref=' + refTag;
		Flyovers.ajaxRequestObj.URL = url;
		Flyovers.ajaxRequestObj.CompletedCallback = null;
		Flyovers.ajaxRequestObj.Cancel();
		Flyovers.ajaxRequestObj.StartRequest();
	}
}

/* method to call once all the start flyover stuff is done including the AJAX call */
Flyovers.startFlyoverDone = function(params)
{	
	var flyoverType = params[params.length - 1];
	var anchorID = params[params.length - 2];

	var htmlContent = "";

	if (flyoverType.IsAjax) {
		htmlContent = Flyovers.ajaxRequestObj.result;
		htmlContent = htmlContent.replace(/^\s+|\s+$/, '');
	}

	htmlContent = flyoverType.GetFlyoverContent(params, htmlContent);

	var objAnchor = document.getElementById(anchorID);
	
	Flyovers.showFlyover(objAnchor, htmlContent, flyoverType);
}

Flyovers.cancelFlyover = function(flyoverType)
{
	Flyovers.isCancel = true;

 	var isAjax = flyoverType.IsAjax;

	if (isAjax) {
		Flyovers.ajaxRequestObj.Cancel();
	}
}

Flyovers.showFlyover = function(anchor, htmlContent, flyoverType)
{
	//verify the request has not been cancelled Flyovers can happen if the ajax result has been downloaded but the user has mouseout of the anchor
	if (Flyovers.isCancel == true) { return; }
	Flyovers.delayedHoversByAnchorID[anchor.id].enabled = false;

	if (Flyovers.curContainer != null) { Flyovers.curContainer.style.display = "none"; }
	if (Flyovers.curTail != null) { Flyovers.curTail.style.display = "none"; }

	Flyovers.cancelFlyover(flyoverType);	//ajax done loading cancel or close it

	flyoverType.GetContentContainer().innerHTML = htmlContent;
	Flyovers.curContainer = flyoverType.GetContainer();

	if (Flyovers.parallelEffects) {
		Flyovers.parallelEffects.end();
	}

	Flyovers.positionPopover(anchor, flyoverType);

	addHandler(Flyovers.curContainer, "mouseout", Flyovers.hideCallback[anchor.id]);
	addHandler(anchor, "mouseout", Flyovers.hideCallback[anchor.id]);
	addHandler(document.body, "mousemove", Flyovers.hideCallback[anchor.id]);

	Flyovers.fadeFlyover(true, flyoverType);
}

Flyovers.hideFlyover = function(evt, anchorID, flyoverType)
{
	if (evt != null) {
		var curX, curY;

		if (typeof evt.pageX == "undefined") {
			curX = evt.clientX + document.body.scrollLeft;
			curY = evt.clientY + document.body.scrollTop;
		} else {
			curX = evt.pageX; 
			curY = evt.pageY;
		}

		if (hitTestFixedCoords([curX, curY], Flyovers.curAnchorCoords)) { return; }
		if (hitTestFixedCoords([curX, curY], Flyovers.curContainerCoords)) { return; }
	}
	var objAnchor = document.getElementById(anchorID);

	removeHandler(Flyovers.curContainer, "mouseout", Flyovers.hideCallback[anchorID]);
	removeHandler(objAnchor, "mouseout", Flyovers.hideCallback[anchorID]);
	removeHandler(document.body, "mousemove", Flyovers.hideCallback[anchorID]);

	Flyovers.fadeFlyover(false, flyoverType);
	Flyovers.delayedHoversByAnchorID[anchorID].enabled = true;
}


Flyovers.fadeFlyover = function(isShow, flyoverType) 
{
	if (Flyovers.parallelEffects) {
		Flyovers.parallelEffects.end();
	}
	
	var effects = new Array();

	var startOpacity = 0;
	var endOpacity = 1;
	var fadeTime = flyoverType.FadeInDelayTime;

	if (!isShow) {
		startOpacity = 1;
		endOpacity = 0;
		fadeTime = flyoverType.FadeOutDelayTime;
	}
	
	if (isShow) {
		Flyovers.curContainer.style.display = "block";
		if (flyoverType.ShowTail) { Flyovers.curTail.style.display = "block"; }
	}

	var containerFade = new Fade(Flyovers.curContainer, fadeTime, startOpacity, endOpacity);
	effects.push(containerFade);
	
	if (flyoverType.ShowTail == true) {
		var tailFade = new Fade(Flyovers.curTail, fadeTime, startOpacity, endOpacity);
		effects.push(tailFade);
	}

	var obj = Flyovers;
	var effectsDoneReference = Flyovers.effectsDone;
	Flyovers.effectsDoneCallback = function() { effectsDoneReference.call(obj, isShow, flyoverType); };

	Flyovers.parallelEffects = new Parallel(effects);
	Flyovers.parallelEffects.registerCallback(Flyovers.effectsDoneCallback);
	Flyovers.parallelEffects.start();
}

Flyovers.effectsDone = function(isShow, flyoverType) 
{
	if (isShow)
	{
		Flyovers.curContainer.style.display = "block";	
		if (flyoverType.ShowTail) { Flyovers.curTail.style.display = "block"; }
	} else {
		Flyovers.curContainer.style.display = "none";
		if (Flyovers.curTail != null) { Flyovers.curTail.style.display = "none"; }
	}
}

Flyovers.positionPopover = function(objAnchor, flyoverType)
{
	var VERTICAL_GAP_BETWEEN = flyoverType.VerticalGap;
	var HORIZONTAL_GAP_BETWEEN = flyoverType.HorizontalGap;

	//Compute Anchor coords and save to future hitTests
	var anchorCoords = findPos(objAnchor);
	Flyovers.curAnchorCoords = [anchorCoords[0], anchorCoords[1], objAnchor.offsetWidth, objAnchor.offsetHeight];

	//Provide buffer around anchor -- for some reason findPos is returning the wrong y-coord for text_flyovers (anchor tags)
	if (flyoverType.AnchorBuffer > 0) {
		Flyovers.curAnchorCoords[0] = anchorCoords[0] - flyoverType.AnchorBuffer;
		Flyovers.curAnchorCoords[1] = anchorCoords[1] - flyoverType.AnchorBuffer;
		Flyovers.curAnchorCoords[2] = objAnchor.offsetWidth + (2 * flyoverType.AnchorBuffer);
		Flyovers.curAnchorCoords[3] = objAnchor.offsetHeight + (2 * flyoverType.AnchorBuffer);
	}
	
	//compute popover width
	Flyovers.curContainer.style.width = "auto";
	Flyovers.curContainer.style.display = "block";
	var containerWidth = Flyovers.curContainer.offsetWidth;
	Flyovers.curContainer.style.display = "none";

	if (flyoverType.MaxContainerWidth > 0 && containerWidth > flyoverType.MaxContainerWidth) {
		Flyovers.curContainer.style.width = flyoverType.MaxContainerWidth + 'px';
	}

	Flyovers.curContainer.style.display = "block";
	var containerWidth = Flyovers.curContainer.offsetWidth;
	var containerHeight = Flyovers.curContainer.offsetHeight;
	Flyovers.curContainer.style.display = "none";
	Flyovers.curContainer.style.width = containerWidth + 'px'; 		//force to stick to selected auto size

	var x, y;

	var isLeft = true;
	var isTop = true;
	var isMiddle = false;
	var tryLeftFirst = false;

	anchorCoords[0] = anchorCoords[0] - document.body.scrollLeft;
	anchorCoords[1] = anchorCoords[1] - document.body.scrollTop;

	//position popover to left or right?
	if (anchorCoords[0] > document.body.clientWidth / 2) { tryLeftFirst = true; }	
	if (tryLeftFirst && (anchorCoords[0] - containerWidth > HORIZONTAL_GAP_BETWEEN)) {
		x = anchorCoords[0] - containerWidth - HORIZONTAL_GAP_BETWEEN;
		isLeft = false;
	}
	else if (anchorCoords[0] + objAnchor.offsetWidth + containerWidth + HORIZONTAL_GAP_BETWEEN < document.body.clientWidth) {
		x = anchorCoords[0] + objAnchor.offsetWidth + HORIZONTAL_GAP_BETWEEN;
		isLeft = true;
	}
	else {
		x = anchorCoords[0] - containerWidth - HORIZONTAL_GAP_BETWEEN;
		isLeft = false;
	}

	//position popover top, middle or bottom?
	if (anchorCoords[1] + containerHeight < document.body.clientHeight || flyoverType.ForcePosition == "bottom") {
		y = anchorCoords[1];
		if (y < 0) { y = 0 + VERTICAL_GAP_BETWEEN; }
		isTop = true;
	}
	else {
		y = anchorCoords[1] - (containerHeight - objAnchor.offsetHeight);
		if (y + containerHeight > document.body.clientHeight) { y = document.body.clientHeight - containerHeight - VERTICAL_GAP_BETWEEN; }
		isTop = false;
	}

	if (y < 0 || y + containerHeight > document.body.clientHeight && flyoverType.ForcePosition == "") {
		isMiddle = true;
		y = anchorCoords[1] - (containerHeight / 2);
		
		if (y < 0 || y + containerHeight > document.body.clientHeight) {
			y = 0 + VERTICAL_GAP_BETWEEN;
		}
	}

	//reposition popover since no tail (display below or above anchor)
	if (!flyoverType.ShowTail) {
		if (isLeft) { x = anchorCoords[0]; }
		else { x = anchorCoords [0] + objAnchor.offsetWidth - containerWidth + VERTICAL_GAP_BETWEEN; }

		if (isTop) { y = anchorCoords[1] + objAnchor.offsetHeight; }
		else { y = anchorCoords[1] - containerHeight; }

		if (y < 0) { y = anchorCoords[1] + objAnchor.offsetHeight; }
	}

	if (flyoverType.CenterTail) {
		var oldY = y;	//incase cannot center
		var oldX = x;	//incase cannot center

		if (isTop) { y = anchorCoords[1] + (objAnchor.offsetHeight / 2) - flyoverType.CenterOffset; }

		if  (isLeft) { x = x - (objAnchor.offsetHeight / 2); }
		else { x = x + (objAnchor.offsetHeight / 2); }

		if (y < 0 || y + containerHeight > document.body.clientHeight) { y = oldY; }
		
		if (x < 0 || x + containerWidth > document.body.clientWidth) { y = oldX; }
	}

	x = x + document.body.scrollLeft;
	y = y + document.body.scrollTop;

	Flyovers.curContainerCoords = [x, y, containerWidth, containerHeight];
	Flyovers.curContainer.style.left = x;
	Flyovers.curContainer.style.top = y;

	if (flyoverType.ShowTail) {		
		//leftX, rightX, leftY, rightY -- represents the amount of white space in borders so that the tails are positioned correctly
		var tailOffsets = flyoverType.TailOffsets;
		var tailPrefix = flyoverType.TailPrefix;
		var tailYMargin = flyoverType.TailMargin;

		if (isMiddle && isLeft) {
			Flyovers.curTail = document.getElementById(tailPrefix + 'TopLeftTail');
			Flyovers.curTail.style.left = x - tailOffsets[0];
			Flyovers.curTail.style.top = anchorCoords[1] + (objAnchor.offsetHeight / 2) + document.body.scrollTop; 
		}
		else if (isMiddle && !isLeft) {
			Flyovers.curTail = document.getElementById(tailPrefix + 'TopRightTail');
			Flyovers.curTail.style.left = x + containerWidth - tailOffsets[1];
			Flyovers.curTail.style.top = anchorCoords[1] + (objAnchor.offsetHeight / 2) + document.body.scrollTop 
		}
		else if (isTop && isLeft) {
			Flyovers.curTail = document.getElementById(tailPrefix + 'TopLeftTail');
			Flyovers.curTail.style.left = x - tailOffsets[0];
			Flyovers.curTail.style.top = y + tailYMargin;  
		}
		else if (isTop && !isLeft) {
			Flyovers.curTail = document.getElementById(tailPrefix + 'TopRightTail');
			Flyovers.curTail.style.left = x + containerWidth - tailOffsets[1];
			Flyovers.curTail.style.top = y + tailYMargin;  
		}
		else if (!isTop && isLeft) {
			Flyovers.curTail = document.getElementById(tailPrefix + 'BottomLeftTail');
			Flyovers.curTail.style.left = x - tailOffsets[0];
			Flyovers.curTail.style.top = y + containerHeight - tailOffsets[2] - tailYMargin;  
		}
		else {
			Flyovers.curTail = document.getElementById(tailPrefix + 'BottomRightTail');
			Flyovers.curTail.style.left = x + containerWidth - tailOffsets[1];
			Flyovers.curTail.style.top = y + containerHeight - tailOffsets[3] - tailYMargin; 
		}
	}
}

Flyovers.Initalize();


Flyovers.TextArray['season_buybox_subscribe'] = '<h3>TV Passes</h3><div class="unbox_info">TV Passes include all previous and future episodes of the season. Previous episodes will download immediately and future episodes will download when they\'re available. You\'ll often get a discounted price for buying a TV Pass instead of buying episodes one at a time. <a href="/gp/help/customer/display.html/ref=atv_dp_spass?nodeId=200182030">Learn more about TV Passes</a></div>';

Flyovers.TextArray['buybox_united_states'] = '<h3>Downloads Within the United States Only</h3><div class="unbox_info">Videos must be purchased using a US credit card and billing address. The videos can only be downloaded while inside the United States (the 48 contiguous states, Alaska, Hawaii, and the District of Columbia). US citizens located overseas will be unable to download the videos. Sorry!</div>';

Flyovers.TextArray['movie_buybox_rent_details'] = '<h3>Rental Details</h3><div class="unbox_info">Your rental video can be stored on your PC or TiVo DVR for 30 days. Once you press play, you have 24 hours to watch the video before it expires. Your rental video can be downloaded to only one PC or TiVo and cannot be transferred to a portable device. <a href="/gp/help/customer/display.html/ref=atv_dp_mobile?nodeId=3748#renting">What is the different between renting and purchasing videos?</a></div>';

Flyovers.TextArray['movie_buybox_purchase_details'] = '<h3>Purchase Details</h3><div class="unbox_info">You can keep purchased videos on 2 PCs or TiVo boxes and 2 portable video players at the same time. Your purchase allows you to download a high-quality video file for playback on a Windows-based PC, a TiVo Best Quality video for playback on a Series 2 or Series3 TiVo box, and a smaller video file designed for playback on <a href="/gp/feature.html/ref=atv_dp_mobile?docId=1000038721">compatible Windows Media video portable devices</a>.</div>';

