/* 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";
/*
	This function adds the required DOM elements for the 
	Selection container and selection dropdowns.
*/
Topspin.BundleHelper.domInit = function() {
    /*
<div id="selectioncontainer" style="display:none;">
	<div id="selectioninnercont">
		<div id="sizelabel">Choose your shirt size:</div>
		<div>
			<select id="sel_size" name="sel_size">
				<option value="0">Please select a size</option>
				<option value="1">Men's Small</option>
				<option value="2">Men's Medium</option>						
				<option value="3">Men's Large</option>
				<option value="4">Men's XL</option>
				<option value="5">Women's Small</option>
				<option value="6">Women's Medium</option>						
				<option value="7">Women's Large</option>
			</select>
		</div>
		<div>Choose a file format:</div>
		<div>
			<select id="sel_format" name="sel_format">
				<option value="0">Please select a format</option>
				<option value="1">320 Kbps MP3</option>
				<option value="2">Apple Lossless</option>																												
			</select>
		</div>
		<div id="contBtnContainer">
			<a id="cancelButton" href="javascript:void(0);" onclick="YAHOO.util.Dom.setStyle('selectioncontainer', 'display', 'none'); return false;">Cancel</a>
			<a id="continueButton" href="javascript:void(0);" onclick="Topspin.BundleHelper.buyContinue(''); return false;">Continue</a>
		</div>
	</div>

</div>
*/
    // Selection Container
    var elmSelCont = document.createElement("div");
    elmSelCont.setAttribute("id", "selectioncontainer");
    elmSelCont.setAttribute("style", "display:none;");
    // Selection Inner Container
    var elmInnerCont = document.createElement("div");
    elmInnerCont.setAttribute("id", "selectioninnercont");
    // Size Label
    var elm = document.createElement("div");
    elm.setAttribute("id", "sizelabel");
    var elmText = document.createTextNode("Choose your shirt size:");
    elm.appendChild(elmText);
    elmInnerCont.appendChild(elm);
    // Shirt Selector
    elm = document.createElement("div");
    elm.innerHTML = '\
		<select id="sel_size" name="sel_size">\
			<option value="0">Please select a size</option>\
			<option value="1">Men\'s Small</option>\
			<option value="2">Men\'s Medium</option>\
			<option value="3">Men\'s Large</option>\
			<option value="4">Men\'s XL</option>\
			<option value="5">Women\'s Small</option>\
			<option value="6">Women\'s Medium</option>\
			<option value="7">Women\'s Large</option>\
		</select>\
	';
    elmInnerCont.appendChild(elm);
    // File Label
    var elm = document.createElement("div");
    elm.setAttribute("id", "filelabel");
    var elmText = document.createTextNode("Choose a file format:");
    elm.appendChild(elmText);
    elmInnerCont.appendChild(elm);
    // Shirt Selector
    elm = document.createElement("div");
    elm.innerHTML = '\
		<select id="sel_format" name="sel_format">\
			<option value="0">Please select a format</option>\
			<option value="1">320 Kbps MP3</option>\
			<option value="2">Apple Lossless</option>\
		</select>\
	';
    elmInnerCont.appendChild(elm);
    // Button Container
    var elmBtnCont = document.createElement("div");
    elmBtnCont.setAttribute("id", "contBtnContainer");
    // Cancel Button
    var elmBtnCancel = document.createElement("a");
    elmBtnCancel.setAttribute("id", "cancelButton");
    elmBtnCancel.setAttribute("href", "javascript:void(0);");
    /*
	elm.onclick = "YAHOO.util.Dom.setStyle('selectioncontainer', 'display', 'none'); return false;";
	elm.setAttribute("onclick", "YAHOO.util.Dom.setStyle('selectioncontainer', 'display', 'none'); return false;");
	*/
    elmText = document.createTextNode("Cancel");
    elmBtnCancel.appendChild(elmText);
    elmBtnCont.appendChild(elmBtnCancel);
    // Continue Button
    var elmBtnContinue = document.createElement("a");
    elmBtnContinue.setAttribute("id", "continueButton");
    elmBtnContinue.setAttribute("href", "javascript:void(0);");
    elmText = document.createTextNode("Continue");
    elmBtnContinue.appendChild(elmText);
    elmBtnCont.appendChild(elmBtnContinue);
    elmInnerCont.appendChild(elmBtnCont);
    elmSelCont.appendChild(elmInnerCont);
    var elmBody = document.body;
    elmBody.appendChild(elmSelCont);
    // Attach click event handler to cancel button.
    YAHOO.util.Event.addListener(elmBtnCancel, "click", Topspin.BundleHelper.hideSelections);
    // Attach click event handler to continue button.
    YAHOO.util.Event.addListener(elmBtnContinue, "click", Topspin.BundleHelper.buyContinue);
    // Clean up
    elm = null;
    elmText = null;
    elmBtnContinue = null;
    elmBtnCont = null;
    elmInnerCont = null;
    elmSelCont = null;
    elmBody = null;
};
/*	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;
};
Topspin.BundleHelper.hideSelections = function() {
    YAHOO.util.Dom.setStyle('selectioncontainer', 'display', 'none');
    return false;
};
/*  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 "deluxe":
				bFiles = true;
				var bShirt = true;
				break;
			default:
				bFiles = false;
				bShirt = false;
        }
        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 file 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("filelabel", "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");
        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 == "digital"
		|| productType == "prem_digital"
		|| productType == "dvd")
	{
		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 "digital":
                    {
                        aBundles = [{ bundleID: 36737,
                            campaignID: 10018624,
                            name: "Digital"
                        }];
                        break;
                    }
                    case "prem_digital":
                    {
                        aBundles = [{ bundleID: 36738,
                            campaignID: 10018625,
                            name: "Premium Digital"
                        }];
                        break;
                    }
                    case "dvd":
                    {
                        aBundles = [{ bundleID: 36739,
                            campaignID: 10018626,
                            name: "DVD + Premium Digital"
                        }];
                        break;
                    }
                    case "deluxe":
                    {
                        aBundles = [[{ bundleID: 36754,
                            campaignID: 10018635,
                            name: "Deluxe, MP3, Men's Small Shirt"
                        },
                        { bundleID: 36766,
                            campaignID: 10018639,
                            name: "Deluxe, Apple Lossless, Men's Small Shirt"
                        }],
                        [{ bundleID: 36768,
                            campaignID: 10018641,
                            name: "Deluxe, MP3, Men's Medium Shirt"
                        },
                        { bundleID: 36769,
                            campaignID: 10018642,
                            name: "Deluxe, Apple Lossless, Men's Medium Shirt"
                        }],
                        [{ bundleID: 36770,
                            campaignID: 10018644,
                            name: "Deluxe, MP3, Men's Large Shirt"
                        },
                        { bundleID: 36771,
                            campaignID: 10018645,
                            name: "Deluxe, Apple Lossless, Men's Large Shirt"
                        }],
                        [{ bundleID: 36772,
                            campaignID: 10018646,
                            name: "Deluxe, MP3, Men's XL Shirt"
                        },
                        { bundleID: 36773,
                            campaignID: 10018647,
                            name: "Deluxe, Apple Lossless, Men's XL Shirt"
                        }],
                        [{ bundleID: 36743,
                            campaignID: 10018627,
                            name: "Deluxe, MP3, Women's Small Shirt"
                        },
                        { bundleID: 36745,
                            campaignID: 10018629,
                            name: "Deluxe, Apple Lossless, Women's Small Shirt"
                        }],
                        [{ bundleID: 36748,
                            campaignID: 10018630,
                            name: "Deluxe, MP3, Women's Medium Shirt"
                        },
                        { bundleID: 36750,
                            campaignID: 10018631,
                            name: "Deluxe, Apple Lossless, Women's Medium Shirt"
                        }],
                        [{ bundleID: 36751,
                            campaignID: 10018632,
                            name: "Deluxe, MP3, Women's Large Shirt"
                        },
                        { bundleID: 36753,
                            campaignID: 10018634,
                            name: "Deluxe, Apple Lossless, Women's Large Shirt"
                        }]];
                        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") {
                    // FOR DEBUGGING ONLY
                    //alert(bundle.name + "\n" + Topspin.BundleHelper.artistID + "\n" + bundle.bundleID + "\n" + bundle.campaignID)
                    // 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;
};
Topspin.BundleHelper.domInit();