var _adm_preview = false;

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_update_articles_list
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_update_articles_list()
{
	var next_year 	   = $('#id_adm_years option:selected').val();
	var next_category  = $('#id_adm_categories option:selected').val();

	var ajax = new msAjax();
	ajax.send(
		'adm_update_articles_list',
		function( retval ){
			if( !!retval.status == true ){
				$('#id_adm_articles').html( retval.options );
			}
		},
		{
			year	: next_year,
			category: next_category
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_update_articles_with_comments_list
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_update_articles_with_comments_list()
{
	var next_year 	   = $('#id_adm_years option:selected').val();
	var next_category  = $('#id_adm_categories option:selected').val();

	var ajax = new msAjax();
	ajax.send(
		'adm_update_articles_with_comments_list',
		function( retval ){
			if( !!retval.status == true ){
				$('#id_adm_articles').html( retval.options );
			}
		},
		{
			year	: next_year,
			category: next_category
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_show_article_content
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_show_article_content()
{
	var sel_art_id = $('#id_adm_articles option:selected').val();

	var ajax = new msAjax();
	ajax.send(
		'adm_show_article_content',
		function( retval ){
			if( !!retval.status == true ){
				$('#id_adm_article_title').val( retval.title );
				$('#id_adm_article_content').val( retval.text );
				$('#id_adm_article_date').val( retval.c_date );
				$('#id_adm_article_time').val( retval.c_time );
				$('#id_adm_article_tags').val( retval.tags );
				$('#id_adm_article_page').val( retval.page );
				$('#id_adm_article_rights').val( retval.rights );
				$('#id_adm_article_type').val( retval.type );
				$('#id_adm_article_clicks').val( retval.clicks );
				$('#id_adm_article_prevlen').val( retval.prevlen );

				$('#id_adm_article_closed').val( [] );
				if( retval.closed == 1 )
				{
					$('#id_adm_article_closed').val( ["cb"] );
				}

				$('#id_adm_articles_rel').html( retval.rel_articles );
				$('#id_adm_articles_rel_num').html( retval.rel_articles_num );
				$('#id_adm_article_book_id').val( retval.book_id );
			}
		},
		{
			art_id	: sel_art_id
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_show_article_content
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_show_new_article_content()
{
	var sel_art_id = $('#id_adm_articles option:selected').val();

	var ajax = new msAjax();
	ajax.send(
		'adm_show_article_content',
		function( retval ){
			if( !!retval.status == true ){
				$('#id_adm_article_content').val( retval.text );
				$('#id_adm_article_tags').val( retval.tags );
				$('#id_adm_article_title').val( retval.title );
			}
		},
		{
			art_id	: sel_art_id
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_refresh_related_list
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_refresh_related_list()
{
	var sel_art_id = $('#id_adm_articles option:selected').val();

	if( !!sel_art_id == false ){ return; }

	var ajax = new msAjax();
	ajax.send(
		'adm_refresh_related_list',
		function( retval ){
			if( !!retval.status == true ){
				$('#id_adm_articles_rel').html( retval.rel_articles );
				$('#id_adm_articles_rel_num').html( retval.rel_articles_num );
			}
		},
		{
			art_id	: sel_art_id
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_show_index_preview
 * @TYPE    : function
 * @NOTE	:
 * @PARAM   :
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_show_index_preview()
{
	var val = $('#id_adm_article_content').val();
	if( !!val == false ){ return false; }

	if( _adm_preview == true )
	{
		$('#id_adm_article_preview').html('');
		$('#id_index_preview_on_off').html('Index Vorschau anzeigen');
		_adm_preview = false;
		return false;
	}

	var ajax = new msAjax();
	ajax.send(
		'adm_show_index_preview',
		function( retval ){
			if( !!retval.status == true ){
				$('#id_adm_article_preview').html( retval.content );
				$('#id_index_preview_on_off').html('Index-Vorschau ausblenden');
				_adm_preview = true;
			}
		},
		{
			length		: $('#id_adm_article_prevlen').val(),
			article		: val
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_update_article
 * @TYPE    : function
 * @NOTE	:
 * @PARAM   :
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_update_article()
{
	var sel_art_id = $('#id_adm_articles option:selected').val();
	var pwd = $('#id_adm_article_pwd').val();
	var login = $('#id_adm_article_login').val();

	if( !!sel_art_id == false ){ return; }
	if( !!pwd == false || !!login == false )
	{
		alert("Fehler: Admin-Password und/oder Login eingeben!");
		return;
	}

	var title 	= $('#id_adm_article_title').val();
	var content = $('#id_adm_article_content').val();
	var date 	= $('#id_adm_article_date').val();
	var time 	= $('#id_adm_article_time').val();
	var tags 	= $('#id_adm_article_tags').val();
	var page 	= $('#id_adm_article_page').val();
	var rights 	= $('#id_adm_article_rights').val();
	var type 	= $('#id_adm_article_type').val();
	var clicks 	= $('#id_adm_article_clicks').val();
	var prevlen = $('#id_adm_article_prevlen').val();
	var closed 	= !!($('#id_adm_article_closed:checked').val()) ? 1 : 0;
	var relart 	= $('#id_adm_articles_rel').val() || [];
	var book_id = $('#id_adm_article_book_id option:selected').val();

	var ajax = new msAjax();
	ajax.send(
		'adm_update_article',
		function( retval ){
			if( !!retval == false ){
				alert("Fehler beim updaten ...");
			}
		},
		{
			a_title		: title,
			a_content	: content,
			a_date		: date,
			a_time		: time,
			a_page		: page,
			a_rights	: rights,
			a_clicks	: clicks,
			a_prevlen	: prevlen,
			a_closed	: closed,
			a_relart	: relart,
			a_type		: type,
			a_tags		: tags,
			a_book_id	: book_id,
			adm_pwd		: pwd,
			adm_login	: login,
			art_id		: sel_art_id
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_show_article_comments
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_show_article_comments()
{
	var sel_art_id = $('#id_adm_articles option:selected').val();
	var sel_cmt_typ= $(':checked').val();

	var ajax = new msAjax();
	ajax.send(
		'adm_show_article_comments',
		function( retval ){
			if( !!retval == true ){
				$('#id_adm_article_comments_num').html( retval.comments_num );
				$('#id_adm_article_comments').html( retval.comments );
			}
		},
		{
			cmt_typ	: sel_cmt_typ,
			art_id	: sel_art_id
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_do_comment_action
 * @TYPE    : function
 * @NOTE	:
 * @PARAM   :
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_do_comment_action()
{
	var sel_art_id = $('#id_adm_articles option:selected').val();
	var pwd = $('#id_adm_article_pwd').val();
	var login = $('#id_adm_article_login').val();

	if( !!sel_art_id == false ){ return; }
	if( !!pwd == false || !!login == false )
	{
		alert("Fehler :: Admin-Password und Login eingeben!");
		return;
	}

	var update	= $(":radio:checked[name^='com_act_'][value!='none']").get() || [];

	var elements = new Array();
	for( i=0; i<update.length; i++ ){
		// extract comment id from name-attribute
		var cmt_id = update[i].name.replace( /com_act_/, '' );

		element = new Array();
		element.push( update[i].value );
		element.push( cmt_id );

		element.push( encodeURIComponent( $('#id_cmt_name_' + cmt_id ).val()));
		element.push( encodeURIComponent($('#id_cmt_mail_' + cmt_id ).val()));
		element.push( encodeURIComponent($('#id_cmt_inet_' + cmt_id ).val()));
		element.push( encodeURIComponent( $('#id_cmt_text_' + cmt_id ).val()));

		elements.push( element );
	}

	var ajax = new msAjax();
	ajax.send(
		'adm_do_comment_action',
		function( retval ){
			if( !!retval.status != 0 ){
				alert( retval.status );
			}else{
				adm_show_article_comments();
			}
		},
		{
			elements_ary : elements
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_build_path
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_build_path()
{
	var category = $('#id_adm_dest_category option:selected').text();
	var directory= $('#id_adm_dir').val();

	var path = '/pages/blog/';

	if( config.is_live == false )
	{
		path = '/myonics' + path;
	}

	if( !!category != false )
	{
		path += category + '/';

		$('#id_adm_article_type').val( 0 )
		if( category == 'puzzles' )
		{
			$('#id_adm_article_type').val( 1 );
		}
	}
	if( !!directory != false )
	{
		path += directory + '/';
	}

	$('#id_adm_article_page').val( path + 'index.php' );

	return true;
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_page_dir_exist
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_page_dir_exist()
{
	var ajax = new msAjax();
	ajax.send(
		'adm_page_dir_exist',
		function( retval ){
			if( !!retval == false ){
				adm_insert_new_article();
			}else{
				alert("Page Directory existiert bereits!\nBitte ein anderes festlegen.");
			}
		},
		{
			page_dir : $('#id_adm_article_page').val()
		}
	);
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_insert_new_article
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_insert_new_article()
{
	var title	 = $('#id_adm_article_title').val();
	var category = $('#id_adm_dest_category option:selected').val();
	var directory= $('#id_adm_dir').val();
	var pwd 	 = $('#id_adm_article_pwd').val();
	var login 	 = $('#id_adm_article_login').val();

	if( !!pwd == false || !!login == false )
	{
		alert("Fehler :: Admin-Password und Login eingeben!");
		return;
	}

	if( !!title == false || category == false || directory == false )
	{
		alert("Fehler :: Titel, Kategorie und Verzeichnis eingeben!");
		return;
	}

	var ajax = new msAjax();
	ajax.send(
		'adm_insert_new_article',
		function( retval ){
			if( !!retval == false ){
				alert("Fehler aufgetreten! Konnte Artikel nicht anlegen!");
			}else{
				alert("Artikel erfolgreich angelegt!");
			}
		},
		{
			n_title		: title,
			n_category	: category,
			n_directory : directory,
			n_page		: $('#id_adm_article_page').val(),
			n_content	: $('#id_adm_article_content').val(),
			n_tags		: $('#id_adm_article_tags').val(),
			n_rights	: $('#id_adm_article_rights').val(),
			n_type		: $('#id_adm_article_type').val(),
			n_clicks	: $('#id_adm_article_clicks').val(),
			n_prevlen	: $('#id_adm_article_prevlen').val(),
			n_closed	: !!($('#id_adm_article_closed:checked').val()) ? 1 : 0,
			n_book_id	: $('#id_adm_book option:selected').val(),
			feeds		: !!($('#id_adm_feeds_cb:checked').val()) ? 1 : 0,
			feeds_text	: $('#id_adm_feed_excerpt').val(),
			category_name : $('#id_adm_dest_category option:selected').text(),
			adm_pwd		: pwd,
			adm_login	: login
		}
	);
//
//	var retval = ajaxWidgetHandler(
//						'adm_insert_new_article',
//						function( retval )
//						{
//							if( retval.value.status == 1 )
//							{
//								alert("Artikel erfolgreich angelegt!");
//							}
//							else
//							{
//								alert("Fehler " + retval.value.status + " aufgetreten!");
//							}
//						},
//						{
//							async 		: true,
//							n_title		: encodeURIComponent( title ),
//							n_category	: category,
//							n_directory : directory,
//							n_page		: $('#id_adm_article_page').val(),
//							n_content	: encodeURIComponent( $('#id_adm_article_content').val()),
//							n_tags		: encodeURIComponent( $('#id_adm_article_tags').val()),
//							n_rights	: $('#id_adm_article_rights').val(),
//							n_type		: $('#id_adm_article_type').val(),
//							n_clicks	: $('#id_adm_article_clicks').val(),
//							n_prevlen	: $('#id_adm_article_prevlen').val(),
//							n_closed	: !!($('#id_adm_article_closed:checked').val()) ? 1 : 0,
//							n_book_id	: $('#id_adm_book option:selected').val(),
//							feeds		: !!($('#id_adm_feeds_cb:checked').val()) ? 1 : 0,
//							feeds_text	: encodeURIComponent( $('#id_adm_feed_excerpt').val()),
//							category_name : $('#id_adm_dest_category option:selected').text(),
//							adm_pwd		: pwd,
//							adm_login	: login
//						}
//					);
//
//	return true;
}

/* -------------------------------------------------------------------------- *
 * @NAME    : adm_enable_feed_excerpt
 * @TYPE    : function
 * @NOTE	:
 * @RETURN  :
 * -------------------------------------------------------------------------- */
function adm_enable_feed_excerpt()
{
	if( !!$('#id_adm_feeds_cb:checked').val())
	{
		$('#id_adm_feed_excerpt').attr("disabled","");
		return;
	}

	$('#id_adm_feed_excerpt').attr("disabled", "disabled");
}
