function EpisodeBuybox(seasonASIN, episodeASINS, isCustomerRecognized) {
        this.seasonASIN = seasonASIN;
        this.episodeASINS = episodeASINS;
	this.isCustomerRecognized = isCustomerRecognized;
	this.hasInit = false;
	this.currentButtonClicked = null;
	
	var episodeBuybox = getElem("buy_episodes_" + this.seasonASIN);
	addHandler(episodeBuybox, "click", createClosure(this, this.startEpisodesPurchase, 'regular'));

	var pepsiBuybox = getElem("pepsi_buy_episodes");
	if (pepsiBuybox) { addHandler(pepsiBuybox, "click", createClosure(this, this.startEpisodesPurchase, 'pepsi')); }

	var continueButton = getElem("episodes_continue");
	if (continueButton) { addHandler(continueButton, "click", createClosure(this, this.submitEpisodesPurchase)); }
	var cancelButton = getElem("episodes_cancel");
	if (cancelButton) { addHandler(cancelButton, "click", createClosure(this, this.cancelEpisodesPurchase)); }
	var closeButton = getElem("close_episode_dialog");
	addHandler(closeButton, "click", createClosure(this, this.closeInfoDialog));
	var closeButtonPepsi = getElem("close_episode_dialog_pepsi");
	if (closeButtonPepsi) { addHandler(closeButtonPepsi, "click", createClosure(this, this.closeInfoDialog)); }
	var downloadSelector = getElem("download_to_selector");
	if (downloadSelector) { downloadSelector.style.display = "block"; }

	this.collapsedHeight = 0;
	this.playerContainer = getElem("episode_player_container");
	this.skipResizeIE = true;

	if (isIE()) {
		addHandler(window, "load", createClosure(this, this.initEpisodeList, null));
	} else {
		this.initEpisodeList();
	}
}

EpisodeBuybox.prototype.initEpisodeList = function() {
	if (this.episodeASINS && this.episodeASINS.length > 0) {
		this.collapsedHeight = getElem("synopsis_" + this.episodeASINS[0]).parentNode.offsetHeight - 8;
	}

	this.episodeTitle = new Array();
	this.episodeSynopses = new Array();
        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeASIN = this.episodeASINS[ii];

		//remove HREF tags from episode titles for javascript enabled browsers
		var titleElement = getElem("title_" + episodeASIN);
		var titleInnerHTML = titleElement.innerHTML;

		for (var jj=0; jj < titleElement.childNodes.length; jj++) {
			if (titleElement.childNodes[jj].tagName == "A") {
				titleInnerHTML = titleElement.childNodes[jj].innerHTML;
				break;
			}
		}
		titleElement.innerHTML = titleInnerHTML;

		this.episodeTitle[episodeASIN] = titleInnerHTML;
                this.episodeSynopses[episodeASIN] = getElem("synopsis_" + episodeASIN).innerHTML;
        }

	this.truncateRows();

        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeASIN = this.episodeASINS[ii];
                var row = getElem("episode_row_" + episodeASIN);
                addHandler(row, "mouseover", createClosure(this, this.enterRow, episodeASIN));
                addHandler(row, "mouseout", createClosure(this, this.exitRow, episodeASIN));

                var callback = createClosure(this, this.handleEpisodeClick, episodeASIN);
                for (var jj = 0; jj < row.cells.length - 1; jj++) {
                        var cell = row.cells[jj];
                        addHandler(cell, "click", callback);
                }

		callback = createClosure(this, this.closeEpisodeRow, episodeASIN);
		addHandler(getElem("collapse_" + episodeASIN), "click", callback);

                var checkbox = getElem("checkbox_" + episodeASIN);
                if (checkbox) {
                        addHandler(checkbox, "click", createClosure(this, this.selectEpisode, null));
                        checkbox.checked = false;
                }

		if (isIE()) {
			getElem("title_" + episodeASIN).style.textOverflow = "ellipsis";
                	getElem("synopsis_" + episodeASIN).style.textOverflow = "ellipsis";
		}
        }

        var selectAllCheckbox = getElem("select_all_" + this.seasonASIN);
        addHandler(selectAllCheckbox, "click", createClosure(this, this.selectAllEpisodes, null));
        selectAllCheckbox.checked = false;
	this.hasInit = true;

	addHandler(window, "resize", createClosure(this, this.truncateRows, null));
}

