/* Functions used to assist in package selection for purchase.
	Provided by Topspin Media http://www.topspinmedia.com/
	Dependencies:
		YUI Yahoo Global object: <script src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js"></script> 
		YUI DOM object: <script src="http://yui.yahooapis.com/2.6.0/build/dom/dom-min.js"></script>
		YUI Event object: <script src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js"></script>
*/

if (typeof(Topspin) == "undefined" || !Topspin)
{
	Topspin = {};
}
Topspin.BundleHelper = {};

Topspin.BundleHelper.selectContainerName = "selectioncontainer";
Topspin.BundleHelper.continueButtonName = "continueButton";
Topspin.BundleHelper.cancelButtonName = "cancelButton";
Topspin.BundleHelper.shirtSelectName = "sel_size";
Topspin.BundleHelper.fileSelectName = "sel_format";
Topspin.BundleHelper.productContainerClass = "module";

/*	Utility function to get the selected value of a radio button group. 
	Params:
		radioGroup - reference to the radio group form element for which you would like to retrieve the selected value.
	Return: string; the value of the selected radio button, or null, if none selected.
*/
Topspin.BundleHelper.radioValue = function (radioGroup)
{
	if (radioGroup)
	{
		for (var i = 0; i < radioGroup.length; i++)
		{
			if (radioGroup[i].checked)
			{
				var retVal = radioGroup[i].value;
				radioGroup = null;
				return retVal;
			}
		}
	}
	return null;
};

/* 	Determines whether the selection box is currently being displayed or not.
	Returns: true or false.
*/
Topspin.BundleHelper.selectionBoxOpen = function()
{
	var elmContainer = document.getElementById(this.selectContainerName);
	var retVal = false;
	if (elmContainer)
	{
		if ((elmContainer.currentStyle && elmContainer.currentStyle.display != "none")
			|| (elmContainer.style && elmContainer.style.display != "none")
		)
		{
			retVal = true;
		}
		elmContainer = null;
	}
	return retVal;
};

