/**
 * 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.indexOf('sgan') > -1 || KM.baseUrl.indexOf('roff') > -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');
		 }
	});
};

// Displays the overlay login box
KM.initLogin = function(e) {
	$('#login-button').click(function(e) {
		e.preventDefault();
		//$('#login-form').dialog();
	});
};

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

//Loads font for headings and run immediately
KM.initCufon = function() {
	Cufon.replace('.cufon', { fontFamily: 'Gotham' });
	Cufon.replace('.grunge', { fontFamily: 'SM_pigisM' });

};
KM.initCufon();

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

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