EpisodeBuybox.prototype.isLastEpisode = function(episodeASIN) {
	if (this.episodeASINS.length <= 0) {
		return false;
	}

	if (this.episodeASINS[this.episodeASINS.length - 1] == episodeASIN) {
		return true;
	}

	return false;
}

EpisodeBuybox.prototype.truncateRows = function() {
        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeASIN = this.episodeASINS[ii];
		this.truncateEpisodeText(episodeASIN);
	}
}

EpisodeBuybox.prototype.truncateEpisodeText = function(episodeASIN) {
	var titleDiv = getElem("title_" + episodeASIN);
	var text = this.episodeTitle[episodeASIN];
	var hasNewBadge = false;

	//only the last episodeASIN can have a new badge
	if (document.getElementById('newBadge') && this.isLastEpisode(episodeASIN)) { hasNewBadge = true; }

	if (hasNewBadge && this.hasInit == false) { 
		while (titleDiv.offsetWidth >= titleDiv.scrollWidth) {
			var newWidth = titleDiv.offsetWidth - 1;
			titleDiv.style.width = newWidth;
		}
		titleDiv.style.width = titleDiv.offsetWidth + 1;

		if (titleDiv.offsetWidth < 151) {
			var marginOffset = 153 - titleDiv.offsetWidth;
			document.getElementById('newBadge').style.marginRight = marginOffset + 'px';
			document.getElementById('newBadgeDetail').style.marginRight = marginOffset + 'px';
		}
	}

	if (isIE()) { return; }

	if (titleDiv.scrollWidth > titleDiv.offsetWidth) {
		tIndex = text.length - Math.ceil((titleDiv.scrollWidth - titleDiv.offsetWidth) /
			titleDiv.scrollWidth * text.length);
		titleDiv.innerHTML = text.substr(0, tIndex) + "...";
	}
	var count = 0;
	while (titleDiv.scrollWidth > titleDiv.offsetWidth) {
		titleDiv.innerHTML = titleDiv.innerHTML.substr(0, titleDiv.innerHTML.length - 4) + "...";
		count ++;
		if (count > 20) {
			break;
		}
	}
	var synopsisDiv = getElem("synopsis_" + episodeASIN);
	synopsisDiv.innerHTML = this.episodeSynopses[episodeASIN];
	var text = this.episodeSynopses[episodeASIN];
	if (synopsisDiv.scrollWidth > synopsisDiv.offsetWidth) {
		tIndex = text.length - Math.ceil((synopsisDiv.scrollWidth - synopsisDiv.offsetWidth) /
			synopsisDiv.scrollWidth * text.length);
		synopsisDiv.innerHTML = text.substr(0, tIndex) + "...";
	}
	count = 0;
	while (synopsisDiv.scrollWidth > synopsisDiv.offsetWidth) {
		synopsisDiv.innerHTML = synopsisDiv.innerHTML.substr(0, synopsisDiv.innerHTML.length - 4) + "...";
		count ++;
		if (count > 20) {
			break;
		}
	}

}

EpisodeBuybox.prototype.enterRow = function(episodeASIN) {
	if (this.selectedEpisode == episodeASIN) {
		return;
	}
        getElem("episode_row_" + episodeASIN).className = "unbox_episode_row_selected";
}

EpisodeBuybox.prototype.exitRow = function(episodeASIN) {
        getElem("episode_row_" + episodeASIN).className = "unbox_episode_row";
}

EpisodeBuybox.prototype.closeEpisodeRow = function(episodeASIN) {
	if (this.parallel) { this.parallel.end() };

	var effects = new Array();
        if (this.selectedEpisode == episodeASIN) {
        	effects = this.createHideEffects(episodeASIN);
        }
        this.parallel = new Parallel(effects);
        this.parallel.start();
}

