var ListMachine = new Class({
							
	dblclickcatch : false,
	
	list : function(userid, itemid, itemtype, listingtype) {
		
		if(this.dblclickcatch) {
			return;
		} else {
			this.dblclickcatch = true;	
		}
		
		this.userid = userid;
		this.itemid = itemid;
		this.itemtype = itemtype;
		this.listingtype = listingtype;
		this.action = 'list';
		
		if($chk($(listingtype + '-listings-none'))) {
			$(listingtype + '-listings-none').dispose();
		}

		$(listingtype + '-listings-container').set('html','<a href="javascript:oListMachine.unlist(' + userid + ', ' + itemid + ', \'' + itemtype + '\', \'' + listingtype + '\')">' + Localization.unlistyourself + '</a>');
		
		this.request();
		
	}, 
	
	unlist : function(userid, itemid, itemtype, listingtype) {
		
		if(this.dblclickcatch) {
			return;
		} else {
			this.dblclickcatch = true;	
		}
		
		this.userid = userid;
		this.itemid = itemid;
		this.itemtype = itemtype;
		this.listingtype = listingtype;
		this.action = 'unlist';
		
		if($chk($(listingtype + '-listing-' + userid))) {
			var oByeBye = new Fx.Tween($(listingtype + '-listing-' + userid), {
										 
				onComplete : function() {
					$(listingtype + '-listing-' + userid).getNext().dispose();
					$(listingtype + '-listing-' + userid).dispose();	
				}
										 
			});
			oByeBye.start('height',0);
		}
		
		$(listingtype + '-listings-container').set('html','<a href="javascript:oListMachine.list(' + userid + ', ' + itemid + ', \'' + itemtype + '\', \'' + listingtype + '\')">' + Localization.getlisted + '</a>'); // + Localization.visitorswillcontactyou
		
		this.request();
		
	},
	
	request : function() {
	
				var oPost = new Hash();
				oPost.set('userid', this.userid);
				oPost.set('itemid', this.itemid);
				oPost.set('itemtype', this.itemtype);
				oPost.set('listingtype', this.listingtype);
				oPost.set('action', this.action);
	
				new Request.JSON({
					method: 'post',
					data: JSON.encode(oPost),
					url: '/ajax/ListMachine.request.ajax.php',
					instance : this,
					onRequest: function() {
						//
					},
					onFailure: function(exc) {
						ToolBox.alert(Localization.js_default_ajax_error);
						oListMachine.dblclickcatch = false;
					},
					onError: function() {
						ToolBox.alert(Localization.js_default_ajax_error);
						oListMachine.dblclickcatch = false;
					},
					onSuccess: function(o) {
						if($type(o) == 'object') {
							switch(o.success) {
								case(0):
									ToolBox.alert(Localization.js_default_ajax_error);
									break;
								case(1):
									
									if(oListMachine.action == 'list') {
										//insert user row
										
										if(Context.userid != o.userid) {
											var sendmessage = '<a href="javascript:LightboxGuru.viewHtmlbox(\'Lightbox.send-message.ajax.php\', \'' + Localization.sendmessage +  '\', {to:' + o.userid + '});">' + Localization.sendmessage + '</a>';
										} else {
											var sendmessage = '<span style="color:#888;">' + Localization.sendmessage + '</span>';
										}
										
										var listing = '<img class="rounded-corners" src="/images/uploads/users/25-' + o.profilepic + '" width="25" height="25" border="0" style="float:left; margin-right:7px;" /><div style="float:right; width:141px; margin-top:-3px; color:#1c4f6b;">' + o.firstname + ' ' + o.lastname + '<br />' + sendmessage + '</div></div><div class="spacer7px" style="clear:both;">';
										
										var oElement = new Element('div', {id:oListMachine.listingtype + '-listing-' + o.userid, 'style':'height:0px; overflow:hidden'});
										
										oElement.set('html', listing);
										
										oElement.inject($(oListMachine.listingtype + '-listings-container'), 'before');
										
										var oFxHighlight = new Fx.Tween($(oListMachine.listingtype + '-listing-' + o.userid), {
																   onComplete : function() {
																	   $(oListMachine.listingtype + '-listing-' + o.userid).highlight('#b5e6bd');
																   }});
										oFxHighlight.start('height', 25);
										
										var oSpacer = new Element('div', {'class':'spacer7px', 'style':'clear:both;'});
										oSpacer.inject($(oListMachine.listingtype + '-listings-container'), 'before');
									}
									
									break;
							}
						} else {
							ToolBox.alert(Localization.js_default_ajax_error);
						}
						oListMachine.dblclickcatch = false;
					}
				}).send();
	
	}
	
});
