function show_comments(id, e){
	if(!$('comments_'+id)) return false;
	debug($('comments_'+id).childNodes.length);	
	if($('comments_'+id).childNodes.length > 1) return hide_comments(id);
	if($('comments_'+id+'_form')) return hide_comments(id);
	show_loading(e.clientX, e.clientY);
	url='/phpajax/handler.php?comment='+id;
	ajax = new Ajax.Request(
		url,
		{
			onSuccess: function(r){parse_comments_xml(r, id)}
		}
	);
}

function hide_comments(id){
	div = $('comments_'+id);
	nodes = $('comments_'+id).childNodes;
	debug(nodes, true);
	for(i= nodes.length-1; i>= 0; i--){
		debug(nodes[i].nodeName);
		node = nodes[i];
		if(nodes[i].nodeName != '#text'){
			new Effect.DropOut(nodes[i], {
				"afterFinish": function(obj){
					node = obj.element;
					debug('hola '+obj.effects.length);
					node = obj.effects[0].element;
					node.parentNode.removeChild(node);
/* 					debug(node.nodeName + ' = ' + node.parentNode); */
/* 					node.parentNode.removeChild(node); */
				},
				'duration': 0.5
			})
/* 			div.removeChild(node); */
		}
	}
	debug('',true);
	formdiv = $('comments_'+id+'_form');
	formdiv.parentNode.removeChild(formdiv);
}

function add_comment(id){
	debug(id, true);
	if(!$('comments_'+id)) return false;
	text = encodeURIComponent($('textarea_'+id).value);
	user = encodeURIComponent($F('user_'+id));

	if(text.blank()){
		alert("Please write a comment");
		$('textarea_'+id).focus();
		return false;
	}
	if(user.blank()){
		alert("Please write your name");
		$('user_'+id).focus();
		return false;
	}
	show_loading();

	debug(text, true);

	debug(user);
	pars = 'rant='+id+'&user='+user+'&comment='+text;
	debug(pars);
	url='/phpajax/handler.php';
	ajax = new Ajax.Request(
		url,
		{
			encoding: 'UTF-8',
			method: 'post',
			parameters: pars,
			onSuccess: function(r){parse_comments_xml(r, id, true)}
		}
	);
}

function show_loading(x, y){
	if(!$('loadimg')){
		img = new Element('img', {'src': '/images/loading.gif', 'style': 'position:fixed; z-index: 66', 'id': 'loadimg'});
		if(x) img.style.left = x-8+'px';
		if(y) img.style.top = y-8+'px';
		document.body.appendChild(img);
	}else{
		$('loadimg').style.display='block';
		if(x) img.style.left = x-8+'px';
		if(y) img.style.top = y-8+'px';
	}
	Event.observe(window, 'mousemove', move_img);
}

function move_img(e){
/*   	debug(e.clientX + ' ' + e.clientY); */
  	img = $('loadimg');
  	img.style.left = e.clientX - 8+'px';
  	img.style.top = e.clientY - 8 +'px';
}
function hide_loading(){
	Event.stopObserving(window, 'mousemove', move_img);
	$('loadimg').style.display='none';
}

function handle_search(){
	keywords = $F('search');
	if(keywords == '' || keywords == undefined){
		alert('please specify a search query');
		return false;
	}
	document.location = '/search/'+keywords;
	return false;
}

