window.addEvent('domready',function(){
	
    $$('.chromelesswin').each(function(l){
	    l.addEvent('click',function(){
	    	window.open(l.href,'popup','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=400,titlebar=yes');
			return false;    	
	    });
    });
	
	
	$$('commentsubmitbutton').addClass('hide');
	$$('.action_comment').each(function(thislink){
		thislink.addEvent('click',function(){
			var itemid = thislink.id.substring('comment'.length);
//			alert(itemid);
			// return false;
		});
	});
	$$('.action_move').each(function(thislink){
		thislink.addEvent('click',function(){
			var itemid = thislink.id.substring('move'.length);
//			alert(itemid);
//			return false;
		});
	});
	$$('.action_edit').each(function(thislink){
		thislink.addEvent('click',function(){
			var itemid = thislink.id.substring('edit'.length);
//			alert(itemid);
//			return false;
		});
	});
	$$('.action_addtomine').each(function(thislink){
		thislink.addEvent('click',function(){
			if($('userid')){
				var itemid = thislink.id.substring('addtomine'.length);
//				alert(itemid);
			}else{
				alert('you must be logged in to use this feature');
			}
//			return false;
		});
	});
	$$('.commentsubmitbutton').each(function(it){
		it.addClass('hide');
	});
	$$('.action_delete').each(function(thislink){
		thislink.addEvent('click',function(){
			if(confirm("Are you sure you want to remove this item?")){
				var itemid = thislink.id.substring('delete'.length); 
				var qs = new Hash({
					'action':'item.delete',
					'itemid':itemid,
					'format':'json'
				}).toQueryString();
				var url = '/nifty_api_client/';
				var deleteitemajax = new Request({
					'url':url,
					'data':qs,
					onSuccess:function(responseText, responseXML){
						var json = JSON.decode(responseText);
						if(json.success){
							$('item'+itemid).dispose();
						}else{
							alert("Sorry, that item couldn't be removed. Are you still signed in? Try refreshing the page (hit F5).");
						}
					}
				}).send();
			}
			return false;
		});
	});


	var commentboxes = $$('.comments');
	var commentsliders = new Array();
	commentboxes.each(function(commentbox){
		// get its id
		var itemid = commentbox.id.substring('commentsbox'.length);
		var trigger = $('commentlink'+itemid);
		var commentslide = new Fx.Slide(commentbox,{
			onComplete:function(){
				if(commentbox.getParent().getStyle('height').toInt()>0){commentbox.getParent().setStyle('height','');}
//				commentbox.getParent().setStyle('height','');
			}
		});
		commentslide.hide();
		// hook up the trigger
		trigger.addEvent('click',function(){
			commentslide.toggle();
			return false;
		});
	});
	
	$$('.commentinputbox').each(function(commentinputbox){
		var itemid = commentinputbox.id.substring('commentinputbox'.length);
		commentinputbox.itemid = itemid;
		commentinputbox.addEvent('focus',function(){
			if(this.value == 'Write a comment'){
				this.value = '';
			}
			$('commentsubmitbutton'+this.itemid).removeClass('hide');
			// show submit button
		});
		commentinputbox.addEvent('blur',function(){
			if(this.value == ''){
				$('commentsubmitbutton'+this.itemid).addClass('hide');
				this.value = 'Write a comment';
			}
			// hide submit button
		});
		commentinputbox.addEvent('keyup',function(){
		});
	});
	
	$$('.commentform').each(function(commentform){
		commentform.addEvent('submit',function(){
			var itemid = commentform.id.substring('commentform'.length);
			var commenttext = $('commentinputbox'+itemid).value;
			var qs = new Hash({
				'action':'comment.put',
				'itemid':itemid,
				'commenttext':commenttext,
				'format':'json'
			}).toQueryString();
			var url = '/nifty_api_client/';
			var commentajax = new Request({
				'url':url,
				'method':'post',
				'data':qs,
				onSuccess:function(responseText, responseXML){

					var datajson = JSON.decode(responseText);
					if(datajson.success){

						var commentid = datajson['commentid'];
						var categoryid = $('categoryid').innerHTML;
						var itemid = datajson['itemid'];
						var username = $('userfullname').innerHTML;
						var userid = $('userid').innerHTML;
	
						var newcomment = new Element('div',{
							'id':'comment'+commentid,
							'class':'comment'
						}).inject(commentform,'before');
						
						var userlink = new Element('a',{
							'href':'/people/'+userid+'/'
						}).set('text',username).inject(newcomment,'top');
						
						// now, why write a verbose href in here? if JS was disabled, we wouldn't be in this function to begin with. But for the sake of precision, we should...
						var dellink = new Element('a',{
							'href':'/people/'+userid+'/wantit/'+categoryid+'/'+itemid+'/'+commentid+'/delete/',
							'id':'deletecomment'+commentid,
							'class':'deletecomment'
						}).set('text','delete').inject(newcomment,'top');
						
						// clear the form
						$('commentinputbox'+itemid).blur();
						$('commentinputbox'+itemid).value = 'Write a comment';
						$('commentsubmitbutton'+itemid).addClass('hide');
					
						var compp = new Element('p',{}).set('text',commenttext).inject(newcomment,'bottom');
	
						var commentcount = parseInt($('commentcountdatum'+itemid).innerHTML);
						$('commentcountdatum'+itemid).set('text',commentcount+1);
						$('commentcountnoun'+itemid).set('text',(commentcount+1)==1?'Comment':'Comments');
	
						// adjust the height of the slider now
						$('commentsbox'+itemid).getParent().setStyle('height','');
						
						dellink.addEvent('click',function(){
							var url = '/nifty_api_client/';
							var qs = new Hash({  
								'action':'comment.delete',
								'commentid':commentid,
								'format':'json'
							}).toQueryString();
							var commentajax = new Request({
								'url':url,
								'method':'post',
								'data':qs,
								onSuccess:function(responseText, responseXML){
									var json = JSON.decode(responseText);
									if(json.success){
										$('comment'+commentid).dispose();
										
										var commentcount = parseInt($('commentcountdatum'+itemid).innerHTML);
										$('commentcountdatum'+itemid).set('text',commentcount-1);
										$('commentcountnoun'+itemid).set('text',(commentcount-1)==1?'Comment':'Comments');
										
									}else{
										alert("Sorry, your comment couldn't be deleted. Are you still signed in? Try refreshing the page (hit F5)");
									}
								},
								onComplete:function(){
									
								}
							}).send();
							return false;
						
						});
					}else{
						alert("Sorry, your comment didn't save. Are you still signed in? Try refreshing the page (hit F5).");
					}
				},
				onComplete:function(){
					
				}
			}).send();
			var itemid = commentform.getElement('itemid');
			return false;
		});
	});
	
	$$('.deletecomment').each(function(link){
		link.addEvent('click',function(){
			var commentid = link.id.substring('deletecomment'.length);
			var itemid = link.getParent('.item').id.substring('item'.length);
			var url = '/nifty_api_client/';
			var qs = new Hash({
				'action':'comment.delete',
				'commentid':commentid,
				'format':'json'
			}).toQueryString();
			
			var commentajax = new Request({
				'url':url,
				'data':qs,
				'method':'post',
				onSuccess:function(responseText, responseXML){
					var json = JSON.decode(responseText);
					if(json.success){
					
						$('comment'+commentid).dispose();
						
						var commentcount = parseInt($('commentcountdatum'+itemid).innerHTML);
						$('commentcountdatum'+itemid).set('text',commentcount-1);
						$('commentcountnoun'+itemid).set('text',(commentcount-1)==1?'Comment':'Comments');
						
					}else{
						alert('Sorry - your comment didn\'t delete properly. Are you still signed in? Try refreshing the page. (hit F5)');
					}
				},
				onComplete:function(){
				}
			}).send();
			return false;
		});
	});

	
	$$('.registryincrement').each(function(inc){
		inc.addEvent('click',function(){
			purchase(inc,1);
			return false;			
		});
	});
	$$('.buythis').each(function(inc){
		inc.addEvent('click',function(){
			purchase(inc,1);
			return false;			
		});
	});
	$$('.registrydecrement').each(function(inc){
		inc.addEvent('click',function(){
			purchase(inc,-1);
			return false;			
		});
	});
	function purchase(elem,qty){
			var itemid = elem.getParent('.item').id.substring('item'.length);
			var quantity = $('quantitydatum'+itemid).innerHTML;
			var url = '/nifty_api_client/';
			var qs = new Hash({
				'action':'registry.post',
				'itemid':itemid,
				'format':'json',
				'quantity':qty
			}).toQueryString();
			var commentajax = new Request({
				'url':url,
				'data':qs,
				'method':'post',
				onRequest:function(){
					$('purchaseajaxload'+itemid).removeClass('hidden');	
				},
				onSuccess:function(responseText, responseXML){
					var json = JSON.decode(responseText);
					if(json.success){
						var mypurchases = json.mypurchases;
						var quantitygot = json.quantitygot;
						$('quantitygotdatum'+itemid).set('text',quantitygot);
						$('mypurchasesdatum'+itemid).set('text',mypurchases);
						if(quantitygot >= quantity){
							$('isregistrycheckmark'+itemid).addClass('itemcheckmark');
						}else{
							$('isregistrycheckmark'+itemid).removeClass('itemcheckmark');
						}
						if(mypurchases <= 0){
							$('buythis'+itemid).removeClass('hide');
							$('mypurchases'+itemid).addClass('hide');
						}else{
							$('buythis'+itemid).addClass('hide');
							$('mypurchases'+itemid).removeClass('hide');
						}
					}else{
						alert('Sorry - your purchase request didn\'t go through. Are you still signed in? Try refreshing the page. (hit F5)');
					}
				},
				onComplete:function(){
					$('purchaseajaxload'+itemid).addClass('hidden');	
				}
			}).send();
	}

	$$('.butt-del').each(function(buttdel){
		buttdel.addEvent("click",function(){
			return confirm("Are you sure you want to delete this category?");
		});
	});
	$$('.action_delete_item').each(function(actiondelete){
		actiondelete.addEvent("click",function(){
			return confirm("Are you sure you want to delete this item?");
		});
	});
	
	
});

