/************************************************************************
*************************************************************************
@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 : 'Unlock this form to submit it.',
			txtUnlock : 'Form is unlocked. You can now submit it.',
			disabledSubmit : true,
			autoRevert : false,
			PHPfile : '/ajax/qaptcha/'
        };   
		
		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'}),
                sliderWrapper = jQuery('<div>',{id:'SliderWrapper'}),
				bgSlider = jQuery('<div>',{id:'bgSlider'}),
				Slider = jQuery('<div>',{id:'Slider'}),
				UnlockIcon = jQuery('<div>',{id:'Unlock-Icon', 'class': 'SliderIcon'}),
                LockIcon = jQuery('<div>',{id:'Lock-Icon', 'class': 'SliderIcon'}),
				TxtStatus = jQuery('<p>',{id:'TxtStatus','class':'dropError',text:opts.txtLock}),
                HelpLink = jQuery('<a>',{id:'Help-Link', 'class': 'SliderIcon fancybox', href: '#QapTcha-Help'}),
                HelpText =  jQuery('<div>',{id:'QapTcha-Help'}),
				inputQapTcha = jQuery('<input>',{name:'iQapTcha',value:generatePass(),type:'hidden'});
			
			/** Disabled submit button **/
			if(opts.disabledSubmit) form.find('input[type=\'submit\']').attr('disabled','disabled');

            $this.addClass('locked');

            HelpText.html('<h4>Form Unlocking</h4>' +
                '<p>To submit this form drag the slider from the left (locked) position to the right ' +
                '(unlocked) position. We used this mechanism to avoid form spam and to keep you from having to worry ' +
                'about those hard to read CAPTCHA challenges.</p>');
			
			/** Construct DOM **/
			bgSlider.appendTo(sliderWrapper);
            LockIcon.insertBefore(bgSlider);
			UnlockIcon.insertAfter(bgSlider);
            Slider.appendTo(bgSlider);
            sliderWrapper.appendTo($this);
			//Clr.insertAfter(Icons);
			TxtStatus.appendTo($this);
			inputQapTcha.appendTo($this);
            HelpLink.appendTo($this);
            HelpText.appendTo($this);
			$this.show();
			
			Slider.draggable({ 
				revert: function(){
					if(opts.autoRevert)
					{
						if(parseInt(Slider.css("left")) > 150) return false;
						else return true;
					}
				},
				containment: bgSlider,
				axis:'x',
				stop: function(event,ui){
					if(ui.position.left > 150)
					{
						// set the SESSION iQaptcha in PHP file
						$.post(opts.PHPfile,{
							action : 'qaptcha'
						},
						function(data) {
							if(!data.error)
							{
                                $this.removeClass('locked').addClass('unlocked');
								Slider.draggable('disable').css('cursor','default');
								inputQapTcha.val("");
								TxtStatus.text(opts.txtUnlock).addClass('dropSuccess').removeClass('dropError');
								form.find('input[type=\'submit\']').removeAttr('disabled');
							}
						},'json');
					}
				}
			});

            if(jQuery().fancybox) {
                HelpLink.fancybox();
            } else {
                HelpLink.hide();
            }
            
			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;
