/* 
 * Hides an object and displays a div containing a preloader
 * Div & Object should be named the say way (starting with l or o respectively)
 * Params: object	string		Name of the object to be shown or hidden
 * Uses prototype & script.aculo.us for DOM access and effects respectively
 */
function showLoader(object) {
	var duration = 0.3;  //Duration for Fade/Appear
	var obj = $(object);
	var loa = $(object+"l");
	Effect.Fade(obj, {duration: duration, 
		afterFinish: function(){
			Effect.Appear(loa, {duration: duration})
		}
	});
}

/* 
 * Hides a loader and displays a the loaded object
 * Div & Object should be named the say way (starting with l or o respectively)
 * Params: object	string		Name of the object to be shown or hidden
 * Uses prototype & script.aculo.us for DOM access and effects respectively
 */
function hideLoader(object) {
	var duration = 0.3;  //Duration for Fade/Appear
	var obj = $(object);
	var loa = $(object+"l");
	Effect.Fade(loa, {duration: duration, 
		afterFinish: function(){
			Effect.Appear(obj, {duration: duration})
		}
	});
}

/*
 * Cleans up a field on click and sets default text on blur (if empty)
 * Params
 * field: string or DOMId
 * text:  string
 * Requires prototype
 */
function fieldToggle(field, text) {
	if ($(field).value == text) {
		$(field).value = '';
	} 
	else if ($(field).value == '') {
		$(field).value = text;
	}
}

/*
 * Adds a trailing slash to a url if needed
 * Params:
 * url: string	Url we want to add the slash to
 */
function slashUrl(url) {
	var lastC = url.charAt(url.length-1);
	
	if(lastC != '/') {
		url = url + '/';
	}
	return url;
}


/*
 * Sets maxlength for a textarea
 */
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	/*console.log('checking');*/
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

/*
 * Fires an event when the maxlength is reached
 */
function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	//console.log(this.name);
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		//console.log(this.name);
	}

}


/*
 * Switch between elements using a scriptaculous animation
 */
function swapel(el1, el2) {
	var duration = 0.3;  //Duration for Fade/Appear
	var el1 = $(el1);
	var el2 = $(el2);
	Effect.Fade(el1, {duration: duration, 
		afterFinish: function(){
			Effect.Appear(el2, {duration: duration, 
					afterFinish: function() {
							if(Modalbox) {
								Modalbox.resizeToContent();
							}
			}
			})
			
		}
	});
}


/*
 * Switch between elements using a scriptaculous animation
 Params:
 id: ID of the element to be resized
 w: new width
 h: new height
 d: animation duration
 */
function resizeObj(id, w, h, d) {
	new Effect.Morph(id, {
	  style: {
	    width: w+'px',
	    height: h+'px'
	  }, // CSS Properties
	  duration: 0.5 // Core Effect properties
	});

}


/* Tool tips for Question post */
function show_message(id){
	var id = $(id);
	Effect.toggle(id,'appear',{ duration: 0.3 });
}


function hide_all(id){

}