/*
 * Count Characters for a given from. Also restrict the forms maxlength
 * 
 * @param form 			selector, count chars in there
 * @param target 		selector, place to show counter
 * @param maxlength   	max length of form
 * @param text   		target counter msg
 * 
 */
var countChar = (function($){

    return {
		init: function(form, counter, maxlength, text) {
		
			var elTextarea      = $(form).first();
		    var elCounter   = $(counter).first();
		    var maxLength       = Number(maxlength);
		    
		    if (elTextarea.length == 0 || elCounter.length == 0) {
		    	return false;
		    }
		    
		    countChar.__performCount(elTextarea, elCounter, maxLength,text);
		    
		    elTextarea.bind('keyup click mouseup paste blur input', function(ev) { 
		    	countChar.__performCount(elTextarea, elCounter, maxLength,text);
			});
		    
		    return true;
	
		}
		,__performCount: function( ev, elCounter, maxLength, text ) {
			
			var target = null != ev.target ? $(ev.target) : $(ev);
			
			//count /n as /n/r, so its equal with dw
			var tmpvalue = target.val().replace(/\r\n/g,'\n').replace(/\n/g,'\r\n');
			var targetLength = tmpvalue.length;
			
			if (null != target.val()) {
				if( targetLength >= maxLength ) {
					
				    elCounter.html(text.replace(/\{chars\}/,""+0));
				    
				    if(targetLength > maxLength) {
				    	
				    	target.val(tmpvalue.substring(0 ,maxLength).replace(/[\r]/g,''));
				    	//target.addClassName("warning");
				    } else {
				    	
				    	//target.removeClassName("warning");
				    }
				    	
				} else {
					elCounter.html(text.replace(/\{chars\}/,""+ (maxLength - targetLength)));
					//target.removeClassName("warning");
		
				}
			}
		}
    }
})(jQuery);

/*
 * Select radio button on input event
 * 
 * @param form 			selector, calling input
 * @param target 		selector, target radio
 * 
 */
var selectRadio = (function($){

    return {
		init: function(form, target) {
		
			var elInput		= $(form);
		    var elRadio		= $(target);
		    		    
		    elInput.bind('keyup click mouseup blur input', function(ev) { 
		    	selectRadio.__performSelect(elInput, elRadio);
			});
	
		}
		,__performSelect: function( ev, elRadio) {
			
			elRadio.click();
		}
    }
})(jQuery);

//attention prototype code
/*
var copyFormValue = {
	enabled : true
	
	,init: function(sourceForm1, sourceForm2, destForm, text, defaulttext1, defaulttext2) {
	
		var elSourceForm1     = $(sourceForm1);
		var elSourceForm2     = $(sourceForm2);
		var elDestForm      = $(destForm);
		
		
		if (elDestForm.innerHTML == '') {
			
			copyFormValue.__performCopy(elSourceForm1, elSourceForm2, elDestForm, text, /\{0\}/, /\{1\}/, defaulttext1, defaulttext2);
	
		} else {
			
			copyFormValue.enabled = false;
		}
	    
		elSourceForm1.observe('keyup', copyFormValue.__performCopy.bindAsEventListener(elSourceForm1, elSourceForm2, elDestForm, text, /\{0\}/, /\{1\}/, defaulttext1, defaulttext2) );
		elSourceForm1.observe('click', copyFormValue.__performCopy.bindAsEventListener(elSourceForm1, elSourceForm2, elDestForm, text, /\{0\}/, /\{1\}/, defaulttext1, defaulttext2) );
		elSourceForm1.observe('mouseup', copyFormValue.__performCopy.bindAsEventListener(elSourceForm1, elSourceForm2, elDestForm, text, /\{0\}/, /\{1\}/, defaulttext1, defaulttext2) );
		elSourceForm1.observe('blur', copyFormValue.__performCopy.bindAsEventListener(elSourceForm1, elSourceForm2, elDestForm, text, /\{0\}/, /\{1\}/, defaulttext1, defaulttext2) );
		elSourceForm1.observe('input', copyFormValue.__performCopy.bindAsEventListener(elSourceForm1, elSourceForm2, elDestForm, text, /\{0\}/, /\{1\}/, defaulttext1, defaulttext2) );
		
		elSourceForm2.observe('keyup', copyFormValue.__performCopy.bindAsEventListener(elSourceForm2, elSourceForm1, elDestForm, text, /\{1\}/, /\{0\}/, defaulttext2, defaulttext1) );
		elSourceForm2.observe('click', copyFormValue.__performCopy.bindAsEventListener(elSourceForm2, elSourceForm1, elDestForm, text, /\{1\}/, /\{0\}/, defaulttext2, defaulttext1) );
		elSourceForm2.observe('mouseup', copyFormValue.__performCopy.bindAsEventListener(elSourceForm2, elSourceForm1, elDestForm, text, /\{1\}/, /\{0\}/, defaulttext2, defaulttext1) );
		elSourceForm2.observe('blur', copyFormValue.__performCopy.bindAsEventListener(elSourceForm2, elSourceForm1, elDestForm, text, /\{1\}/, /\{0\}/, defaulttext2, defaulttext1) );
		elSourceForm2.observe('input', copyFormValue.__performCopy.bindAsEventListener(elSourceForm2, elSourceForm1, elDestForm, text, /\{1\}/, /\{0\}/, defaulttext2, defaulttext1) );
		
		elDestForm.observe('keyup', copyFormValue.__performDisable.bindAsEventListener(elDestForm) );
		elDestForm.observe('click', copyFormValue.__performDisable.bindAsEventListener(elDestForm) );
		elDestForm.observe('mouseup', copyFormValue.__performDisable.bindAsEventListener(elDestForm) );
		elDestForm.observe('paste', copyFormValue.__performDisable.bindAsEventListener(elDestForm) );
		elDestForm.observe('blur', copyFormValue.__performDisable.bindAsEventListener(elDestForm) );
		elDestForm.observe('input', copyFormValue.__performDisable.bindAsEventListener(elDestForm) );
	
	}
	,__performCopy: function( ev, elSourceForm2, elDestForm, text,  placeholderTarget, playceholder2, defaulttextTarget, defaulttext2) {
		
		if (copyFormValue.enabled) {
					
			var target = null != ev.target ? ev.target : ev;
			
			elDestForm.innerHTML = text.replace(placeholderTarget, target.value!='' ? target.value : defaulttextTarget);
			
			elDestForm.innerHTML = elDestForm.innerHTML.replace(playceholder2, elSourceForm2.value!='' ? elSourceForm2.value : defaulttext2);
		}
		
	}
	
	,__performDisable: function( ev ) {
		
		copyFormValue.enabled = false;	
	}
};*/

jQuery(document).ready(function($){
	
	// attention prototype code
	//copyFormValue.init("giftrecipient", "giftfrom", "giftmsg", $$('.giftpretext').first().innerHTML, $$('.giftrecipienttext').first().innerHTML, $$('.giftfromtext').first().innerHTML);
	
	countChar.init("#giftmsg", ".giftcounter", 250, $('.giftremain').html());
	
	selectRadio.init("#giftamount", "#giftamountradio");
	
});
