// Init the originalTheme variable if it not exists
// Whe use the origianlTheme variable to store all 
// parameters and functions which is needed globaly
if (typeof originalTheme === 'undefined') {
	var originalTheme = {};
}

(function() {
	
	/**
	* This is part of a login check for ajax requests. RWAR (RefinedWiki Ajax Request)
	* If the response data don't start with RWAR then the the user is no longer authenticated. 
	* Advice the user that the user are not longer authenticated 
	*/
	originalTheme.validateAjaxResponse = function (html)
	{
		if (html.substring(0,4) == "RWAR")
		{
			return html.substring(4);
		}
		else
		{
			return "<div class='warning_box'><a href='" + contextPath + "/login.action?os_destination=" + encodeURIComponent(window.location) + "'>" + notLoggedIn +"</a></div>";
		}
		
	}
	
	
	
	//
	// Favourite actions spaces
	//
	var operationInProgressArray = new Array(); // use this array to prevent the user from triggering off another labelling operation when one is in progress
	
	originalTheme.addOrRemoveFav = function (spaceKey, imgElement)
	{
			
		if (operationInProgressArray[imgElement.id] == null) {
	
		   operationInProgressArray[imgElement.id] = true;
		
		   var url;
		   var newClass;
		   var oldClass;
		
		   if (jQuery(imgElement).hasClass("yellow_star")) { // if on
		       url = contextPath + "/ajaxactions/remove-favourite-space.action";
		       newClass = "gray_star";
		       oldClass = "yellow_star";
		       jQuery(imgElement).removeClass("yellow_star"); // Remove the star. The loader will now be visible instead of the star
		   }
		   else {
		       url = contextPath + "/ajaxactions/add-favourite-space.action";
		       newClass = "yellow_star";
		       oldClass = "gray_star";
		       jQuery(imgElement).removeClass("gray_star"); // Remove the star. The loader will now be visible instead of the star
			
		   }
		
		   AJS.safe.ajax({
		       url: url,
		       type: "POST",
		       data: { "spaceKey" : spaceKey },
		       success: function() {
		    	   // Adds the new class and automativally removes the loader. 
		    	   jQuery(imgElement).addClass(newClass); 
		           operationInProgressArray[imgElement.id] = null;
		       },
		       error: function(xhr, text, error) {
		           alert("Error : " + text);
		           operationInProgressArray[imgElement.id] = null;
		           jQuery(imgElement).addClass(oldClass); 
		        }
		   });
		}	
	}
	
	
	//
	// Favourite actions abstractpages
	//
	originalTheme.addOrRemoveFavPage = function (pageId, imgElement)
	{
			
		var url;
		var newClass;
		var oldClass;
		
		if (jQuery(imgElement).hasClass("yellow_star")) { // if on
			url = contextPath + "/ajaxactions/remove-favourite-page.action";
			newClass = "gray_star";
			oldClass = "yellow_star";
			jQuery(imgElement).removeClass("yellow_star"); // Remove the star. The loader will now be visible instead of the star
		}
		else {
			url = contextPath + "/ajaxactions/add-favourite-page.action";
			newClass = "yellow_star";
			oldClass = "gray_star";
			jQuery(imgElement).removeClass("gray_star"); // Remove the star. The loader will now be visible instead of the star
		}
			
		AJS.safe.ajax({
			url: url,
			type: "POST",
			data: { "pageId" : pageId },
			success: function() {
				// Adds the new class and automativally removes the loader. 
				jQuery(imgElement).addClass(newClass); 
			},
			error: function(xhr, text, error) {
				alert("Error : " + text);
				jQuery(imgElement).addClass(oldClass); 
	    	}
		});
	}

})();

