//Credential for Saysme API
var credential = '0ec4c274b60eaf53c0570bed9a2e655d';
//Base URL for the api
var api_path_remote = "https://api.saysme.tv/";
//Addition. bsmith 11/26/2008 15:57:51 Array to store all networks and dayparts which have spotbuys
var networkdaypart = new Array();
//Addition. bsmith 11/26/2008 16:01:28 Store the current total price
var totalPrice = 0;

//Addition. bsmith 11/25/2008 13:19:34 Get these from the session variable SES_CORP
//Addition. bsmith 11/25/2008 13:16:29 Store the movie id
//var movie_id = 169;
//Addition. bsmith 11/25/2008 13:19:19 Store the zipcode
var zipcode = 0;

var spotbuySelectboxOptions = '<option value="0">0</option>'
								+ '<option value="1">1</option>'
								+ '<option value="2">2</option>'
								+ '<option value="3">3</option>'
								+ '<option value="4">4</option>'
								+ '<option value="5">5</option>'
								+ '<option value="6">6</option>'
								+ '<option value="7">7</option>'
								+ '<option value="8">8</option>'
								+ '<option value="9">9</option>'
								+ '<option value="10">10</option>';

function setZip(zip)
{
	this.zipcode = zip;
}

function redeemUpdateOnLoad(path_remote, zip)
{
	setPathRemote(path_remote);
	playFirstVideo();
	loadNetworks(zip);
}

function loadNetworks(zip)
{
	var discount = null;
	
	setZip(zip);
	
	if(this.zipcode > 0)
	{
		var movie_id = $('#movieSelectOne option:selected').val();
		
		
		//Addition. bsmith 11/25/2008 13:24:43 SaysMe API docs say it's "prices", but in the url it's "price"
		$.getJSON(api_path_remote + credential + "/price/zip/" + zip + "/ad/" + movie_id + "?format=jsonp&jsonpcallback=?", function(data)
			{
				if(data.error || data.count < 1)
				{
					alert("We will support this market soon");
				}
				else
				{
					var tableString = '';
					var rowString = '';
					var insertedCount = 0;
					$.each(data.networks, function(i, network)
						{
							var count = 0;
							if(insertedCount == 0)
							{
								rowString = '<tr id="multiNetworkRow"><td id="networkColumn" class="TDleft TDtop">';
							}
							else
							if((insertedCount % 4) == 0)
							{
								rowString = '<tr id="multiNetworkRow"><td id="networkColumn" class="TDleft">';
							}
							else
							if(insertedCount > 0 && insertedCount < 4)
							{
								rowString = '<td id="networkColumn" class="TDtop">';
							}
							else
							{
								rowString = '<td id="networkColumn">';
							}
							
							if(network.dayparts)
							{
								var networkString = '<div id="superDiv"><div id="daypartTableDiv"><table>';
								$.each(network.dayparts, function(j, daypart)
									{
										if(j > 0)
										{
											networkString += '</tr>';
										}
										
										//Addition. bsmith 11/25/2008 18:57:54 Change the times from #:## format to #
										var start = daypart.daypart_start.substring(0, daypart.daypart_start.indexOf(':')) + ' ' + daypart.daypart_start.substring((daypart.daypart_start.length-1), daypart.daypart_start.length);
										var end = daypart.daypart_end.substring(0, daypart.daypart_end.indexOf(':')) + ' ' + daypart.daypart_end.substring((daypart.daypart_end.length-1), daypart.daypart_end.length);
										//var networkName = network.network_name.replace(/\s+/g, "").replace(/\&amp;/, "")
										//var daypartName = daypart.daypart_name.replace(/\s+/g, "").replace(/\&amp;/, "");
										if(discount)
										{
											var price = daypart.price * discount;
											//Addition. bsmith 12/3/2008 13:44:03 Round the prices to whole numbers
											price = price.toFixed(0);
										}
										else
										{
											var price = daypart.price;
										}
										
										networkString += '<tr id="singleNetworkRow"><td id="daypartColumn">' + start + ' - ' + end + '</td>'
														+ '<td id="priceColumn">$' + price + '</td>'
														+ '<td id="quantityColumn">'
														+ '<select name="' + network.network_id + '.' + daypart.daypart_id + '" id="' + network.network_id + '.' + daypart.daypart_id + '" onchange="changeSpotbuys(' + network.network_id + ', ' + daypart.daypart_id + ');">'
														+ spotbuySelectboxOptions + '</select></td>';
										
										var current = new Array();
										//current['network_name'] = networkName;
										//current['daypart_name'] = daypartName;
										current['network_id'] = network.network_id;
										current['daypart_id'] = daypart.daypart_id;
										current['spotbuy_price'] = price;
										current['number_spots'] = 0;
										networkdaypart[current['network_id'] + '.' + current['daypart_id']] = current;
										
										count = j;
									});
									
								if(count != 0)
								{
									countInt = count + 1;
									networkString += '</tr></table></div><div id="networkIcon"><img src="' + network.thumbnail_path_small + '" alt="' + network.network_name + '" /></div></div>';
									rowString += networkString;
								}
							}
							
							if(insertedCount != 0 && ((insertedCount % 4) == 3))
							{
								rowString += '</td></tr>';
							}
							else
							{
								rowString += '</td>';
							}
							
							if(count != 0)
							{
								insertedCount++;
								tableString += rowString;
							}
						});
					
					if(insertedCount > 0 && (insertedCount % 4) == 1)
					{
						tableString += '<td id="networkColumn" class="hideMe">&nbsp;</td><td id="networkColumn" class="hideMe">&nbsp;</td><td id="networkColumn" class="hideMe">&nbsp;</td></tr>';
					}
					else
					if(insertedCount > 0 && (insertedCount % 4) == 2)
					{
						tableString += '<td id="networkColumn" class="hideMe">&nbsp;</td><td id="networkColumn" class="hideMe">&nbsp;</td></tr>';
					}
					else
					if(insertedCount > 0 && (insertedCount % 4) == 3)
					{
						tableString += '<td id="networkColumn" class="hideMe">&nbsp;</td></tr>';
					}
					
					$('#networksTable').html(tableString);
				}
			});
	}
}

