var editing = false;
var saved = false;
var code;
var type;

$(function()
{
	var url = '/language_codes.php';

        $('.translation .save').click(save);
        $('.translation .cancel').click(cancel);
	$('.translation.name, .translation.description').change(function(){
		saved = false;
	});

	$.getJSON(url, function(data)
	{
		$('form.translate .language').append('<option>English</option>');

		$.each(data, function(ix, obj) {
			$('form.translate .language').append('<option>' + obj.desc_ + '</option>');
		});
	});
});



function edit()
{   
        $('.translation .save').attr('disabled', false);
		$('.translation .cancel').attr('disabled', false);
		
        $('#savedmsg').hide();
	if(editing && !saved) {
		if(!confirm('You have not saved your edit. Are you sure you wish to continue?')) {return false;}
	}
	editing = true;
	
	
	//$('.translation .name').val($(this).parent().find('.text').text());
	//$('.translation .description').text('editing'+ $(this).parent().data('code'));
       $('#catalogue .current').removeClass('current'); 
	$(this).parent().addClass('current');
		
	code = $(this).parent().data('code');
	type = $(this).parent().data('type');

	if(type == 'prod'){
		url = '/catalogue_data.php?prod='+code;
	}else{
		url = '/catalogue_data.php?cat='+code;
	}


	$.getJSON(url, function(data){
            $('.translation .name').val('');
            $('.translation .description').text('');
            $('.translation .name').val(data.title);
            $('.translation .description').text(data.description);
		saved = false;
        })
}

function cancel()
{
	$('#catalogue .current').removeClass('current');

	$('.translation .save').attr('disabled', true);
	$('.translation .cancel').attr('disabled', true);
	editing = false;
	
	$('#savedmsg').hide();
	
	$('.translation .name').val('');
	$('.translation .description').text('');
	
	return false;
}

function save(){
    $.post(
        "/uploadcatlang.php",
            {title: $('div.translation input.name').val(), description: $('div.translation textarea.description').val(), code: code, type:type},
            function(response){
				if(response == 'LoginError'){alert('Your session has timed out, please log back in.');return false;}
				saved = true; $('#savedmsg').fadeIn().delay(1000).fadeOut();
				$('#catalogue .current').removeClass('current');
			}
        );
            
    return false;
}




/*

var key = 'AIzaSyDQjvLrJxtuTau67JUlXL8k52PwbN3hSGk';
var lang = 'de';
var callbacks = [];
var callbackID = 0;

function enableSave(el)
{
	if(el && el.type) { el = $(this); }
	el.parent().parent().find('img.save').show();
	el.parent().parent().find('img.disabled').hide();
}

function translate()
{
	var text = escape($(this).parent().parent().find('td.original').text());

	var input = $(this).parent().parent().find('input.entry');
	input.addClass('loading');
	input.val('translating...');

	callbacks[callbackID] = function(response)
	{
		var translation = response.data.translations[0].translatedText;
		input.val(translation);
		input.removeClass('loading');
		enableSave(input);
	};
	
	var url = 'https://www.googleapis.com/language/translate/v2?key=' + key
			+ '&source=en'
			+ '&target=' + lang
			+ '&q=' + text
			+ '&callback=' + 'callbacks[' + callbackID + ']';

	callbackID++;

	$.ajax({
		url : url,
		dataType: 'jsonp',
		cache: false
	});
}

function saveTranslation()
{
	$(this).hide();
	$(this).siblings('.saving').show();

	var el = $(this);
	setTimeout(function(){
		translationSaved(el);
	},1000);
}

function translationSaved(element)
{
	element.parent().parent().addClass('done');
	element.siblings('.saving').hide();
	element.show();
	element.parent().next().find('.saved').show();
}

*/

