jQuery.noConflict();

(function ($, window, document) {
    $(document).ready(function () {
        "use strict";

        var cq_pb_id = 'cryptoniq-paybox',
            cq_pb = {
                a: cq_pb_id + '-',
                b: '#' + cq_pb_id + '-',
                c: '.' + cq_pb_id + '-',
                d: '#' + cq_pb_id,
                e: '.' + cq_pb_id,
                f: cq_pb_id,
                g: cq_pb_id.replace(/-/g, '_')
            };

        function resizer() {
            var $paybox = $(cq_pb.c + 'layer'),
                width = $paybox.width(),
                class_name = cq_pb.a + 'resize-',
                sizes = [780, 750, 660, 480],
                count = -1;
			
            for (var size in sizes) {
                if ($paybox.hasClass(class_name + sizes[size])) {
                    $paybox.removeClass(class_name + sizes[size]);
                }
                if (width <= sizes[size]) {
                    count++;
                }
            }
			
            if (count > -1 && count < sizes.length) {
                $paybox.addClass(class_name + sizes[count]);
            }
        }
        resizer();
        $(window).resize(resizer);

        var address = $(cq_pb.b + 'address').text(),
            $txBar = $(cq_pb.b + 'tx-bar'),
            $loader = $(cq_pb.b + 'loader'),
            $alert = $(cq_pb.b + 'alert'),
            coin = $txBar.data('coin'),
            url = cryptoniq_paybox_ajax_data.url,
            nonce = cryptoniq_paybox_ajax_data.nonce,
            id = $txBar.data('id'),
            time = $txBar.data('time'),
            txLink;

        // generate qr code from address
        $(cq_pb.b + 'qrcode').text('').qrcode({
            width: 120,
            height: 120,
            text: address
        });

        // copy address
        new ClipboardJS(cq_pb.b + 'address-copy');

        $(cq_pb.b + 'tooltip-button').click(function () {
            $(cq_pb.c + 'instructions').addClass(cq_pb.a + 'instructions-open');
        });
		
        $(cq_pb.b + 'instructions-close').click(function () {
            $(cq_pb.c + 'instructions').removeClass(cq_pb.a + 'instructions-open');
        });

        function timeTwoDigits(number) {
            return (number < 10 ? '0' : '') + number;
        }

        function counter(timestamp) {
            // Set the date we're counting down to
            var countDownDate = timestamp,
                extratime = 60 * 60 * 1,
                // Update the count down every 1 second
                x = setInterval(function () {
                    // Get todays date and time
                    var now = Math.floor(Date.now() / 1000),
                        // Find the distance between now an the count down date
                        distance = countDownDate + extratime - now,
                        // Time calculations for days, hours, minutes and seconds
                        hours = Math.floor((distance % (60 * 60 * 24)) / (60 * 60)),
                        minutes = Math.floor((distance % (60 * 60)) / (60)),
                        seconds = Math.floor((distance % (60)));
                    // adding 0 if one symbol
                    // Display the result in the element with id="demo"
                    $(cq_pb.b + 'tx-counter').text(timeTwoDigits(hours) + '-' + timeTwoDigits(minutes) + '-' + timeTwoDigits(seconds));
                    // If the count down is finished, write some text 
                    if (distance < 0) {
                        clearInterval(x);

						$txBar.addClass(cq_pb.a + 'tx-failed');
						$loader.hide();
						$alert.hide();
						$(cq_pb.c + 'tx-counter-text').text(cryptoniq_paybox_notes.fail);
                        $(cq_pb.b + 'tx-counter').hide();
                    }
                }, 1000);
        }

        // check tx func
        function checker() {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: url,
                cache: false,
                data: {
                    'action': cq_pb.g + '_checker',
                    'nonce': nonce,
                    'id': id
                },
                success: function (data) {					
					if (data.failed === true) {
						$txBar.addClass(cq_pb.a + 'tx-failed');
						$loader.hide();
						$alert.hide();
						$(cq_pb.c + 'tx-counter-text').text(cryptoniq_paybox_notes.fail);
                        $(cq_pb.b + 'tx-counter').hide();
					}
					
					if (data.paid === true) {
						$txBar.addClass(cq_pb.a +'tx-processing');
						$loader.hide();
						$alert.hide();
						$(cq_pb.c + 'tx-counter-text').text(cryptoniq_paybox_notes.process);
                        $(cq_pb.b + 'tx-counter').hide();
						
						setTimeout(function () {
							window.location.href = data.redirect;
						}, 500);					
					} else {
						setTimeout(checker, 1000);
					}
                },
                error: function () {
                    $txBar.addClass(cq_pb.a + 'tx-checking-error').find($alert).text(cryptoniq_paybox_notes.error);
                },
                complete: function () {
                }
            });
        }

        // check tx
        if ( $(cq_pb.d).length > 0 ) {
            checker();
            counter(time);
        }
    });
}(jQuery, window, document));