EpisodeBuybox.prototype.handleEpisodeClick = function(episodeASIN) {
	if (this.selectedEpisode == episodeASIN) {
		return;
	}

        if (this.parallel) { this.parallel.end() };

	var effects = new Array();
        if (this.selectedEpisode) {
                effects = this.createHideEffects(this.selectedEpisode);
        }
	effects = effects.concat(this.createShowEffects(episodeASIN));
	this.selectedEpisode = episodeASIN;

        this.parallel = new Parallel(effects);
        this.parallel.start();
}

EpisodeBuybox.prototype.createShowEffects = function(episodeASIN) {
	getElem("expand_" + episodeASIN).style.display = "none";
	getElem("collapse_" + episodeASIN).style.display = "block";


        var synopsis = getElem("synopsis_" + episodeASIN);
        synopsis.style.display = "none";
        getElem("title_" + episodeASIN).style.display = "none";

	if (document.getElementById('newBadge') && this.isLastEpisode(episodeASIN)) { 
		document.getElementById('newBadge').style.display = "none"; 
	}

        var details = getElem("details_" + episodeASIN);
        details.style.display = "block";
        var media = getElem("media_" + episodeASIN);
        media.style.display = "block";

        var heightTo = Math.max(details.scrollHeight, media.scrollHeight);

        var effects = new Array();
	effects.push(new Resize(details, 250, details.offsetWidth, heightTo, details.offsetWidth, this.collapsedHeight));
	var mediaShow = new Resize(media, 250, media.offsetWidth, heightTo, media.offsetWidth, this.collapsedHeight);
	mediaShow.registerCallback(createClosure(this, this.endShowEffects));
        effects.push(mediaShow);

	return effects;
}

EpisodeBuybox.prototype.endShowEffects = function() {
	var anchorPos = findPos(getElem("episode_player_anchor"));
	var rowPos = findPos(getElem("player_container_" + this.selectedEpisode));

	this.playerContainer.style.display = "block";
	this.playerContainer.style.left = rowPos[0] - anchorPos[0];
	this.playerContainer.style.top = rowPos[1] - anchorPos[1];
	this.playerContainer.style.width = "182";
	this.playerContainer.style.height = "175";

	DPPlayer.LoadPreview("episode_player", this.selectedEpisode);
}

EpisodeBuybox.prototype.createHideEffects = function(episodeASIN) {
	this.playerContainer.style.width = "1";
	this.playerContainer.style.height = "1";
	DPPlayer.Clear("episode_player");

	getElem("expand_" + episodeASIN).style.display = "block";
        getElem("collapse_" + episodeASIN).style.display = "none";

        var hideDetails = getElem("details_" + episodeASIN);
        var hideMedia = getElem("media_" + episodeASIN);
        var duration = 250;

	var effects = new Array();
        effects.push(new Resize(hideMedia, duration, hideMedia.offsetWidth, this.collapsedHeight));
        var detailsHide = new Resize(hideDetails, duration, hideDetails.offsetWidth, this.collapsedHeight);
        detailsHide.registerCallback(createClosure(this, this.endHideEffects, episodeASIN));
        effects.push(detailsHide);
	return effects;
}

EpisodeBuybox.prototype.endHideEffects = function(episodeASIN) {

	if (document.getElementById('newBadge') && this.isLastEpisode(episodeASIN)) { 
		document.getElementById('newBadge').style.display = "inline";
	}

        getElem("synopsis_" + episodeASIN).style.display = "block";
        getElem("title_" + episodeASIN).style.display = "block";
        getElem("details_" + episodeASIN).style.display = "none";
        getElem("media_" + episodeASIN).style.display = "none";

	this.truncateEpisodeText(episodeASIN);

	// TODO: This is hacky
	if (this.selectedEpisode == episodeASIN) {
		this.selectedEpisode = null;
	}
}

EpisodeBuybox.prototype.selectAllEpisodes = function() {
        var selectAllCheckbox = getElem("select_all_" + this.seasonASIN);

        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeCheckbox = getElem("checkbox_" + this.episodeASINS[ii]);
                if (episodeCheckbox) {
                        episodeCheckbox.checked = selectAllCheckbox.checked;
                }
        }
}

