

/**
* Set up our Promo class to contain promo-specific Javascript code.
*/
function Promo() {
}

/**
* @param string root The root URI of our installation. 
*	This should be "/" in production, or something like "/~dmuth/md/html"
*	in dev.
*/
Promo.prototype.init = function(root) {

	//
	// Set a static variable that points to our object.  This is so that
	// code exeucted from a jQuery handler can interact with object methods.
	//
	Promo.prototype.obj = this;

	//
	// Focus on our first field.
	// If I understand Javascript right, a form where #job_id does not exist
	// will NOT cause focus to be lost from #item[name].
	//
	$("[id='item[name]']").focus();
	$("[id='search[id]']").focus();

	//
	// When the master checkbox is clicked, check/uncheck every
	// other checkbox for disabling records.
	//
	$("#disable_all").click(function() {

		var boxes = $("input[type='checkbox'].disable");

		if ($(this).attr("checked")) {
			boxes.attr("checked", true);

		} else {
			boxes.attr("checked", false);

		}

		});

	//
	// When this dropdown is changed, change the main shape dropdown to
	// match, and submit the form.
	//
	$("select[id='searchNoop[ShapeRow]']").change(function() {

		var val = $(this).val();
		$("select[id='search[Shape]']").val(val);
		$("#search_submit").click();

		});

	//
	// When checked, all banners are enlarged.
	//
	$("#enlarge_all").click(function() {

		if ($(this).attr("checked")) {
			$(".banner_full").show();
			$(".button_enlarge").hide();
			$(".button_shrink").show();

		} else {
			$(".banner_full").hide();
			$(".button_enlarge").show();
			$(".button_shrink").hide();

		}

		});

	//
	// Enlarge a specific banner.
	//
	$(".button_enlarge").click(function() {
		$(this).parent().find(".button_enlarge").hide();
		$(this).parent().find(".button_shrink").show();
		$(this).parent().find(".banner_full").show();

		return(false);

		});

	//
	// Shrink a specific banner.
	//
	$(".button_shrink").click(function() {
		$(this).parent().find(".button_enlarge").show();
		$(this).parent().find(".button_shrink").hide();
		$(this).parent().find(".banner_full").hide();

		return(false);

		});

	//
	// When an affiliate link is clicked on, highlight the text, and display
	// a "popup" message underneath the link.
	//
	$(".affiliate_link").click(function() {

		$(this).select();

		//
		// Note that after() seems to move the element, NOT copy it!
		// Hide the message first, in case it was already visible at another
		// form element.
		//
		var message = $("#link_message");
		message.hide();
		$(this).after(message);
		message.fadeIn("slow");

		//
		// If we previously converted an object, convert it back to a text
		// field before converting the next one.
		//
		// @todo Move these lines into a plugin to toggle the input type
		//
		if (typeof(this_old) != "undefined") {
			textareaToText(this_old);
		}

		this_old = $(this);

		textToTextarea($(this));

		});

	/**
	* Turn one or more text fields into a textarea.
	*
	* @param object A Jquery object that is a span/div/etc.  Basically 
	*	anything but the actual input element, since we'll be 
	*	replacing it.
	*
	* @return null
	*/
	function textToTextarea(obj) {

		obj.find("input").each(function() {

			var tmp = $("<textarea rows=\"8\" cols=\"" 
				+ $(this).attr("size") 
				+ "\" ></textarea>");

			var val = $(this).val();
			val = val.replace(/</g, "&lt;");
			val = val.replace(/>/g, "&gt;");

			tmp.append(val);
			$(this).replaceWith(tmp);
			tmp.select();

			});


	} // End of textToTextarea()


	/**
	* Turn one or more textareas into a text field.
	*
	* @param object A Jquery object that is a span/div/etc.  Basically 
	*	anything but the actual input element, since we'll be 
	*	replacing it.
	*
	* @return null
	*/
	function textareaToText(obj) {

		obj.find("textarea").each(function() {

			var tmp = $("<input type=\"text\" size=\"" 
				+ $(this).attr("cols") + "\" />");

			tmp.val($(this).val());
			$(this).replaceWith(tmp);
			tmp.select();

		});

	} // End of textareaToText()


	$(".affiliate_link").blur(function() {
		//
		// Nothing happens here.  For the record, I tried fading/hiding the
		// link message here, but since these functions are called asynchronously
		// by the browser, sometimes the click() handler would get called
		// *before* this blur() handler, and that let to all sorts of chaos.
		//
		});

	//
	// Handler for clicking on a sortable header.
	//
	$(".sort_order").click(function() {

		//
		// Get the ID of what was clicked, and split it up so that
		// we get the sorting field name and the sorting order.
		// The sorting order defaults to ascending.
		//
		var id = $(this).attr("id");
		//var results = id.split("_");
		var results = id.split("-");
		var name = results[1];
		var order = results[2];
		if (typeof(order) == "undefined") {
			order = "asc";
		}

		//
		// Set the hidden form values and submit the form.
		//
		$("#sort_name").val(name);
		$("#sort_order").val(order);
		$("#search_submit").click();

		return(false);

		});

		/**
		* Disable all submit buttons after they are clicked on to prevent
		*	double submissions.
		*/
		var beenhere = 0;
		$("input[type='submit']").click(function() {

			//
			// If this function was already fun, then just return false on 
			// subsequent clicks.
			//
			// Since Javascript is single-threaded, this should be safe.
			//
			if (beenhere) {
				return(false);
			}

			var beenhere = 1;

			});

	$(".search_submit").click(function() {
		$("#action").val("search");

		//
		// Select all of our categoryIds so they are submitted
		//
		$("[id='search[CategoryIds][]'] option").attr("selected", "selected");
		$("[id='campaign[CategoryIds][]'] option").attr("selected", "selected");

		});

	//
	// If we click on the download link, change our action to download.
	//
	$(".search_submit_download").click(function() {
		$("#action").val("download");

		//
		// Select all of our categoryIds so they are submitted
		//
		$("[id='search[CategoryIds][]'] option").attr("selected", "selected");
		$("[id='campaign[CategoryIds][]'] option").attr("selected", "selected");

		});

	Promo.prototype.obj.hookFieldsets();

	//
	// On submission, select all category IDs so that they are submitted with the form.
	//
	$("#promo_video_edit").submit(function() {
		$("[id='attrib[CategoryIds][]'] option").attr("selected", "seleted");
	});

} // End of Promo.init()


/**
* Hook our fieldsets so that clicking the legend link in each of them will
* toggle the contents of that fieldset.
*/
Promo.prototype.hookFieldsets = function() {

	//
	// The clickable link in every legend tag should toggle the 
	// contents of that fieldset.
	//
	$("fieldset").find("legend").find("a").click(function() {
		var e = $(this).parent().parent().find(".fields");
		e.toggle();
		return(false);
		});

} // End of hookFieldset()


/**
* Set up handlers for our campaign form.
*/
Promo.prototype.initCampaign = function(root) {

	//
	// Selecting a specific campaign should make us switch to that campaign.
	//
	$("select[id='campaign[campaign_id]']").change(function() {

		var val = $(this).val();

		var url = document.location['protocol'] 
			+ "//" + document.location['hostname'] 
			+ document.location['pathname']
			+ "?campaign_id=" + escape(val)
			;
		//alert(url); // Debugging

		document.location = url;

		});

} // End of initCampaign()


/**
* Return the current instance of our object.
*/
Promo.prototype.getObj = function() {
	return(this.obj);
}


