/************************************************************************
*************************************************************************
@Name :       	QapTcha - jQuery Plugin
@Revison :    	2.6
@Date : 		26/01/2011
@Author:     	 ALPIXEL - (www.myjqueryplugins.com - www.alpixel.fr) 
@License :		 Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
 
**************************************************************************
*************************************************************************/
(function ($) {

	$.fn.qaptcha = function (options) {
        var defaults = {
			txtLock : '',
			txtUnlock : 'Unlocked : form can be submited',
			disabledSubmit : true,
			autoRevert : false,
			PHPfile : '/wp-content/themes/rw3c/Qaptcha.jquery.php',
			EnabledImage : '/wp-content/uploads//sign_up_for_a_free_demo.png',
			DisabledImage : '/wp-content/uploads//sign_up_for_a_free_demo-disabled.png'
        };   
		
		if(this.length>0) {
			return jQuery(this).each(function(i) {
				/** Vars **/
				var 
					opts = $.extend(defaults, options),      
					$this = jQuery(this),
					form = jQuery('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 **/
				if(opts.disabledSubmit) { form.find('input[name=\'submit\']').attr('src',opts.DisabledImage); form.find('input[name=\'submit\']').attr('disabled', true); form.find('input[name=\'submit\']').css('cursor','default'); }
				
				/** Construct DOM **/
				bgSlider.appendTo($this);
				Icons.insertAfter(bgSlider);
				Clr.insertAfter(Icons);
				TxtStatus.insertAfter(Clr);
				inputQapTcha.appendTo($this);
				Slider.appendTo(bgSlider);
				$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)
								{
									Slider.draggable('disable').css('cursor','default');
									inputQapTcha.val("");
									TxtStatus.text(opts.txtUnlock).addClass('dropSuccess').removeClass('dropError');
									Icons.css('background-position', '-16px 0');
									form.find('input[name=\'submit\']').attr('src',opts.EnabledImage); form.find('input[name=\'submit\']').attr('disabled', false); form.find('input[name=\'submit\']').css('cursor','pointer');
								}
							},'json');
						}
					}
				});
				
				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);
