
function loading( message ) {
	if (message) { $(".loading-text").html(message);}	
	var x = ( $(window).width()  - $(".aniloading").width()) / 2;
	var y = $(document).scrollTop() + ($(window).height() / 2) - 100;


	$(".aniloading").css( {
		left : x + "px",
		top : y + "px",
		position : 'absolute',
		zIndex : '100'
	});
	$('.aniloading_fullscreen').css({
		width: $(window).width() + 'px',
		height: $(window).height() + 'px'
		});
	$(".aniloading_fullscreen").fadeTo('fast', 0.5);
	$(".aniloading").fadeTo('slow', 0.7);
	}

function loading_close(time) {
	if(time == '') { time = 1000; }
		setTimeout(function() {
		$(".aniloading").fadeOut('slow');
		$(".aniloading_fullscreen").fadeOut('slow');
		}, time);
	}

function show_error(msg, arg) {

	$(".aniloading").fadeOut('slow').css({display: 'none'});
	
	$(".loading-text").html('<div class="error_div">'+msg+'<div class="error_close_buttom_div"><button class="error_close_buttom">'+error_close+'</button></div></div>');
	
	var x = ( $(window).width()  - 300  ) / 2;
	var y = $(document).scrollTop() + ($(window).height() / 2) - 100;
	
	$(".aniloading").css({
		left : x + "px",
		top : y + "px"
	}); 
	
	$(".aniloading").fadeIn('slow');
	$('.error_div').css({display: 'run-in'});
	$(document).keydown(function(event){
    if (event.which == 27) {
		$(".aniloading").fadeOut('slow');
		$(".aniloading_fullscreen").fadeOut('slow');
		 }
	});

	setTimeout(function() {
		$(".aniloading").fadeOut('slow');
		$(".aniloading_fullscreen").fadeOut('slow');
		}, 5000);
	
	$('.error_close_buttom').mouseup(function() {
		$(".aniloading").fadeOut('slow');
		$(".aniloading_fullscreen").fadeOut('slow');
		});	
		}
			
function explode(delimiter,item) {
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);

while (tempString.indexOf(delimiter)>0) {
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}

tempArray[Count]=tempString;
return tempArray;
} 
		
function comment_navigate(page, news) {
	loading(loading_msg);
		$.post('../core/AJAX/showcomments.php', 'page='+page+'&news='+news, function(data) {
			$('.ani_news_comment').html(data);
				loading_close(500);			
		}).fail(function() {
			 show_error(error_internet_connect);
			});
	}
	
function add_comment() {
	
		loading(loading_msg);
		newsid = $('.ani_news_comment').attr('aniid');
		ed = tinyMCE.get('ani_comment_text');
		text = ed.getContent();
		$.post('../core/AJAX/addcomments.php', 'news='+newsid+'&text='+text).done(function(data) {
					if(data == 'good') {
						$.post('../core/AJAX/showcomments.php', 'page=last&news='+newsid,function(data) {

							$('.ani_news_comment').html(data);
							ed.setContent('');
							loading_close(500);
						}).fail(function() {show_error(error_internet_connect);});

					} else {
							show_error(data); 
					}
			}).fail(function() {show_error(error_internet_connect);});
			
}

function comment_menu(id) {
		dom = $('.ani_comment_admin_img[aniid='+id+']');
		el = dom.next();
		pos = dom.offset();
		el.css({
			left: pos.left+dom.width(),
			top: pos.top,
			display: 'block'});
		el.mouseleave(function() {
			el.css('display', 'none');
			});	
	}

function comment_del(id) {
	loading(loading_msg);
		newsid = $('.ani_news_comment').attr('aniid');
		$.post('../core/AJAX/delcomments.php', 'id='+id+'&news='+newsid, function(data) {
			$.post('../core/AJAX/showcomments.php', 'page='+$('.ani_pag_current').attr('aniid')+'&news='+newsid).done(function(data) {
				$('.ani_news_comment').html(data);
				loading_close(500);
					});
			}).fail(function() {show_error(error_internet_connect);});;
	}

function comment_edit(id, loading_text){

loading(loading_msg);
html = $('.ani_one_comment[aniid='+id+']').html();
data = '<div class="ani_comment_edit_div" style="display:none;"><textarea id="ani_comment_text_edit">'+html+'</textarea><button class="ani_comment_button_edit">'+loading_text+'</button></div>';

	$('.ani_one_comment[aniid='+id+']').html(data);
	$('.ani_comment_edit_div').fadeIn('fast');
	$('#ani_comment_text_edit').tinymce({
			script_url : '/core/editor/tiny_mce/tiny_mce.js',
			theme : "advanced",
			plugins : "emotions,inlinepopups",
			theme_advanced_buttons1:"bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,emotions",
			theme_advanced_buttons2:'',
			theme_advanced_buttons3:'',
			theme_advanced_buttons4:'',
			template_replace_values: {
				username : "Some User",
				staffid : "991234"
			}
		});		
		loading_close(500);

	$('.ani_comment_button_edit').mouseup(function() {
		loading(loading_msg);
		newsid = $('.ani_news_comment').attr('aniid');
		ed = tinyMCE.get('ani_comment_text_edit');
		text = ed.getContent();
			$.post('../core/AJAX/savecomment.php', 'news='+newsid+'&text='+text+'&id='+id, function(data) {
				if(data.match('good')) {
				$('.ani_one_comment[aniid='+id+']').html(text); 
					loading_close(500);
				} else {
					show_error(data); 
					}
				}).fail(function() {show_error(error_internet_connect);});
		});
		
	}



