/**
 * File: http://demo.facilitywarehouse.com/gallery/js/scripts.js
 */

var products = new Hash();
var popup;
var collPopup;

function removeProductFromCollection(pid, cid) {
	var ajax = new Ajax('/assets/ajax/gallery/remove_product_from_collection.php', {
		method: 'post',
		data: Object.toQueryString({
			product_id: pid,
			collection_id: cid
		}),
		onComplete: function() {
			window.location = './';
		}
	}).request();
}

function refreshCollections(collection_id) {
	var ajax = new Ajax('/gallery/js/async/collection.php', {
		method: 'post',
		data: Object.toQueryString({
			action: 'refreshcollections',
			collection_id: collection_id
		}),
		onComplete: function(response) {
			var collections = $E('div.main div.gallery div.lft ul.collections');
			collections.setHTML(response);
			(function(){
				if ($E('div.sidebar ul.collections li.addedto'))
					$E('div.sidebar ul.collections li.addedto').removeClass('addedto');
			}).delay(2500);
		}
	}).request();
}

window.addEvent('domready', function() {
	popup = new Popup();
	collPopup = new AddToCollectionPopup();
	
	$$('div.category').each(function(div) {
		new Category(div);
	});
	
	$$('div.product').each(function(div) {
		var product = new Product(div);
		product.quantityfield = {state: 0, el: $('quantity' + (product.id))};
		products.set(product.id, product);
	});
});

function toggleEmailForm() {}
function sendEmail() {}

function updateDisplayPrice(id, product_id, user_id) {
	var ajax = new Ajax('/gallery/js/async/displayprice.php', {
		method: 'post',
		data: Object.toQueryString({
			'u' : user_id,
			'p' : product_id,
			'price' : $(id).value
		}),
		onComplete: function(response) {
			$(id).value = response;
			$(id).removeClass('changed');
			alert('Display price has been updated.');
		}
	}).request();
}

function hasChanged(el) {
	el = $(el);
	el.addClass('changed');
}