/**
 * @author Melnaron
 */

// ajax request to add goods to order
function addToOrder(id) {
	var amount = $('#amount_'+id).val();
	if (! isNaN(amount) && amount > 0) {
		$.ajax({
			url: 'ajax/add_to_order.php',
			data: 'id='+id+'&amount='+amount,
			type: 'post',
			dataType: 'text',
			success: function(data) {
				if (data) {
					alert(data);
					$('#customer-menu span').show().find('a')
						.fadeOut('fast').fadeIn('fast').fadeOut('fast').fadeIn('fast')
					;
				}
			}
		});
	}
}

function sendOrder() {
	document.location = '?page=customer&show=order_form&send';
}

function showRecipe(recipe_id) {
	$.ajax({
		url: 'ajax/load_recipe.php',
		data: 'recipe_id='+recipe_id,
		type: 'post',
		dataType: 'json',
		success: function(data) {
			if (data.title && data.desc) {
				$('#recipes-popup h3').html(data.title);
				$('#recipes-popup .desc').html(data.desc);
				$('#recipes-popup').popup();
			}
		}
	});
}

// document ready init
$(document).ready(function() {
	
	// events for open & close popups
	$('.open-login').click(function() {
		$('#login-popup').popup();
	});
	$('.close-login').click(function() {
		$('#login-popup').fadeOut('slow', function() {
			$('#login-popup .error').remove();
		});
	});
	
	$('.open-reg').click(function() {
		$('#reg-popup').popup();
	});
	$('.close-reg').click(function() {
		$('#reg-popup').fadeOut('slow', function() {
			$('#reg-popup .error').remove();
		});
	});
	
	$('.open-feedback').click(function() {
		$('#feedback-popup').popup();
	});
	$('.close-feedback').click(function() {
		$('#feedback-popup').fadeOut('slow', function() {
			$('#feedback-popup .error').remove();
		});
	});
	
	$('#recipe-popup .close_for_today').click(function() {
		$('#recipe-popup').fadeOut('slow');
		document.location = '?norecipes';
	});
	
	// start random recipe timer
	if ($('#recipe-popup').text()) {
		var recipe_timer = setTimeout(function(){
			$.ajax({
				url: 'ajax/random_recipe.php',
				type: 'post',
				dataType: 'xml',
				success: function(data) {
					var title = $('rtitle', data).text();
					var desc = $('rdesc', data).text();
					if (title && desc) {
						$('#recipe-popup .title').html(title);
						$('#recipe-popup .desc').html(desc);
						if ($.browser.msie) {
							$('#recipe-popup').css('position', 'absolute');
						}
						$('#recipe-popup').fadeIn('slow');
					}
				}
			});
		}, 60000);
	}
	
	// re-popup popups =)
	$('.re-popup').popup();
	
	// browser check
	//if ($.browser.msie && $.browser.version == '6.0') {
	//	if(confirm(lang[1001])) {
	//		document.location = 'http://www.microsoft.com/windows/downloads/ie/getitnow.mspx';
	//	}
	//}
	
});
