/* events */

var wuempty = false;
var maxEventsOnPage = 10;
var lastDeletedEvent = 0
var myTopId
var eventAutoHide = true
var evetTimer

function whatsupempty(){
	if(!wuempty){
		$('whats_up').value = '';
		$('whats_up').style.color = '#000000';
		wuempty = true
	}
}

function sendWhatsUp(){
	
	if(wuempty && $('whats_up').value.length >3){
		wuempty = false;
		new Ajax.Request('/ajaxevent.php?type=whatsup', {
			method: 'post',
			parameters: { name: $('whats_up_name').value, text: $('whats_up').value, id: $('whats_up_id').value, sex: $('whats_up_sex').value, stamp: lastPostedEvent() },
			onSuccess: function(request) {
				if(request.responseText !== 'error'){
					new Insertion.After($('eventspace'), ''+request.responseText+'')
					$('whats_up').value = 'Oszd meg másokkal gondolataid vagy linkeket, szövegeket.';
					$('whats_up').style.color = '#999999';	
					hideEvent()
				}
			}
		});
	}
}

function lastPostedEvent(){
	var eventChilds = $('eventslist').childElements();
	var topElement = eventChilds[1]
	var topCont = topElement.childElements()
	var topIdCont = topCont[0]
	var idValami = topIdCont.childElements()
	var topId = idValami[0].innerHTML
	return topId
}

function lastShowEvent(){
	if($('eventslist') !== null){
		var eventChilds = $('eventslist').childElements();
		var eventChildsCount = eventChilds.length-1
		
		var lastShowElement = eventChilds[eventChildsCount]
		var lastShowId = lastShowElement.childElements()
		lastShowId = lastShowId[0].childElements()
		lastShowId = lastShowId[0]
		lastShowId = parseInt(lastShowId.innerHTML)
	
		return lastShowId
	}else{
		//var onlyChilds = $('onlyEvent').childElements();
		//var oneEventId = onlyChilds[0].getElementsByClassName('eventid');
		return 0
	}
}

function autoEventRefresh(){
	//alert('vbcvxbcxvbcx')
	evetTimer = setTimeout("myPeridicalUpdater()",10000);
}


function myPeridicalUpdater(){
	clearTimeout(evetTimer)
	
	var timestamp = Number(new Date());
	new Ajax.Updater('', '/ajaxevent.php?type=newevents', {
	  method: 'post', 
	  parameters: { stamp: lastPostedEvent()},
	  onSuccess: function(request) {
	  			if(request.responseText !== 'error'){
	  				evetTimer = setTimeout("myPeridicalUpdater()",10000);
					new Insertion.After($('eventspace'), ''+request.responseText+'')
					newComments()
	  			}
				hideEvent()
			}
	});
	
}

function hideEvent(){
	//alert('aaaa')
	if(eventAutoHide){
		var eventChilds = $('eventslist').childElements();
		var eventChildsCount = eventChilds.length-1
		if(eventChildsCount == maxEventsOnPage+1){
			var deletableElement = eventChilds[eventChildsCount]
			var deletedId = deletableElement.childElements()
			deletedId = deletedId[0].childElements()
			deletedId = deletedId[0]
			lastDeletedEvent = parseInt(deletedId.innerHTML)
			deletableElement.remove()
		}
	}
}


function loadMoreEvent(num){
	eventAutoHide = false
	
	//alert(lastShowEvent())
	
	new Ajax.Request('/ajaxevent.php?type=oldevents', {
		method: 'post',
		parameters: { lastid: lastShowEvent(), limit: num},
		onSuccess: function(request) {
			if(request.responseText !== 'error'){
				new Insertion.Bottom($('eventslist'), ''+request.responseText+'')
			}
		}
	});
}

var commentable = true
var commentLimited = true
var maxComments = 3

function evt_show_commentform(id){
	/*$('evt_textarea_'+id).value = '';
	Effect.SlideDown('evt_commentform_'+id, { duration: 0.6 });*/
}

function evt_hide_commentform(id){
	/*Effect.SlideUp('evt_commentform_'+id, { duration: 0.4, afterFinish: function(){commentable = true} });*/
}

function evt_send_comment(id, user){
	/*
	if(commentable){
		commentable = false
		if($('evt_textarea_'+id).value.length >1){
			new Ajax.Request('/ajaxevent.php?type=comment', {
				method: 'post',
				parameters: { comment: $('evt_textarea_'+id).value, user: user, comid: id, stamp: lastShowEvent()},
				onSuccess: function(request) {
					if(request.responseText !== 'error'){
						evt_hide_commentform(id)
						readCommentXml(request.responseXML);
					}
				}
			});
		}
	}*/
	
}

