function cartAdd()
{
	try {
		this.bs = null;
		this.prfData = null;
		this.info = null;
		
		// values
		this.id = null;
		this.type = null;
		this.lastType = null;
		this.lastSize = null;
		this.lastOption = null;
		this.position = null;
		
		// signals
		this.initialized = false;
		this.busy = false;

		this.baseUrl = '';
		this.popup = false;

		// div/util stuff
		this.div = this.divTop = this.divMain = this.divMsg
			 = this.divContent = this.divBusy = this.imgThumb
			 = this.fObj = null;
		this.TM = null;
		
	}
	catch(e) { alert(e); }
	
	this.init = function(baseUrl, popup)
	{
		try {
			this.bs = new BSApi();
			this.div = getDivObj('cartAdd');
			this.divTop = getDivObj('cartAddTop');
			this.divMain = getDivObj('cartAddMain');
			this.divMsg = getDivObj('cartAddMsg');
			this.divContent = getDivObj('cartAddContent');
			this.divBusy = getDivObj('cartAddBusy');
			this.imgThumb = getDivObj('cartAddThumb');
			this.fObj = getDivObj('cartAddForm');
			this.TM = ToolMan.coordinates();
			
			// IE hack only
			this.mask = (document.all) ? document.getElementById('cartAddMask') : null;
			
			this.baseUrl = baseUrl;
			this.popup = popup;

			this.initialized = true;
		}
		catch(e) { alert(e); }
	}
	
	this.checkInit = function()
	{
		if (!this.initialized) {
			alert("Please wait a moment until the page has finished loading.");
			return false;
		}
		
		return true;
	}
		
	this.imgPrcGetRdc = function(a)
	{
		var                     t, type;
		var			prf_types = new Array('pr', 'rf', 'rm');
		a = arrRdc(a, 3);

		for (var h = 0; h < prf_types.length; h++) {
			type = prf_types[h];
			if (!isset(a[type]) || !isset(a[type]['PROFILE'])) continue;
			for (var i=0, t=a[type]["PROFILE"]; i<t.length; i++) {
				var opt = null;
				if (isset(t[i]['OPTIONS'])) {
					opt = t[i]['OPTIONS'];
					for (var j in opt) arrRdc(opt[j]);
				}
				arrRdc(t[i]);
				t[i]['OPTIONS'] = opt;
			}
		}
	
		if (isset(a["info"])) a["info"] = arrRdc(a["info"]);

		return a;
	}
	
	this.setId = function(id) { this.id = id; }
	
	this.setContent = function(msg, str)
	{
		this.setBusy(false);
		this.divMsg.innerHTML = msg;
		this.divContent.innerHTML = str + '<div id="cartAddFooter"><a href="javascript:CA.hide();"><img src="/img/pixel.gif" width="55" height="16" border="0"></a></div>';
		this.redraw();
	}
	
	this.setThumb = function(clear)
	{
		if (clear) {
			this.imgThumb.src = '/img/pixel.gif';
			return;
		}
		
		if (!this.checkInit() || !this.info) return;
		this.imgThumb.src = '/img-get/' + this.id + '/t';	
	}
	
	this.redraw = function()
	{
		this.divTop.style.height = 'auto';	
		this.div.style.visibility = 'hidden';

		if (this.divTop.offsetHeight < this.imgThumb.height)
			this.divTop.style.height = (this.imgThumb.height + 2) + 'px';

		setTimeout('CA.redrawCb()', 100);
	}

	this.redrawCb = function()
	{
		this.div.style.height = (this.divMain.offsetHeight + this.divTop.offsetHeight + 15).toString() + 'px';
		this.div.style.visibility = 'visible';
		
		// IE hack
		if (!this.mask) return;
		this.mask.style.height = this.div.offsetHeight + 'px';
		this.mask.style.width = this.div.offsetWidth + 'px';
		this.mask.style.top = this.div.style.top;
		this.mask.style.left = this.div.style.left;
		this.mask.style.visibility = 'visible';
	}
	
	this.operate = function()
	{
		if (!this.checkInit()) return;
		
		try {
			if (!this.prfData)
				this.getPrf();
			else if (!this.type && !this.setPrfType())
				this.showPrfTypes();
			else if (!this.setPrfSize())
				this.showPrfSizes();
		}
		catch(e) {
			switch (e) {
			case 'HALT':
				return;
			default: throw(e);
			}
		}
	}
		
	this.show = function(id, type)
	{
		if (!this.checkInit() || this.busy) return;
		
		if (id != this.id) {
			this.reset();
			this.setId(id);
			this.setThumb(true);
		}

		this.type = null;
		this.lastType = type;

		this.operate();
	}
	
	this.showLink = function(id, type, obj)
	{
		if (this.popup) {
			if (!type) type = '';
			popupWin(this.baseUrl + '/cart/cartAdd/' + id + '/' + type, 500, 350);
			return;
		}
		
		if (obj) {
			this.position = this.TM.topLeftOffset(obj);

			x = this.position.x - 250;
			if (x < 20) x = 20;
			
			this.div.style.left = x + 'px';
			this.div.style.top = (this.position.y - 50) + 'px';
		}

		this.show(id, type);
	}

	this.getPrf = function()
	{
		if (!this.id || this.busy) return;
	
		this.setBusy(true);

		try {
			this.bs.open('/ajax/cartAdd/' + this.id, this.getPrfCb.bind(this));
		}
		catch (e) {
			alert(e);
			this.cancel();
		}
	}

	this.getPrfCb = function(data)
	{
		if (!this.busy) return;
		
		try {
			this.prfData = this.imgPrcGetRdc(this.bs.responseParse(data));
			this.info = this.prfData['info'];
			delete(this.prfData['info']);
			this.setThumb();
			this.operate();
		}
		catch(e) {
			alert(e);
			this.cancel();
		}
	}
	
	this.showPrfTypes = function()
	{
		if (!this.prfData) {
			this.operate();
			return;
		}

		var			exists = false;
		
		try {
			var str = '<h3>Available purchase options:</h3><ul class="batch">';
			for (var i in this.prfData) {
				if (!isset(this.prfData[i]['PROFILE'])) continue;
				exists = true;
				switch (i) {
				case 'rf': name = 'Royalty-Free Online Download'; break;
				case 'pr': name = 'Prints'; break;
				case 'rm': name = 'Rights-managed Purchase'; break;
				default: continue;
				}
				str += '<li><a href="javascript:CA.setPrfTypeLink(\'' + i + '\')">' + name + '</a></li>';
			}
			str += '</ul>';
			if (!exists) throw 'empty';
		}
		catch(e) {
			var str = '<h3>This image has not been made available for purchase.</h3>';
		}
		
		this.setContent(str, '');
	}

	this.setPrfType = function()
	{
		if (!isset(this.prfData[this.lastType]))
			this.lastType = null;
		
		if (!this.lastType)
			return false;
		
		if (this.lastType == 'rm') {
			var url = this.baseUrl + '/cart/cart-img-rights-form?PRF_TYPE=rm&I_ID=' + this.id;
			if (this.popup) window.opener.top.location = url;
			else window.location = url;

			this.cancel();
			throw('HALT');
		}

		this.type = this.lastType;
		this.lastType = null;
		return true;
	}
	
	this.setPrfTypeLink = function(type)
	{
		this.type = null;
		this.lastType = type;
		this.operate();
	}
	
	this.showPrfSizes = function()
	{
		var			str = '', msg = '';

		if (!isset(this.prfData[this.type]['PROFILE'])) {
			msg = "Sorry, this image is not currently available in this format.";
			msg += '<div style="height:20px"></div>';
			this.setContent(msg, str);
			/*
			this.type = null;
			this.operate();
			*/
			return;
		}
		
		var			profile = this.prfData[this.type];
		var			checked = (profile['PROFILE'].length == '1') ?
						' checked' : '';

		if (isset(profile['PRF_LDESC']) && profile['PRF_LDESC'] != '')
			msg += profile['PRF_LDESC'] + '<br><br>';		

		switch (this.type) {
		case 'rf':
			msg += 'This image is available in the following sizes for download. The pixel dimension given is the longest dimension of the image. All files are delivered as high-quality JPEGs.<br><br>';
			msg += 'Original file dimensions: <b>' + this.info['I_WIDTH'] + 'x' + this.info['I_HEIGHT'];
			str += '<b>Select an image size:</b><br>';
			str += '<div class="cartAddSelectHead"><div style="width:30px">&nbsp;</div>';
			str += '<div style="width:250px">Size</div>';
			str += '<div style="width:80px">Price</div></div>';
			str += '<ul class="cartAddSelect">';
			for (var i in profile['PROFILE']) {
				var		v = profile['PROFILE'][i];
				str += '<li><div style="width:30px"><input type="radio" name="PROFILE" value="' + i + '"' + checked + '></div>';
				str += '<div style="width:250px">' + v['NAME'] + '</div>';
				str += '<div style="width:80px">' + this.info['CURRENCY'] + v['PRICE'] + '</div>';
				str += '</li>';
			}
			str += '</ul>';
			break;
		case 'pr':
			if (msg == '')
				msg += 'This image is available in the print sizes and finishes shown below.';
			str += '<b>Select an image size and finish option:</b><br>';
			str += '<div class="cartAddSelectHead"><div style="width:30px">&nbsp;</div>';
			str += '<div style="width:180px">Description</div>';
			str += '<div style="width:100px">Finish</div>';
			str += '<div style="width:80px">Price</div></div>';
			str += '<ul class="cartAddSelect">';
			for (var i in profile['PROFILE']) {
				var		v = profile['PROFILE'][i];
				str += '<li><div style="width:30px"><input type="radio" name="PROFILE" value="' + i + '"' + checked + '></div>';
				str += '<div style="width:180px">' + v['NAME_HTML'] + '</div>';
				str += '<div style="width:100px">';
				if (v['OPTIONS'] && v['OPTIONS'].length) {
					if (v['OPTIONS'].length > 1) {
						str += '<select name="OPTIONS' + i + '" class="btext" onChange="CA.profileSelect(' + i + ');">';
						for (var j in v['OPTIONS']) {
							var opt = v['OPTIONS'][j];
							str += '<option value="' + j + '">';
							str += opt['NAME'] + '</option>\n';
						}
						str += '</select>';
					} else {
						str += v['OPTIONS'][0]['NAME'];
						str += '<input type="hidden" name="OPTIONS' + i + '" value="0">';
					}
				} else str += '<span style="color:#999">n/a</span>';
				str += '</div>';
				str += '<div style="width:80px">' + this.info['CURRENCY'] + v['PRICE'] + '</div>';
				str += '</li>';
			}
			str += '</ul>';
			break;
		default:
			this.type = null;
			this.operate();
			return;
		}
		
		str += '<div style="clear:both;height:5px"></div><div id="cartAddButton"><a href="javascript:CA.setPrfSizeAct();"><img src="/img/pixel.gif" width="120" height="25" border="0" alt="Add to Cart"></a></div>';

		this.setContent(msg, str);
	}
	
	this.setPrfSize = function()
	{
		if (!isset(this.prfData[this.type]))
			this.type = null;
		
		if (!isset(this.prfData[this.type]['PROFILE']) ||
		    !isset(this.prfData[this.type]['PROFILE'][this.lastSize]))
			this.lastSize = null;
		
		if (!this.type || !this.lastSize)
			return false;
		
		var		profile = this.prfData[this.type];
		var		item = profile['PROFILE'][this.lastSize];
		var		url;
		
		// determine ref url
		try {
			if (this.popup && window.opener) {
				if (window.opener.location.href.indexOf('lightbox') >= 0)
					url = '';
				else url = window.opener.location.href;
			} else url = location.href;
		} catch(e) {
			url = '';
		}

		switch (this.type) {
		case 'rf':
			this.fObj.srcUrl.value = url;
			this.fObj.CI_PRICE.value = item['PRICE'];
			this.fObj.UPRD_ID.value = isset(item['UPRD_ID']) ? item['UPRD_ID'] : '';
			this.fObj.UPRD_SDESC.value = item['NAME'];
			this.fObj.SIZE.value = isset(item['SIZE']) ? item['SIZE'] : '';
			break;
		case 'pr':
			this.fObj.srcUrl.value = url;
			this.fObj.CI_PRICE.value = item['PRICE'];
			this.fObj.UPRD_ID.value = isset(item['UPRD_ID']) ? item['UPRD_ID'] : '';
			this.fObj.UPRD_SDESC.value = item['NAME'];
			this.fObj.SIZE.value = (isset(item['SIZE']) && item['SIZE'] != '') ? 
				item['SIZE'] : item['HEIGHT'] + 'x' + item['WIDTH'];
			if (item['OPTIONS']) {
				this.fObj.CI_OPTIONS.value = item['OPTIONS'][this.lastOption]['ID'];
				this.fObj.FINISH.value = item['OPTIONS'][this.lastOption]['NAME'];
			} else {
				this.fObj.CI_OPTIONS.value = this.fObj.FINISH.value = '';
			}
			break;
		default:
			this.type = null;
			this.lastSize = null;
			return false;
		}
		
		this.fObj.I_ID.value = this.id;
		this.fObj.PRF_TYPE.value = this.type;
		
		this.fObj.submit();
		
		this.reset();
		return true;
	}
	
	this.setPrfSizeAct = function()
	{
		var			size = null, option = null;

		if (this.busy) return;

		if (this.fObj.PROFILE.length && this.fObj.PROFILE.length > 1) {
			for (var i = 0; i < this.fObj.PROFILE.length; i++) {
				if (this.fObj.PROFILE[i].checked) {
					size = this.fObj.PROFILE[i].value;
					if (this.fObj.elements['OPTIONS'+i])
						option = getValSingle(this.fObj.elements['OPTIONS'+i]);
					break;
				}
			}
		} else if (this.fObj.PROFILE.checked) {
			size = this.fObj.PROFILE.value;
			if (this.fObj.OPTIONS0) option = getValSingle(this.fObj.OPTIONS0);
		}
		
		if (size == null) {
			alert('Please select a size option.');
			return;
		}

		this.lastSize = size;
		this.lastOption = option;
		this.setBusy(true);
		this.operate();
	}
	
	this.profileSelect = function(i)
	{
		if (this.fObj.PROFILE.length) {
			this.fObj.PROFILE[i].checked = true;
		} else this.fObj.PROFILE.checked = true;
	}

	this.hide = function()
	{
		if (this.popup) window.close();
		else {
			this.div.style.visibility = 'hidden';
			if (this.mask) this.mask.style.visibility = 'hidden';
		}
		
	}	
	
	this.cancel = function()
	{		
		this.reset();
		this.setBusy(false);
		this.hide();
	}
	
	this.reset = function()
	{
		// clean up
		delete(this.prfData);
		delete(this.info);
		
		this.id = null;
		this.prfData = this.info = null;
		this.type = null;
		this.lastType = this.lastSize = this.lastOption = null;
		this.busy = false;
	}
	
	this.setBusy = function(flag)
	{
		this.busy = flag;
		
		if (this.position) {
			this.divBusy.style.top = this.position.y + 'px';
			this.divBusy.style.left = ((this.position.x > 160) ? 
				this.position.x - 100 : 60) + 'px';
			
		}
		this.divBusy.style.visibility = (flag) ? 'visible' : 'hidden';
	}
	
	this.nothing = function() { return; }

}