EpisodeBuybox.prototype.selectEpisode = function() {
        var selectAllCheckbox = getElem("select_all_" + this.seasonASIN);

        var allSelected = true;
        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeCheckbox = getElem("checkbox_" + this.episodeASINS[ii]);
                if (episodeCheckbox) {
                        allSelected &= episodeCheckbox.checked;
                }
        }

        selectAllCheckbox.checked = allSelected;
}

EpisodeBuybox.prototype.isAnyEpisodeSelected = function() {
        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeCheckbox = getElem("checkbox_" + this.episodeASINS[ii]);
                if (episodeCheckbox && episodeCheckbox.checked) {
			return true;
                }
        }	
}

EpisodeBuybox.prototype.startEpisodesPurchase = function(clickAction) {
	// disable the buy button after a purchase
	if (this.hasPurchased == true) { return; }
	if (this.episodeASINS.length == 0) { return; }
	var button = getElem("buy_episodes_" + this.seasonASIN);
	var position = findPos(button);
	var offset = findPos(getElem("episode_offset_container"));

	var form = getElem("episodes_form_" + this.seasonASIN);
	form['clickAction'].value = clickAction;
	this.currentButtonClicked = clickAction;

	if (this.isAnyEpisodeSelected()) {
		if (this.isCustomerRecognized) {
			var selector = getElem("episode_selector_container");
			if (selector) {
				selector.style.display = "block";
				selector.style.left = position[0] - offset[0] + (button.offsetWidth / 2) - selector.offsetWidth;
				selector.style.top = position[1] - offset[1] + (button.offsetHeight / 2);
			} else {
				this.submitEpisodesPurchase();
			}
		} else {
			this.submitEpisodesPurchase();
		}
	} else {
		var dialog = getElem("episode_info_container");
		if (clickAction == "pepsi") { dialog = getElem("episode_info_container_pepsi"); }
		dialog.style.display = "block";
		dialog.style.left = position[0] - offset[0] + (button.offsetWidth / 2) - dialog.offsetWidth;
		dialog.style.top = position[1] - offset[1] + (button.offsetHeight / 2);
	}
}

EpisodeBuybox.prototype.showPepsiDialog = function() {
	var selectedEpisodes = 0;
	var costPerEpisode = 12;

        for (var ii=0; ii < this.episodeASINS.length; ii++) {
                var episodeCheckbox = getElem("checkbox_" + this.episodeASINS[ii]);
                if (episodeCheckbox) {
			if (episodeCheckbox.checked == true) {
				selectedEpisodes = selectedEpisodes + 1;			
			}
                }
        }
	
	return (selectedEpisodes * costPerEpisode > this.curPepsiPointBalance);
}

EpisodeBuybox.prototype.cancelEpisodesPurchase = function() {
	this.hasPurchased = false;
	this.currentButtonClicked = null;
	getElem("episode_selector_container").style.display = "none";
}

EpisodeBuybox.prototype.submitEpisodesPurchase = function() {
	var form = getElem("episodes_form_" + this.seasonASIN);

	var clientSelect = getElem("client.atv.clientid");
	if (clientSelect) {
		form['ref.atv.clientid'].value = clientSelect.value
		form['forceUnrec'].value = 0;
	}

	if (this.currentButtonClicked == "pepsi" && this.showPepsiDialog() && this.isCustomerRecognized) {
		var button = getElem("pepsi_buy_episodes");
		var position = findPos(button);
		var pepsiDialog = getElem("pepsi_info_container");
		var offset = findPos(getElem("episode_offset_container"));
		
		pepsiDialog.style.display = "block";
		pepsiDialog.style.left = position[0] - offset[0] + (button.offsetWidth / 2) - pepsiDialog.offsetWidth;
		pepsiDialog.style.top = position[1] - offset[1] + (button.offsetHeight / 2);
		this.cancelEpisodesPurchase();
	} else {
		form.submit();
	}

}

EpisodeBuybox.prototype.closeInfoDialog = function() {
	getElem("episode_info_container").style.display = "none";

	if (getElem("episode_info_container_pepsi")) {
		getElem("episode_info_container_pepsi").style.display = "none";
	}
}
