$(document).ready(function(){
	/* recommend */
	
	$(".recommend").livequery(function() {
		var jobj=$(this);
		var pageid=$(this).attr('rel');
		var smalid=pageid.split(',');
		var secode="";

		var recvalue = "<p>&nbsp;</p><p id='recpanel"+smalid[2]+"' class='seperatetop'><a href='#'>Diese Veranstaltung weiterempfehlen ...<\/a><\/p>";
		recvalue+="<div style='display:none;'>";
		recvalue+="<form id='recform"+smalid[2]+"' class='normform'>";
		recvalue+="<p><label for='amail'>Ihre E-Mail<\/label><input class='grey' type='text' id='amail' name='amail' value='' size='30' maxlength='50' \/><\/p>";
		recvalue+="<p><label for='email'>Empf&auml;nger E-Mail<\/label><input class='grey' type='text' id='email' name='email' value='' size='60' maxlength='200' \/><\/p>";
		recvalue+="<p class='small'><label>&nbsp;<\/label>mehrere Empf&auml;nger durch Komma trennen<\/p>";
		recvalue+="<p><label for='atext'>Nachricht<\/label><textarea class='grey' id='atext' name='atext' cols='60' rows='3' \/><\/textarea><\/p>";
		recvalue+="<div id='msgbox"+smalid[2]+"'><input type='submit' value='absenden' \/><\div><\/form><\/div>";
		$(jobj).html(recvalue);	
		
		/* Slide-Panel */
		$('#recpanel'+smalid[2]).click(function(){
			$(this).next().toggle();
			return false;
		});  

		$('#recform'+smalid[2]).submit(function()
		{
			// start fading
			$("#msgbox"+smalid[2]).html('<img src="images/loading_small.gif" /> ...').fadeIn(1000);
			
			// get secode
			$.post("estemp/functions/recommend.php", { p_pageid:pageid,p_secode:"getsecode"}, function (secode) 
			{
					
				// check data	
				$.post("estemp/functions/recommend.php",{ p_amail:$('#recform'+smalid[2]+' #amail').val(),p_email:$('#recform'+smalid[2]+' #email').val(),p_atext:$('#recform'+smalid[2]+' #atext').val(),p_pageid:pageid,p_secode:secode }, function (data)
				{
					if(data=='OK') // if correct login detail
					{
						$("#msgbox"+smalid[2]).fadeTo(200,0.1,function()	//start fading the messagebox
						{
							//add message and change the class of the box and start fading
							$('#recpanel'+smalid[2]).trigger("click").unbind("click").html('Ihre Empfehlung wurde versendet. Vielen Dank!').css("cursor","default");
						});
					}
					else
					{
						if(data.indexOf("(#") != -1)
						{
							$("#msgbox"+smalid[2]).fadeTo(200,0.1,function()	//start fading the messagebox
							{
								//add message and change the class of the box and start fading
								$('#recpanel'+smalid[2]).trigger("click").unbind("click").html(data).css("cursor","default");
							});
						}
						else
						{
							$("#msgbox"+smalid[2]).fadeTo(200,0.1,function() //start fading the messagebox
							{
								//add message and change the class of the box and start fading
								$(this).html("<input type='submit' value='absenden' \/>&nbsp;"+data).fadeTo(900,1);
							});
						}
					}
				});
			});
			
			return false;//not to post the form physically
		});
	});
});
