(function ($) {
    Calculator = function Calculator (target) {
        var self = this;
        var info  = target.find('.info');
        var text = info.html();
            info.data('text', text)
                .html('')
                .hover($.proxy(this._onInfo, this), function () {});

        var incoming_slider = $('#incoming');
        var incoming_text = $('#incoming-min');
        var incoming_price = $('#price-1');

        var outgoing_slider = $('#outgoing');
        var outgoing_text = $('#outgoing-min');
        var outgoing_price = $('#price-2');

		window.price_other = [0, 0];
		window.price_airbaltic = [0, 0];

		var spaces = [30, 30, 120, 120, 300, 600, 1200, 2400],
			marks = [0, 30, 60, 180, 300, 600, 1200, 2400];



        incoming_slider.slider({
                range: "min",
                value: 0,
                min: 0,
                max: 500,
                step: 10,
                slide: function(e, ui)
                {
                    incoming_text.html( ui.value );
                },
                change: function(e, ui)
                {
                    self.timeout = window.clearTimeout(self.timeout);
                    self.timeout = window.setTimeout($.proxy(self.price, self), 1000);
                }
        });
        incoming_text.html(incoming_slider.slider('value') + ' ');

        outgoing_slider.slider({
                range: "min",
                value: 0,
                min: 0,
                max: 500,
                step: 10,
                slide: function(e, ui)
                {
                    outgoing_text.html( ui.value );
                },
                change: function(e, ui)
                {
                    self.timeout = window.clearTimeout(self.timeout);
                    self.timeout = window.setTimeout($.proxy(self.price, self), 1000);
                }
        });
        outgoing_text.html(outgoing_slider.slider('value') + ' ');

		target.find('.country').dropdown('calculator');
    };

    Calculator.prototype = {
        /*
         * Slider resulting timeout
         */
        timeout: null,

		price: function () {

			var incoming_price = $('#price-1');
			var outgoing_price = $('#price-2');

			/* 1-BEG A.Spunitis
			-------------------*/
			if (this.off == 1) return;
			this.off = 1;

			var self = this;

			var in_sec = $('#incoming-min').html();
			var out_sec = $('#outgoing-min').html();
			var my_c = $('#country-reg .select').attr('rel');
			var in_c = $('#country-in .select').attr('rel');
			var out_c = $('#country-out .select').attr('rel');


			//var ps = 'my_c='+my_c+'&in_c='+out_c+'&out_c='+in_c+'&in_sec='+in_sec+'&out_sec='+out_sec;
			var ps = 'my_c='+my_c+'&in_c='+in_c+'&out_c='+out_c+'&in_sec='+in_sec+'&out_sec='+out_sec;

			$.ajax(
			    {
			        url: '/ajax/ajax-calculator-db.php',
			        data: ps,
			        dataType: 'json',
                    type: 'post',
                    success: function (j)
                        {
                            incoming_price.airport(j.myOp);
                            outgoing_price.airport(j.abcOp);

                            //incoming_price.airport("12");
                            //outgoing_price.airport("21");
                        }
                });

            self.off = 0;
            /* 1-END A.Spunitis
			-------------------*/

		},

        /*
         * On info icon hover event
         */
        _onInfo: function (e) {
			var target = $(e.currentTarget);
            var top = target.offset().top - 4;
            var left = target.offset().left - 194;
            var text = target.data('text');

            if (!window.zi) window.zi = $('.country:last').css('z-index') || 10;
            var popup = $('<div class="calculator-info"><div class="t"><img src="images/design/calculator-info.png" alt="" /></div><div class="c">' + text + '</div><div class="b"></div></div>');
                popup.css({left: left + 'px', top: top + 'px', 'z-index': ++window.zi})
                    .appendTo('body')
                    .fadeIn(($.browser.msie ? 0 : 'fast'))
                    .mouseleave(function () {
                        popup.fadeOut(($.browser.msie ? 0 : 'fast'), function () { $(this).remove(); });
                    });
        }
    };

    $.fn.calculator = function () {
        $(this).each(function () {
            var node = $(this);
            node.data('calculator', new Calculator(node));
        });

        return $(this);
    };
})(jQuery);
