
/**
*  Our Custom Theaters widget.  This plugin handles loading of custom theater 
*	lists as well as setting the currently selected custom theater, if there
*	is one.
*
* @param object ctElement Object of the element that holds the list of 
*	custom theaters.
*
* @param object obj The PromoCampaign object
* 
* @param string prefix Prefix of the form element we are working with
*/
jQuery.fn.customTheatersWidget = function(ctElement, obj, prefix) {

	return(this.each(function() {

		//
		// The element we are working with
		//
		var element = jQuery(this);

		var program = $("[id='" + prefix + "[RefAVCProgramId]']").val();
		var genre = $("[id='" + prefix + "[RefVODContentTypeId]']").val();

		var url = root + "api/json/feeds.php/theaters/?action=getCTSelectLegacy"
			+ "&jsoncallback=?"
			+ "&program=" + program
			+ "&genre=" + genre
			;

		//$("h3").append("BEFORE: " + element.find("option").length + ", "); // Debugging

		//
		// Disable the element while we're selecting.
		//
		ctElement.attr("disabled", true);
		ctElement.loadingWidget("Custom Theaters");

		//
		// Set up our callback to include the prefix we are using
		//
		var callback = function(data) {
			jQuery.fn.customTheatersWidget.loadCTCallback(data, obj, prefix);
		}

		$.getJSON(url, {}, callback);
		//$.getJSONDebug(url, {}, callback); // Debugging

	})); // End of this.each()

}; // End of customTheatersWidget()


/**
* This callback is called when loading custom theaters via AJAX.
*
* @param array data Array of custom theaters.
*
* @param object obj The PromoCampaign object
* 
* @param string prefix Prefix of the form element we are working with
*/
jQuery.fn.customTheatersWidget.loadCTCallback = function(data, obj, prefix) {

	var element = $("select[id='" + prefix + "[RefVODTheaterId]']");
	//
	// If no ID is found, then we didn't dind the element (or it 
	// wasn't a select element, as is the case on the categories 
	// form where it is hidden).  In any case, get rid of the 
	// loading widget and stop early.
	//
	if (element.attr("id") == ""
		|| element.attr("id") == undefined
		) {
		$("[id='" + prefix + "[RefVODTheaterId]']").loadingWidget();
		return(null);
	}

	jQuery.fn.customTheatersWidget.pruneCTList(prefix);
	jQuery.fn.customTheatersWidget.setCTTypeHandlers(data, prefix);

	//
	// Now that we're done loading custom theaters, make the fields 
	// clickable
	//
	$("[id='" + prefix + "[add_new_filter]']").attr("disabled", false);
	//$("[id='campaign[campaign_id]']").attr("disabled", false);

	//
	// We're done.  Re-enable the element.
	//
	$("[id='"+ prefix + "[RefVODTheaterId]']").attr("disabled", false);

	//
	// Set the default type to "none selected".
	//
	var types = $("select[id='CustomTheaterType']");
	types.val(0);

	//
	// Set the custom theater dropdown to the current custom theater.
	//
	//var campaign_data = PromoCampaign.prototype.getObj().campaign_data;
	var campaign_data = obj.campaign_data;
	if (typeof(campaign_data["RefVODTheaterId"]) != "undef") {
		if (
			campaign_data["RefVODTheaterId"] != 0
			&& campaign_data["RefVODTheaterId"] != undefined
			) {
			jQuery.fn.customTheatersWidget.setDefaultCT(data, prefix, 
				campaign_data["RefVODTheaterId"]);
		}
	}

	//
	// If not using a campaign, set the ID to whatever our search criteria was
	//
	//if (PromoCampaign.prototype.getObj().using_campaign == "false") {
	if (obj.using_campaign == "false") {
		//
		// Only set this if the theater id is not 0/false.
		// Otherwise, it gets set to something unexpected, as per Bug #1532.
		//
		//if (PromoCampaign.prototype.getObj().theater_id) {
		if (obj.theater_id) {
			//jQuery.fn.customTheatersWidget.setDefaultCT(data, prefix, 
			//	PromoCampaign.prototype.getObj().theater_id);
			jQuery.fn.customTheatersWidget.setDefaultCT(data, prefix, 
				obj.theater_id);
		}
	}

	$("[id='" + prefix + "[RefVODTheaterId]']").loadingWidget();

	//$("h3").append("AFTER: " + element.find("option").length + ", "); // Debugging

} // End of loadCTCallback()


/**
* Remove all but the first element of the CT List.
*/
jQuery.fn.customTheatersWidget.pruneCTList = function(prefix) {

	var element = $("select[id='" + prefix + "[RefVODTheaterId]']");
	var first = element.find("option");
	element.empty();
	element.append("<option value=\"" + first.val() + "\">" + first.html() + "</option>");

} // End of printCTList()


/**
*
* Set our various custom theater handlers for the different custom theater types.
*
* @param array data Array of various custom theater groups and individual 
*	custom theaters.
*
* @param string prefix The element name prefix.
*/
jQuery.fn.customTheatersWidget.setCTTypeHandlers = function(data, prefix) {

	var types = $("select[id='CustomTheaterType']");

	//
	// Clear our existing types and repopulate the select box.
	//
	types.empty();

	types.append("<option value=\"0\" >None Selected</option>");

	for (var key in data) {

		//
		// Empty group, skip.
		//
		if (data[key] == "OPTGROUP") {
			continue;
		}

		types.append("<option>" + key + "</option>");

	}

	//
	// Clear out current handlers and set up new ones to popular the 
	// list of custom theaters when a type is clicked on.
	//
	types.unbind();

	types.change(function() {

		var val = $(this).val();
		var element = $("select[id='" + prefix + "[RefVODTheaterId]']");
		element.empty();
		var theaters = data[val];

		for (key in theaters) {
			var id = key;
			var name = theaters[key];

			if ((typeof theaters[key] == "object") && ("ct_id" in theaters[key])) {
				id = theaters[key].ct_id;
				name = theaters[key].name;
			}

			element.append("<option value=\"" + id + "\">"
				+ name
				+ "</option>");
		}

		});

} // End of setCTTypeHandlers()


/**
* Set our default theater ID in the double select list of custom theaters.
*
* @param array data Our array of theater info
*
* @param string prefix Our prefix string for form elements
*
* @param integer theater_id Our default custom theater ID.
*/
jQuery.fn.customTheatersWidget.setDefaultCT = function(data, prefix, theater_id) {

	var types = $("select[id='CustomTheaterType']");
	var element = $("[id='"+ prefix + "[RefVODTheaterId]']");

	loop1:
	for (type in data) {

		if (data[type] == "OPTGROUP") {
			continue;
		}

		//
		// Loop through a group of theaters, and stop if we find our 
		// actual theater.
		//
		for (var key in data[type]) {

			if (key == theater_id) {
				types.val(type);
				types.change();
				element.val(key);
				//
				// Break out of both loops.
				//
				break loop1;
			}

		}

	}

} // End of setDefaultCT()




