
var pickerActive = 0;
var mouseX, mouseY;

$(document).ready(function() {
	if (document.getElementById("cardcreationstep") != null) {
		if (document.getElementById("cardcreationstep").value == "1") {
			setUpload();
			setColorPicker();
			setColor();
		} else
			setPreview();
	}

	if (document.getElementById("senddate"))
		setCalendars();

	if (document.getElementById("ecardsThemecardsTopGallery"))
		setThemecardCarousel();

	if (document.getElementById("cardBackContent"))
		setECardLinkColor();
});

function setECardLinkColor() {
	var color = $("#cardBackContent").css("color");
	$("#cardBackContent>a").css("color",color);
}

function setUpload() {

	new AjaxUpload("fotouploadBTN", {
		action: "/modules/lzz_ecards/plugins/uploadimg.php",
		name: "upload",
		responseType: "text/html",
		onSubmit : function(file , ext){
			ext = ext.toLowerCase();
			// Alleen afbeeldingen toestaan. OOK SERVER SIDE CHECKEN!!!
			if (ext && /^(jpg|png|jpeg|gif|bmp)$/i.test(ext)){
				// Andere data
				this.setData({
					'uploadImage': 'true'
				});
				document.getElementById("fotouploadMSG").innerHTML = '<img src="img/laatzeelandzien/uweigenz/loading.gif"> Bezig met uploaden.';
			} else {					
				// Het is geen afbeelding!!
				document.getElementById("fotouploadMSG").innerHTML = '<span style="color: #FF0000;">U kunt alleen afbeeldingen uploaden.</span>';
				// Annuleer de upload.
				return false;				
			}		
		},
		onComplete : function(file, response){
			if (response.substring(0,4) == "FAIL") {
				// Verwerken van de afbeelding is mislukt.
				document.getElementById("fotoupload").value = "";
				document.getElementById("fotouploadMSG").innerHTML = '<span style="color: #FF0000;">Upload mislukt ' + response.replace("FAIL", "") + '</span>';
			} else if (response.substring(0,4) == "INVALIDFILE") {
				// Verwerken van de afbeelding is mislukt.
				document.getElementById("fotoupload").value = "";
				document.getElementById("fotouploadMSG").innerHTML = '<span style="color: #FF0000;">Upload mislukt ' + response.replace("FAIL", "") + '</span>';
			} else {
				// Verwerken gelukt!
				var uploadedFile = response;
				document.getElementById("fotoupload").value = uploadedFile;
				document.getElementById("fotouploadMSG").innerHTML = "";
			}
		}		
	});
}

function setColorPicker() {

	$(document).mousemove(function(e){
		mouseX = e.pageX;
		mouseY = e.pageY;
	}); 
	$(document).click(function() {
		if (pickerActive == 1)
			pickerActive = 2;
		else if (pickerActive == 2) {
			var divXL, divXY, divYT, divYB;
			divXL = $("#colorpicker").position().left;
			divXR = $("#colorpicker").position().left + $("#colorpicker").width();
			divYT = $("#colorpicker").position().top;
			divYB = $("#colorpicker").position().top + $("#colorpicker").height();			

			if (divXL < mouseX && mouseX < divXR && divYT < mouseY && mouseY < divYB) {
				// Op de div geklikt!
			} else 
				hideColorPicker();
		}
	});
}

function hideColorPicker() {
	pickerActive = 0;
	$("#colorpicker").css("display","none");
}

function activateColorPicker() {
	$("#colorpicker").css("top", $("#color").position().top+$("#color").height()+5);
	$("#colorpicker").css("display","block");
	pickerActive = 1;
	return false;
}

function pickColor(color) {
	hideColorPicker();
	document.getElementById("color").value = color;

	document.getElementById("bgcolor").value = "";
	document.getElementById("txtcolor").value = "";
	var rgbbg = $("#"+color.toLowerCase()).css("background-color").match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	if (rgbbg != null) {
		for (var i = 1; i <= 3; ++i) {
			rgbbg[i] = parseInt(rgbbg[i]).toString(16);
			if (rgbbg[i].length == 1) rgbbg[i] = '0' + rgbbg[i];			
			document.getElementById("bgcolor").value += rgbbg[i];
		}
	} else
		document.getElementById("bgcolor").value = $("#"+color.toLowerCase()).css("background-color").replace("#","");
	
	var rgbtxt = $("#"+color.toLowerCase()).css("color").match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	if (rgbtxt != null) {
		for (var i = 1; i <= 3; ++i) {
			rgbtxt[i] = parseInt(rgbtxt[i]).toString(16);
			if (rgbtxt[i].length == 1) rgbtxt[i] = '0' + rgbtxt[i];			
			document.getElementById("txtcolor").value += rgbtxt[i];
		}
	} else
		document.getElementById("txtcolor").value = $("#"+color.toLowerCase()).css("color").replace("#","");
	
	setColor();
}

