﻿var previousClickedOn = null;

//remove prom popup obj
var RemovePromPopup = {
	el: new Element('span', {'id': 'removePromPopup'}).update('<div id=page>' +
			'<div class="remove_proms popover" style="display: block;">' +
				'<div class="head">' +
					'<a class="lnk_close close_button" href="#"> close <span>x</span></a>' +
					'<h4>Remove</h4></div>' +
				'<div><div class="inner">' +
						'<p>Are you sure you want to remove this item from your Proms Plan?</p></div></div>' +
				'<div class="foot">' +
					'<a id="A1" class="button bg910101" title="Yes,remove this Prom from my Plan" href="javascript:RemovePromPopup.confirmRemove();">Yes, remove</a>' +
				'</div></div>'),
	bgEl: new Element('div', {'class': 'popup-background'}),
	originButton: null,
	offsetTop:0,
	offsetLeft:0,
	isDisplayed: false,
	isDetailsPopupOnPage: false,
	perfId: null,
	id: null,
	promTitle:null,
	injectRemovedMessageAfter:null,
	injectRemovedMessageBefore:null,
	removeCompleteAction:'revertButton',
	redirUrl:null,
	removedMessageStyle:null,
	show: function(event, fromPopup){
	    var element = Event.element(event);	     
	    previousClickedOn = element;
		var pop = RemovePromPopup;
		if (pop.isDisplayed) pop.close(event);
		pop.bgEl.observe('click', pop.close);
		event.stop();
		if (pop.isDetailsPopupOnPage)
			PromDetailPopup.remove();
		if (!fromPopup)
			pop.originButton = Event.element(event);
		if (pop.originButton.tagName.toLowerCase() != 'a')
			pop.originButton = pop.originButton.parentNode;
		if (pop.originButton.readAttribute('alreadyRemoved') != null){
			location.href = pop.originButton.readAttribute('href');
			return;
		}
		pop.id = pop.originButton.readAttribute("promid");
		pop.perfId = pop.originButton.readAttribute("perfid");
		
		$('wrapper').insert({bottom: pop.el}); 
		$('wrapper').insert({bottom: pop.bgEl}); 
        var bodyW = $(document).viewport.getWidth();
        var top = pop.offsetTop + pop.originButton.cumulativeOffset().top - 55;
        var left = (bodyW / 2) - 266 + pop.offsetLeft;
        pop.el.setStyle({
			top: (top - document.viewport.getScrollOffsets().top) + 'px',
			left: left + 'px',
			display: 'block',
			position:'fixed'
		});
		pop.bgEl.setStyle({
			top: '0px',
			display:'block', 
			position:'fixed'
		});
		pop.el.getElementsBySelector('.close_button').first().writeAttribute("title","Click here to cancel the request to remove the Prom").observe('click', pop.close);
		pop.isDisplayed = true;
	},
	close: function(event){
	    previousClickedOn.focus();
		if (event) event.stop();
		var pop = RemovePromPopup;
		pop.el.setStyle({'display': 'none'});
		pop.bgEl.setStyle({'display': 'none'});
		pop.isDisplayed = false;
		
	},
	confirmRemove: function(){ // after user clicks yes, remove
		var pop = RemovePromPopup;
		new Ajax.Request("/svc/proms.asmx/RemoveProm", {
			method: 'get',
			parameters: { id: parseInt(pop.perfId) },
			onSuccess: function(xhr, json){  
				var pop = RemovePromPopup;
				switch(pop.removeCompleteAction){
					case 'revertButton' :
						pop.originButton.writeAttribute('href', 
							'/tickets/reserveproms.aspx?perfid=' + pop.id
						);
						pop.originButton.innerHTML = 'Add to my Proms Plan';
						pop.originButton.removeClassName('bg666666');
						pop.originButton.addClassName('bg910101');
						pop.originButton.stopObserving('click', RemovePromPopup.show);
						pop.originButton.stopObserving('click', pop.show);
						pop.originButton.writeAttribute('alreadyRemoved', 'true');
						//essentially we change this
						//<a href="../account/promsremove.aspx?id=302&amp;perf=14444&amp;action=prompt&amp;return=browse" class="button bg666666">Remove from my Proms Plan</a>
						
						//to this
						//<a href="reserveproms.aspx?perfid=14443" class="button bg910101">Add to my Proms Plan</a>
						break;
					case 'redir' :
						location.href = pop.redirUrl.evaluate({id:pop.id});
						break;
					case 'reload' :
						location.reload();
						break;
				}
				if (pop.injectRemovedMessageAfter != null || pop.injectRemovedMessageBefore != null){
					var removedHTML = new Template('<div class="upper"></div>' +
						'<div class="inner">#{promTitle} has been removed from your Plan.<br /><strong>Your Plan has been automatically saved and you can now browse for more Proms or log out. Please note you will not receive an email confirmation of your Plan. You can login any time up to midnight on Saturday 7 May to view or amend you plan. You must return to this site from 9.00am on Saturday 7 May to submit your plan and make your booking.</strong></div><div class="lower"></div><br />');
					if ($$('div.carttimer').length == 1)
						$$('div.carttimer').first().update(removedHTML.evaluate({promTitle:pop.promTitle}));
					else{
						var messageDiv = new Element('div', {'class':'carttimer'}).update(removedHTML.evaluate({promTitle:pop.promTitle}));
						if (pop.injectRemovedMessageAfter != null)
							RemovePromPopup.injectRemovedMessageAfter.insert({after: messageDiv});
						else
							RemovePromPopup.injectRemovedMessageBefore.insert({before: messageDiv});
					}
					if (RemovePromPopup.removedMessageStyle != null)
						$$('div.carttimer').first().setStyle(RemovePromPopup.removedMessageStyle);
				}
				
				pop.close(false);
			}
		});
	}
};
