﻿$(document).ready(function () {
    // animate to top
    $("#back-top").hide();

    $(window).scroll(function () {
        if ($(this).scrollTop() > 500) {
            $('#back-top').fadeIn();
        } else {
            $('#back-top').fadeOut();
        }
    });

    $('#back-top').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 600);

        return false;
    });

    $(".EmptyLogo").click(function () {
        $("#lside_img").rotate({
            animateTo: 60,
            duration: 400
        });

        setTimeout(function () { window.location.href = "/section/home"; }, 700);
    });

    setTimeout(ShowLImg, Math.floor(Math.random() * 21) * 1000 + 15000);

    //Set Join Project Btn Click
    $("#BJPSend").on("click", function () {
        if (IsButtonDisabled(this)) return;
        JoinProject();
    });

    BindEvents();
    SetTextAreaMaxLength();
});

function ShowLImg() {
    $("#lside_img").rotate({
        animateTo: 60,
        duration: 400
    });

    setTimeout(function () {
        $("#lside_img").rotate({
            animateTo: 0,
            duration: 400
        });
    }, 3000);
}
//------------------------------------------------------

$(function () {
    $('a[rel="lightbox"]').lightBox();
});

function Count(text, long) {
    var maxlength = new Number(long);
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
        /*alert("Максимальная длинна строки: " + long + " символов.");*/
    }
};

String.prototype.ReplaceAll = function (stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
};

function SpoilerAShow(id) {
    if ($('#' + id).find(".SpoilerCatsSelected").length == 0) {
        $(".SpoilerCatsSelected").find(".SpoilerCatsDescr").html("+");
        $("div[class^=SpoilerBox]").slideUp("medium");
        $(".SpoilerCatsSelected").animate({ "background-color": "#F5F5F5" }, 100);
        $(".SpoilerCatsSelected").addClass("SpoilerCats").removeClass("SpoilerCatsSelected");

        var visible = $('#' + id).find("div[class^=SpoilerBox]").css('display');
        var plusMinus = (visible == "none" ? "&ndash;" : "+");
        $('#' + id).find(".SpoilerCatsDescr").html(plusMinus);
        $('#' + id).find("div[class^=SpoilerBox]").slideToggle("medium");
    }
};

function SetMenuCircle(marginleft) {
    if ($("#MenuSliderCirc").css("visibility") == "hidden") {
        $("#MenuSliderCirc").css("margin-left", marginleft + "px");
        $("#MenuSliderCirc").css("visibility", "visible");
    }
    else {
        $("#MenuSliderCirc").animate({ "margin-left": marginleft + "px" }, 200);
    }
};

function BindEvents() {

    $(".SpoilerCats").click(function() {
        var Selected = $(this).hasClass("SpoilerCatsSelected");
        if (!Selected) {
            $(this).addClass(Selected ? "SpoilerCats" : "SpoilerCatsSelected").removeClass(Selected ? "SpoilerCatsSelected" : "SpoilerCats");

            if (!Selected) {
                $(this).animate({ "background-color": "#EFDDFC" }, 100);
            }
            else {
                $(this).animate({ "background-color": "#EBEBEB" }, 100);
            }
        }
    });

    $('.HP').hover(function () {
        $(this).find('.HoverImgLook').stop().fadeTo('fast', 1);
    }, function () {
        $(this).find('.HoverImgLook').stop().fadeTo('fast', 0);
    });

    $(".SpoilerCats").hover(
        function() {
            if ($(this).hasClass("SpoilerCats"))
                $(this).animate({ "background-color": "#EBEBEB" }, 100);
        },
        function() {
            if ($(this).hasClass("SpoilerCats"))
                $(this).animate({ "background-color": "#F5F5F5" }, 100);
        }
        );
    
        $(".SpoilerCats").find(".SpoilerCatsDescr").html("+");
};

//----------- POPUP Dialog & BtnWait & ClearInputs & ErrorInputs

function ShowInfoPopUp(Title, Body, ControlID) {
    if (ControlID == 'null') {
        $("#iPanelCTitle").html(Title);
        var $html = $("#" + Body + " > div").clone(true);
        $("#iPanelCBody").html("").append($html);
    }

    var w = (ControlID == 'null' ? $("#iPanel").width() : $("#" + ControlID).width()) + 40;
    var h = (ControlID == 'null' ? $("#iPanel").height() : $("#" + ControlID).height()) + 40;
    var left = ($(window).width() - w) / 2 + $(window).scrollLeft();
    //var top = $(window).height() - 80 > h ? ($(window).height() - h) / 2 + $(window).scrollTop() : 30 + $(window).scrollTop();
	var top = $(window).scrollTop() + 70;
	
    if (ControlID == 'null') {
        $("#iPanel").css("left", left + "px");
        $("#iPanel").css("top", top + "px");
    }
    else {
        $("#" + ControlID).css("left", left + "px");
        $("#" + ControlID).css("top", top + "px");
    }

    $('#popup_opaque').show();

    if (ControlID == 'null')
        $('#iPanel').fadeIn('medium');
    else
        $('#' + ControlID).fadeIn('medium');
}
function HideiMpe(Control) {
    $('#popup_opaque').hide();
    $('#' + Control).fadeOut('medium');
}