function newComments(){
	/*new Ajax.Request('/ajaxevent.php?type=comment', {
		method: 'post',
		parameters: { stamp: lastShowEvent()},
		onSuccess: function(request) {
			if(request.responseText !== 'error'){
				readCommentXml(request.responseXML);
			}
		}
	});*/
}

function readCommentXml(xmldata){
	/*xmlDoc = xmldata;

	xml_eventid = xmlDoc.getElementsByTagName('eventid')
	xml_userid = xmlDoc.getElementsByTagName('userid')
	xml_sex = xmlDoc.getElementsByTagName('sex')
	xml_username = xmlDoc.getElementsByTagName('username')
	xml_text = xmlDoc.getElementsByTagName('text')
	
	for(var n=0; n<xml_eventid.length; n++){
		evt_add_new_comment(xml_eventid[n].childNodes[0].nodeValue, xml_userid[n].childNodes[0].nodeValue, xml_sex[n].childNodes[0].nodeValue, xml_username[n].childNodes[0].nodeValue, xml_text[n].childNodes[0].nodeValue)
	}
	
	
	xml_likeid = xmlDoc.getElementsByTagName('evtid')
	xml_likenum = xmlDoc.getElementsByTagName('likenum')
	xml_ilike = xmlDoc.getElementsByTagName('ilike')
	
	for(var l=0; l<xml_likeid.length; l++){
		evt_update_tetszik_line(xml_likeid[l].childNodes[0].nodeValue, xml_ilike[l].childNodes[0].nodeValue, parseInt(xml_likenum[l].childNodes[0].nodeValue))
	}*/

}


function  evt_add_new_comment(evtid, uid, sex, uname, text){
	/*var commentHtml = '<li class="evt_comment"><table width="100%" cellpadding="0" cellspacing="0" border="0" class="event_print_comment"><tr><td align="left" valign="top" style="width:38px;"><a href="/user/esemenyek/'+uid+'"><img src="/resized_images/'+uid+'_'+sex+'_30.jpg" alt=""/></a></td><td align="left" valign="top"><a href="/user/esemenyek/'+uid+'" class="username">'+uname+'</a>  '+text+'</td></tr></table></li>';
	
	new Insertion.Before($('evt_comments_last_'+evtid), commentHtml)
	
	var commentChilds = $('evt_comments_'+evtid).childElements();
	var commentsNum = parseInt($('evt_allcomments_num_'+evtid).innerHTML)
	//alert(commentChilds.length)
	
	if(commentChilds.length > 2){
		$('evt_allcomments_num_'+evtid).innerHTML = commentsNum + 1
	}
	
	if(commentsNum > maxComments-1){
		$('evt_allcomments_'+evtid).style.display = 'block'
	}
	
	if(commentChilds.length > maxComments+1){	
		
		var deletableElement = commentChilds[1]
		if(commentLimited){
			deletableElement.remove()
		}
		
	}*/
}

function evt_send_tetszik(id){
		
		//$('evt_like_butt_'+id).style.display = 'none'
		
		
		new Ajax.Request('/ajaxevent.php?type=ilikeit', {
			method: 'post',
			parameters: { event_id: id},
			onSuccess: function(request) {
				if(request.responseText !== 'error'){
					evt_update_tetszik_line(id, true, 0)
					readCommentXml(request.responseXML);
				}
			}
		});
}

function evt_update_tetszik_line(id, iLike, num){
	var likes = parseInt($('evt_likes_num_'+id).innerHTML)
	//alert('aaaaa')
	if(iLike == 0){iLike = false}
	if(iLike == 1){iLike = true}
	
	if(iLike){
		$('evt_like_butt_'+id).style.display = 'none'
		
		if(likes > 0){
			$('evt_like_neked_'+id).style.display = 'inline'
			$('evt_like_es_'+id).style.display = 'inline'
			
			if(num > 0){$('evt_likes_num_'+id).innerHTML = num-1}
			$('evt_likes_num_'+id).style.display = 'inline'
			$('evt_like_embernek_'+id).style.display = 'inline'
			$('evt_like_tetszik_'+id).style.display = 'inline'
		}else{
			$('evt_like_neked_'+id).style.display = 'inline'
			$('evt_like_tetszik_'+id).style.display = 'inline'
		}
	}else{
		if(likes > 0){
			$('evt_likes_num_'+id).innerHTML = num
			$('evt_likes_num_'+id).style.display = 'inline'
			$('evt_like_embernek_'+id).style.display = 'inline'
			$('evt_like_tetszik_'+id).style.display = 'inline'
		}
	}
}

function showeventhide(id){
	if($('eventhider_'+id).style.display == 'block'){
		$('eventhider_'+id).style.display = 'none'
	}else{
		$('eventhider_'+id).style.display = 'block'
	}
	
}