//Addition. bsmith 11/26/2008 15:18:51 Function to update the total price of the order
//and to store the networks and dayparts chosen
function changeSpotbuys(network, daypart)
{
	var numberOfSpots = document.getElementById(network + '.' + daypart)[document.getElementById(network + '.' + daypart).selectedIndex].value;
	
	if(numberOfSpots == 0)
	{
		if(networkdaypart[network + '.' + daypart]['number_spots'] != 0)
		{
			var priceToRemove = networkdaypart[network + '.' + daypart]['number_spots'] * networkdaypart[network + '.' + daypart]['spotbuy_price'];
			totalPrice -= priceToRemove;
		}
		
		networkdaypart[network + '.' + daypart]['number_spots'] = numberOfSpots;
		
		$('#total_price').val(totalPrice);
		$('#totalPrice').html('Total: $' + totalPrice);
	}
	else
	if(networkdaypart[network + '.' + daypart]['number_spots'] != numberOfSpots)
	{
		if((networkdaypart[network + '.' + daypart]['number_spots'] != 0) && (numberOfSpots != 0))
		{
			var priceToRemove = networkdaypart[network + '.' + daypart]['number_spots'] * networkdaypart[network + '.' + daypart]['spotbuy_price'];
			totalPrice -= priceToRemove;
			var currentTotal = networkdaypart[network + '.' + daypart]['spotbuy_price'] * numberOfSpots;
			totalPrice += currentTotal;
			
			networkdaypart[network + '.' + daypart]['number_spots'] = numberOfSpots;
			
			$('#total_price').val(totalPrice);
			$('#totalPrice').html('Total: $' + totalPrice);
		}
		else
		if((networkdaypart[network + '.' + daypart]['number_spots'] == 0) && (numberOfSpots != 0))
		{
			var currentTotal = networkdaypart[network + '.' + daypart]['spotbuy_price'] * numberOfSpots;
			totalPrice += currentTotal;
			
			networkdaypart[network + '.' + daypart]['number_spots'] = numberOfSpots;
			
			$('#total_price').val(totalPrice);
			$('#totalPrice').html('Total: $' + totalPrice);
		}
		else
		{
			var priceToRemove = networkdaypart[network + '.' + daypart]['number_spots'] * networkdaypart[network + '.' + daypart]['spotbuy_price'];
			totalPrice -= priceToRemove;
			
			$('#total_price').val(totalPrice);
			$('#totalPrice').html('Total: $' + totalPrice);
		}
	}
}

function setSessionSpotbuys()
{
	var paramString = '';
	var j = 0;
	for(var i in networkdaypart)
	{
		if(networkdaypart[i]['number_spots'] > 0)
		{
			//alert(i);
			paramString += '&network_id_' + j + '=' + networkdaypart[i]['network_id'] + '&daypart_id_' + j + '=' + networkdaypart[i]['daypart_id'] +
							'&spotbuy_price_' + j + '=' + networkdaypart[i]['spotbuy_price'] + '&spot_count_' + j + '=' + networkdaypart[i]['number_spots'];
			
			j++;
		}
	}
	
	var totalParam = 'zipcode=' + zipcode + '&ndpart_count=' + j + paramString;
	
	//alert(totalParam);
	$.ajax({
			type: 'POST',
			url: '/pooling/setredeemspotbuys/',
			data: totalParam,
			success: function(){ $('form:first').submit(); }
		});
}
