﻿$(function() {
    $("#aCities").unbind("click").click(function() {
        if ($("#divCities").hasClass("open")) {            
            $("#divCities").removeClass("open");
            $("#divCities").hide();
        }
        else {            
            $("#divCities").addClass("open");
            $("#divCities").fadeIn();
        }
        return false;
    });
});

window.onload = (function() {
    $("input[id$=searchBox]").focus(function() {
        $(".search").toggleClass('active');
        if ($(this).val() == "Поиск") {
            $(this).val("");
        }
    }).blur(function() {
        $(".search").toggleClass('active');
        if ($(this).val() == "") {
            $(this).val("Поиск");
        }
    });

    $(".popup-anchor").hover(
        function() {
            //$(this).next("div").css('display', 'block').fadeIn(500);
        },
        function() {
            $(this).next("div").css('display', 'none');
        }
    );
    $(".popup-anchor").click(function() {
        $(this).next("div").css('display', 'block').fadeIn(500);
    });

    $(document).bind("click", function() {
        $("div.plus-popup").removeClass('opened');
    });
    $("a.bg-out").click(function(event) {
        $("div.plus-popup").toggleClass('opened');
        event.stopPropagation();
    })

    $(".open-video-popup").click(function() {
        $(".video-link").fadeIn();
    })
    $("#aCallOpen").click(function() {
        $("#divCallMePlease").fadeIn();
        return false;
    });

    $("#aCallOpen").hover(
        function() {
            $("#aAddCallMe").show();
            $("#divCallNote").fadeIn(300);
        },
        function() {
            $("#divCallNote").hide();
        }
    );

    $("*[class*=popup-close]").click(function() {
        $("*[class*=popup-position]").fadeOut();
    })

    $("#divCallMePlease #tbPhone1").unbind("keyup").keyup(function() {
        $(this).val($(this).val().replace(/[^0-9+\-() ]/g, ""));
    });
    $("#divCallMePlease #tbPhone2").unbind("keyup").keyup(function() {
        $(this).val($(this).val().replace(/[^0-9+\-() ]/g, ""));
    });

    $("#aAddCallMe").unbind("click").click(function() {
        $(this).hide();
        var name = HtmlEncode($("#divCallMePlease #tbName").val());
        var email = $("#divCallMePlease #tbEmail").val();                
        if (name != '' && email != '') {
            $.ajax({
                type: "POST",
                url: "/jQueryAjaxRequest.aspx/AddCallMe",
                data: '{name: "' + name + '", email: "' + email + '", url: "' + top.location + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) { $('#divCallMePlease').fadeOut(); pageTracker._trackPageview("/let_me_know"); $.notifier("Ваша заявка принята.", "success"); },
                error: function() { $('#divCallMePlease').fadeOut(); $.notifier("Ошибка. Попробуйте еще раз.", "error"); }
            });
        }
        else {
            $(this).show();
            $.notifier("Ошибка. Поля заполнены неверно", "error");            
        }
    });
});

function ValidateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}

//notifier
(function($) {
    $.notifier = function(message, type) { notify(message, type); }

    function notify(message, type) {
        var instance = $(document.getElementById('divNoticeContainer'));
        var html = jQuery.notifier.settings.noticeTemplate;
        html = html.replace('%Message%', message);

        if (type == "success")
            html = html.replace('%CssClass%', 'b-notif-success');
        else if (type == "error")
            html = html.replace('%CssClass%', 'b-notif-error');

        var notice = $(html).hide().fadeIn(jQuery.notifier.settings.noticeFadeTimeout);

        $.notifier.settings.noticeDisplay(notice);
        instance.append(notice);

        if ($.notifier.settings.displayTimeout > 0) {
            setTimeout(function() {
                jQuery.notifier.settings.noticeRemove(notice, function() {
                    notice.remove();
                });
            }, jQuery.notifier.settings.displayTimeout);
        }
    };

    $.notifier.settings = {
        noticeTemplate: '<div class="%CssClass%">%Message%</div>',
        noticeDisplay: function(notice) {
            notice.fadeIn(jQuery.notifier.settings.noticeFadeTimeout);
        },
        noticeRemove: function(notice, callback) {
            notice.animate({ opacity: '0', height: '0px' }, { duration: jQuery.notifier.settings.noticeFadeTimeout, complete: callback });
        },
        noticeFadeTimeout: 'slow',
        displayTimeout: 5000
    };
})(jQuery);

//function pageLoad(sender, args) {
//    if (args.get_isPartialLoad()) {
//        completeNotify();
//    }
//}

function completeNotify() {
    var inputs = $("input[id*=hCommandStatusMessage]");
    for (var i = 0; i < inputs.length; i++) {
        var message = inputs[i].value;
        if (message) {
            $.notifier(message);
            inputs[i].value = '';
        }
    }
}

if (Array.indexOf == "undefined" || !Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

function HtmlEncode(value) {
    return $('<div/>').text(value).html().replace(/\"/g, "&quot;");
}

function HtmlDecode(value) {
    return $('<div/>').html(value).text().replace(/&quot;/g, "\"");
}