/*  Display bundle selection options in the appropriate area. 
	Params:
		buyButton - reference to the html element that the user clicked to launch the purchase process.
		productType - string; unique name of the type of product being purchased. See function body for valid values.
	Return: null
*/
Topspin.BundleHelper.buyClick = function (buyButton, productType, alignment)
{
	if (buyButton && productType && productType.length > 0)
	{
		var bFiles = false;
		var bShirt = false;
		// Determine whether or not to show a shirt size select box.
		switch (productType)
		{
			case "utotee":
			case "owltee":
			{
				bFiles = true;
				break;
			}
		}
		
		var elmContinue = document.getElementById(this.continueButtonName);
		if (elmContinue)
		{
			// Set productType attribute on the Continue button. This will be used in the buyContinue function to determine the type of product being purchased.
			elmContinue.setAttribute("productType", productType);
			elmContinue = null;
		}
		
		// Reset selections.
		var elm = document.getElementById(this.selectContainerName);
		if (elm)
		{		
			var aSelsRaw = elm.getElementsByTagName("select");
			var aSels = new Array();
			if (aSelsRaw && aSelsRaw.length > 0)
			{
				for (var i = 0; i < aSelsRaw.length; i++)
				{
					var elmSelect = aSelsRaw[i];
					if (i == 0)
					{
						elmSelect.value = "0";
					}
					else
					{
						elmSelect.value = "1";
					}
					elmSelect = null;
				}
				delete aSelsRaw;
				aSelsRaw = null;
			}
		}
		// Display bundle select or not?
		YAHOO.util.Dom.setStyle(this.fileSelectName, "visibility", (bFiles ? "visible" : "hidden"));
		YAHOO.util.Dom.setStyle(this.fileSelectName, "display", (bFiles ? "block" : "none"));
		YAHOO.util.Dom.setStyle("bundleoptions", "display", (bFiles ? "block" : "none"));
		
		// Display t-shirt select or not?
		YAHOO.util.Dom.setStyle(this.shirtSelectName, "visibility", (bShirt ? "visible" : "hidden"));
		YAHOO.util.Dom.setStyle(this.shirtSelectName, "display", (bShirt ? "block" : "none"));
		YAHOO.util.Dom.setStyle("sizelabel", "display", (bShirt ? "block" : "none"));
		
		if (!bFiles && !bShirt)
		{
			// No need for the selection dialogue. Just go to the p-flow.
			this.buyContinue(productType);
			return;
		}
		
		// Show the selection container.  It has to be visible before the calculations below will work.
		YAHOO.util.Dom.setXY(this.selectContainerName, [-500, 0]);
		YAHOO.util.Dom.setStyle(this.selectContainerName, "display", "block");
		
		// Determine where to place selection container.
		
		var elm = buyButton;
		
		// Get the position of the buy button that the user pressed.
		var pos = YAHOO.util.Dom.getXY(elm);
		if (alignment && alignment.indexOf("r") > -1)
		{
			// Align to the right of the button.
			
			// Get the width of the button.
			var modWidth = parseInt(YAHOO.util.Dom.getStyle(elm, "width"));
			// Get the width of the selection container.
			var contWidth = parseInt(YAHOO.util.Dom.getStyle(this.selectContainerName, "width"));
			// Adjust the X value to the left by the amount of the difference in widths.
			pos[0] -= (contWidth - modWidth);
		}
		
		if (alignment && alignment.indexOf("b") > -1)
		{
			// Align to the bottom of the button.
			
			// Get the height of the button.
			var modHeight = parseInt(YAHOO.util.Dom.getStyle(elm, "height"));
			// Get the height of the selection container.
			var contHeight = parseInt(YAHOO.util.Dom.getStyle(this.selectContainerName, "height"));
			// Adjust the Y value down by the amount of the difference in heights.
			pos[1] += (modHeight - contHeight);
		}
		YAHOO.util.Dom.setXY(this.selectContainerName, pos);
	}
};

