/**
 * Store and namespace base functions used sitewide.
 */

//Create the Kumutu namespace KM
if (KM == null || typeof(KM) != "object") { var KM = new Object();}

/**
 * Set base URL for development environment.
 */
KM.setBaseUrl = function() {
	if (KM.baseUrl.indexOf('fmullenger') > -1) {
		KM.baseUrl = KM.baseUrl+'/kumutu';
	};
};

/**
 * Convenience base URL for ajax calls.
 */
KM.baseUrl = location.protocol+'//'+location.hostname;

/**
 * Handle ajax errors site wide.
 * 
 * Possible error codes include:
 * 400 : Bad Request, general use when request cannot be handled by the server
 * 403 : Forbidden, authorisation denied
 * 408 : Request Timeout, timeout on the server
 * 409 : Conflict, exception thrown or similar server side error
 * 500 : Internal Server Error, error server side
 */
KM.ajaxFailureHandler = function() {
	
	//Handling any ajax errors
	$("#ajax-error-dialog").ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){

		 if (XMLHttpRequest.status == '409' || XMLHttpRequest.status == '403') {
		 
		   //Unbind ajaxStop
		   $(this).unbind('ajaxStop');
		 
		   //Use the ajax-error message dialog to display an error
		   $('#ajax-error-response', this).text(XMLHttpRequest.responseText);
		
		   $(this).dialog({
				bgiframe: true,
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				}
			});
		   $(this).dialog('open');
		 }
	});
};

/**
 * Initiate the overlay for quickly signing in
 * Uses jquery UI dialog
 *
KM.initLogin = function() {
        $('#signin-button').click(function(){
        	//If dialog is not open and has not been opened, then open
        	if (!$('#signin').dialog('isOpen')) {
        		$('#signin').dialog('open');
        	    return false;
        	}   
        });

		$('#signin').dialog({
			modal: true,
			resizable: false,
			autoOpen: false,
			show: 'slide',
			title: 'Sign in to Kumutu',
			width: 680,
			height: 500,

		});
	
};


// Hides the overlay login box
KM.initCancelLogin = function(e) {
	$.each( ['#btnCancelLogin', '#linkCancelLogin'], function(i, n){
		$(n).click(function() {
			e.preventDefault();
		});
	});
};
*/

KM.loadingTabs = function() {
	$('#loading-tabs').hide();
};

$(document).ready(function(){
	KM.setBaseUrl();
	KM.ajaxFailureHandler();
	KM.loadingTabs();
	//KM.initLogin();
//	KM.initCancelLogin();
});
