function answer(where) {

	//Look for answer buttons
	if(where = $(where)) {
	
		answer = document.getElementsByTagName('span');
	
		for(i = 0; i < answer.length; i++) {
		
			if(answer[i].className.substr(0, 6) == 'answer') {

				answer[i].onclick = function() {
					
					new Effect.Highlight('messages', { endcolor: '#F8F8F8'});		
					user = this.className.substr(7);
					where.focus();
					where.value += '@'+user+': ';
			
				}
			}
		
		}
		
	}

}

function countChars(text, counter, max, extraText) {

	//Shows remaining characters in #counter
	text = $(text);
	counter = $(counter);
	
	text.onkeyup = function() {
	
		charsLeft = max - text.value.length;
		
		textData = document.createTextNode(charsLeft+' '+extraText)
		counter.removeChild(counter.firstChild);
		counter.appendChild(textData)
		
		if(charsLeft >= 0)
			counter.className = '';
		else
			counter.className = 'tooMany';
		
	}

}

function browserWindowSize() {
	var browserWinWidth = 0, browserWinHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		browserWinWidth = window.innerWidth;
		browserWinHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		browserWinWidth = document.documentElement.clientWidth;
		browserWinHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		browserWinWidth = document.body.clientWidth;
		browserWinHeight = document.body.clientHeight;
	}

	return { width: browserWinWidth, height: browserWinHeight}

}

function getYScroll() {
	return (window.pageYOffset || document.documentElement.scrollTop || 0);
}

function getPageHeight() {
	return $('container').getDimensions().height;
}

function setupHTMLTextareas() {

	tinyMCE.init({
		language: 'es',
		mode : "specific_textareas",
		editor_selector : "html",
		theme : "advanced", 
		theme_advanced_buttons1 : "bold,italic,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,paste,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : ""
	});

}

function setupWelcomeMessage() {
	
	if($('welcomeMsg')) {
		
		var days = 90;
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		
		$$('#welcomeMsg span')[0].onclick = function() {
			Element.extend(this).up().fade({from: 1, to: 0})
			
			document.cookie = "closedMsg=1"+expires+"; path=/";
		}
		
		$$('#welcomeMsg a')[0].onclick = function() {
			document.cookie = "closedMsg=1"+expires+"; path=/";
		}
		
	}
	
}

document.observe("dom:loaded", function() {
	answer('message');
	setupHTMLTextareas()
	setupWelcomeMessage();
	
	$$('a.deleteMessage').each(function(el) {
		el.onclick = function() {
			if(!confirm("¿Deseas borrar el mensaje seleccionado?"))
				return false;
		}
	})
})
