

var AskForPrice = function(negotiating) {

    var that = this;
    this.negotiate = negotiating;
    if (this.negotiate) {
        this.box = jQuery('#negotiate_price_box');
    }
    else {
        this.box = jQuery('#ask_for_price_box');
    };
    this.box.find('.info').css('display', 'none');
    this.pid = 0;

    this.productViewClose = function(ev) {
        ev.preventDefault();
        that.box.fadeOut(600, function() {
            that.box.find('.info').css('display', 'none');
            that.box.find('form').fadeIn(1);
        });
    }

    this.productViewBtnClick = function(ev) {
        that.pid = jQuery(ev.target.parentNode).find(".pid").attr("value");
        var position = jQuery(ev.target.parentNode).offset();
        var mainPosition = jQuery('#pageholder').offset();
        var parentWidth = jQuery(ev.target.parentNode).width();
        var boxWidth = that.box.width();
        that.box.css({top: (position.top-mainPosition.top-20)+'px',
                      left: (position.left-mainPosition.left+parentWidth-boxWidth+10)+'px',
                     });
        that.box.fadeIn();
    }

    this.mailSentAction = function(json) {
        if (json.status == 'success') {
            that.box.find('.info').html('Zapytanie wysłano pomyślnie. Niebawem konsultant wyśle do Ciebie e-mail na podany adres.').fadeIn();
        }
        else {
            that.box.find('.info').html('Wystąpił błąd podczas przetwarzania podanych danych. Jeśli jesteś pewien ich poprawności, poinformuj obsługę sklepu o tej sytuacji.').fadeIn();
        };
        //that.box.fadeOut();
    }

    this.productPageSubmitAction = function(ev) {
        ev.preventDefault();

        var emailfield = that.box.find("input[name=email]");
        var email = emailfield.attr('value');
        emailfield.attr('value','');

        var betterfield = that.box.find("input[name=address]");
        var commentfield = that.box.find('textarea');
        if (betterfield.size() > 0) {
            var better = betterfield.attr('value');
            betterfield.attr('value','');
            var comment = commentfield.val();
            commentfield.attr('value', '');
        }
        else {
            var better = "";
            var comment = "";
        };

        var data = {
            'class' : 'AskForPrice',
            'method' : (that.negotiate) ? 'sendNegotiationRequest' : 'sendOfferToEmail',
            'email' : email,
            'better': better,
            'comment': comment,
            'productid' : that.pid
        };
        that.box.find('form').fadeOut(500, function() {
            jQuery.getJSON('/_json/',
                           data,
                           that.mailSentAction);
        });

    }

    if (this.negotiate) {
        jQuery(".product_negotiateprice_btn").click(this.productViewBtnClick);
        jQuery("#negotiateprice_close").click(this.productViewClose);
        jQuery("#negotiateprice_submit").click(this.productPageSubmitAction);
    }
    else {
        jQuery(".product_askforprice_btn").click(this.productViewBtnClick);
        jQuery("#askforprice_close").click(this.productViewClose);
        jQuery("#askforprice_submit").click(this.productPageSubmitAction);
    }

}


jQuery(document).ready(
    function () {
        var ask_for_price = new AskForPrice(false);
        var negotiate_price = new AskForPrice(true);
    });

