var rated1 = false;   //wurde bereits gevoted?

$(document).ready(function() {
	$('#loginLink').click(function(){
		//$(this).unbind();
		$('#loginBoxTop').animate({height : 'toggle', opacity : 'toggle'}, function(){
			$('#email').focus();
		});
	});

	$('#closeLoginLink').click(function() {
		$('#loginBoxTop').animate({height : 'toggle', opacity : 'toggle'}, function(){
			$('#email').focus();
		});
	});

	$('#message').val('');

	$('#email').focus();

	$('#txtsearch').focus();
});


function doAjaxLogin()
{
	var email = 		$('#email').val();
	var password = 	$('#pass').val();
	var stayCheckbox = $('#stay').get(0);
	var stay = 		(stayCheckbox.checked == true) ? 'true' : '';

	var error = false;

	if(email == "")
	{
		$('#email').css({border : '2px solid #FF0000'});
		error = true;
	}
	else
	{
		$('#email').css({border : '1px solid #898A92'});
	}

	if(password == "")
	{
		$('#pass').css({border : '2px solid #FF0000'});
		error = true;
	}
	else
	{
		$('#pass').css({border : '1px solid #898A92'});
	}

	if(error)
	{
		alert('Your email address or password are to short!');
	}
	else
	{
		$.post("/ajax",
		{ what : 'checkLogin', email : email, pass : password, stay : stay},
		function(respond) {
			if(respond.status == true)
			{
				//erfolgreich eingeloggt
				$('#errorLogin').hide();
				$('#successLogin').show();
				$('#successLogin').fadeOut('slow').fadeIn('fast').fadeOut('slow').fadeIn('fast').fadeOut('slow').fadeIn('fast');
				//page reload
				location.reload();
			}
			else
			{
				$('#errorLogin').show();
				$('#successLogin').hide();
				$('#errorLogin').fadeOut('slow').fadeIn('fast').fadeOut('slow').fadeIn('fast').fadeOut('slow').fadeIn('fast');
			}
		}, 'json');
	}

	return false;
}



function insertComment(itemId)
{
	var text = $('#message').val();

	if(text != "" && itemId > 0)
	{
		$('#message').css({border : '1px solid #898A92'});
		$('#message').get(0).disabled = true;
		$('input[@type=submit]').get(0).disabled = true;
		$('input[@type=submit]').val('Adding Comment');

		$.post("/ajax",
		{ what : 'postComment', itemId : itemId, message : text},
		function(respond) {
			if(respond.status == true)
			{
				//erfolgreich eingetragen
				$('input[@type=submit]').val('Comment posted');
				$('#comentError').hide();
				$('#commentPosted').show();
				location.href = location.href + '#commentForm';
				location.reload();
			}
			else
			{
				$('#message').get(0).disabled = false;
				$('input[@type=submit]').get(0).disabled = false;
				$('input[@type=submit]').val('Post Comment');
				$('#commentPosted').hide();
				$('#comentError').fadeOut('slow').fadeIn('fast').fadeOut('slow').fadeIn('fast').fadeOut('slow').fadeIn('fast');
			}
		}, 'json');
	}
	else
	{
		$('#message').css({border : '2px solid #FF0000'});
		$('#message').focus();
	}

	return false;
}


function submitRating(itemId, rateValue)
{
	if(itemId && rateValue && !rated1)
	{
		rated1 = true;
		$.post("/ajax",
		{ what : 'rateItem', itemId : itemId, rateValue : rateValue},
		function(respond) {
			if(respond.status == true)
			{
				$('.respondStatusMessage').get(0).innerHTML = '<strong>Thanks for rating!</strong>';
			}
			else
			{
				$('.respondStatusMessage').get(0).innerHTML = '<strong>Rating Error (E083) - Please try again later!</strong>';
			}
		}, 'json');
	}
}


function deleteItem(itemId)
{
	if(!itemId) return false;

	var question = "Do you really want to delete this entry?";

	if(confirm(question))
	{
		//buttons disablem
		$('input').each(function() {
			$(this).get(0).disabled = true;
		});

		//ajax request
		$.post("/ajax",
		{ what : 'deleteItem', itemId : itemId},
		function(respond) {
			if(respond.status == true)
			{
				$('.row' + itemId).remove();
			}
			else
			{
				alert(respond.message + ' - Please try again later!');
			}
		}, 'json');

		$('input').each(function() {
			$(this).get(0).disabled = false;
		});
	}
}


function goBack()
{
	if(browserBack != "")
	{
		location.href = browserBack;
	}
	else if(history.length > 1)
	{
		history.back();
	}
	else
	{
		location.href = '/search';
	}
}