$(document).ready(function(){	

	$('.ani_logout').mouseup(function() {
		loading(loading_msg);
		$.post('../core/AJAX/logout.php', 'logout=logout', function(data) {
			location.href = location.href;
			}).fail(function() {show_error(error_internet_connect);});
		});
	
	$('.ani_login_form').submit(function() {
		loading(loading_msg);
		$.post('../core/AJAX/login.php', $(this).serialize(), function(data) {
			if(data.match('true')) {
					location.href = location.href;
				} else {
					show_error(data);
					}
			}).fail(function() {show_error(error_internet_connect);});
			return false;
		});
	
	$("#ani_register_form").submit(function() {
			var bool = $(this).valid();
			if(bool == true) {
				loading(loading_msg);
				$.post('../core/AJAX/registration.php', $(this).serialize(), function(data){
					if(data.match('true')) {
							show_error(reg_success);
							$(document).keydown(function(event){
								if (event.which == 27) {
									$(".aniloading").fadeOut('slow');
									$(".aniloading_fullscreen").fadeOut('slow');
									 location.href = location.href;
									 }
								});
								setTimeout(function() {
									$(".aniloading").fadeOut('slow');
									$(".aniloading_fullscreen").fadeOut('slow');
									location.href = location.href;
									}, 5000);
								
								$('.error_close_buttom').mouseup(function() {
									$(".aniloading").fadeOut('slow');
									$(".aniloading_fullscreen").fadeOut('slow');
									location.href = location.href;
									});	
								
				 	} else {
						show_error(data);
						}
					}).fail(function() {show_error(error_internet_connect);});
			}
			return false; 	
		});
	
$('.ani_edit_user').mouseup(function() {
	loading(loading_msg);
		userid = $(this).attr('aniid');
		$.post('../core/AJAX/edituser.php', 'userid='+userid, function(data){
				if(data.match('form') || data.match('input')) {
					$('#anicms_contentdiv').html(data);
					loading_close(500);		
					$('.ani_user_edit').submit(function() {
					$.post('../core/AJAX/saveuseredit.php', $(this).serialize()+'&userid='+userid, function(data){	
						if(data.match('true')) {
							location.href = location.href;
							} else {
							show_error(data);
								}
						
						});
						return false;
					});

					$('#ani_user_cansel').mouseup(function() {
						location.href = location.href;
						return false;
					});
					
					
				} else {
					show_error(data);
				} 
			}).fail(function() {show_error(error_internet_connect);});
	
	return false;
	});


	$('.ani_news_favorites').mouseup(function() {
		loading(loading_msg);
		newsid = $('.ani_news_comment').attr('aniid');
		$.post('../core/AJAX/newsfavorites.php', 'id='+newsid, function(data) {
			$('.ani_news_favorites').html(data);
		
			loading_close(500);
			}).fail(function() {show_error(error_internet_connect);});
		});

	$('#ani_checkbox_main').change(function(){
	
		if($(this).attr('checked')) {
			$('input[type=checkbox]').attr('checked', true);
			} else {
			$('input[type=checkbox]:checked').each(function() {
				$(this).removeAttr('checked');
				});
			}
		});

$('textarea.init_wys').each(function() {

		var wys_plugins;
		var buttons1;
		var buttons2;
		var buttons3;
		var buttons4;	
	var text = $(this).attr('class');
	if(text == 'init_wys full') {
		
		wys_plugins = "style,table,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,media,directionality,xhtmlxtras,advlist,inlinepopups";
		buttons1 ="bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect";
		buttons2 ="bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,|,forecolor,backcolor";
		buttons3 ="tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl";
		buttons4 = "";
		
		} else if (text == 'init_wys short') {
			
		wys_plugins = "emotions,inlinepopups";
		buttons1="bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,emotions";
		buttons2="";
		buttons3="";
		buttons4="";

			} else if (text == 'init_wys short-emo') {
	
		wys_plugins = "";
		buttons1="bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull";
		buttons2="";
		buttons3="";
		buttons4="";

			}
	
		$(this).tinymce({
			script_url : '/core/editor/tiny_mce/tiny_mce.js',
			theme : "advanced",
			plugins : wys_plugins,
			theme_advanced_buttons1 :buttons1,
			theme_advanced_buttons2 :buttons2,
			theme_advanced_buttons3 :buttons3,
			theme_advanced_buttons4 :buttons4,
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});			

	});

});                                                                                                          