/* 	Launches the (Flash) purchase flow when the user makes their selections 
	and presses the "Continue" button.
	Params:
		productType - string; unique name of the type of product being purchased. See function body for valid values.
	Return: null
*/
Topspin.BundleHelper.buyContinue = function (productType)
{
	/* 	This function might either be called directly or as an event handler.
		If it is called as an event handler, the first argument will be an object representing the event.
		If that's the case, we want to disregard this object and get the productType attribute 
		  from the calling DOM element (which should be the "continue" button).
	*/
	if (typeof (productType) == "object")
	{
		var prop = this.getAttribute("productType");
		var productType = prop;
	}
	
	var bBypass = false;
	// For some productTypes, there is no need for user selections, so set a flag that we can jump straight to the purchase flow.
	/*
	if (productType == "cd"
		|| productType == "vinyl")
	{
		bBypass = true;
	}
	*/
	
	var elm = document.getElementById(Topspin.BundleHelper.selectContainerName);
	if (elm || bBypass === true)
	{
		// Get all the select boxes which are currently being displayed.
		var aSelsRaw = elm.getElementsByTagName("select");
		var aSels = new Array();
		if (aSelsRaw && aSelsRaw.length > 0)
		{
			var nDimensions = 0;
			for (var i = 0; i < aSelsRaw.length; i++)
			{
				var elmSelect = aSelsRaw[i];
				if (elmSelect && ((elmSelect.currentStyle && elmSelect.currentStyle.display != "none") || (elmSelect.style && elmSelect.style.display != "none")))
				{
					// This one is visible, so add it to the filtered array and add a dimension.
					aSels[aSels.length] = elmSelect;
					nDimensions++;
				}
				elmSelect = null;
			}
			// Clean up a little.
			delete aSelsRaw;
			aSelsRaw = null;
			
			// Define array of actual selected values (as opposed to the previous arrays of select elements).
			var aSelections = new Array(nDimensions);
			var blnValid = true;
			for (var i = 0; i < aSels.length; i++)
			{
				elmSel = aSels[i];
				if (elmSel)
				{
					if (elmSel.value == "0" && bBypass !== true)
					{
						// The user has not selected anything.  Message them to do so.
						alert("Please make a selection.");
						aSelections[i] = null;
						blnValid = false;
						elmSel = null;
						break;
					}
					else
					{
						// Store the selected value in our array.
						aSelections[i] = parseInt(elmSel.value) - 1;
						elmSel = null;
					}
					elmSel = null;
				}
			}
			
			// Ugly-ass hack
			if (bBypass === true)
			{
				nDimensions = 1;
				aSelections[0] = 0;
			}
			
			if (blnValid === true || bBypass === true)
			{
				// Define bundle array for each of the various product types.
				
				var aBundles = new Array();
				switch (productType)
				{
					case "utotee":
					{
						aBundles = [
							{bundleID:80134,
							campaignID:10042636,
							name:"UTO Tee"},
							{bundleID:80213,
							campaignID:10042640,
							name:"UTO Digital, UTO Tee"},
							{bundleID:80244,
							campaignID:10042643,
							name:"UTO CD, Digital, UTO Tee"},
							{bundleID:80274,
							campaignID:10042645,
							name:"UTO LP, Digital, UTO Tee"},
							{bundleID:80361,
							campaignID:10042679,
							name:"TTCOOE Digital, UTO Tee"},
							{bundleID:80362,
							campaignID:10042680,
							name:"TTCOOE CD Digital, UTO Tee"},
							{bundleID:80367,
							campaignID:10042684,
							name:"VEVC0010, UTO Tee"}
						];
						break;
					}
						case "owltee":
					{
						aBundles = [
							{bundleID:80368,
							campaignID:10042685,
							name:"Owl Tee"},
							{bundleID:80369,
							campaignID:10042686,
							name:"UTO Digital, Owl Tee"},
							{bundleID:80370,
							campaignID:10042687,
							name:"UTO CD, Digital, Owl Tee"},
							{bundleID:80372,
							campaignID:10042688,
							name:"UTO LP, Digital, Owl Tee"},
							{bundleID:80373,
							campaignID:10042689,
							name:"TTCOOE Digital, Owl Tee"},
							{bundleID:80374,
							campaignID:10042690,
							name:"TTCOOE CD Digital, Owl Tee"},
							{bundleID:80375,
							campaignID:10042692,
							name:"VEVC0010, Owl Tee"}
						];
						break;
					}
				}
				
				// Get the Bundle object that represents the user's selections.
				var bundle;
				
				if (nDimensions == 1)
				{
					bundle = aBundles[aSelections[0]];
				}
				else
				{
					for (var i = 0; i < nDimensions; i++)
					{
						if (i == (nDimensions - 1))
						{
							bundle = bundle[aSelections[i]];
						}
						else
						{
							bundle = aBundles[aSelections[i]];
						}
					}
				}
				
				// Hide the selection container.
				YAHOO.util.Dom.setStyle(elm, "display", "none");
				
				if (bundle && typeof(bundle) == "object")
				{
					// Launch the purchase flow.
					TSPurchase.load({aId:Topspin.BundleHelper.artistID, bId:bundle.bundleID, cId:bundle.campaignID, persist:true});
				}
				
				// Clean up
				delete aBundles;
				aBundles = null;
			}
		}
		// Clean up
		delete aSels;
		aSels = null;
		elm = null;
	}
	return false;
};


// Attach click event handler to continue button.
var elm = document.getElementById(Topspin.BundleHelper.continueButtonName);
if (elm)
{
	YAHOO.util.Event.addListener(elm, "click", Topspin.BundleHelper.buyContinue);
	elm = null;
}