﻿/* Invite Your Friends 
   JQuery Plugin
   */
var inEmail, inMessage, inName, publicCaptchaKey;      
function invite_init(){
    var theForm = $("#frmInvite");
    var formAction = theForm.attr("action");
    
    /* Add required field check */
    $("#invite .required").blur(function(){
       validateInput(this);
    });
    
    /* Add sending and sent messages */
    theForm.append("<div class='sending'><span class='msg'>sending your invitation..</span></div><div class='sent'><h3>Thank you</h3><p>Your invite has been sent.</p><p class='last'><a href='#'>Send another</a></p></div>");    
    $("#invite .sent a").click(function(){
        $("#invite input").add("#invite textarea").val('');
        //createCaptcha(publicCaptchaKey);
        invite_trans("#invite .sent", "#invite .form", function(){
            $("#invite input:first").focus();
        });
        return false;
    });
    
    /* Submit form */
    theForm.submit(function(){
        if (!validateForm($("#frmInvite"))){
            return false;
        }
        // check captcha 
        /*if ($("#recaptcha_challenge_field").val()==''){
            $("#captchaError").show();
            return false;
        }
        if ($("#recaptcha_response_field").val()==''){
            $("#captchaError").show();
            return false;
        }*/
        
        $("#invite div.form")
            .animate({height: 'toggle', opacity: 'toggle'},"fast","easein",
                function(){                    
                    $("#invite div.sending").animate({opacity: 'show'}, "fast", "easein");                    
                    sendForm($("#frmInvite"), function(data){
                        var ret = data.split('\n');                        
                        invite_trans("#invite div.sending", "#invite div.sent");                            
                        /*if (ret[0]=='true'){
                            //$("#captchaError").hide();
                            //$("#captchaWrongError").hide();
                            invite_trans("#invite div.sending", "#invite div.sent");                            
                        } else {
                            // Captcha error                            
                            $("#invite div.sending").animate({opacity: 'hide'}, "fast", "easein", function(){
                                $("#invite div.form").animate({height: 'toggle', opacity: 'toggle'},"fast","easein", function(){
                                    $("#recaptcha_response_field").focus();
                                });
                                //$("#captchaError").hide();
                                //$("#captchaWrongError").show();
                            });                            
                        }*/
                    });                    
                });
        return false;
    });
}


/* Transition between divs */
function invite_trans(from, to, fn){
    $(from).animate({height:'hide',opacity:'hide'},"fast","swing",function(){
        $(to).animate({opacity:'show'},"fast","swing",fn);                
    });
}

function createCaptcha(key){
    publicCaptchaKey = key;
    Recaptcha.create(key,
        "captcha-container", {
           theme: "white"           
    });
}

$(function(){
    invite_init();
});