function setColor() {
	if (document.getElementById("bgcolor").value != "")
		$("#color").css("background-color", "#"+document.getElementById("bgcolor").value);

	if (document.getElementById("txtcolor").value != "")
		$("#color").css("color", "#"+document.getElementById("txtcolor").value);
}

function gotoStep2() {

	var error = new Array();

	if (document.formCardCreatorStep1["fotourl"].value == "" && document.formCardCreatorStep1["fotoupload"].value == "")
		error.push("U heeft geen foto opgegeven");

	if (document.formCardCreatorStep1["subscript"].value == "")
		error.push("U heeft geen bijschrift opgegeven");

	if (document.formCardCreatorStep1["yourmessage"].value == "")
		error.push("U heeft geen boodschap opgegeven");

	if (document.formCardCreatorStep1["bgcolor"].value == "")
		error.push("U heeft geen kleur opgegeven");

	if (error.length == 0)
		document.formCardCreatorStep1.submit();
	else {
		document.getElementById("errorMSG").innerHTML = "";
		for (var i=0; i<error.length; i++) {
			document.getElementById("errorMSG").innerHTML += error[i] + "<br />";
		}
	}
}

function setPreview() {

	$("#previewMessageFront").css("background-color", "#"+document.formCardCreatorStep2["bgcolor"].value);
	$("#previewMessageFront").css("color", "#"+document.formCardCreatorStep2["txtcolor"].value);
	$("#previewMessageFrontContent").css("color", "#"+document.formCardCreatorStep2["txtcolor"].value);
	$("#previewMessageBack").css("background-color", "#"+document.formCardCreatorStep2["bgcolor"].value);
	$("#previewMessageBack").css("color", "#"+document.formCardCreatorStep2["txtcolor"].value);
	$("#previewMessageBackContent").css("color", "#"+document.formCardCreatorStep2["txtcolor"].value);

	var imgPreview = "/modules/lzz_ecards/plugins/preview.php";	
	imgPreview += "?value=" + document.formCardCreatorStep2["foto"].value;
	imgPreview += "&posy=" + document.formCardCreatorStep2["imagePosY"].value;
	imgPreview += "&posx=" + document.formCardCreatorStep2["imagePosX"].value;
	imgPreview += "&rotation=" + document.formCardCreatorStep2["imageRotation"].value;
	imgPreview += "&zoom=" + document.formCardCreatorStep2["imageZoom"].value;

	$("#imgPreview").attr("src", imgPreview);
	$("#imgPreview").width(255);
	$("#imgPreview").height(255);
}

function setMoveImageY(move) {
	var posY = document.formCardCreatorStep2["imagePosY"].value - 0;
	posY += move - 0;
	document.formCardCreatorStep2["imagePosY"].value = posY;
	setPreview();
}

function setMoveImageX(move) {
	var posX = document.formCardCreatorStep2["imagePosX"].value - 0;
	posX += move - 0;
	document.formCardCreatorStep2["imagePosX"].value = posX;
	setPreview();
}

function setRotate(degrees) {
	var rotation = document.formCardCreatorStep2["imageRotation"].value - 0;
	rotation += degrees - 0;
	document.formCardCreatorStep2["imageRotation"].value = rotation;
	setPreview();
}

function setZoom(lvl) {
	var zoom = document.formCardCreatorStep2["imageZoom"].value - 0;
	zoom += lvl - 0;
	document.formCardCreatorStep2["imageZoom"].value = zoom;
	setPreview();
}

function showPreviewBack() {
	$("#previewMessageFront").css("display","none");
	$("#previewMessageBack").css("display","block");
	$("#previewShowFront").css("display","none");
	$("#previewShowBack").css("display","block");
}

