/**
 * @author edsadr
 */
jQuery.preloadImages = function(){
    for(var i = 0; i<arguments.length; i++){
        jQuery("<img>").attr("src", arguments[i]);
    }
}

$(function(){
    var url = window.location.pathname;
    $('#main-menu li a').removeClass('active');
    urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
    // now grab every link from the navigation
    $('#main-menu li a').each(function(){
        // and test its normalized href against the url pathname regexp
        if(urlRegExp.test(this.href.replace(/\/$/,''))){
            $('#main-menu li a').removeClass('active');
            $(this).addClass('active');
            return false;
        }
    });

     $("#loginform").validate({       
        rules:{
            username:{
                required:true
            },
            password:{
                required:true
            }
        }
    });
    
    $("#recoveryform").validate({       
        rules:{
            r_email:{
                required:true,
                email:true
            }
         
        }
    });

    $('#loginDialog').dialog({
        autoOpen: false,
        width: 600,
        buttons:{
            Login: function() {
                // Manually trigger validation
                if ($("#loginform").validate().form() == true) {
                    $('#loginform').ajaxSubmit({
                        success: authDone
                    });
                }
            },
            Cancel: function() {
                $('#loginform').clearForm();
                $(this).dialog("close");
            }
        },
        modal: true
    });

    $('#loginDialoglink,#loginDialoglink2').click(function(){
        $('#loginDialog').dialog('open');
        return false;
    });

    $('#registerDialog').dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Cancel": function() {
                $(this).dialog("close");
            }
        },
        modal: true
    });


    $('#tryItLink2 , .tryItLink3').click(function(){
        $('#registerDialog').dialog('open');
        return false;
    });   

    $.preloadImages('/images/try-it-for-free-hover');
    $('img[src*="try-it-for-free.png"]').hover(function(){
        var newsrc = $(this).attr('src');
        newsrc = newsrc.replace('.png', '-hover.png');
        $(this).attr('src', newsrc);
    }, function(){
        var newsrc = $(this).attr('src');
        newsrc = newsrc.replace('-hover.png', '.png');
        $(this).attr('src', newsrc);
    });

    $('.linkbutton').button();
});

function authDone(responseText,statusText){
    if(responseText=='0'){
        jAlert('Invalid login details, please check and try again','Error');
    }
    else{
        window.location.href="/properties";
    }
}

function showRecoveryform(){
    $("#loginDialog").dialog( "option", "title", 'Password recovery' );
    $("#loginDialog").dialog( "option", "buttons", {"Recover": function(){recover();},"Cancel":function(){showLoginform();}});
    $("#Cntloginform").hide(300,function(){
        $("#forgotPaswd").hide();
        $("#Cntrecoveryform").show();
    });
}

function showLoginform(){
    $("#loginDialog").dialog( "option", "title", 'Realtor login' );
    $("#loginDialog").dialog( "option", "buttons", {"Login": function(){btnLogin();},"Cancel":function(){btnCancel();}} );
    $("#Cntrecoveryform").hide(300,function(){
        $("#Cntloginform").show();
        $("#forgotPaswd").show();
        
    });
}




function recover(){
    if ($("#recoveryform").validate().form() == true) {
        $('#recoveryform').ajaxSubmit({
            success: recoveryDone
        });
    }
    
}

function recoveryDone(responseText,statusText){
    if(responseText!='1'){
        jAlert('Given email address was not found in our database.<br /> Please check the email address and try again','Error');
    }
    else{
        jAlert('Password was sent to your email address<br />Please check your email and try to login again.','Success');
        showLoginform();
        
    }
}

function btnLogin(){
    if ($("#loginform").validate().form() == true) {
        $('#loginform').ajaxSubmit({
            success: authDone
        });
    } 
}

function btnCancel(){
     $('#loginform').clearForm();
     $('#loginDialog').dialog("close");
}
