    
/**
 * @title           L'Oréal Paris homepage
 * @description     Home page scripts, 2010-03
 */

LOP.homeLoginAppear = function() {
    $('#vip').animate({width: '182px'}, 1000);
};

/**
 * @description     Get value of the url parameter 'name'
 */
function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
    var regexS = '[\\?&]' + name + '=([^&#]*)';
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if(results == null)
        return "";
    else
        return results[1];
}

var direct = getUrlParameter('direct');
if(direct == '') {
    direct = 1;
    /* direct = Math.round(Math.random() * 1 + 1); */
}

LOP.addSWF({
    'url': '/_nl/_nl/home/swf/home.swf',
    'id': 'FlashSWF',
    'background': 'transparent',
    'width': 950,
    'height': 600,
    'params': {
        'allowScriptAccess': 'always',
        'wmode': 'transparent'
    },
    'variables': {
        'configXML': '/_nl/_nl/home/xml/config.xml',
        'direct' : direct
    },
    'target': 'homeFlash'
});
    
$(document).ready(function() {
    if($('#vip #loginForm').length) {
        BOX.form('loginForm').addValidation(function(form) {
            form.text('login').mustValidate(function(field) {
                if(field.isEmpty()) {
                    return l10n.errorMsg.required.email;
                } else if(!field.isMatching('email')) {
                    return l10n.errorMsg.invalid.email;
                }
            });
            
            form.text('password').mustValidate(function(field) {
                if(field.isEmpty()) {
                    return l10n.errorMsg.required.pwd;
                } else if(!field.isMatching('password')) {
                   return l10n.errorMsg.invalid.pwd;
                }
            });
            
            return l10n.errorMsg.form.plural;
        }, 'a.submit').addListeners({
            'fieldError': {
                'fn': LOP.manageFieldError
            },
            'fieldValid': {
                'fn': LOP.manageFieldError
            },
            'formError': {
                'fn': LOP.addFormError
            },
            'formValid': {
                'fn': function(e, originalEvent) {
                    originalEvent.preventDefault();
                    LOP.removeFormError.call(e.owner, e, originalEvent);
                    $('#loginForm p.loading').show();
                    $('#loginForm a.submit').hide();
                    
                    LOP.wsUserLogin({
                        'login': BOX.form('loginForm').text('login').getValue(),
                        'password': BOX.form('loginForm').text('password').getValue(),
                        'onSuccess': function() {
                            var currentUrl = window.location.href;
                            $.ajax({
                                type: 'POST',
                                url: '/_nl/_nl/club-vip/registrationPersonalInfos.aspx',
                                success: function(data) {
                                    LOP.execStatCM(data);
                                    window.location.href = '/_nl/_nl/club-vip/index.aspx';
                                },
                                error: function() {
                                    window.location.href = currentUrl;
                                }
                            });
                        },
                        'onError': function() {
                            $('#loginForm p.loading').css('display','none');
                            $('#loginForm a.submit').show();
                            $('#loginFormError').html(l10n.errorMsg.invalid.login).css('display', 'block');
                        }
                    });
                }
            }
        });
    }
    
    LOP.tooltips.add({
        selector: '#loginForm:hover',
        ruleNamespace: 'formError',
        rule: function(el) {
            if(el.nodeName.toLowerCase() == 'img') {
                this.setHTML(el.alt).setAttributeCache(el, 'alt').setId('toolTipError');
            }
        }
    });
    
    /* Start FacebookConnect Implementations */
    if(LOP.FBC) {
        LOP.FBC.checkStatus({
            notLogged: function() {
                $('#homeCallToAssociateFB').click(function() {
                    FB.login(function(r) {
                        if(r.session) {
                            FB.api('/me', function(response) {
                                var rId = response.id;
                                
                                LOP.FBC.loadContent();
                                
                                $.get(l10n.FB.url.customerSearch + '?code=' + rId, function(datas) {
                                    if(datas.indexOf('__noData__') > -1) {
                                        LOP.popins.add({
                                            url: l10n.FB.url.customerSignin + '?code=' + response.id,
                                            id: 'FBSignin',
                                            processHTML: LOP.processHTMLPopins,
                                            refresh: true
                                        });
                                    } else {
                                        var lnk = l10n.FB.url.customerLogin + '?code=' + datas + '&id=' + rId + '&url=' + l10n.FB.url.customerLoginSuccess;
                                        window.location.href = lnk;
                                    }
                                });
                            });
                        }
                    }, {perms: 'user_birthday'});
                });
            },
            logged: function() {
                $('#homeCallToAssociateFB').addClass('notActive');
            }
        });
    }
    /* End FacebookConnect Implementations */
});
