var formsdone = false;

function __onresize() {

	$('.Abcum-mobile-tabbar-menu-menuitem').width(
		($('#Abcum-mobile-tabbar-menu').width() / $('.Abcum-mobile-tabbar-menu-menuitem').length) + 'px'
	);
		
	$Contacts.maps.resize();
	$Mailshots.maps.resize();
		
}

function __oncontextmenu(e) {

	e.preventDefault();

}

function __onbeforeunload(e) {
		
	if ($Editing.editing === true) {
	
		return "You have made changes which are not saved.";
			
	}

}

function __ontouchmove(e) {
	
	//e.preventDefault();

}

function __ongesturestart(e) {

	e.preventDefault();

}

function __onorientationchange(e) {

	__onresize;

	if (window.orientation == '0' || window.orientation == '180') {
			
		//alert('Please view this application in landscape view.');
		
	}
	
}


if (window.addEventListener) {

	//window.addEventListener('load', __onload, false);
	window.addEventListener('resize', __onresize, false);
	window.addEventListener('orientationchange', __onorientationchange, false);

	window.addEventListener('beforeunload', __onbeforeunload, false);

	window.addEventListener('touchmove', __ontouchmove, false);
	window.addEventListener('gesturestart', __ongesturestart, false);

	//window.addEventListener('contextmenu', __oncontextmenu, false);
	//document.addEventListener('contextmenu', __oncontextmenu, false);
	
}
else if (window.attachEvent) {

	//window.attachEvent('onload', __onload);
	window.attachEvent('onresize', __onresize);
	window.attachEvent('onorientationchange', __onorientationchange);

	window.attachEvent('onbeforeunload', __onbeforeunload);

	window.attachEvent('ontouchmove', __ontouchmove);
	window.attachEvent('ongesturestart', __ongesturestart);

	//window.attachEvent('oncontextmenu', __oncontextmenu);
	//document.attachEvent('oncontextmenu', __oncontextmenu);
	
}

$(document).ready(function() {
	
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	
	$Admin = new ABCUMGROUP.Admin();
	General = new ABCUMGROUP.General();
	$General = new ABCUMGROUP.General();
	$History = new ABCUMGROUP.History();
	$Editing = new ABCUMGROUP.Editing();
	$Loading = new ABCUMGROUP.Loading();
	$Strings = new ABCUMGROUP.Strings();
	$Cloner = new ABCUMGROUP.Cloner();
	$Deleting = new ABCUMGROUP.Deleting();
	$Search = new ABCUMGROUP.Search();
	$Templates = new ABCUMGROUP.Templates();

	Language = new ABCUMGROUP.Language();
	$Language = new ABCUMGROUP.Language();

	$Site = new ABCUMGROUP.Site();
			
	$Forms = new ABCUMMARKETING.Forms();
	Contacts = new ABCUMMARKETING.Contacts();
	$Contacts = new ABCUMMARKETING.Contacts();
	$Mailshots = new ABCUMMARKETING.Mailshots();
	$Messages = new ABCUMMARKETING.Messages();
	$Prints = new ABCUMMARKETING.Prints();
	$Campaigns = new ABCUMMARKETING.Campaigns();
	
	$Items = new ABCUMCOMMERCE.Items();
	$Users = new ABCUMCOMMERCE.Users();
	$Orders = new ABCUMCOMMERCE.Orders();

	$Pages = new ABCUMEDIT.Pages();

	$ABCUMPRINT_Prints = new ABCUMPRINT.Prints();
	
	$('body').initialise();
	
	__live();
	__onresize();
					
	$History.init();
	
	$Search.init();
	
	$('body').ajaxStart(function() {
				
		$Loading.automatic.show('small');
		
		$Loading.automatic.show('all');
				
	});
	
	$('body').ajaxStop(function() {
		
		$Loading.automatic.hide('small');

		$Loading.automatic.hide('all');
				
	});
	
	$.xhrPool = [];
	$.xhrPool.abortAll = function() {
		$(this).each(function(idx, jqXHR) {
			jqXHR.abort();
		});
		$.xhrPool = [];
	};
	
	$.ajaxSetup({
		beforeSend: function(jqXHR) {
			$.xhrPool.push(jqXHR);
		},
		success: function(data, textStatus, jqXHR) {
			var index = $.xhrPool.indexOf(jqXHR);
			if (index > -1) {
				$.xhrPool.splice(index, 1);
			}
		}
	});
	
	$('body').ajaxComplete(function(e, jqXHR, settings) {
					
		try {
			
			if (jqXHR.getResponseHeader('System-Javascript-Date') > FILEDATE) {
				$General.refresh();
			}
			
			//console.log(window.location.toString());
			//console.log(jqXHR.getAllResponseHeaders());
			
			var url = window.location.toString();
			if (url.indexOf('login', 0) != -1) return null;
			if (url.indexOf('register', 0) != -1) return null;
											
			if (jqXHR.getResponseHeader('System-Session-Needed') && !jqXHR.getResponseHeader('System-Session-Exists')) {
			
				$(this).html('<center><br /><br /><br />Please wait...</center>');
			
				$General.page(settings.url.replace('/a/', '/'));
				
			}
			
		}
		catch(error){

		}
		
	});


	
				
});
