﻿
function pageLoad(sender, args) {
    if (args.get_isPartialLoad()) {
        CommentsNameTextBox();
    }
}


$(function() {
    ArchiveTabs();
    BlogHtmlBlock();
    ArticleBookmark();
    ArticleComments();
    ArticleGalleryPlace();

    CommentsNameTextBox();
});

function CommentsNameTextBox() {
    if ($("input[id$=TB_UserEmail]").val() == "") {
        $("input[id$=TB_UserEmail]").val("Ваш email адрес");
    }
    if ($("input[id$=TB_UserEmail_]").val() == "") {
        $("input[id$=TB_UserEmail_]").val("Ваш email адрес");
    }

    if ($("input[id$=hShowEmail]").val() == "1") {
        $("div[class^=divCommentEmail]").removeClass("divCommentEmailClose");
        $(".commentEmailButtonText").html("спрятать email");
        $(".commentEmailButtonArrow").html("&uarr;");
    }

    $(".commentEmailButton").unbind("click").click(function() {
        if ($("div[class^=divCommentEmail]").hasClass("divCommentEmailClose")) {
            $("div[class^=divCommentEmail]").removeClass("divCommentEmailClose");
            $(".commentEmailButtonText").html("спрятать email");
            $(".commentEmailButtonArrow").html("&uarr;");
        }
        else {
            $("div[class^=divCommentEmail]").addClass("divCommentEmailClose");
            $(".commentEmailButtonText").html("показать email");
            $(".commentEmailButtonArrow").html("&darr;");
        }
    });

    if ($("input[id$=hShowEmail2]").val() == "1") {
        $("div[class^=divUniCommentEmail]").removeClass("divUniCommentEmailClose");
        $(".commentUniEmailButtonText").html("спрятать email");
        $(".commentUniEmailButtonArrow").html("&uarr;");
    }

    $(".commentUniEmailButton").unbind("click").click(function() {
        if ($("div[class^=divUniCommentEmail]").hasClass("divUniCommentEmailClose")) {
            $("div[class^=divUniCommentEmail]").removeClass("divUniCommentEmailClose");
            $(".commentUniEmailButtonText").html("спрятать email");
            $(".commentUniEmailButtonArrow").html("&uarr;");
        }
        else {
            $("div[class^=divUniCommentEmail]").addClass("divUniCommentEmailClose");
            $(".commentUniEmailButtonText").html("показать email");
            $(".commentUniEmailButtonArrow").html("&darr;");
        }
    });
}


function ArticleGalleryPlace() {
    var galleryTop = 0;
    var gallery2Top = 0;
    var gTop = 0;
    if ($("#articlephotogallery")[0] != null) {
        galleryTop = $("#articlephotogallery").position().top;
    }
    if ($("#articlephotogallery2")[0] != null) {
        gallery2Top = $("#articlephotogallery2").position().top;
    }
    if ($("#g-adsence-200")[0] != null) {
        gTop = $("#g-adsence-200").position().top;
    }
    var gHeight = $("#g-adsence-200").height();
    if (galleryTop != null && gTop != null && gHeight != null) {
        if (galleryTop > gTop + gHeight + 100) {
            $("#articlephotogallery").css("margin-right", "-50%");
        }
        else {
            $("#articlephotogallery").css("margin-right", "0px");
        }
    }
    if (gallery2Top != null && gTop != null && gHeight != null) {
        if (gallery2Top > gTop + gHeight + 100) {
            $("#articlephotogallery2").css("margin-right", "-50%");
        }
        else {
            $("#articlephotogallery2").css("margin-right", "0px");
        }
    }
}

function ArticleComments() {
    $('#liAll').addClass('on');
    $('#liR').removeClass('on');
    $('#rC').hide();
    $('#allC').show();

    //    if ($('#rC')[0] != null) {
    //        $(document).everyTime(30000, "timer1", function(i) {
    //            if ($('#liAll').hasClass("on")) {
    //                RubricCommentsClick();               
    //            }
    //            else {
    //                AllCommentsClick();                
    //            }
    //        });
    //    }
}

