/**
 * File: http://www.facilitywarehouse.com/gallery/js/AddToCollectionPopup.js
 */

var AddToCollectionPopup = new Class({
	
	target: null,
	content: null,
	reference: null,
	orientation: 'left',
	
	initialize: function() {
		this.target = $('collPopup');
		this.target.addEvent('click', function(evt) {
			new Event(evt).stop();
		});
	},
	
	placePopup: function() {
		var mp = $E('div.main').getPosition();
		var rs = this.reference.getSize();
		var rp = this.reference.getPosition();
		var ts = this.target.getSize();
		
		if (this.orientation == 'right')
			var pos = {
				x: rp.x + rs.size.x - ts.size.x - mp.x,
				y: rp.y + rs.size.y - mp.y
			};
		else
			var pos = {
				x: rp.x - mp.x,
				y: rp.y + rs.size.y - mp.y
			};
		
		this.target.setStyles({
			'top': pos.y,
			'left': pos.x,
			'visibility': 'visible'
		});
	},
	
	show: function(el, id, or) {
		window.addEvent('click', this.hide.bind(this));
		this.reference = el;
		this.orientation = or;
		this.hide();
		if (this.target.getStyle('display') == 'none') {
			s = 'action=showaddform&pid=' + id;
			var xhr = new Ajax('/gallery/js/async/collection.php', {
				method: 'post',
				data: s,
				update: this.target,
				onComplete: this.onShowCompleteHandler.bind(this)
			});
			xhr.request();
		}
	},

	onShowCompleteHandler: function(response) {
		this.target.setStyles({
			'display': 'block',
			'visibility': 'hidden'
		});
		this.placePopup();
	},
	
	hide: function() {
		window.removeEvent('click', this.hide);
		//if (this.target.getStyle('display') == 'block') {
			//this.content.empty();
			this.target.setStyles({
				'display': 'none',
				'visibility': 'hidden'
			});
		//}
	},

	addProduct: function(pid) {
		var addtonew = $('pupaddtonewcoll' + pid);
		var addtoexist = $('pupaddtoexistcoll' + pid);
		var collection_id = $('pupaddtocollexist_id' + pid);
		var collection_new = $('pupaddtocollnewname' + pid);
		var quantity = $('pupaddtocollqty' + pid);
		
		var data = new Object();
		data.pid = pid;
		data.action = 'addtocollection';
		data.quantity = quantity.value;
		
		if (addtonew.checked) {
			data.addto = 'new';
			data.collection_name = collection_new.value;
			this.collection_id = data.collection_id = 0;
		} else {
			data.addto = 'exist';

			if (collection_id.options)
				collection_id = collection_id.options[$('pupaddtocollexist_id' + pid).selectedIndex];

			this.collection_id = collection_id.value;
			data.collection_id = collection_id.value;
		}
		
		var data = Object.toQueryString(data);
		this.addProductAjax = new Ajax('/gallery/js/async/collection.php', {
			method: 'post',
			data: data,
			onComplete: this.onAddCompleteHandler.bind(this)
		});
		this.addProductAjax.request();

	},
	
	onAddCompleteHandler: function(response) {
		if (this.addProductAjax.getHeader('Error') == '1')
			this.target.setHTML(response);
		else {
			this.hide();
			refreshCollections(this.collection_id);
		}
	}
});