function parse_comments_xml(resp, id, single){
	xmlDoc = resp.responseXML;
	xmlitems = xmlDoc.getElementsByTagName('comment');
	if(!xmlitems || xmlitems.length == 0){
		hide_loading();		
		xmlitems = xmlDoc.getElementsByTagName('error');
		if(xmlitems.length > 0){
			debug(xmlitems.length);
			debug(xmlitems[0].nodeName);
			alert("An error Ocurred \n"+ xmlitems[0].childNodes[0].nodeValue);

			return false;
		}
	}
	debug(xmlitems.length, true);
	for(i =0; i < xmlitems.length; i++){
		try{
			myitem = xmlitems[i];
		}catch(e){
//			item = xmlDoc.all.tags('comment');
			debug(resp.getAllHeaders());
//			item = item[i];
		}
		debug(myitem);
		author = false;
		month = false;
		day = false;
		extra = false;
		post = false;
		for(e = 0; e < myitem.childNodes.length; e++){
			thisnode = myitem.childNodes[e];
			switch(thisnode.nodeName){
				case 'author':
//					author = thisnode.childNodes[0].nodeValue;
					try{
						author = thisnode.childNodes[1].nodeValue;
					}catch(e){
						debug(thisnode.childNodes[0].nodeName);
						debug(thisnode.childNodes[0].nodeValue);						
						author = thisnode.childNodes[0].nodeValue;
					}

					debug('author '+ author);
				break;
				
				case 'post':
					debug('post');
					try{
						post = thisnode.childNodes[1].nodeValue;
					}catch(e){
						debug(thisnode.childNodes[0].nodeName);
						debug(thisnode.childNodes[0].nodeValue);						
						post = thisnode.childNodes[0].nodeValue;
					}
					debug('post '+ post);
				break;

				case 'date':
					debug('date');
					month = thisnode.getElementsByTagName('month')[0].childNodes[0].nodeValue;
					debug('month '+month);
					day = thisnode.getElementsByTagName('day')[0].childNodes[0].nodeValue;
					debug('day '+day);
					extra = thisnode.getElementsByTagName('extra')[0].childNodes[0].nodeValue;
					debug('extra '+extra);
/*
					month = thisnode.childNodes[1].childNodes[0].nodeValue;
					debug('month '+month);
					try{
					    day = thisnode.childNodes[3].childNodes[0].nodeValue;
					}catch(e){
					    day = thisnode.childNodes[2].childNodes[0].nodeValue;
					}
					debug('day '+day);
					try{
						extra = thisnode.childNodes[5].childNodes[0].nodeValue;
					}catch(e){
						extra = thisnode.childNodes[2].nodeValue;
					}
					debug('extra '+extra);

*/				break;
			}
		}
		div = new Element('div', {'class': 'comment', 'style': 'display: none'});
		h2 = new Element('h2', {'class': 'comment_author'});
		datediv = new Element('div', {'class': 'comment_date'});
		spmonth = new Element('span', {'class': 'month'});
		spday = new Element('span', {'class': 'day'});
		spextra = new Element('span', {'class': 'extra'});
		p = new Element('p');
		
		br = new Element('br', {'style': 'clear: both'});
		
		h2.update(author);
		spmonth.update(month);
		spday.update(day);
		spextra.update(extra);
		p.update(post);
		
		div.appendChild(h2);
		datediv.appendChild(spmonth);
		datediv.appendChild(spday);
		datediv.appendChild(spextra);
		div.appendChild(datediv);
		div.appendChild(p);
		div.appendChild(br);
		$('comments_'+id).appendChild(div);
		new Effect.SlideDown(div);
	}
	if(!single){
		div = new Element('div', {'style': 'display: none', 'class': 'commentsform', 'id': 'comments_'+id+'_form'});
		p = new Element('p');
		p.update('Post Your Comments');
		text = new Element('textarea', {'id': 'textarea_'+id, 'name': 'Comments'});
		user = new Element('input', {'type': 'text', 'id': 'user_'+id, 'name': 'User Name'});
		button = new Element('input', {'type': 'button', 'value': 'Submit!', 'style':'display: block'});
		button.onclick = function(event){
			add_comment(id, event);
			return false;
		};
		div.appendChild(p);
		div.appendChild(text);
		div.appendChild(new Element('p').update('Your Name <br />'));
		div.appendChild(user);
		div.appendChild(button);
		div.appendChild(new Element('br', {'style': 'clear: both'}));
		$('comments_'+id+'_wrapper').appendChild(div);
		new Effect.Appear(div);
	}
	hide_loading();
}