$('.prev_rec_item').click(function () {
   window.location = $(this).find('a').attr('href');
});

$('.hiddenul').hide();

$('#rec_searchbutton').click(function() {
    $(this).closest('form').submit();
});

$(document).ready(function() {
});

$('#black_back').click(function () {
	$('#black_back').fadeOut();
	$('#addrec_maindiv').fadeOut();
	hideConditions();
});

$('#rec_addreceptbutton').click(function () {
	$('.firststep').fadeIn();
	$('#black_back').height($('body').height());
	$('#black_back').fadeIn();
	$('#addreceptform')[0].reset();
	$('#files').html("");
	$('#addrec_maindiv').fadeIn(function() {
		$(this).find('.rec_slidediv').slideDown();
	});
	$('#addreceptform table').show();
});

$('#addrec_conditions_btn').click(function () {
	$('#rec_conditionsmaindiv').fadeIn(function() {
		$(this).find('.rec_slidediv').slideDown();
	});
});

$('.closeblueback').click(function () {
	hideConditions();
});

$('#addrec_confirm_btn').click(function () {
	hideConditions();
});

$('#rec_nextstep').click(function() {
	if (validateInputs('.firststep')) {
		$('.firststep').fadeOut();
		$('.secondstep').fadeIn();
	}
});

$('#rec_backstep').click(function() {
	$('.firststep').fadeIn();
	$('.secondstep').fadeOut();
});

function hideConditions() {
	$('#rec_conditionsmaindiv .rec_slidediv').slideUp();
	$('#rec_conditionsmaindiv').slideUp();
}

$('#addrecept').click(function() {
	var emailInput = $('input[name="rec_auth_email"]');
	if (validateInputs('#addreceptform')) {
		if (!valEmail(emailInput.val())) {
			emailInput.addClass("inputwitherror");
		} else {
			
			var url="../?option=com_amylonrecepts&format=raw&view=addrecept";
			
			var submitbtn = $(this);
			
//			alert(url);
			$.ajax( {
				type : "GET",
				url : "" + url,
				dataType : "html",
				data : read('#addreceptform'),
				success : function(g) {
					if (isNaN(g)) {
						alert(g);
					} else {
						$('#addreceptform table').hide();
						$('#addrec_status')
							.html("Váš recept byl uložen, děkujeme za Váš zájem")
							.fadeIn(function () {
								$('#black_back').fadeOut();
								$('#addrec_maindiv').fadeOut();
								hideConditions();
							});
					}
				},
				error : function(xhr, ajaxOptions, thrownError) {
					alert("Chyba při kontaktu serveru. Zkuste to prosím znovu."
							+ xhr.statusText + ' ' + thrownError);
		
				}
			});
		}
	}
});
		
$('#upload').click(function() {
	var btnUpload=$('#upload');
	var status=$('#status');
	new AjaxUpload(btnUpload, {
		action: '../?option=com_amylonrecepts&format=raw&view=uploadfiles',
		name: 'uploadfile',
		onSubmit: function(file, ext){
			 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                // extension is not allowed 
				status.text('Povoleny jsou pouze obrázky, soubory s koncovkou JPG, PNG, nebo GIF');
				return false;
			}
			status.text('Nahrávám...');
		},
		onComplete: function(file, response){
//			alert(response);
			//On completion clear the status
			status.text('');
			//Add uploaded file to list
			if(isNaN(response)){
				$('<li></li>').appendTo('#files').text("Soubor " + file + " se nepodařilo nahrát.").addClass('error');
			} else{
				$('#imagesIds').val($('#imagesIds').val() + ", " + response);
				$('<li style="list-style: none;"></li>').appendTo('#files').html('<img src="../images/amylon/recepty/'+file+'" alt="" width="200px"/>');
			}
		}
	});
});
