﻿var loadingTimer;
var loadingFrame = 1;

jQuery(document).ready(function() {
    /* Close */
    jQuery(".close").click(
		function() {
		    jQuery(this).parent().fadeTo(400, 0, function() { // Links with the class "close" will close parent
		        jQuery(this).slideUp(400);
		    });
		    return false;
		}
	);
})

function showLoading() {
    clearInterval(loadingTimer);
    var w = getViewport();
    jQuery("#fancy_loading").css({ 'left': ((w[0] - 40) * 0.5 + w[2]), 'top': ((w[1] - 40) * 0.5 + w[3]) }).show();
    loadingTimer = setInterval(animateLoading, 60);
}

function hideLoading() {
    clearInterval(loadingTimer);

    jQuery("#fancy_loading").hide();
}

function animateLoading() {
    jQuery("#fancy_loading > div").css('top', (loadingFrame * -40) + 'px');
    loadingFrame = (loadingFrame + 1) % 12;
}

function getViewport() {
    return [jQuery(window).width(), jQuery(window).height(), jQuery(document).scrollLeft(), jQuery(document).scrollTop()];
}

function showNotification(type, text) {
    var notification = jQuery("#notification");
    var w = getViewport();
    notification.css({ 'left': ((w[0] - 40) * 0.5 + w[2]), 'top': ((w[1] - 40) * 0.5 + w[3]) });

    switch (type) {
        case "success":
            notification.attr("class", "notification success");
            jQuery("div", notification).html(text);
            break;
        case "error":
            notification.attr("class", "notification error");
            jQuery("div", notification).html(text);
            break;
        case "attention":
            notification.attr("class", "notification attention");
            jQuery("div", notification).html(text);
            break;
        default:
            break;
    }

    notification.show();
    notification.fadeTo(400, 1);
    setTimeout(hideNotification, 1500);
}

function hideNotification() {
    var notification = jQuery("#notification");
    notification.fadeTo(400, 0, function() { // Links with the class "close" will close parent
        jQuery(this).hide();
    });
}
