
lang = get_site_lang();

$(document).ready(function () {
	$(".inline_edit").editable('/' + lang + "/admin/edit", {
		indicator : loader,
		tooltip   : "Click to edit...",
		height: 18,
                width:150,
		style		: "inherit",
		placeholder :'<em>Click to edit...</em>',
		onblur: 'submit'
	});

	$(".inline_textarea").editable('/' + lang + "/admin/edit", {
		type: 'textarea',
		indicator : loader,
		tooltip   : "Click to edit...",
		width: 530,
		height: 60,
		style		: "inherit",
		placeholder :'<em>Click to edit...</em>',
		submit: 'OK',
		cancel: 'Cancel',
		data: function(value, settings) {
			return $(this).attr('plain').replace(/\|/g, '\n');
		},
		onsubmit: function(settings, original) {
			entry = $('textarea', $(this)).val();
			entry = entry.replace(/\n/g, '|');
			$('textarea', $(this)).val(entry);
			return true;
		},
		onreset: function(settings, original) {
			return $(original).attr('plain').replace(/\|/g, ', ');
		},
		callback: function(modified, settings){
			$(this).attr('plain', modified);
			$(this).text(modified.replace(/\|/g, ', '));
                        $(this).parent().removeClass('unreview');
                        $('#bio_verify').remove();
			var row = $(this).parent().parent();
			if ($('.approve', row))
			{
				$('td', row).css('font-weight', 'normal');
				$('.approve', row).before('Approved');
				$('.approve', row).remove();
			}
		}
	});

	subscribeTagsInput('.edit_tags', '#tags_list', '/'+ lang +'/admin/tag');
        subscribeTagsInput('.edit_categories', '#category_list', '/' + lang + '/admin/category');
        subscribeTagsInput('.edit_genres', '#genres_list', '/admin/genre');
        subscribeTagsInput('.edit_media_tags', '#tags_list', '/admin/edit_media_tags');

        $(".inline_select_sex").editable("/admin/edit", {
		indicator : '<img src="/images/loader.gif">',
		data   : "{'male':'male','female':'female'}",
		type   : "select",
		submit : "",
		width: 80,
		style  : "inherit",
		placeholder :'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
		onblur: 'submit'
	});

	$('.deletable').click(function (){
		deleteEntry($(this));
		return false;
	});

	$('.approve').click(function (){
		var entry = $(this);
		$.post('/admin/approve', {id: entry.attr('id')}, function(data, textStatus) {
			if (data=='')
			{
				var row = entry.parent().parent();
				entry.before('Approved');
				entry.next('img').remove();
				entry.remove();
				$('td', row).css('font-weight', 'normal');
			}
		});
		entry.after(loader);
		return false;
	});

	$('.unwhite').click(function (){
		var entry = $(this);
		$.post('/admin/unwhite', {id: entry.attr('id')}, function(data, textStatus) {
			if (data=='')
			{
				var row = entry.parent().parent();
				var white = $('.match_terms_white .inline_textarea', row);
				var normal = $('.match_terms .inline_textarea', row);
				normal.text(white.text()).attr('ui', white.attr('ui')).attr('plain', white.attr('plain'));
				white.html('<em>Click to edit...</em>').attr('ui', '').attr('plain', '');

				entry.before('Done');
				entry.next('img').remove();
				entry.remove();

				$('td', row).css('font-weight', 'normal');
				$('.approve', row).before('Approved');
				$('.approve', row).remove();
			}
		});
		entry.after(loader);
		return false;
	});

	$('.white').click(function (){
		var entry = $(this);
		$.post('/admin/white', {id: entry.attr('id')}, function(data, textStatus) {
			if (data=='')
			{
				var row = entry.parent().parent();
				var white = $('.match_terms_white .inline_textarea', row);
				var normal = $('.match_terms .inline_textarea', row);
				white.text(normal.text()).attr('ui', normal.attr('ui')).attr('plain', normal.attr('plain'));
				normal.html('<em>Click to edit...</em>').attr('ui', '').attr('plain', '');

				entry.before('Done');
				entry.next('img').remove();
				entry.remove();

				$('td', row).css('font-weight', 'normal');
				$('.approve', row).before('Approved');
				$('.approve', row).remove();
			}
		});
		entry.after(loader);
		return false;
	});


	$(".inline_add").keydown(function(event){
		return inline_add(event, this);
	});

});

