/*
 This file is part of HiNii.

 Copyright (C) 2008 Carnino Claudio (jollyr0ger) <jollyr0g3r@gmail.com>
 */

/**
 * Ajax utilities
 * @author jollyr0ger
 *
 * Attention need:
 * -Prototype
 * -script.aculo.us
 * -HNjs/AjaxUtils.js
 */
var AjaxUtils = Class.create({ });
Object.extend(AjaxUtils, {
	

	/**
	 * Set the result of fieldvalidate or fieldregistered request, 
	 * into the form using css classes and setting error divs
	 * Expect that the return of the Ajax.Request is JSON data
	 * Created for RegisterController::fieldvalidateAction()
	 * and RegisterController::fieldregisteredAction()
	 * 
	 * @param 	Ajax.Response	response			String representing the Json data
	 * @param	string|DomNode	fieldChecked		Element checked
	 * @param	string			errorElem			Suffix of the name of the element, where put the error messages. Ussually a div
	 * @param	bool			keepPreviousMsg		If true does not eliminate previous error message 
	 * @param	string			styleWithMessage	Name of the css style to apply if there are messages
	 * @param	string			styleWithNoMessage	Name of the css style to apply if there arn't message
	 */
	setCheckResult: function(response, fieldChecked, errorElem, keepPreviousMsg, styleWithMessage, styleWithNoMessage){
		
		// Get the JSON object
		var responseJSON = response.responseJSON;
		
		// Get the elements
		fieldChecked = $(fieldChecked);
		errorElement = $(errorElem);
		

		// If any of these conditions match, there's an error
		if( responseJSON.result == true ){
			
			if(!keepPreviousMsg){
				// Clear the div
				Element.update(errorElement, '');
			}
			
			// Else promt the error messages
			fieldChecked.removeClassName(styleWithNoMessage);
			fieldChecked.addClassName(styleWithMessage);
			
	
			// Promt all the returned errors
			responseJSON.errors.each(function(item){

				errorElement.insert(item +"<br />");
			});
			
			
		}else{
	
			// Restore the style and remove the errors
			if(!keepPreviousMsg){
				fieldChecked.removeClassName(styleWithMessage);
				fieldChecked.addClassName(styleWithNoMessage);
			}
			
		}
		
		// If the error div has content, show it else hide it
		if(responseJSON.result){
			
			new Effect.SlideDown(errorElement, {afterStart: function(){
				
				new Effect.Appear(errorElement);
			}});
			
		}else{
			new Effect.SlideUp(errorElement, { afterFinish: function(){

				if(!keepPreviousMsg){
					// Clear the div
					Element.update(errorElement, '');
				}
				
			} });
		}
		
	},
	
	
	
	/**
	 * Light the image of the button to Bookmark a point
	 * @param	string		elementName		Image element, name
	 * @param	string		imgSource		Image souce path
	 */
	lightBookmarkButtons: function(elementName, imgSource){
		
		elementName = $(elementName);
		elementName.writeAttribute("onmouseover", "");
		elementName.writeAttribute("onmouseout", "");
		elementName.writeAttribute("src", imgSource);
	},
	
	
	
	/**
	 * Light the image of the button to Bookmark a point
	 * @param	string		imgElement			Image element, name
	 * @param	string		imgSource			Image souce path
	 * @param	string		sentenceElement		Sentence element, name
	 * @param	string		sentenceSource		Sentence to update
	 */
	lightBookmarkButtonsAlt: function(imgElement, imgSource, sentenceElement, sentenceSource){

		imgElement = $(imgElement);
		imgElement.writeAttribute("onmouseover", "");
		imgElement.writeAttribute("onmouseout", "");
		imgElement.writeAttribute("src", imgSource);

		sentenceElement = $(sentenceElement);
		sentenceElement.update(sentenceSource);
	}
	
	


});
	
	