function showPreviewFront() {
	$("#previewMessageFront").css("display","block");
	$("#previewMessageBack").css("display","none");
	$("#previewShowFront").css("display","block");
	$("#previewShowBack").css("display","none");
}

function sendCard() {

	document.formCardCreatorStep2.submit();
}


function showViewBack() {
	$("#cardFront").css("display","none");
	$("#cardBack").css("display","block");
	$("#viewShowFront").css("display","none");
	$("#viewShowBack").css("display","block");
}

function showViewFront() {
	$("#cardFront").css("display","block");
	$("#cardBack").css("display","none");
	$("#viewShowFront").css("display","block");
	$("#viewShowBack").css("display","none");
}

function forwardCard() {
	$("#forwardCard").css("display","block");	

	$("#cardFront").css("display","block");
	$("#cardBack").css("display","none");
	$("#cardReaction").css("display","none");

	$("#viewShowFront").css("display","block");
	$("#viewShowBack").css("display","none");
	$("#viewSendReaction").css("display","none");
}

function sendForwardCard() {

	var errors = new Array();

	if (document.formForwardCard["yourmessage"].value == "")
		errors.push("U heeft geen boodschap opgegeven");

	if (document.formForwardCard["sendername"].value == "")
		errors.push("U heeft us naam niet opgegeven");

	if (document.formForwardCard["sendercity"].value == "")
		errors.push("U heeft uw woonplaats niet opgegeven");

	if (document.formForwardCard["senderemail"].value == "")
		errors.push("U heeft uw email adres niet opgegeven");

	if (!document.formForwardCard["acceptterms"].checked)
		errors.push("U moet akkoord gaan met de voorwaarden");

	if (errors.length == 0)
		document.formForwardCard.submit();
	else {
		document.getElementById("forwardErrorMSG").innerHTML = "";
		for (var i=0; i<errors.length; i++) {
			document.getElementById("forwardErrorMSG").innerHTML += errors[i] + "<br />";
		}
	}
}

function sendExistingCard() {

	var errors = new Array();

	if (document.formSendCard["yourmessage"].value == "")
		errors.push("U heeft geen boodschap opgegeven");

	if (document.formSendCard["sendername"].value == "")
		errors.push("U heeft us naam niet opgegeven");

	if (document.formSendCard["sendercity"].value == "")
		errors.push("U heeft uw woonplaats niet opgegeven");

	if (document.formSendCard["senderemail"].value == "")
		errors.push("U heeft uw email adres niet opgegeven");

	if (!document.formSendCard["acceptterms"].checked)
		errors.push("U moet akkoord gaan met de voorwaarden");

	if (errors.length == 0)
		document.formSendCard.submit();
	else {
		document.getElementById("forwardErrorMSG").innerHTML = "";
		for (var i=0; i<errors.length; i++) {
			document.getElementById("forwardErrorMSG").innerHTML += errors[i] + "<br />";
		}
	}
}

function reactToCard() {
	$("#forwardCard").css("display","none");

	$("#cardFront").css("display","none");
	$("#cardBack").css("display","none");
	$("#cardReaction").css("display","block");

	$("#viewShowFront").css("display","none");
	$("#viewShowBack").css("display","none");
	$("#viewSendReaction").css("display","block");
}

var calendarActive = 0;
function setCalendars() {

	var div = document.createElement("div");
	div.id = "ecardCalender";
	document.body.appendChild(div);

	var datum = document.getElementById("senddate").value;
	var datArr = new Array();
	datArr = datum.split("-");
	
	new Calendar(
		"ecardCalender", 
		function(a) {
			document.getElementById("senddate").value = a;
			hideEcardCalendar();
		}, 
		0,0,0,1,0,0, datArr[0], datArr[1], datArr[2]
	);

	$("#ecardCalender").css("display", "none");
	$("#ecardCalender").css("position", "absolute");
	
	$(document).mousemove(function(e) {
		mouseX = e.pageX;
		mouseY = e.pageY;
	});
	$(document).click(function(e) {
		if (calendarActive == 1)
			calendarActive = 2;
		else if (calendarActive > 1) {
			var topY = $("#ecardCalender").position().top;
			var bottomY = $("#ecardCalender").position().top + $("#ecardCalender").height();
			var leftX = $("#ecardCalender").position().left;
			var rightX = $("#ecardCalender").position().left + $("#ecardCalender").width();

			if (leftX >= mouseX && mouseX <= rightX && topY >= mouseY && mouseY <= bottomY) {
				// Klik op de calendar. Doe niets.
			} else
				hideEcardCalendar();
		}
	});
}

