if (typeof(DRAGPAD) == 'undefined') {
	DRAGPAD	= {};
}

DRAGPAD.Global	= function() {

	var init = function() {		
		closeForm();
		ajaxThrobber();
		
		$('a.external').attr('target', '_blank');		
		//setTimeout(function() {$('#status').fadeOut()}, 10000);
	}

	var resizeMargin = function(type) {
		var originalpadding = 0;
		var topbarh = $('#topbar').height();
		var toolbarh =  $('#toolbar').height();
		var height = parseInt(topbarh) + parseInt(toolbarh);
		if (type == 'open') {
			originalpadding = parseInt($('#sitePageHTML').css('padding-top')) + 13; 
			$('#sitePageHTML').css('padding-top', height + 18);
		}
		else if (type == 'close') {
			$('#sitePageHTML').css('padding-top', height+2);
		}
		else if (type == 'reset') {
			$('#sitePageHTML').css('padding-top', originalpadding+34);
		}				
	}
	
	var statusUpdate = function(type, msg) {
		$('#status').show();		
		if (type == 'success') {
			$('#status').html('<div id="alert" class="success center"><img src="/img/icons/tick.png" class="icon" alt="" />'+msg+'</div>');
		}
		else if (type == 'error') {
			$('#status').html('<div id="alert" class="error center"><img src="/img/icons/error.png" class="icon" alt="" />'+msg+'</div>');
		}		
		setTimeout(function() {
			$('#status.top').fadeOut();
			$('#alert').fadeOut();			
		}, 10000);
				
	}
	
	//checks for errors and if the user has been logged out
	var checkResponse = function(data) {		
		if (data.errorMsg) {
			statusUpdate('error', data.errorMsg);
			return false;
		}
		else if (data.sessionDeath) {
			window.location.pathname = '/login';
			return false;
		}
		else if (typeof(data) == 'undefined') {
			statusUpdate('error', 'Something errored, no message sent');
			return false;
		}
		else if (data == '') {
			statusUpdate('error', 'Something errored, no message sent');
			return false;
		}	
		return true;		
	}
	
	var closeForm = function() {
		$('.closeForm').click(function(e) {
			e.preventDefault();
			$(this).closest('.form').hide();
		});		
	}
	
	var ajaxThrobber = function(call) {
		$('body.editor').ajaxStart(function() {
			$('#status').show();
			$('#status').html('<div id="alert" class="throbber center"><img src="/img/throbber.gif" class="icon" alt="" />Requesting..</div>');
		});
	}
	
	var confirmAction = function(msg) {
		var answer = confirm(msg);
  		return answer;
	}
	
	return {
		init: init,
		resizeMargin: resizeMargin,
		statusUpdate: statusUpdate,
		checkResponse: checkResponse,
		ajaxThrobber: ajaxThrobber,
		confirmAction: confirmAction
	}
	
}();

jQuery(function($) { DRAGPAD.Global.init(); });
