﻿/// <reference path="jquery-1.4.2.min.js" />
var gRightMenuBlockHeight = null;

$(function () {
    $.fn.clicked = function () {
        return this.each(function () {
            $(this).mousedown(function () { $(this).css("top", "1px"); }).mouseleave(function () { $(this).css("top", "0"); }).mouseup(function () { $(this).css("top", "0"); });
        });
    };
    $.fn.pushed = function () {
        return this.each(function () {
            $(this).mousedown(function () { $(this).css("top", "1px"); }).mouseleave(function () { $(this).css("top", "0"); });
        });
    };
    $.fn.cleanSpecialSymbols = function () {
        return this.each(function () {
            $(this).val(RemoveSpecialSymbols($(this).val()));
        });
    };
    $.fn.emptyText = function () {
        var empties = new Array();
        this.each(function () {
            $(this).val($.trim($(this).val()));
            if ($(this).val() == "") empties.push(this);
        });
        return $(empties);
    };
    $.fn.notemptyText = function () {
        var notempties = new Array();
        this.each(function () {
            $(this).val($.trim($(this).val()));
            if ($(this).val() != "") notempties.push(this);
        });
        return $(notempties);
    };
    $.fn.notintegerText = function () {
        var notinteger = new Array();
        this.each(function () {
            $(this).val($.trim($(this).val()));
            if (!ValidateInteger($(this).val())) notinteger.push(this);
        });
        return $(notinteger);
    }

    AdjustLocale();
    
    gRightMenuBlockHeight = $(".rightMenuBlock").outerHeight();
    $((gCurrentLocale == "en") ? ".langEn" : ".langRu").addClass("langActive");

    $(".search input[type=text]").focusin(function () {
        if ($(this).hasClass("hint")) $(this).removeClass("hint").val("");
    }).focusout(function () {
        if ($(this).val() == "") $(this).addClass("hint").val((gCurrentLocale == "en") ? "site search" : "поиск по сайту");
    }).keypress(function (event) {
        if (event.keyCode == 13) FindPages(this);
    });

    $("#sm-login").focusin(function () {
        if ($(this).hasClass("hint")) $(this).removeClass("hint").val("");
    }).focusout(function () {
        if ($(this).val() == "") $(this).addClass("hint").val("логин");
    }).keypress(function (event) {
        if (event.keyCode == 27) { $(this).addClass("hint"); $(".loginKey").click(); }
    });

    $("#sm-password").focusin(function () {
        $(".loginText span").hide();
    }).focusout(function () {
        if ($(this).val() == "") $(".loginText span").show();
    }).keypress(function (event) {
        if (event.keyCode == 13) LogIn();
        else if (event.keyCode == 27) $(".loginKey").click();
    });

    $(".loginText span").click(function () {
        $("#sm-password").focus();
        $(".loginText span").hide();
    });

    $(".loginKey").toggle(function () {
        $("#sm-login").addClass("hint").val("логин");
        $("#sm-password").val("")
        $(".loginText span").show();
        $(".loginText").slideDown();
    }, function () {
        $(".loginText, .loginError").slideUp();
    });

    $(".logout").click(function () {
        LogOut();
    });

    $("#galleryImages").coinslider({ height: 293, width: 993, spw: 10, sph: 3, sdelay: 50, navigation: false, links: false, delay: 5000 });

    if ($("#map_canvas").length == 1) {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(61.670328,50.85228), 11);
            map.setMapType(G_SATELLITE_MAP);
            map.setUIToDefault();
        }
    }

    $(".clicked").clicked();
    $(".pushed").pushed();

    window.onresize = StretchSideBars;
});

function StretchSideBars() {
    var maxheight = $("#content").outerHeight();
    var mh = $("#smc-menu").outerHeight() + $("#weather").outerHeight();
    if (mh > maxheight) maxheight = mh;
    if (gRightMenuBlockHeight != null && gRightMenuBlockHeight > maxheight)
        maxheight = gRightMenuBlockHeight;
    if (maxheight < 100) maxheight = 100;
    $(".leftMenuBlock, .rightMenuBlock").height(maxheight);
}

function ShowProcessing() {
    $(".loading").show();
}

function HideProcessing() {
    $(".loading").hide();
}

function GetLocalePath(path) {
    if (gCurrentLocale == "en") return "/en" + path;
    return path;
}

function GoTo(address) {
    window.location.href = GetLocalePath(address);
}

function FindPages(textcontrol) {
    if ($(textcontrol).removeClass("inputError").cleanSpecialSymbols().emptyText().addClass("inputError").length != 0) return;
    GoTo("/Search/" + $(textcontrol).val());
}

function LogIn() {
    $(".loginError").slideUp();
    if ($("#sm-login, #sm-password").emptyText().length != 0) return;
    ShowProcessing();
    $.post(GetLocalePath("/Authenticate/LogIn"), { login: $("#sm-login").val(), password: $("#sm-password").val() },
        function (data) {
            if (data === true) window.location.reload();
            else { $("#sm-password").val(""); $(".loginError").html("Неверный логин или пароль").slideDown(); }
            HideProcessing();
        });
}

function LogOut() {
    ShowProcessing();
    $.post(GetLocalePath("/Authenticate/LogOut"), function () {
        $("#sm-login, #sm-password").val("");
        window.location.reload();
    });
}

function AdjustLocale() {
    $('a.locale').each(function (i) { $(this).attr('href', GetLocalePath($(this).attr('href'))); });
}

function ConvertToInteger(str) {
    var regex = /([0-9]+)/g;
    var matches = str.match(regex);
    var result = "";
    if (matches != null) $.each(matches, function () { result += this; });
    return result;
}

function ValidateInteger(str) {
    var regEx = /^[0-9]+$/;
    return str.match(regEx);
}

function RemoveSpecialSymbols(str) {
    var regex = /([_0-9a-zA-Zа-яёА-ЯЁ]+)/g;
    var matches = str.match(regex);
    var result = "";
    if (matches != null) $.each(matches, function () { if (result == "") result = this; else result += " " + this; });
    return result;
}

function ValidateDate(str) {
    var regEx = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    return str.match(regEx);
}

function ValidateTime(str) {
    var regEx = /^([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}$/;
    return str.match(regEx);
}

function ValidateEmail(str) {
    var regEx = /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/;
    return str.match(regEx);
}

function SendPollVote(pollName) {
    var data = { variantId: $(pollName + ' :radio:checked').val() };

    if (data.variantId) {
        ShowProcessing();
        $.post(GetLocalePath("/Poll/Vote"), data, function (result) {
            HideProcessing();
            if (result === true) {
                GoTo("/");
            }
        });
    }
}

function TranslitRuToEn(str) {
    var alphabetrus = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
    var transliteng = ['a', 'b', 'v', 'g', 'd', 'e', 'jo', 'zh', 'z', 'i', 'jj', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'kh', 'c', 'ch', 'sh', 'shh', "''", 'y', "'", 'eh', 'ju', 'ja'];
    for (var i = 0; i < alphabetrus.length; ++i) {
        var eng = transliteng[i];
        var regex = new RegExp(alphabetrus.charAt(i), "g");
        str = str.replace(regex, eng);
        regex = new RegExp(alphabetrus.charAt(i).toLocaleUpperCase(), "g");
        eng = eng.charAt(0).toLocaleUpperCase() + eng.substr(1);
        str = str.replace(regex, eng);
    }
    return str;
}