function hideEcardCalendar() {
	$("#ecardCalender").css("display", "none");
	calendarActive = 0;
}

function selectSendDate() {
	$("#ecardCalender").css("display", "block");
	$("#ecardCalender").css("top", $("#senddate").position().top + "px");
	$("#ecardCalender").css("left", $("#senddate").position().left + "px");
	calendarActive = 1;
}

function setThemecardCarousel() {    
	
	$("#ecardsThemecardsTopGallery").jcarousel({
        scroll: 1,
        initCallback: themecardCarouselCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
}

function themecardCarouselCallback(carousel) {

    $('#themecardgallerynext').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#themecardgalleryprev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function selectThemecard(id, name, message, bgcolor, txtcolor) {

	document.getElementById("selectedThemecard").value = id;
	var src = "img/laatzeelandzien/ecards/themecards/";

	$("#ecardsThemecardsBottomQuote>img").attr("src", src + name + ".thumb.jpg");
	$("#ecardsThemecardsBottomImageSmall>img").attr("src", src + name + ".small.jpg");
	$("#ecardsThemecardsBottomImageLarge>img").attr("src", src + name + ".large.jpg");

	$("#ecardsThemecardsBottomQuote>img").attr("alt", name);
	$("#ecardsThemecardsBottomImageSmall>img").attr("alt", name);
	$("#ecardsThemecardsBottomImageLarge>img").attr("alt", name);

	$("#ecardsThemecardsBottomMessageMsg").css("background-color", "#" + bgcolor);
	$("#ecardsThemecardsBottomMessageMsg").css("color", "#" + txtcolor);
	document.getElementById("ecardsThemecardsBottomMessageMsg").innerHTML = message;
}

function showViewThemeBack() {
	$("#themeCardQuote").css("display","none");
	$("#themeCardIMGSmall").css("display","none");
	$("#themeCardIMGLarge").css("display","block");
	$("#themeCardMessage").css("display","block");
	$("#viewThemeShowFront").css("display","none");
	$("#viewThemeShowBack").css("display","block");
}

function showViewThemeFront() {
	$("#themeCardQuote").css("display","block");
	$("#themeCardIMGSmall").css("display","block");
	$("#themeCardIMGLarge").css("display","none");
	$("#themeCardMessage").css("display","none");
	$("#viewThemeShowFront").css("display","block");
	$("#viewThemeShowBack").css("display","none");
}

function reactToThemeCard() {
	$("#forwardThemeCard").css("display","none");
	$("#cardThemeReaction").css("display","block");
}

function forwardThemeCard() {
	$("#forwardThemeCard").css("display","block");	
	$("#cardThemeReaction").css("display","none");
}

function showThemecardPreview() {

	if (document.getElementById("fadeBGdiv"))
		removeTransDiv();
	
	var height = $(document).height();
	if ($(window).height() > $(document).height())
		height = $(window).height();
	
	var width = $(document).width();
	if ($(window).width() > $(document).width())
		width = $(window).width();

	var transDiv = document.createElement("div");
	transDiv.id = "fadeBGdiv";
	document.body.appendChild(transDiv);
	
	$("#fadeBGdiv").css("position", "absolute");
	$("#fadeBGdiv").css("z-index", "2");
	$("#fadeBGdiv").css("top", "0");
	$("#fadeBGdiv").css("left", "0");
	$("#fadeBGdiv").width(width);
	$("#fadeBGdiv").height(height);
	$("#fadeBGdiv").css("background-color", "#000000");
	$("#fadeBGdiv").css("filter", "alpha(opacity=70)");
	$("#fadeBGdiv").css("-moz-opacity", "0.7");
	$("#fadeBGdiv").css("-khtml-opacity", "0.7");
	$("#fadeBGdiv").css("opacity", "0.7");
	
	$("#fadeBGdiv").click(function() {
		removeTransDiv();
	});

	var name = document.formSendMailThemeCard["stuurThemakaartName"].value;
	var bgcolor = document.formSendMailThemeCard["stuurThemakaartBGColor"].value;
	var txtcolor = document.formSendMailThemeCard["stuurThemakaartTXTColor"].value;
	var message = document.formSendMailThemeCard["stuurThemakaartMSG"].value;

	var previewHTML='<div style="text-align: right; margin-bottom: 3px; margin-top: -8px;">'+
					'	<a href="#" onclick="removeTransDiv(); return false;" title="Sluit Preview">X</a>'+
					'	<a href="#" onclick="removeTransDiv(); return false;" title="Sluit Preview">Sluiten</a>'+
					'</div>'+
					'<div id="viewThemeCard">'+
					'	<div id="themeCardQuote">'+
					'		<img id="card" src="img/laatzeelandzien/ecards/themecards/'+name+'.thumb.jpg" />'+
					'	</div>'+
					'	<div id="themeCardIMGSmall">'+
					'		<img id="card" src="img/laatzeelandzien/ecards/themecards/'+name+'.small.jpg" />'+
					'	</div>'+
					'	<div id="themeCardIMGLarge">'+
					'		<img id="card" src="img/laatzeelandzien/ecards/themecards/'+name+'.large.jpg" />'+
					'	</div>'+
					'	<div id="themeCardMessage">'+
					'		<div id="themeCardMessageTxt" style="background-color: #'+bgcolor+'; color: #'+txtcolor+';">'+
					'			'+ message +
					'		</div>'+
					'	</div>'+
					'</div>'+
					'<div id="viewThemeShowFront">'+
					'	<a href="#" onclick="showViewThemeBack(); return false;">&gt;&gt; Naar de achterzijde</a>'+
					'</div>'+
					'<div id="viewThemeShowBack">'+
					'	<a href="#" onclick="showViewThemeFront(); return false;">&lt;&lt; Naar de voorzijde</a>'+
					'</div>';

	var previewDiv = document.createElement("div");
	previewDiv.id = "previewDIV";
	document.body.appendChild(previewDiv);

	var previewSubDiv = document.createElement("div");
	previewSubDiv.id = "previewSubDiv";
	previewSubDiv.innerHTML = previewHTML;
	document.getElementById("previewDIV").appendChild(previewSubDiv);

	$("#previewSubDiv").width($("#viewThemeCard").width());
	$("#previewSubDiv").height($("#viewThemeCard").height()+90);
	$("#previewSubDiv").css("padding", "10px");
	
	$("#previewDIV").css("position", "absolute");
	$("#previewDIV").css("z-index", "4");
	$("#previewDIV").css("background-color", "#FFFFFF");

	$("#previewDIV").width($("#previewSubDiv").width()+20);
	$("#previewDIV").height($("#previewSubDiv").height()+20);

	
	var height = window.innerHeight;
	if (height == 0 || !height)
		height = document.body.offsetHeight;
	
	$("#previewDIV").css("top", Math.round($(window).scrollTop() + (($(window).height()-$("#previewDIV").height())/2)));
	$("#previewDIV").css("left", Math.round(($(window).width()-$("#previewDIV").width())/2));
}

function removeTransDiv() {
	try {
		document.body.removeChild(document.getElementById("fadeBGdiv"));
		document.getElementById("previewDIV").removeChild(document.getElementById("previewSubDiv"));
		document.body.removeChild(document.getElementById("previewDIV"));
	} catch(e) {}
}

function showHelpPopup(helpID) {
	
	if (document.getElementById("fadeHelpBGdiv"))
		hideHelpPopup();
	
	var height = $(document).height();
	if ($(window).height() > $(document).height())
		height = $(window).height();
	
	var width = $(document).width();
	if ($(window).width() > $(document).width())
		width = $(window).width();

	var transDiv = document.createElement("div");
	transDiv.id = "fadeHelpBGdiv";
	document.body.appendChild(transDiv);
	
	$("#fadeHelpBGdiv").css("position", "absolute");
	$("#fadeHelpBGdiv").css("z-index", "2");
	$("#fadeHelpBGdiv").css("top", "0");
	$("#fadeHelpBGdiv").css("left", "0");
	$("#fadeHelpBGdiv").width(width);
	$("#fadeHelpBGdiv").height(height);
	$("#fadeHelpBGdiv").css("background-color", "#000000");
	$("#fadeHelpBGdiv").css("filter", "alpha(opacity=70)");
	$("#fadeHelpBGdiv").css("-moz-opacity", "0.7");
	$("#fadeHelpBGdiv").css("-khtml-opacity", "0.7");
	$("#fadeHelpBGdiv").css("opacity", "0.7");
	
	$("#fadeHelpBGdiv").click(function() {
		hideHelpPopup();
	});
	
	var previewHTML='<div style="text-align: right; margin-bottom: 3px; margin-top: -8px;">'+
					'	<a href="#" onclick="hideHelpPopup(); return false;" title="Sluit Preview">X</a>'+
					'	<a href="#" onclick="hideHelpPopup(); return false;" title="Sluit Preview">Sluiten</a>'+
					'</div>'+
					'<div style="text-align: left;">'+
					'	<p>'+ $("#"+helpID).attr("title") + '</p>'+
					'</div>';

	var previewDiv = document.createElement("div");
	previewDiv.id = "helpDIV";
	document.body.appendChild(previewDiv);

	var previewSubDiv = document.createElement("div");
	previewSubDiv.id = "helpSubDIV";
	previewSubDiv.innerHTML = previewHTML;
	document.getElementById("helpDIV").appendChild(previewSubDiv);

	$("#helpSubDIV").width(300);
	$("#helpSubDIV").css("padding", "10px");
	
	$("#helpDIV").css("position", "absolute");
	$("#helpDIV").css("z-index", "4");
	$("#helpDIV").css("background-color", "#FFFFFF");

	$("#helpDIV").width($("#helpSubDIV").width()+20);
	$("#helpDIV").height($("#helpSubDIV").height()+20);

	
	var height = window.innerHeight;
	if (height == 0 || !height)
		height = document.body.offsetHeight;
	
	$("#helpDIV").css("top", Math.round($(window).scrollTop() + (($(window).height()-$("#helpDIV").height())/2)));
	$("#helpDIV").css("left", Math.round(($(window).width()-$("#helpDIV").width())/2));
}

function hideHelpPopup() {
	try {
		document.body.removeChild(document.getElementById("fadeHelpBGdiv"));
		document.getElementById("helpDIV").removeChild(document.getElementById("helpSubDIV"));
		document.body.removeChild(document.getElementById("helpDIV"));
	} catch(e) {}
}

function addReceiver() {

	var nr = document.getElementById("nrOfReceivers").value - 0 + 1;

	var html = "<div id='receiver" + nr + "'>\n";
	html += "	<div class='form'>\n";
	html += "		<label for='receivername" + nr + "'>Naam ontvanger " + nr + ":</label>\n";
	html += "		<input type='text' id='receivername" + nr + "' name='receivername" + nr + "' /><br />\n";
	html += "	</div>\n";
	html += "	<div class='form'>\n";
	html += "		<label for='receiveremail" + nr + "'>E-mail ontvanger " + nr + ":</label>\n";
	html += "		<input type='text' id='receiveremail" + nr + "' name='receiveremail" + nr + "' /><br />\n";
	html += "	</div>\n";
	html += "	<div class='form receiverlink'>\n";
	html += "		<a href='#' onclick='removeReceiver(\"" + nr + "\"); return false;'>Ontvanger " + nr + " verwijderen</a>\n";
	html += "	</div>\n";
	html += "</div>\n";

	$("#receivers").append(html);
	document.getElementById("nrOfReceivers").value = nr;
}

function removeReceiver(nr) {
	$("#receiver" + nr).remove();
}

jQuery.fn.limitMaxlength = function(options){

	var settings = jQuery.extend({
		attribute: "maxlength",
		onLimit: function(){},
		onEdit: function(){}
	}, options);

	// Event handler to limit the textarea on key press
  var onPress = function() {
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length == maxlength){
			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
      
      return false;
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
  }
  
	var onEdit = function(){
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length > maxlength){
			textarea.val(textarea.val().substr(0, maxlength));

			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
      
      return false;
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
	}

	this.each(onEdit);

	return this.keypress(onPress)
				.keyup(onEdit)
				.keydown(onEdit)
				.focus(onEdit)
				.live('input paste', onEdit);
}

$(document).ready(function(){

	$('textarea[maxlength]').limitMaxlength({});
});

