$hdr = jQuery.noConflict();

$hdr(document).ready(function () {
    $hdr("#notificationLink").click(function () {
        if ($hdr("#notificationContainer").length > 0) {
            $hdr("#notificationContainer").fadeIn("slow");
        }
        if ($hdr("#notification_count").length > 0) {
            $hdr("#notification_count").fadeOut("slow");
        }
        if ($hdr("#ucHeader_notification_count").length > 0) {
            $hdr("#ucHeader_notification_count").fadeOut("slow");
        }

        $hdr.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Export.aspx/SetNotifySession",
            dataType: "json",
            success: function (msg) {
            }
        });

        //if ($hdr('#hdnNotificationReaded').length > 0) {
        //    $hdr('#hdnNotificationReaded').val("true");
        //}
        //if ($hdr('#ucHeader_hdnNotificationReaded').length > 0) {
        //    $hdr('#ucHeader_hdnNotificationReaded').val("true");
        //}
        return false;
    });

    //Document Click hiding the popup 
    $hdr(document).click(function () {
        if ($hdr("#notificationContainer").length > 0) {
            if ($hdr("#notificationContainer").css('display') != 'none') {
                //$hdr.ajax({
                //    type: "POST",
                //    contentType: "application/json; charset=utf-8",
                //    url: "Export.aspx/SetNotifySession",
                //    dataType: "json",
                //    success: function (msg) {
                //    }
                //});
                if ($hdr("#notification_count").length > 0) {
                    $hdr("#notification_count").fadeOut("slow");
                }
                if ($hdr("#ucHeader_notification_count").length > 0) {
                    $hdr("#ucHeader_notification_count").fadeOut("slow");
                }
                $hdr("#notificationContainer").hide();
            }
        }
        //if ($hdr("#ucHeader_btnDummy").length > 0) {
        //    $hdr('#ucHeader_btnDummy').trigger('click');
        //}
        //if ($hdr("#btnDummy").length > 0) {
        //    $hdr('#btnDummy').trigger('click');
        //}
    });

    //Popup on click
    $hdr("#notificationContainer").click(function () {
        return false;
    });

});
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
    // This Function Will Called When Page Is Refreshed
    $hdr("#notificationLink").click(function () {
        if ($hdr("#notificationContainer").length > 0) {
            $hdr("#notificationContainer").fadeIn("slow");
        }
        if ($hdr("#notificationContainer").length > 0) {
            $hdr("#notification_count").fadeOut("slow");
        }
        if ($hdr("#notificationContainer").length > 0) {
            $hdr("#ucHeader_notification_count").fadeOut("slow");
        }

        $hdr.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Export.aspx/SetNotifySession",
            dataType: "json",
            success: function (msg) {
            }
        });

        //if ($hdr('#hdnNotificationReaded').length > 0) {
        //    $hdr('#hdnNotificationReaded').val("true");
        //}
        //if ($hdr('#ucHeader_hdnNotificationReaded').length > 0) {
        //    $hdr('#ucHeader_hdnNotificationReaded').val("true");
        //}
        return false;
    });

    //Document Click hiding the popup 
    $hdr(document).click(function () {
        if ($hdr("#notificationContainer").length > 0) {
            if ($hdr("#notificationContainer").css.display != 'none') {
                //$hdr.ajax({
                //    type: "POST",
                //    contentType: "application/json; charset=utf-8",
                //    url: "Export.aspx/SetNotifySession",
                //    dataType: "json",
                //    success: function (msg) {
                //    }
                //});
                if ($hdr("#notification_count").length > 0) {
                    $hdr("#notification_count").fadeOut("slow");
                }
                if ($hdr("#ucHeader_notification_count").length > 0) {
                    $hdr("#ucHeader_notification_count").fadeOut("slow");
                }
                $hdr("#notificationContainer").hide();
            }
        }
        //if ($hdr("#ucHeader_btnDummy").length > 0) {
        //    $hdr('#ucHeader_btnDummy').trigger('click');
        //}
        //if ($hdr("#btnDummy").length > 0) {
        //    $hdr('#btnDummy').trigger('click');
        //}
    });

    //Popup on click
    $hdr("#notificationContainer").click(function () {
        return false;
    });
});

var myVar = notificationpoll();

function notificationpoll() {
    setTimeout(function () {
        //PageMethods.LoadNotifications();
        //loadnotifications();
    }, 15000)
}

function loadnotifications() {
    $hdr.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Export.aspx/LoadNotifications",
        dataType: "json",
        success: function (msg) {
            var result = (msg.d);
            var resultset = result.split('$');
            document.getElementById('ucHeader_notificationsBody').innerHTML = resultset[0];
            var notificationcount = document.getElementById('ucHeader_notification_count');
            if (resultset[1] > 0) {
                var ua = window.navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
                M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
                if (M[0] === 'MSIE' && parseInt(M[1]) >= 10) {
                    notificationcount.style.display = 'inline-block';
                }
                if (M[0] === 'Trident' && parseInt(M[1]) >= 7) {
                    notificationcount.style.display = 'inline-block';
                }
                else if (M[0] === 'Firefox' && parseInt(M[1]) >= 37) {
                    notificationcount.style.display = 'inline-flex';
                }
                else if (M[0] === 'Chrome' && parseInt(M[1]) >= 42) {
                    notificationcount.style.display = 'inline-block';
                }
                else {
                    notificationcount.style.display = 'inline-block';
                }
                notificationcount.innerHTML = resultset[1];
            }
            else {
                notificationcount.style.display = 'none';
            }
            notificationpoll();
        }
    });
}