function BlogHtmlBlock() {
    var hiddenControl = $("*[id$=hfBlogHtml]")[0];
    if (hiddenControl != null) {
        if (hiddenControl.value == "0") {
            $("#divBlogHtml").hide();
            $("#spanBlogHtml").removeClass("b-social-blogit-open");
        }
        else {
            $("#divBlogHtml").show();
            $("#spanBlogHtml").addClass("b-social-blogit-open");
        }
    }

    $("#spanBlogHtml").click(function() {
        var hiddenControl = $("*[id$=hfBlogHtml]")[0];
        if (hiddenControl != null) {
            if (hiddenControl.value == "0") {
                $("#divBlogHtml").fadeIn(300);
                $("#spanBlogHtml").addClass("b-social-blogit-open");
                hiddenControl.value = "1";
            }
            else {
                $("#divBlogHtml").fadeOut(300);
                $("#spanBlogHtml").removeClass("b-social-blogit-open");
                hiddenControl.value = "0";
            }
        }
    });
}

function ArticleBookmark() {
    $("textarea[id$=tbBlogHtml]").click(function() {
        $("textarea[id$=tbBlogHtml]")[0].focus();
        $("textarea[id$=tbBlogHtml]")[0].select();
    });

    $("#dlArticleBookmark").click(function() {
        if ($("#dlArticleBookmark").hasClass("b-social-bkmrk-open")) {
            $("#dlArticleBookmark").removeClass("b-social-bkmrk-open");
        }
        else {
            $("#dlArticleBookmark").addClass("b-social-bkmrk-open");
        }
        $("#dlArticleBookmark").mousedown(function() { return false });
    });
}

function ArchiveTabs() {
    $("*[id*=spanArchive]").click(function() {
        $("*[id*=liArchive]").removeClass("cur");
        var index = this.id.substring(11);
        $("*[id*=ulArchive]").hide();
        $("*[id*=ulArchive" + index + "]").fadeIn(1000);
        $("*[id*=liArchive" + index + "]").addClass("cur");
    });
}

function RubricCommentsClick() {
    $('#liR').addClass('on');
    $('#liAll').removeClass('on');

    $('#allC').fadeOut(500, function() {
        $('#allC').hide();
        $('#rC').fadeIn(500);
    });
    ArticleGalleryPlace();
    return false;
}

function AllCommentsClick() {
    $('#liAll').addClass('on');
    $('#liR').removeClass('on');

    $('#rC').fadeOut(500, function() {
        $('#rC').hide();
        $('#allC').fadeIn(500);
    });
    ArticleGalleryPlace();
    return false;
}

//New Header (Countries, Search and other)
$(function() {
    $("#searchText").unbind("keydown").keydown(function(e) {
        var keynum;
        if (window.event) // IE
            keynum = e.keyCode;
        else if (e.which) // Netscape/Firefox/Opera
            keynum = e.which;
        if (keynum == 13 && $(this).val() == "")
            return false;
        else
            return keynum;
    });
});

$(document).bind("mousedown", function() {
    $("#aspnetForm").removeAttr("onsubmit");
    ResetPopups();
});

function ResetPopups() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divUser').removeClass('b-usernav-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').removeClass('tn-fave-open');
    $("#dlArticleBookmark").removeClass("b-social-bkmrk-open");
}

function CountryClick() {
    $('#divUser').removeClass('b-usernav-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').removeClass('tn-fave-open');
    $('#divCountry').toggleClass('tn-region-open');
    $("#divCountry").mousedown(function() { return false });
}

function FaveClick() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divUser').removeClass('b-usernav-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').toggleClass('tn-fave-open');
    $("#divFave").mousedown(function() { return false });
}

function UserClick() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').removeClass('tn-fave-open');
    $('#divUser').toggleClass('b-usernav-open');
    $("#divUser").mousedown(function() { return false });
}

function SearchClick() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divUser').removeClass('b-usernav-open');
    $('#divFave').removeClass('tn-fave-open');
    $('#divSearch').toggleClass('tn-search-open');
    $("#divSearch").mousedown(function() { $('#searchtext').focus(); return false });
    $('input#searchText').focus();
}
//End New Header

