function limitChars(textid, limit, infodiv) {
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	
	if(textlength > limit) {
		$('#' + infodiv).html('The description can\'t contain more than '+limit+' chars!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	} else {
		$('#' + infodiv).html((limit - textlength) +' chars left.');
		return true;
	}
}

$(document).ready(function() {
	
	$('#help-button').click(function() {
		$('#help').slideToggle(700);
	}); 
	
	$('.accordion .head').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();	
	
	//Removes border-bottom from image links
	$('img').parents('a').css({"border-bottom" : "0",'background' : 'transparent'});
	
	
	//Hides my email from spam bots
	var spt = $('span.mailme');
	var at = / at /;
	var dot = / dot /g;
	var addr = $(spt).text().replace(at,"@").replace(dot,".");
	$(spt).after('<a href="mailto:'+addr+'" title="Please send me a mail!">'+ addr +'</a>')
	.hover(function(){window.status="Please drop me an e-mail!";}, function(){window.status="";});
	$(spt).remove();
	
	
	//Count chars
	$('#Item0Description').keyup(function(){
		 limitChars('Item0Description', 400, 'charlimitinfo');
	 });
	
	
	//A mozilla fix for cmxform
	if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false);

	function cmxform(){
	  // Hide forms
	  $( 'form.cmxform' ).hide().end();

	  // Processing
	  $( 'form.cmxform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){
	    var labelContent = this.innerHTML;
	    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
	    var labelSpan = document.createElement( 'span' );
	        labelSpan.style.display = 'block';
	        labelSpan.style.width = labelWidth;
	        labelSpan.innerHTML = labelContent;
	    this.style.display = '-moz-inline-box';
	    this.innerHTML = null;
	    this.appendChild( labelSpan );
	  } ).end();

	  // Show forms
	  $( 'form.cmxform' ).show().end();
	}
});
