var App = {
	delNote: function() {
		if($('.note-error')) { $('.note-error').remove(); }
		if($('.note-success')) { $('.note-success').remove(); }
	},
	note: function(m,e) {
		if(m != "") {
			//<div class="note-error">Пустое сообщение.</div>
			var n = 'note-' + (e == true ? 'error' : 'success' );
			App.delNote();
			$( ($('#location').text() != undefined ? '#location' : '#main' )).after('<div class="' + n + '">' + m + '</div>');
		}
	},
	post: function(a,e) {
		var type = $(a).attr('data-type');
		$('.p-error').css('display','none');
		if(type == "arbour") {
			var f = $(a);
			var submit = f.find('input[type="submit"]');
			var submitVal = submit.val();
			TYPE_DATA = {
				URL: "/api/arbour.php",
				DATA: f.serialize(),
				BEFORE: function(s) {
					submit.attr('disabled','disabled');
					submit.val('Отправляется..');
				},
				SUCCESS: function(d) {
					console.log(d);
					if(!d.error) {

						$('textarea[name="message"]').val("");

						var HTML = '<div class="list" id="arbour_p' + d.id + '">\
										<div class="profile-list-r">\
											<div style="display: block;">' + d.page + ' <span class="_font-m _gray">' + d.time + '</span></div>\
											<span style="vertical-align: sub;">' + d.message + '</span>\
										</div>\
									</div>';


						//$('.app-post-text', $(HTML)).html(d.text_f);
						var posts = $('.post-list-all');
						posts.html(HTML + posts.html());


					} else {
						App.note(d.error,true);
						//$('.p-error').text(d.error);
						//$('.p-error').css('display','block');
					}
					submit.removeAttr('disabled');
					submit.val(submitVal);
				}
			};
		}
		$.ajax({
			url : TYPE_DATA.URL,
			data : TYPE_DATA.DATA,
			dataType: "json",
			beforeSend: function(s){
				App.delNote();
				App.QUERY = true;
				TYPE_DATA.BEFORE(s);
			}, 
			type : "POST",
			success: function (d){
				TYPE_DATA.SUCCESS(d);
				App.QUERY = false;

			}
		});
	},
	Init: function(r) {
		$('.post-add').on('submit', function(){
			App.post(this,event);
			App.On = true;
			r.preventDefault();
			return false;
		});
	},
	On: false
}
function ajaxLoad(href,tt) {
	var e = event;
if (href != undefined) {
			if($('#ixrf')) { $('#ixrf').remove(); }
			var el = document.createElement("iframe");
			el.id = 'ixrf';
			el.style.width = "0px";
			el.style.height = "0px";
			el.style.display = "none";
			el.src = href;
			$('#main').append(el);
			$('#ixrf').load(function() {
				if($(this).attr('src') == href) {
					var x = $(this).contents();
					var t = x.find('title').text();
					document.title = t;
					var l = x.find('#main').html();
					$('#main').html(l);
					window.history.replaceState(l, t);
					$(document).scrollTop('0');
					App.Init(event);
					if(tt == true){ history.pushState(l, document.title, href); }
				}
			});
			e.preventDefault();
		}	
}
if(App.On != true) {
jQuery(document).ready(function() {
	App.Init(event);
	if (history.pushState){
	$(window).on('popstate', function(event){
	var loc = event.location || ( event.originalEvent && event.originalEvent.location )|| document.location;
		ajaxLoad(loc.href, false);
	});
	}
	$(document).on('click', 'a', function(e) {
		var href = $(this).attr('href');
		ajaxLoad(href,true);
	});
});
}
