(function($){
    $(document).ready(function() 
    {
        var sNbn_InitNotification;
        var oNbn_Notification = nbn_notification_wc_ajax;
        var oNbn_Duration = oNbn_Notification.nbn_duration_notification;
        var oNbn_CustomDuration = oNbn_Notification.nbn_duration_custom_notification;
        var data = {
            action: 'nbn_notification_wc_ajax',
            nbn_post_id : oNbn_Notification.nbn_post_id,
            nbn_is_buddypress_page : oNbn_Notification.nbn_is_buddypress_page,
            nbn_is_home_page : oNbn_Notification.nbn_is_home_page
        };
        clearTimeout(sNbn_InitNotification);
        sNbn_InitNotification = setInterval(function(){
            oNbn_showNotification();
        }, oNbn_Notification.nbn_first_time_notifcation);
        
        function oNbn_showNotification()
        {
            $.ajax({
                url: oNbn_Notification.ajaxurl,
                type: 'get',
                data: data,
                dataType: 'html',
                beforeSend: function() {
                    $('.nbn-wrapper').remove();
                    clearTimeout(sNbn_InitNotification);
                },
                complete: function() {
                },
                success: function(html) {
                    sNbn_InitNotification = setInterval(function(){
                    console.log('interval : ' + oNbn_Notification.nbn_time_notifcation);
                        oNbn_showNotification();
                    }, oNbn_Notification.nbn_time_notifcation);
                    if(html !== ''){
                        var eNotification = $(html);
                        eNotification.css({'z-index':9999});
                        eNotification.css({opacity:0});
                        $('body').append(eNotification);
                        if($('.nbn-wrapper-custom').length > 0){
                            eNotification.css({'z-index':9990});
                            doAnimation(eNotification, 500, false, false, Math.round($('.nbn-wrapper-custom').outerHeight()));
                        } else {
                            doAnimation(eNotification, 500);
                        }
                        setTimeout(function(){
							$('.nbn-wrapper').find('.nbn-close-btn').trigger('click');
                        }, oNbn_Duration);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {

                }
            });   
        }
        
        var sNbn_InitCustomNotification;
        var customData = {
            action: 'nbn_custom_notification_wc_ajax'
        };
        clearTimeout(sNbn_InitCustomNotification);
        if(oNbn_Notification.nbn_status_custom_notification === "1"){
            sNbn_InitCustomNotification = setInterval(function(){
                oNbn_showCustomNotification();
            }, (Math.round(oNbn_Notification.nbn_first_time_notifcation) + 3000));
        }
        
        function oNbn_showCustomNotification()
        {
            $.ajax({
                url: oNbn_Notification.ajaxurl,
                type: 'get',
                data: customData,
                dataType: 'html',
                beforeSend: function() {
                    $('.nbn-wrapper-custom').remove();
                    clearTimeout(sNbn_InitCustomNotification);
                },
                complete: function() {
                },
                success: function(html) {
                    sNbn_InitCustomNotification = setInterval(function(){
                        oNbn_showCustomNotification();
                    }, oNbn_Notification.nbn_time_custom_notifcation);
                    if(html !== ''){
                        var eNotification = $(html);
                        eNotification.css({'z-index':9999});
                        eNotification.css({opacity:0});
                        $('body').append(eNotification);
                        if($('.nbn-wrapper').length > 0){
                            eNotification.css({'z-index':9990});
                            doAnimation(eNotification, 500, false, false, Math.round($('.nbn-wrapper').outerHeight()));
                            
                            if($("a.mr_social_sharing_popup_link".length) > 0){
                                $.each($("a.mr_social_sharing_popup_link"), function() {
                                    var elem = $(this);
                                    elem.click( function (event) {
                                        event.preventDefault();
                                        var popup = window.open(elem.attr('href'),'mr_social_sharing','height=400,width=740');
                                        if (popup) {
                                            popup.focus();
                                        }
                                    });
                                });
                                $(".mr_social_sharing_wrapper p").each(function(){
                                    $(this).remove();
                                });
                            }
                        } else {
                            doAnimation(eNotification, 500);
                        }
                        setTimeout(function(){
                            $('.nbn-wrapper-custom').find('.nbn-close-btn').trigger('click');
                        }, oNbn_CustomDuration);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {

                }
            });   
        }

        $('body').on('click', '.nbn-close-btn', function(event) {
            event.preventDefault();
            doAnimation($(this).parent().parent(), 500, true, function(){
                //$(this).parent().parent().remove();
				if($('.nbn-wrapper-custom').length > 0){
                    $('.nbn-wrapper-custom').css({bottom:20});
                }
                if($('.nbn-wrapper').length > 0){
                    $('.nbn-wrapper').css({bottom:20});
                }
            });
        });

        $('body').on('click', 'p.external-product a.single_add_to_cart_button, a.single_add_to_cart_button,.single_add_to_cart_button, a.product_type_external', function(event) {
            
            var iProductID = $('#comment_post_ID').val();
            if(iProductID === undefined || iProductID === 0){
                iProductID = $(this).data('product_id');
            }
            if(iProductID === undefined || iProductID === 0){
                iProductID = parseInt($('.product').attr('id').replace(/[A-Za-z$-]/g, ''));
            }
			
            var oAttrib = $(this).attr('href');
            var sUrl = '';
            if (typeof oAttrib !== typeof undefined && oAttrib !== false) {
                sUrl = oAttrib.replace(/&amp;/g, '&');
            }
            var data = {
                action: 'update_time_ordered_product_ajax',
                product_id: iProductID,
                is_ajax : 'true'
            };
            $.ajax({
                url: oNbn_Notification.ajaxurl,
                type: 'post',
                data: data,
                dataType: 'html',
                beforeSend: function() {
                },
                complete: function() {
                },
                success: function(html) {
                    if(html === 'success' && typeof oAttrib !== typeof undefined && oAttrib !== false){
                        window.location.href = sUrl;
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
					if (typeof oAttrib !== typeof undefined && oAttrib !== false) {
                    window.location.href = sUrl;
					}
                }
            });
        });

        function doAnimation(eBlock, iDuration, bHide, fCallback, iBottomMargin)
        {
            var ePos = eBlock.position();
            if(bHide === true){
                eBlock.css({opacity:1});
                eBlock.animate({bottom:6, opacity:0}, {duration: iDuration, complete: function () {
                        eBlock.remove();
                        if (typeof fCallback === "function") {
                            fCallback();
                        }
                    }
                });   
            } else {
                eBlock.css({bottom:6});
                eBlock.css({opacity:0});
                var iBottomPix = 20;
                if(iBottomMargin){
                    iBottomPix = iBottomMargin + 30;
                }
                eBlock.animate({bottom:iBottomPix, opacity:1}, {duration: iDuration, complete: function () {
                        if (typeof fCallback === "function") {
                            fCallback();
                        }
                    }
                });   
            }
        }
    });
})(jQuery);