﻿$(function () {


    /* Login layer
    ****************************/
    function initLoginLayer() {
        $('#registerLink').click(function () {
            $.rcn.ui.login.hide();
            $.rcn.ui.createUser.show($.rcn.sourceOfRegisterPage.LoginLayer);
        });

        $('a.#loginbutton').click(function () {
            if ($('div.#loginArea').validate().form()) {
                var username = $('input.#loginusername').val();
                var password = $('input.#loginpassword').val();
                $('div.#loginWorking').show();
                $('div.#loginArea').hide();
                $.rcn.membership.login(username, password,
                    function () {
                        if ($.rcn.ui.login.afterLogin)
                            $.rcn.ui.login.afterLogin();
                        $('div.#loginLayer').jqmHide();
                    },
                    function () {
                        $('div.#loginWorking').hide();
                        $('div.#loginArea').show();
                        loginFailure();
                    }
                );
            }
        });

        $('a.#createUser').click(function () {
            if ($('div.#createUserArea').validate().form()) {
                var username = $('input.#registerUsername').val();
                var password = $('input.#registerPassword').val();
                var acceptNewsLetter = $('input.#acceptNewsLetter').is(':checked');
                $('div.#registerWorking').show();
                $('div.#createUserArea').hide();
                $.rcn.membership.createUser(
                    username,
                    password,
                    acceptNewsLetter,
                    function (messages) {
                        createUserHandler(messages);
                    },
                    function (err) {
                        $('div.#registerWorking').hide();
                        $('div.#createUserArea').show();
                        if (err)
                            $('#registerError').text(err);
                        else
                            createUserServerError();
                    }
                );
            }
        });

        $('a.#btnFetchPersonal').click(function () {
            var civicno = $.trim($('input.#registerCivicNo').val());
            if (civicno.length > 0 && $.validator.methods.civicNumber(civicno)) {
                $('div.#fetchPersonalArea').hide();
                $('div.#applyForCredit').hide();
                $('div.#fetchPersonalWorking').show();
                $.rcn.customer.lookupPersonalData(
                    civicno,
                    function (info) {
                        $('div.#fetchPersonalWorking').hide();
                        $('div.#fetchPersonalArea').show();

                        if (info.Errors.length > 0) {
                            fetchPersonalInfoHandler(info);
                        }
                        else {
                            $('input.#registerFirstName').val(info.FirstName);
                            $('input.#registerLastName').val(info.LastName);
                            $('input.#registerStreet').val(info.Street);
                            $('input.#registerCO').val(info.COAddress);
                            $('input.#registerZipCode').val(info.ZipCode);
                            $('input.#registerCity').val(info.City);
                            $('input.#registerMobilePhone').val(info.MobilePhone);
                        }
                        if (info.CanApplyForCredit) {
                            $('div.#applyForCredit').slideDown('slow');
                        }
                        else {
                            $('div.#applyForCredit').hide();
                        }
                    },
                    function (err) {
                        $('div.#fetchPersonalWorking').hide();
                        $('div.#fetchPersonalArea').show();
                    });
            }
        });
    }

    function makeWatermark(textbox, text) {
        $(textbox).val(text).addClass("layerWatermark");
        $(textbox).click(function () {
            $(this).filter(function () {
                return $(this).val() == "" || $(this).val() == text
            }).val("").removeClass("layerWatermark");
        });
        $(textbox).keypress(function () {
            $(this).filter(function () {
                return $(this).val() == "" || $(this).val() == text
            }).val("").removeClass("layerWatermark");
        });
        $(textbox).blur(function () {
            $(this).filter(function () {
                return $(this).val() == ""
            }).val(text).addClass("layerWatermark");
        });
    }

    $.fn.addWatermark = function (text) {
        return makeWatermark(this, text);
    }

    function makePasswordTextbox(textboxTypeText, textboxTypePassword) {
        $(textboxTypeText).focus(function () {
            $(this).hide();
            $(textboxTypePassword).show().focus();
        });
        $(textboxTypePassword).blur(function () {
            if ($(this).val() == "") {
                $(this).hide();
                $(textboxTypeText).show();
            }
        });
    }

    $.fn.addPasswordTextbox = function (textboxTypePassword) {
        return makePasswordTextbox(this, textboxTypePassword);
    }

    /* Sitetoolbar
    ****************************/
    $('a.#showCreateUser').click(function () {
        $.rcn.ui.createUser.show($.rcn.sourceOfRegisterPage.Header);
    });

    $.rcn.bind($.rcn.events.loggedIn, function () {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(initToolbar);
        $('input[id$=loginTrigger]').click();
    });
    $.rcn.bind($.rcn.events.loggedOut, function () {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(initToolbar);
        __doPostBack('updateSiteToolbar', '');
    });

    function initToolbar() {
        $('a.#showlogin').click(function () {
            $.rcn.ui.login.show();
        });
        initLoginLayer();
        Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(initToolbar);
    }

    initToolbar();

    /* Header
    ****************************/
    $('a.#searchExamples').hover(function () {
        $('div.#searchExamplesContent').toggle("fast");
    });

    $('.hoverMenu').hover(function () {
        $(this).children('.subMenu').show();
    }, function () {
        $(this).children('.subMenu').hide();
    });

    $('.hoverCustomerSupport').hover(function () {
        $('.customerSupportLayer').show().css('left', $('.customerSupport').offset().left + 6);
        $('.customerSupport').addClass('hover');
    }, function () {
        $('.customerSupport').removeClass('hover');
        $('.customerSupportLayer').hide();
    });

    $('.hoverMyPages').hover(function () {
        $('.myPagesLayer').show().css('left', $('.myPages').offset().left + 6);
        $('.myPages').addClass('hover');
    }, function () {
        $('.myPages').removeClass('hover');
        $('.myPagesLayer').hide();
    });
});

