var ContactSellerForm = {
	
	count: 0,
	open: false,
	startAnimation: false,
	message: '',
		
	init: function () {
		
		//Support success or error message from serverside
		if (arguments.length) {
			ContactSellerForm.message = arguments[0];
		} 
		
		ContactSellerForm.setOpacityLayer();
		$('main-content').reset();
		$('second-hand-price-list').addClass('script-on');
		$$('span.sec-book-contact').each(function (e) {
			var label = e.getFirst();
			label.set('class', 'unticked');
			label.addEvent('click', function () {
				checkBox = this.getNext();
				receiverId = 'receiver-'+checkBox.value;
				this.set('id', receiverId+'-label');
				if (!checkBox.checked) {
					this.set('class', 'ticked');
					ContactSellerForm.addReceiver(receiverId);
					if (Browser.Engine.name == 'presto' || Browser.Engine.name == 'trident') checkBox.checked = true;
				} else {
					this.set('class', 'unticked');
					ContactSellerForm.removeReceiver(receiverId);
					if (Browser.Engine.name == 'presto' || Browser.Engine.name == 'trident') checkBox.checked = false;
				}
			});
		});
	},

	setOpacityLayer: function () {
		var coordinates = $$('#contact-seller-box fieldset')[0].getParent().getCoordinates();
		var opacityLayer = new Element('div', {
			'styles': {
				'background': '#99C0E1',
//				'background': 'green',
				'height': coordinates.height-20 + 'px',
//				'left': coordinates.left+5 + 'px',
				'left': 5 + 'px',
				'position': 'absolute',
//				'top': coordinates.top+10 + 'px',
				'top': 10 + 'px',
				'width': coordinates.width-10 + 'px',
				'opacity': 0,
				'z-index': 1
			},
			'id': 'opacity-layer'
		}).set('html', '<span>' + ContactSellerForm.message + '</span>');
		opacityLayer.inject($('contact-seller-box'), 'top');
		
		if (ContactSellerForm.startAnimation) {
			new Fx.Tween('opacity-layer', {
				'property': 'opacity',
				'duration': '500'
			}).start(0, 0.9);
		} else {
			opacityLayer.setStyle('opacity', 0.9);
		}
		
		ContactSellerForm.startAnimation = true;
		ContactSellerForm.open = false;
	},
	
	removeOpacityLayer: function () {
		new Fx.Tween('opacity-layer', {
			'property': 'opacity',
			'duration': '500'
		}).start(0.9, 0).addEvent('chainComplete', function () {
			$('opacity-layer').dispose();
		});
		ContactSellerForm.open = true;
	},
	
	addReceiver: function (elementId) {
		if (!ContactSellerForm.open) {
			ContactSellerForm.removeOpacityLayer();
		}
		
		var receiverList = $('contact-seller-list');
		if (!receiverList) {
			receiverList = new Element('ul', {
				'id': 'contact-seller-list'
			}).inject($$('#contact-seller-box h3')[0], 'after');
		}
		
		receiverString = $(elementId + '-place').get('text') + ' - ' + $(elementId + '-condition').get('text') + ' - ' + $(elementId + '-price').get('text');
		receiverListItem = new Element('li', {
			'id': elementId,
			'styles': {
				'cursor': 'pointer',
				'opacity': 0
			}
		}).set('text', receiverString);
		
		receiverListItem.addEvent('click', function () {
			ContactSellerForm.removeReceiver(this.getProperty('id'));
			$(elementId+'-label').set('class', 'unticked').getNext().checked = false;
		});
		receiverListItem.injectTop(receiverList);
		fade(elementId, 'in', 500);
		
		ContactSellerForm.count++;
		$('count-seller').set('text', ContactSellerForm.count);
	},
	
	removeReceiver: function (elementId) {
		ContactSellerForm.count--;
		$('count-seller').set('text', ContactSellerForm.count);
		
		fade(elementId, 'out',500).addEvent('chainComplete', function () {
			$(elementId).dispose();
			
			receiverList = $('contact-seller-list');
			if (!receiverList.getChildren().length) {
				receiverList.dispose();
				ContactSellerForm.setOpacityLayer();
			}
		});
	},
	
	setMessage: function (msg) {
		ContactSellerForm.message = msg;
	}
}