//Comments
function ReplyToCommentClick(commentId) {
    var isLogged = false;
    if ($("input[id$=hIsLogged]").val() == "1") {
        isLogged = true;
    }
    var appendToId = "*[id$=divAppendToMe_" + commentId + "]";
    $(appendToId).append($("*[id$=divUniversalReplyForm]"));
    $("*[id$=LB_Add_]").attr("href", "javascript:__doPostBack('ctl00$mainPH$LB_Add_','A_" + commentId + "')");
    $("*[id$=divUniversalReplyForm]").show();

    var textId = "*[id$=TB_Text_]";
    var nameId = "*[id$=TB_UserName_]";
    var emailId = "*[id$=TB_UserEmail_]";
    var lbAddId = "*[id$=LB_Add_]";
    var ltrAnswerId = "*[id$=LTR_Answer_]";

    $(textId).val("");
    $(textId).focus();
    $(ltrAnswerId).html("");

    $(textId).bind('keydown', function(event) {
        if (event.ctrlKey && event.keyCode == '13') {
            var nameBox = $(nameId)[0];
            var emailBox = $(emailId)[0];
            var textBox = $(textId)[0];
            var label = $(ltrAnswerId)[0];
            if (isLogged) {
                nameBox = textBox;
                emailBox = textBox;
            }

            if (textBox != null && label != null && nameBox != null) {
                var email = emailBox.value;
                var name = nameBox.value;
                var text = textBox.value;
                if (!isLogged) {
                    if (name.replace("\n", "").trim().length == 0 || name == "Ваше имя") {
                        label.innerHTML = "Пожалуйста, введите свое имя.";
                        return false;
                    }
                    if (email.replaceAll(" ", "") != "" && email != "Ваш email адрес") {
                        if (!ValidEmail(email)) {
                            label.innerHTML = "Пожалуйста, введите корректный email адрес.";
                            return false;
                        }
                    }
                }
                if (text.replace("\n", "").trim().length == 0) {
                    label.innerHTML = "Пожалуйста, введите свой комментарий.";
                    return false;
                }
                else if (text.trim().length > 1000) {
                    label.innerHTML = "Длина комментария не должна превышать 1000 символов.";
                    return false;
                }
                else {
                    var appendToOriginalId = "*[id$=divOriginalPlace]";
                    $(appendToOriginalId).append($("*[id$=divUniversalReplyForm]"));
                    $("*[id$=divUniversalReplyForm]").hide();
                    event.returnValue = 0; __doPostBack('ctl00$mainPH$LB_Add_', 'A_' + commentId);
                }
            }
        }
    });

    $(lbAddId).click(function() {
        var nameBox = $(nameId)[0];
        var emailBox = $(emailId)[0];
        var textBox = $(textId)[0];
        var label = $(ltrAnswerId)[0];
        if (isLogged) {
            nameBox = textBox;
            emailBox = textBox;
        }
        if (textBox != null && label != null && nameBox != null) {
            var email = emailBox.value;
            var name = nameBox.value;
            var text = textBox.value;
            if (!isLogged) {
                if (name.replace("\n", "").trim().length == 0 || name == "Ваше имя") {
                    label.innerHTML = "Пожалуйста, введите свое имя.";
                    return false;
                }
                if (email.replaceAll(" ", "") != "" && email != "Ваш email адрес") {
                    if (!ValidEmail(email)) {
                        label.innerHTML = "Пожалуйста, введите корректный email адрес.";
                        return false;
                    }
                }
            }
            if (text.replace("\n", "").trim().length == 0) {
                label.innerHTML = "Пожалуйста, введите свой комментарий.";
                return false;
            }
            else if (text.trim().length > 1000) {
                label.innerHTML = "Длина комментария не должна превышать 1000 символов.";
                return false;
            }
            else {
                var appendToOriginalId = "*[id$=divOriginalPlace]";
                $(appendToOriginalId).append($("*[id$=divUniversalReplyForm]"));
                $("*[id$=divUniversalReplyForm]").hide();
                return true;
            }
        }
    });
}