// Btn Wait
function BtnWait(BtnObj, Text) {
    $(BtnObj).attr("OnClick", "return false;");
    $(BtnObj).text(Text == null ? "Подождите..." : Text);
    $(BtnObj).removeClass("button_yes").addClass("button_yes_disabled");

    if ($(".btnWait").length == 0) {
        $(BtnObj).after("<div class=\"btnWait\"><div>Обработка...</div></div>");
        var left = ($(window).width() - $(".btnWait").width() + 310);
        var top = 30;
        $(".btnWait").css("left", left + "px").css("top", top + "px");
        $(".btnWait").show();
		$(".btnWait").animate({ "left": left - 260 + "px" }, 100);
    }
}
function SBW() {
    if ($(".btnWait").length == 0) {
        $("body").after("<div class=\"btnWait\"><div>Обработка...</div></div>");
        var left = ($(window).width() - $(".btnWait").width() + 310);
        var top = 30;
        $(".btnWait").css("left", left + "px").css("top", top + "px");
        $(".btnWait").show();
		$(".btnWait").animate({ "left": left - 260 + "px" }, 200);
    }
}
function HBW() {
    $(".btnWait").animate({ "left": $(".btnWait").css("left") + $(".btnWait").width() + 310 + "px" }, 200, function () {
        $(".btnWait").remove();
    });
}

// Disable Button on PostBack
function IsButtonDisabled(obj) {
    return $(obj).hasClass("button_yes_disabled");
}
function DisableButton(btnId, textToSet) {
    var id = "[id$=" + btnId + "]";
    $(id).removeClass("button_yes").addClass("button_yes_disabled");
    $(id).html("<span class='button_yes_text'>" + $("#" + btnId).first().text() + "</span>" + (textToSet != null ? textToSet : "Подождите..."));
}
function EnableButton(btnId) {
    var id = "[id$=" + btnId + "]";
    $(id).removeClass("button_yes_disabled").addClass("button_yes");
    var t = $(id).find(".button_yes_text").first().text();
    $(id).html(t);
    $(id).find(".button_yes_text").remove();
}

// Clear Inputs
function ClearInput(obj, this_obj) {
    $("textarea[id$=" + obj + "],input[id$=" + obj + "]").val("");
    $(this_obj).animate({ opacity: "hide" }, 150);
}
function ShowClearBtn(obj, dontShow) {
    //Hide Error State
    HideErrorState(obj.id);

    if (dontShow != null) return;
    
    if ($("#iClear_" + obj.id).length == 0) {
        $(obj).after($("<span />", {
            id: "iClear_" + obj.id,
            style: "top: " + ($(obj).offset().top - 10) + "px; left: " + ($(obj).offset().left + $(obj).width() + 9) + "px;"
            }).addClass("inputClear").hover(function () {
                tooltip(this,'Очистить поле');
            }, function () {
                tooltip_r();
            }).click(function () {
                ClearInput(obj.id, this);
            })
        );
    }
    
    var l = $(obj).val().length;
    if (l == 0)
        $("#iClear_" + obj.id).animate({ opacity: "hide" }, 150);
    else
        $("#iClear_" + obj.id).animate({ opacity: "show" }, 150);
}

//Msx Length inputs
function SetTextAreaMaxLength() {
    $('textarea[maxlength]').each(function () {
        // Store the jQuery object to be more efficient...
        var $textarea = $(this);

        // Store the maxlength and value of the field.
        var maxlength = $textarea.attr('maxlength');
        var val = $textarea.val();

        // Trim the field if it has content over the maxlength.
        $textarea.val(val.slice(0, maxlength));

        // Bind the trimming behavior to the "keyup" event.
        $textarea.bind('keyup', function () {
            $textarea.val($textarea.val().slice(0, maxlength));
        });
    });
}

// Input Errors
function SetInputError(objTextId, btnId) {
    var objId = objTextId;
    $("[id=" + objId + "_Label]").fadeOut(150, function () {
        $("[id=" + objId + "_Label]").addClass("inputErrText");
        $(this).fadeIn(150);
    });
    $("input[id$=" + objTextId + "], textarea[id$=" + objTextId + "]").fadeOut(150, function () {
        $("input[id$=" + objTextId + "], textarea[id$=" + objTextId + "]").removeClass("inputTextbox").addClass("Error_Input");
        $(this).fadeIn(150);
    });
    EnableButton(btnId);
}

// Check Inputs For Errors
function CheckInputsForErrors(inputsArray, strToCheck, btnId) {
    var ind = strToCheck.replace("err_", "");
    if (strToCheck.indexOf("err_") != -1) {
        SetInputError(inputsArray[ind], btnId);
        return false;
    }
    return true;
}
// Hide Error State
function HideErrorState(objId) {
    $("[id=" + objId + "_Label]").removeClass("inputErrText");
    $("input[id$=" + objId + "], textarea[id$=" + objId + "]").removeClass("Error_Input").addClass("inputTextbox");
}
// -------------------------------------------------------------------------------------- //

function tooltip(elem, str) {
    xOffset = 3;
    yOffset = 30;
    
    $("body").append("<p id='tooltip'>" + str + "</p>");
            $("#tooltip")
    			.css("top", ($(elem).offset().top - $("#tooltip").height() - yOffset) + "px")
    			.css("left", ($(elem).offset().left + xOffset) + "px")
    			.fadeIn("fast");
};

function tooltip_r() {
    $("#tooltip").remove();
};


