var sendToFriend = function(){

	/*
	 * CONSTRUCTION DU FORMULAIRE
	 */
	
	var reg = '/http:\/\/[^\/]+(\/[^#]+)/gi';		
	var document_url = document.location.href;
	
	this._constructField = function (id, fieldLabel, size, maxlength, type) {
		
		var html = '';
		html += '<p>';
        if (fieldLabel != null) {
			 html += '<label class="clear" for="'+id+'" id="lbl_'+id+'" cl >'+fieldLabel+' :</label>';			
		}
        html += '<input id="'+id+'"  name="'+id+'" size="'+size+'" value=""  class="input-text" maxlength="'+maxlength+'" type="'+type+'">';
        html += '</p>';		
		
		return html;
	}
	
	this._constructTextArea = function (id, fieldLabel, cols, rows ) {
		
		var html = '';
		html += '<p>';
		if (fieldLabel != null) {
			 html += '<label for="'+id+'" id="lbl_'+id+'" >'+fieldLabel+' : </label> <span></span>';			
		}
       
        html += ' <textarea cols="'+cols+'" rows="'+rows+'" name="'+id+'" id="'+id+'" class="input-textarea" ></textarea>';
        html += '</p>';		
		
		return html;
	}
	
	// construction du popup
	this.constructPopUp = function () {
		var html = '';
		html += '<div id="sendtofriend" style="display:none" class="highslide-html-content sendtofriend">';
		
        html += '<h3>Envoyer cette page par e-mail</h3>';
		html += '<form id="frm_sendtofriend">';
		
				
		html +=	  this._constructField('to', 'Destinataire', 30, 100,'TEXT');
		html +=	  this._constructField('from', 'Votre email', 30, 100,'TEXT');		
		html +=	  this._constructTextArea('message', 'Message', 30,5);
		
        
    

        html +=	  '<div class="captcha">'
    		html += '<p>Système anti-spam (copiez le mot ci-dessous)</p>';
    		html += '<p class="captcha-image clear tool-refresh">';
                html += '<img src="" id="captcha_img" alt="Captcha Image" width="150" height="40">';
                html += '<a href="#" id="captcha_link" title="Changer d\'image">Changer d\'image</a>';
            html += '</p>';
    		html +=	  this._constructField('captcha', null, 30, 100,'TEXT');
    		html +=	  this._constructField('captcha_key', null, 30, 100,'HIDDEN');
		html +=	  '</div>'
        
		
		html += '</form>';
		
        html += '<p><a href="#" class="sendtofriend-cancel" onclick="return hs.close(this)" id="frm_cancel">annuler</a></p>';
        html += '<p><a href="#send" class="sendtofriend-submit"  id="frm_send">envoyer</a></p>';
		html += '</div>';
		
		$('body').append(html);
		
		// Initialisalisation des actions
		this._formValidator = $("#sendtofriend #frm_sendtofriend").validate({
    		rules: {
    			to: {
    				required: true,
					email: true
    			},
    			from: {
    				required: true,
					email: true
    			},
    			captcha: {
    				required: true
    			}
    		},
            errorElement: "em",

    		messages: {
    			to: {
                    required: "(requis)",
                    email: "(adresse mal formatée)"
                },
    			from: {
                    required: "(requis)",
                    email: "(adresse mal formatée)"
                },
    			captcha: "Ce champ est requis"
    		},
            errorPlacement: function(error, element) {
                if (element.attr("name") == "captcha") {
                    element.parent().parent().find('p:first').addClass('captcha-error');
                }else{
                    error.appendTo( element.prev());
                }
                
            }
    	});
		
		
		// envoyer
		var self = this;
		$('#frm_send').click(
			function () {
				if ($("#sendtofriend #frm_sendtofriend").valid()) {					
					self._submit();
				}
			}
		);
		
		//annuler
		$('#frm_cancel').click(
			function () {
				hs.close();
			}
		);
		
		// initilisation du captcha
		var self = this;
		$('#sendtofriend #captcha_link').click(
			function(){
				self._initCaptcha()
				return false;
			}
		);
		
	}
	
	this.getPageUrl = function () {
		return document_url;
		/*
		var reg = /http:\/\/[^\/]+(\/[^#]+)/gi;
		
		return reg.exec( window.location.href)[1];
		*/
	}
	
	this.getPageTitle = function () {
		return document.title;
	}
	
	/*
	 * ACTIONS
	 */
	
	// submit le formulaire
	this._submit = function() {
		
	var message = $('#sendtofriend #message').val();
	var to = $('#sendtofriend #to').val();
	var from = $('#sendtofriend #from').val();
	var captcha = $('#sendtofriend #captcha').val();
	var captcha_key = $('#sendtofriend #captcha_key').val();
	var url = document_url;
	var page_title = document.title;
	var datastr="message=" + message + "&to=" + to + "&from=" + from + "&captcha=" + captcha + "&captcha_key=" + captcha_key + "&url=" + document_url + "&page_title=" + page_title;
	var self = this;
	
	jQuery.ajax({
            type: "GET", 
            url: '/lycos/envoyer-recette-ami.php',            
            data: datastr,
            error:function(msg){alert( "Si ce message persiste merci de prevenir le webmaster benegil@benegil.com - Erreur jQuery: " + msg );},
            success: function(response)
            {            	
                if (response == 1) 
                {
                	
                	alert( 'Message envoyé avec succès ! ( vers -> ' + to + ' )' );
                	hs.close();
                } 
                else 
                {
                	self._formValidator.showErrors(response.errors);
                }
            }

        });
		
	}	
	this._initCaptcha = function () {
        
        var key = parseInt(Math.random()*100000000000);
		//var key = parseInt(random()*100000000000);
		$('#sendtofriend #captcha_key').val(key);
		$('#sendtofriend #captcha_img').attr('src','/captcha/show/'+key+'/');
		$('#sendtofriend #captcha').val('');
	}
	
	// reinitialise le formulaire
	this.reset = function() {
		
		this._initCaptcha();
		this._formValidator.resetForm()
	}
	
}

$(document).ready(
	function() {
		
		var sendtofriend = new sendToFriend();
		sendtofriend.constructPopUp();
		
		// ACTIVATION DU POPUP SUR LES LIENS
		$(".send-to-friend").click(
			function() {
				
				sendtofriend.reset();
				
				return hs.htmlExpand(this,
                    { contentId: 'sendtofriend', width:250 } )
			}
		);
	}
);