function EditReplyToCommentClick(commentId) {
    var isLogged = false;
    if ($("input[id$=hIsLogged]").val() == "1") {
        isLogged = true;
    }
    var currentTextId = "*[id$=Div_CommText_" + commentId + "]";
    var appendToId = "*[id$=divAppendToMe_" + commentId + "]";
    $(appendToId).append($("*[id$=divUniversalReplyForm]"));
    $("*[id$=LB_Add_]").attr("href", "javascript:__doPostBack('ctl00$mainPH$LB_Add_','E_" + commentId + "')");
    $("*[id$=divUniversalReplyForm]").show();

    var textId = "*[id$=TB_Text_]";
    var nameId = "*[id$=TB_UserName_]";
    var emailId = "*[id$=TB_UserEmail_]";
    var lbAddId = "*[id$=LB_Add_]";
    var ltrAnswerId = "*[id$=LTR_Answer_]";

    $(textId).val("");
    $(ltrAnswerId).html("");

    var s = $(currentTextId)[0].innerHTML;
    var rx = new RegExp("<a .*?>(.*?)</a>", "i");
    for (var i = 0; i <= 20; i++) {
        s = s.replace(rx, "$1");
    }
    $(textId).attr({ value: s.replace("<br>", "\n").replace("<br>", "\n").replace("<br>", "\n").replace("<br>", "\n").replace("<br>", "\n").replace("<br>", "\n") });
    $(textId).focus();

    $(textId).bind('keydown', function(event) {
        if (event.ctrlKey && event.keyCode == '13') {
            var nameBox = $(nameId)[0];
            var emailBox = $(emailId)[0];
            var textBox = $(textId)[0];
            var label = $(ltrAnswerId)[0];
            if (isLogged) {
                nameBox = textBox;
                emailBox = textBox;
            }
            if (textBox != null && label != null && nameBox != null) {
                var text = textBox.value;
                var name = nameBox.value;
                var email = emailBox.value;
                if (!isLogged) {
                    if (name.replace("\n", "").trim().length == 0 || name == "Ваше имя") {
                        label.innerHTML = "Пожалуйста, введите свое имя.";
                        return false;
                    }
                    if (email.replaceAll(" ", "") != "" && email != "Ваш email адрес") {
                        if (!ValidEmail(email)) {
                            label.innerHTML = "Пожалуйста, введите корректный email адрес.";
                            return false;
                        }
                    }
                }
                if (text.replace("\n", "").trim().length == 0) {
                    label.innerHTML = "Пожалуйста, введите свой комментарий.";
                    return false;
                }
                else if (text.trim().length > 1000) {
                    label.innerHTML = "Длина комментария не должна превышать 1000 символов.";
                    return false;
                }
                else {
                    var appendToOriginalId = "*[id$=divOriginalPlace]";
                    $(appendToOriginalId).append($("*[id$=divUniversalReplyForm]"));
                    $("*[id$=divUniversalReplyForm]").hide();
                    event.returnValue = 0; __doPostBack('ctl00$mainPH$LB_Add_', "E_" + commentId);
                }
            }
        }
    });

    $(lbAddId).click(function() {
        var textBox = $(textId)[0];
        var nameBox = $(nameId)[0];
        var emailBox = $(emailId)[0];
        var label = $(ltrAnswerId)[0];
        if (isLogged) {
            nameBox = textBox;
            emailBox = textBox;
        }
        if (textBox != null && label != null && nameBox != null) {
            var text = textBox.value;
            var name = nameBox.value;
            var email = emailBox.value;
            if (!isLogged) {
                if (name.replace("\n", "").trim().length == 0 || name == "Ваше имя") {
                    label.innerHTML = "Пожалуйста, введите свое имя.";
                    return false;
                }
                if (email.replaceAll(" ", "") != "" && email != "Ваш email адрес") {
                    if (!ValidEmail(email)) {
                        label.innerHTML = "Пожалуйста, введите корректный email адрес.";
                        return false;
                    }
                }
            }
            if (text.replace("\n", "").trim().length == 0) {
                label.innerHTML = "Пожалуйста, введите свой комментарий.";
                return false;
            }
            else if (text.trim().length > 1000) {
                label.innerHTML = "Длина комментария не должна превышать 1000 символов.";
                return false;
            }
            else {
                var appendToOriginalId = "*[id$=divOriginalPlace]";
                $(appendToOriginalId).append($("*[id$=divUniversalReplyForm]"));
                $("*[id$=divUniversalReplyForm]").hide();
                return true;
            }
        }
    });
}

