/************************************************************************
*************************************************************************
@Name :       	QapTcha - jQuery Plugin
@Revison :    	2.6
@Date : 		26/01/2011
@Author:     	 Surrel Mickael (www.myjqueryplugins.com - www.msconcept.fr) 
@License :		 Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
 
**************************************************************************
*************************************************************************/
jQuery.QapTcha = {
	build : function(options)
	{
        var defaults = {
			txtLock : 'Glissez pour déverrouiller',
			txtUnlock : '&nbsp;',
			disabledSubmit : true,
			autoRevert : false,
			url : 'qaptcha.php',
			sendButton : null
        };   
		
		if( this.length>0 )
		return jQuery( this ).each( function( i ) {
			/** Vars **/
			var 
				opts = $.extend(defaults, options),      
				$this = $(this),
				form = $('form').has($this),
				Clr = jQuery('<div>',{'class':'clr'}),
				bgSlider = jQuery('<div>',{id:'bgSlider'}),
				Slider = jQuery('<div>',{id:'Slider'}),
				Icons = jQuery('<div>',{id:'Icons'}),
				TxtStatus = jQuery('<div>',{id:'TxtStatus','class':'dropError',text:opts.txtLock}),
				inputQapTcha = jQuery('<input>',{name:'iQapTcha',value:generatePass(),type:'hidden'});
			
			/** Disabled submit button **/
			opts.sendButton = opts.sendButton || form.find('input[type=\'submit\']');
			if(opts.disabledSubmit) opts.sendButton.attr('disabled','disabled');
			
			/** Construct DOM **/
			bgSlider.appendTo($this);
			Icons.insertAfter(bgSlider);
			Clr.insertAfter(Icons);
			TxtStatus.insertAfter(Clr);
			inputQapTcha.appendTo($this);
			Slider.appendTo(bgSlider);
			$this.show();
			
			var limit = bgSlider.width() - Slider.width() - 2;

			Slider.draggable({ 
				revert: function(){
					if(opts.autoRevert)
					{
						if(parseInt(Slider.css('left')) > limit) return false;
						else return true;
					}
				},
				containment: bgSlider,
				axis:'x',
				stop: function(event,ui){
					if(ui.position.left > limit) {
						$.ajax( {
							url:opts.url,
							processData:true,
							data:{'action':'qaptcha'},
							type:'GET',
							success:function(data) {
								if(!data.error) {
									Slider.draggable('disable').css('cursor','default');
									inputQapTcha.val('');
									TxtStatus.html(opts.txtUnlock).addClass('dropSuccess').removeClass('dropError');
									Icons.css('background-position', '-16px 0');
									opts.sendButton.removeAttr('disabled');
								}
							}
						} );
					}
				}
			});
			
			function generatePass() {
		        var chars = 'azertyupqsdfghjkmwxcvbn23456789AZERTYUPQSDFGHJKMWXCVBN';
		        var pass = '';
		        for(i=0;i<10;i++){
		            var wpos = Math.round(Math.random()*chars.length);
		            pass += chars.substring(wpos,wpos+1);
		        }
		        return pass;
		    }
			
		});
	}
}; jQuery.fn.QapTcha = jQuery.QapTcha.build;
