document.observe("dom:loaded", function(){

	$('VODCheckout').observe('click', VODCheckout);
	
	if($('VODOrder') != null)
		$('VODOrder').observe('click', function(){
			
			new Ajax.Request('/shared/vod_xhr.php', {
				postBody: 'EpisodeID=' + $('EpisodeID').value + '&cmd=AddEpisodeToCart',
				onSuccess: function(transport) {
					LoadCartListing();
				}
			});
			
		});
	
	LoadCartListing();
	
});


//----------------------------------------------------------------------------------------------------------------------

function SubmitVODOrder(Form){
	new Ajax.Request('/shared/vod_xhr.php', {
			postBody: 'cmd=OrderVODs',
			onSuccess: function(transport) {
				Response = transport.responseJSON;
				$('pi_code').value = Response.Code;
				$('VODList').value = Response.VODList;
				$('VODEmailPopUpForm').submit();
			}
		});
}

//----------------------------------------------------------------------------------------------------------------------

function CloseVODPopup(){
	$('Blackout').remove();
	$('VODEmailPopUp').remove();
}

//----------------------------------------------------------------------------------------------------------------------

function BuildVODPopUp(){
	
	var PopUpDiv = Builder.node('div',{id: 'VODEmailPopUp'},
		PopUpForm = Builder.node('form',{id: 'VODEmailPopUpForm', action: 'https://wnu.com/secure/fpost.cgi', method: 'get'},
			[
				new Element('input',{id: 'pi_code', type: 'hidden', name: 'pi_code'}),
				new Element('input',{id: 'VODList', type: 'hidden', name: 'x_VODList'}),
				new Element('input',{type: 'hidden', name: 'co_code', value: '33m'}),
				new Element('input',{type: 'hidden', name: 'reseller', value: 'a'}),
				new Element('input',{type: 'hidden', name: 'response_post', value: '1'}),
				new Element('input',{type: 'hidden', name: 'no_userpass', value: '1'}),
				new Element('label').update('Email address for VOD delivery instructions:'),
				new Element('br'),
				VODEmail = new Element('input',{type: 'text', name: 'VODEmail', id: 'VODEmail'}),
				new Element('br'),
				new Element('input',{type: 'submit', value: 'Order'}),
				CancelButton = new Element('input',{type: 'button', value: 'Cancel'}),
			]
		)
	)
	
	CancelButton.observe('click', CloseVODPopup);
	
	PopUpForm.observe('submit', function(event){
			Event.stop(event);
			SubmitVODOrder();
		}
	);
	
	VODEmail.select();
	
	document.body.appendChild(PopUpDiv);
	
	PopUpDiv.style.left = (document.viewport.getDimensions().width / 2) - (PopUpDiv.getDimensions().width / 2) + 'px';
}

//----------------------------------------------------------------------------------------------------------------------

function VODCheckout(){

	document.body.appendChild(new Element('div',{id:'Blackout'}).setOpacity(0.68));
	BuildVODPopUp();	
}

//----------------------------------------------------------------------------------------------------------------------

function LoadCartListing(){

	$('VODCartListing').childElements().each(function(item){ item.remove() });
	
	new Ajax.Request('/shared/vod_xhr.php', {
		postBody: 'cmd=ListEpisodesInCart',
		onSuccess: function(transport) {
			Cart = transport.responseJSON.Cart
			
			if(Cart.length)
				$('VODs').show();
			else
				$('VODs').hide();
			
			
			Cart.each(function(n){
				var DeleteImg = new Element('img',{src: '/images/remove.png'});
				DeleteImg.observe('click', function(){DeleteVODFromList(this)});
				$('VODCartListing').appendChild(P = new Element('p',{id: 'CartList_' + n.EpisodeID}).update(n.Title));
				P.appendChild(DeleteImg);
			})
		}
	});

}

//----------------------------------------------------------------------------------------------------------------------

function DeleteVODFromList(Img){
	
	var EpisodeID = Img.parentNode.id.split('_')[1]

	new Ajax.Request('/shared/vod_xhr.php', {
		postBody: 'EpisodeID=' + EpisodeID + '&cmd=RemoveEpisodeFromCart',
		onSuccess: function(transport) {
			
			if(transport.responseJSON.Success) Img.parentNode.remove();
			if(!transport.responseJSON.CartCount) $('VODs').hide();
			
		}
	});

}