// Pricing Index: Inner Array: Min GB, Max GB, Price Per GB
var priceArray = new Array(
	new Array(0,1,10.00),
	new Array(1,5,7.00),
	new Array(5,10,5.00),
	new Array(10,15,4.50),
	new Array(15,24,4.50),
	new Array(24,74,4.13),
	new Array(74,99,3.85),
	new Array(99,199,3.34),
	new Array(199,299,3.00),
	new Array(299,399,2.71),
	new Array(399,499,2.43),
	new Array(499,599,2.19),
	new Array(599,699,1.97),
	new Array(699,799,1.77),
	new Array(799,899,1.60),
	new Array(899,999,1.44),
	new Array(999,1099,1.38),
	new Array(1099,0,0)
);

// Accept Days (d), Minutes (m), Viewers (v) and Bitrate (b). Update calculated bandwidth (if DOM present), and return bandwidth in any case.

function updateBandwidth(d,m,v,b)
{
	var mpi;
	var splan;
	var daysVar = 30;
	var link = $("#startLink").val();
	var usageType = $("#usageType").val();
	var mediaType = $("#mediaType").val();
	var service = $("#service").val();

	// Monthly Usage
	if ($("#usageType").val() == 1)
	{
		// Set to average days/month per year.
		d = 365/12;
		splan = "Monthly";
	}

	//var result = (b*m)*v*d;
	//result = (result * 10) / 1024;
	var result = m * (((b * 7.5) / 1024) / 1024);
	result = result * d * v;

	// Less than 1 GB, More than 0
	if (result > 0 && result < 1)
	{
		result = 1;
	}

	// More than 1 GB
	else
	{
		result = Math.round(result * 100) / 100;
	}

	// If One Time Event
	if (usageType == 2)
	{
		mpi = "One Time Event";
		splan = "One Time Event";
		daysVar = d;
	}

	// If Monthly, change MPI based on service (live, both = live).
	else if (service == "Live" || service == "Both")
	{
		mpi = "Live Streaming";
	}

	// If On-Demand
	else if (service == "On-Demand")
	{
		mpi = "On-Demand Streaming";
	}

	// If Video
	else if (mediaType == "Video")
	{
		mpi = "Streaming Video";
	}

	// If Audio
	else if (mediaType == "Audio")
	{
		mpi = "Streaming Audio";
	}

	// Default MPI
	else
	{
		mpi = "Quote";
	}

	var finalUrl = link+"&mpi="+mpi+"&viewers="+v+"&duration="+m+"&bitrate="+b+"&days="+daysVar+"&band="+result+"%20GB&media="+mediaType+"&stype="+service+"&splan="+splan;

	$("#GBresult").text(result);

	// Price base on GB:
	var priceGB = updatePrice(result);

	// If "Spceial Prices"
	if (priceGB == -1)
	{
		//$("#GBprice").text("Special pricing! Contact us for details."); // 10/11/09 - Remove price
	}
	// Price Avaiable
	else
	{
		//$("#GBprice").text("Price: Up to "+priceGB); // 10/11/09 - Remove price
	}

	$(".GBresult a").attr("href", finalUrl);
	$(".GBresult p, .GBresult div").css("display", "block");

	return result;
}

// Accept GB (gb). Update price (if DOM present), and return price in any case.

function updatePrice(gb)
{
	var priceGB;
	var finalPrice;
	var usageType = $("#usageType").val(); // Monthly / One Time Event
	var service = $("#service").val(); // Live, On Demand, Both

	var minGB = priceArray[0][0];
	var maxGB = priceArray[priceArray.length-1][0];

	if (gb > maxGB)
	{
		// No pricing for this amount.
		finalPrice = -1;
	}

	else
	{
		// Search for GB in pricing array.
		for(i=0; i < priceArray.length; i++) {
			if (gb >= priceArray[i][0] && gb <= priceArray[i][1])
			{
				priceGB = priceArray[i][2];
			}
		}

		// Round price to two decimals.
		var priceRounded = (Math.round(priceGB * gb * 100)) / 100;
		finalPrice = "$" + priceRounded;
	}

	return finalPrice;
}

// Create GB Pricing Table.

$(document).ready(function(){
	// If GB Pricing Table Available.
	if ($(".gbPricingTable").length > 0)
	{
		$(".gbPricingTable").ready(function(){

			// Clear No JavaScript Message.
			$(".gbPricingTable tbody").html("");

			jQuery.each(priceArray, function(){
				var minGB = this[0];
				var maxGB = this[1];
				var price = parseFloat(this[2]);

				// No Price Specified (Special Prices)
				if (price == 0)
				{
					$(".gbPricingTable tbody").append("<tr><th>"+minGB+" GB's and over</th> <td>Special Prices</td></tr>");
				}

				// Regular prices
				else
				{
					$(".gbPricingTable tbody").append("<tr><th>"+minGB+" GB's to "+maxGB+" GB's</th> <td>$"+price+"</td></tr>");
				}
			});
		});
	}
});