
var Listing = {

	hash: '',
	perform: '',
	formData: '',
	summaryStatus: 1,

	create: function(cat) {
		//Element.show('newListing');
		this.hash = 'newListing';
		_edit('newListing', '', cat, function(req){ Listing.editCB(req); });
	},

	edit: function(hash, url, cat) {
		//Element.show('edit_'+hash);
		Element.hide('show_'+hash);
		//Status.reset('_'+hash);
		this.hash = hash;
		_edit(hash, url, cat, function(req){ Listing.editCB(req); });
	},

	editCB: function(req) {

		new Insertion.Bottom('listing_'+this.hash, req);

	},

	save: function(hash, perform) {

		this.hash = hash;
		this.perform = perform;

		Status.text('Saving . . .', '_'+hash);

		formData = $('form_'+hash).serialize(true);
		this.formData = formData;
		//alert(formData.length);
		data = new Array();
		['TITLE','DESCRIPTION','URL','CATEGORY','ORIGURL'].each(function(s) {
			data[s] = formData[s];
		});

		sdata = serialize(data);

		_save(sdata, perform, function(req) { Listing.saveCB(req); });

	},

	saveCB: function(req) {

		if (req.substring(0,1) == '-') {
			Status.error(req.substring(2), '_'+this.hash);
			return;
		} else if (req.substring(0,1) == '+') {

			//Status.message(req.substring(2), '_'+this.hash);
			if (this.perform == 'i') {

				if (this.summaryStatus) {
					sumStatus = '';
				} else {
					sumStatus = 'display:none;';
				}

				//Status.reset('_'+this.hash);
				this.hash = req.substring(2); // set hash to new md5 of url
				$('form_newListing').reset();
				this.cancel('newListing');
				new Insertion.Top('listings', '<div id="listing_'+this.hash+'" class="listing"><div id="show_'+this.hash+'"><a href="'+this.formData['URL']+'" id="title_'+this.hash+'">'+this.formData['TITLE']+'</a> &nbsp;<span class="l_edit">[ <a href="javascript:void(0);" onclick="Listing.edit(\''+this.hash+'\', \$(\'url_'+this.hash+'\').innerHTML, \''+this.formData['CATEGORY']+'\');">Edit</a> ]</span><div class="l_desc" id="desc_'+this.hash+'" style="'+sumStatus+'">'+this.formData['DESCRIPTION']+'</div>&nbsp;<a href=\"'+this.formData['URL']+'\" class=\"l_url\" id=\"url_'+this.hash+'\">'+this.formData['URL']+'</a></div></div>');
				
				setTimeout('new Effect.Highlight(\'show_'+this.hash+'\', {startcolor: \'#CEE7FA\', duration: 2})', 0.1);

			} else {

				Element.update('title_'+this.hash, this.formData['TITLE']);
				Element.update('desc_'+this.hash, this.formData['DESCRIPTION']);
				Element.update('url_'+this.hash, this.formData['URL']);
				$('url_'+this.hash).setAttribute('href', this.formData['URL']);
				$('title_'+this.hash).setAttribute('href', this.formData['URL']);

				this.cancel(this.hash);
				new Effect.Highlight('show_'+this.hash, {startcolor: '#CEE7FA', duration: 2});

			}

			return;
		}

	},

	cancel: function(hash) {
		//Element.hide('edit_'+hash);
		Element.remove('edit_'+hash);
		if ($('show_'+hash)) {
			Element.show('show_'+hash);
		}
	},

	remove: function(hash) {
		Element.show('removeConfirm_'+hash);
		//Element.hide('remove_'+hash);
	},

	removeConfirm: function(hash, url, cat) {

		this.hash = hash;

		Status.text('Deleting . . .', '_'+hash);

		_remove(url, cat, function(req){ Listing.removeCB(req); });

	},

	removeCB: function(req) {

		if (req.substring(0,1) == '+') {
			new Effect.BlindUp('listing_'+this.hash, {duration: 1.2});
			setTimeout('Element.remove(\'listing_'+this.hash+'\')', 1000);
		} else {
			this.removeCancel(this.hash);
		}
	},

	removeCancel: function(hash) {
		Element.hide('removeConfirm_'+hash);
		//Element.show('remove_'+hash);
	},

	toggleSummary: function() {

		summaries = document.getElementsByClassName('l_desc');
//alert(summaries.length);
		if (summaries[0].style.display == '') {
			toggle = 'hide';
			this.summaryStatus = 0;
			_summaryStatus(0, null);
			Element.update('toggleSummary', 'Show summaries');
		} else {
			toggle = 'show';
			this.summaryStatus = 1;
			_summaryStatus(1, null);
			Element.update('toggleSummary', 'Hide summaries');
		}

		for (i = 0; i < summaries.length; i ++) {

			if (toggle == 'hide') {
				summaries[i].style.display = 'none';
			} else {
				summaries[i].style.display = '';
			}

		}

	}

}


// updates error/message messages
var Status = {

	reset: function(hash) {

		if (Element.hasClassName('status'+hash,'error')) {
			Element.removeClassName('status'+hash,'error');
		}
		if (Element.hasClassName('status'+hash,'message')) {
			Element.removeClassName('status'+hash,'message');
		}
		Element.update('status'+hash, '');
	},

	error: function(mess, hash) {

		this.reset(hash);
		Element.addClassName('status'+hash, 'error');
		Element.update('status'+hash, mess);

	},

	message: function(mess, hash) {

		this.reset(hash);
		Element.addClassName('status'+hash, 'message');
		Element.update('status'+hash, mess);
	},

	text: function(mess, hash) {
		this.reset(hash);
		Element.update('status'+hash, mess);
	}

}
