function ajax_update(resultid, url) {
	new Ajax.Updater(resultid, url, {
		evalScripts :true
	});
}

function setTemplate()
{
	var text=$('Item_create_text').value;
	if(text=="")
		text="[Dein Text]";
	$('Item_create_text').value='Heute, '+text+ '. So ein Mist!';
	


}

function checkLength()
{
	var current=$('Item_create_text').value.length;
	$('zeichen').innerHTML=350-current;
	new Effect.Pulsate('zeichen');
	if (current>=350){
		//alert("Dein Text ist zu lang.");
		$('Item_create_text').value=$('Item_create_text').value.slice(0,350);
		
	}
	return true;
}
function tag()
{
	//alert("Haus");
	var text = $('Item_create_text').value;
	text=text.replace(". So ein Mist!","");

	//var word = text.split(" ");
	//text = text.substr(0,text.length-14);
	//alert("haus");
	if(text.substr(text.length-1, 1)==" ")
	{
	var url = "/ajax.php?call=Tag.ajax_check&0=" + encodeURIComponent(text);
	new Ajax.Request(url, {
	method: 'get',
	onSuccess: function(transport) {
	var response = transport.responseText;
	$('Item_create_tagstring').value=response;
 
		}
	});
	}
	checkLength();


}

// Comment related stuff
function save_comment(type, id, resultid) {
	var text = $('comment_text_' + type + '_' + id).value;
	//var title = $('comment_title_' + type + '_' + id).value;
	var title="";
	if(text=="")
		{
		alert("Gebe doch bitte einen Text in das Kommentarfeld ein");
		return false;
		}
	else{
	var url = "/ajax.php?call=Comment.ajax_insert&0=" + title + "&1=" + encodeURIComponent(text)
			+ "&2=" + type + "&3=" + id + "&4=Question";
	ajax_update(resultid, url)
	}
}

function edit_comment(cid, resultid) {
	var text = $('edit_comment_text_' + cid).value;
	var title = $('edit_comment_title_' + cid).value;
	var url = "/ajax.php?call=Comment.ajax_update&0=" + title + "&1=" + text
			+ "&2=" + cid
	ajax_update(resultid, url)

}
function rate_comment(id, rate, resultid) {
	var url = "/ajax.php?call=Comment.ajax_rate&0=" + rate + "&1=" + id;
	ajax_update(resultid, url);

}


function rate_item(id, rate, resultid,detail) {
	var a = new Date();
	a = new Date(a.getTime() +1000*60*60*24*365);
	document.cookie = 'item_rate_'+id+'_'+rate+'=true; path=/;domain=.so-ein-mist.de; expires='+a.toGMTString()+';';

	var url = "/ajax.php?call=Item.ajax_rate&0=" + rate + "&1=" + id+'&2='+detail;
	ajax_update(resultid, url);


}

function addFavourite_item(id,resultid,detail) {
	//var a = new Date();
	//a = new Date(a.getTime() +1000*60*60*24*365);
	//document.cookie = 'item_rate_'+id+'_'+rate+'=true; path=/; expires='+a.toGMTString()+';';
	var url = "/ajax.php?call=Item.ajax_favourite&0=" + id+'&1='+detail;
	new Effect.Pulsate('star_'+id);
		
	window.setTimeout("ajax_update('"+resultid+"','"+url+"')",1000);


}

function proof_item(id, proof,resultid) {
	var a = new Date();
	a = new Date(a.getTime() +1000*60*60*24*365);
	document.cookie = 'item_proof_'+id+'_'+proof+'=true; path=/; expires='+a.toGMTString()+';';
	var url = "/ajax.php?call=Item.ajax_proof&0=" + proof + "&1=" + id;
	ajax_update(resultid, url);
	new Effect.Highlight('item', {duration: 2});

}

function show_edit_comment(cid) {
	$('commentid_' + cid).style.display = 'none';
	$('editcommentid_' + cid).style.display = 'block';
}

function hide_edit_comment(cid) {
	$('commentid_' + cid).style.display = 'block';
	$('editcommentid_' + cid).style.display = 'none';
}

function generateOverlay() {
	if (!$('overlayreg')) {
    $$('body').first().insert(new Element('div', { id: 'overlayreg' }));
  }
  if (!$('overlayBackreg')) {
    $$('body').first().insert(new Element('div', { id: 'overlayBackgreg' }));
    $('overlayBackgreg').observe('click', hideOverlay);
  }
  hideOverlay();
  $('overlayreg').update();
}

function hideOverlay() {
	$('overlayreg').hide();
	$('overlayBackgreg').hide();
}

function showOverlay() {
	addOverlayCloseButton();

	$('overlayreg').show();
	$('overlayBackgreg').show();
}

function addOverlayCloseButton() {
	$('overlayreg').insert({
		'top':
		new Element(
			'a',
			{
				'href': 'javascript:void(0)',
				'title': 'schliessen',
				'class': 'closeOverlay',
				'innerHTML': 'ads'
			}
		).insert(
			new Element(
				'img',
				{
					'src': '/layout/images/close_icon.jpg',
					'alt': 'schliessen'
				})
		).observe('click', hideOverlay)
	});
}	