function subscribeTagsInput(css, textbox, url)
{
    $(css).tagSuggest({
        tags: $(textbox).text().split('|'),
        separator: ', ',
        matchClass: 'tag_matches'
    });

    $(css).keyup(function(ev){
        if (ev.keyCode==13)
        {
            saveTags(this, url);
            return false;
        }
        else if (ev.keyCode==27)
        {
            //on escape just remove the currently edited tag
            var tags = $(this).val().split(', ');
            tags.pop();
            $(this).val((tags.length > 0)? (tags.join(', ')+', '):'');
            return false;
        }
        return true;
    });
}

function saveTags(textbox, save_url)
{
	$.ajax({
		data:'slug=' + $(textbox).parent().find('.slug').text() + '&value=' + $(textbox).val(),
		dataType:'html',
		type:'post',
		url: save_url,
		success:function(data, textStatus) {
			$(textbox).after('<span class="good">Saved!!!</span>');
			$(textbox).next('span').fadeOut('slow', function(){
				$(this).remove();
			});
		},
		error:function(XMLHttpRequest, textStatus, errorThrown) {
			$(textbox).after('<span class="bad">Not Saved</span>');
			$(textbox).next('span').fadeOut('slow', function(){
				$(this).remove();
			});
		}
	});
}

deleteEntry = function(entry)
{
    if (!confirm('Are you sure to delete?'))
        return false;
    $.post('/' + lang + '/admin/delete', {id: entry.attr('id')}, function(data, textStatus) {
            if (data=='')
            {
                    entry.next('img').remove();
                    entry.parent().parent().fadeOut(500, function() {entry.parent().parent().remove();});
            }
    });
    entry.after(loader);
};

editEntry = function(entry)
{
	$.post('/admin/edit', {id: entry.attr('id')}, function(data, textStatus) {
		if (data=='')
		{
			entry.before('Moderated');
			entry.remove();
		}
	});
};

deleteTweet = function(entry,id)
{
    if (!confirm('Are you sure to delete?'))
        return false;
    $.post('/admin/delete_tweet/'+id, {}, function(data, textStatus) {
            if (data=='')
            {
                    entry.next('img').remove();
                    entry.before('Deleted');
                    entry.remove();
            }
    });
    entry.after(loader);
};

var inline_add = function(event, context, editMode) {
	if (event.keyCode!=13)
		return true;

	event.returnValue = false; //for IE

	editMode = editMode || false;

	var parent = $(context).parent();
	var main_input = $('.main_input', parent);
	var other_input = $('.other_input', parent);

	var main_value = jQuery.trim(main_input.val());
	var other_value = '';
	if (other_input.length>0)
		other_value = jQuery.trim(other_input.val());

	main_value = jQuery.trim(main_value);
	if (main_value.length==0)
		return false;
	if (other_value!='' && !other_value.match(/(http:\/\/[a-z0-9.-?_&%-]*)/i))
		return false;

	//hide the add text box
	var table_row = editMode? main_input.parent().parent().parent():main_input.parent().parent();
	$(table_row).hide();
	$(table_row).after('<tr><td width="100%" colspan="3"><img src="/images/loader.gif"></td</tr>');


	//fire the request
	$.post(editMode? '/admin/edit':'/admin/add', {id:main_input.attr('id'),value:main_input.val(),other: other_value}, editMode? (function(data){
				//delete the loader
				$(table_row).next().remove();
				//show new entry
				$(table_row).before(data);
				//remove old entry
				$(table_row).remove();
			}):(function(data){
				//delete the loader
				$(table_row).next().remove();
				$(table_row).before(data);

				if (other_value)
					other_input.val('');
				else {
					$(".inline_edit").editable("/profile/edit", {
						indicator : "<img src='/images/loader.gif'>",
						tooltip   : "Click to edit...",
						width: 80,
						height: 18,
						style		: "inherit",
						placeholder :'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
					});
				}

				main_input.val('');
				$(table_row).show();
				main_input.focus();
			})
		);
};
