/**
 * Класс всплывающих форм
 * @param jLink Объект псевдоссылки, управляющей отображением попапа
 * @param jPopup Объект попапа, должен быть построен по определённым законам — содержать в себе блоки с классами popup-container и tail
 */

function ClassAccessoryPopup(jLink, jPopup)
{
    if(!ClassAccessoryPopup.prototype._objects) ClassAccessoryPopup.prototype._objects = new Array();
    ClassAccessoryPopup.prototype._objects[ClassAccessoryPopup.prototype._objects.length] = this;

    var index = ClassAccessoryPopup.prototype._objects.length;
    var oThis = this;
    this.jLink = jLink;
    this.jPopup = jPopup;
    this.jTail = $(".tail", jPopup); // Контейнер хвоста попапа
    this.jContainer = $(".container", jPopup);  // Контейнер попапа

    this.showForm = function()
    {
        var rootWidth = $(".root").width();
        var rootHeight = $(".root").height();

        for(i=index; i<ClassAccessoryPopup.prototype._objects.length; i++) {
            ClassAccessoryPopup.prototype._objects[i].hideAll();
        }
        for(i=index-2; i>=0; i--) {
            ClassAccessoryPopup.prototype._objects[i].hideAll();
        }

        oThis.jPopup
            .css({display: "block"})
            .addClass("f-popup_accessory_opened z-index-top");

        $(".z-index-top").not(oThis.jPopup).removeClass("z-index-top");
        if ($.browser.msie) {
            $(".root").addClass("z-index-top");
        }
        //oThis.jContainer.show();
        var popupTop = $('.label', oThis.jLink).offset().top - 236;
        var topDifference = rootHeight - popupTop - oThis.jContainer.height() - 30;
        if (topDifference < 0) {
            popupTop = popupTop + topDifference;
        }
        var linkLeft = oThis.jLink.offset().left;
        var popupLeft = oThis.jLink.offset().left + 80;
        var tailClass = "tail tail_left";
        if (linkLeft > rootWidth/2) {
            popupLeft = oThis.jLink.offset().left - 380;
            tailClass = "tail tail_right";
        }
        oThis.jPopup.css({top: popupTop + "px", left: popupLeft + "px"});
        oThis.jTail
            .css({display: "block", "margin-top": topDifference < 0 ? -topDifference + "px" : "0px"})
            .attr("class", tailClass);
        $('.switch', oThis.jLink).unbind('click', oThis.showForm);
        $('.switch', oThis.jLink).addClass('switch-black');
        if (($(window).scrollTop() - popupTop) > -20) {
            $.scrollTo(oThis.jPopup, {offset: -20, duration: 300});
        }
        return false;
    }

    this.hideAll = function ()
    {
        oThis.jTail.hide();
        oThis.jPopup
            .css({display: "none"})
            .removeClass("f-popup_accessory_opened");
        //oThis.jContainer.hide();
        $('.switch', oThis.jLink).unbind('click', oThis.hideAll);
        $('.switch', oThis.jLink).click(oThis.showForm);
        $('.switch', oThis.jLink).removeClass('switch-black');
    }



    $('.switch', oThis.jLink).click(oThis.showForm);
    $(document).click(oThis.hideAll);
    $('.close', oThis.jPopup).click(oThis.hideAll);

    this.jPopup.click(function(event) {
        event.stopPropagation();
    });

    $(window).resize(function(){
        if (oThis.jPopup.hasClass("f-popup_accessory_opened")) {
            var rootWidth = $(".root").width();
            var rootHeight = $(".root").height();
            var popupTop = $('.label', oThis.jLink).offset().top - 236;
            var topDifference = rootHeight - popupTop - oThis.jContainer.height() - 30;
            if (topDifference < 0) {
                popupTop = popupTop + topDifference;
            }
            var linkLeft = oThis.jLink.offset().left;
            var popupLeft = oThis.jLink.offset().left + 80;
            var tailClass = "tail tail_left";
            if (linkLeft > rootWidth/2) {
                popupLeft = oThis.jLink.offset().left - 380;
                tailClass = "tail tail_right";
            }
            oThis.jPopup.css({top: popupTop + "px", left: popupLeft + "px"});
            oThis.jTail
                .css({"margin-top": topDifference < 0 ? -topDifference + "px" : "0px"})
                .attr("class", tailClass);
        }
    });
}