function CancelReplyToCommentClick() {
    $("*[id$=divUniversalReplyForm]").hide();
    $("*[id$=TB_Text_]")[0].value = "";
}

function ValidEmail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    }
    return true;
}
//End Comments



/**
* jQuery.timers - Timer abstractions for jQuery
* Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
* Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
* Date: 2009/02/08
*
* @author Blair Mitchelmore
* @version 1.1.2
*
**/

jQuery.fn.extend({
    everyTime: function(interval, label, fn, times, belay) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, times, belay);
        });
    },
    oneTime: function(interval, label, fn) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, 1);
        });
    },
    stopTime: function(label, fn) {
        return this.each(function() {
            jQuery.timer.remove(this, label, fn);
        });
    }
});

jQuery.event.special

jQuery.extend({
    timer: {
        global: [],
        guid: 1,
        dataKey: "jQuery.timer",
        regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
        powers: {
            // Yeah this is major overkill...
            'ms': 1,
            'cs': 10,
            'ds': 100,
            's': 1000,
            'das': 10000,
            'hs': 100000,
            'ks': 1000000
        },
        timeParse: function(value) {
            if (value == undefined || value == null)
                return null;
            var result = this.regex.exec(jQuery.trim(value.toString()));
            if (result[2]) {
                var num = parseFloat(result[1]);
                var mult = this.powers[result[2]] || 1;
                return num * mult;
            } else {
                return value;
            }
        },
        add: function(element, interval, label, fn, times, belay) {
            var counter = 0;

            if (jQuery.isFunction(label)) {
                if (!times)
                    times = fn;
                fn = label;
                label = interval;
            }

            interval = jQuery.timer.timeParse(interval);

            if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
                return;

            if (times && times.constructor != Number) {
                belay = !!times;
                times = 0;
            }

            times = times || 0;
            belay = belay || false;

            var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});

            if (!timers[label])
                timers[label] = {};

            fn.timerID = fn.timerID || this.guid++;

            var handler = function() {
                if (belay && this.inProgress)
                    return;
                this.inProgress = true;
                if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
                    jQuery.timer.remove(element, label, fn);
                this.inProgress = false;
            };

            handler.timerID = fn.timerID;

            if (!timers[label][fn.timerID])
                timers[label][fn.timerID] = window.setInterval(handler, interval);

            this.global.push(element);

        },
        remove: function(element, label, fn) {
            var timers = jQuery.data(element, this.dataKey), ret;

            if (timers) {

                if (!label) {
                    for (label in timers)
                        this.remove(element, label, fn);
                } else if (timers[label]) {
                    if (fn) {
                        if (fn.timerID) {
                            window.clearInterval(timers[label][fn.timerID]);
                            delete timers[label][fn.timerID];
                        }
                    } else {
                        for (var fn in timers[label]) {
                            window.clearInterval(timers[label][fn]);
                            delete timers[label][fn];
                        }
                    }

                    for (ret in timers[label]) break;
                    if (!ret) {
                        ret = null;
                        delete timers[label];
                    }
                }

                for (ret in timers) break;
                if (!ret)
                    jQuery.removeData(element, this.dataKey);
            }
        }
    }
});

jQuery(window).bind("unload", function() {
    jQuery.each(jQuery.timer.global, function(index, item) {
        jQuery.timer.remove(item);
    });
});

String.prototype.replaceAll = function(search, replace) {
    return this.split(search).join(replace);
}


