var isLoading = 0;

function addSocialStat(service) {
    var json = { "action" : "stat_add_event",
                 "event"  : "socialbookmark_" + service};
    sendAPIRequest(json, null);
}


function sendAPIRequest(json, readyFunction) {
    var http;

    try {
        http = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            http = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                http = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                 http = false;
            }
        }
    }

    if (!http) alert("Error initializing XMLHttpRequest!");


    if(http.overrideMimeType){
        http.overrideMimeType("text/json");
    }

    // true = async
    var jsonString = JSON.stringify(json);
    http.open("POST", "/api/json.php", true);
    http.onreadystatechange = function() {
        // nothing
        if (http.readyState == 4) {

            if (http.status == 200) {
                if (readyFunction != null) {
                    try {
                        var jsonResponse = JSON.parse(http.responseText);
                        eval(readyFunction + "(" +  JSON.stringify(jsonResponse) + ");");
                    }
                    catch (e) {
                        alert(e + "\n" + http.responseText);

                        if (isLoading == 1){
                            hideLoadingBar();
                        }
                    }
                }
            }
            else {
                alert(http.status + " " + http.statusText + "\n" + http.responseText);

                if (isLoading == 1){
                    hideLoadingBar();
                }
            }
        }
    }

    http.send(jsonString);
}


$(function() {
        $('button').hover(
                function(){
                $(this).addClass("ui-state-hover");
                },
                function(){
                $(this).removeClass("ui-state-hover");
                }
        ).mousedown(function(){
            $(this).addClass("ui-state-active");
        })
        .mouseup(function(){
            $(this).removeClass("ui-state-active");
        });
});


function centerLoadingBar(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#loadingBar").height();
    var popupWidth = $("#loadingBar").width();

    //centering
    $("#loadingBar").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6

    $("#loadingBackground").css({
        "height": windowHeight
    });

}


function showLoadingBar(){
    if (isLoading == 0){
        centerLoadingBar();

        $("#loadingBackground").css({
            "opacity": "0.2"
        });
        
        $("#loadingBackground").show();
        $("#loadingBar").show();
        isLoading = 1;
    }
}


function hideLoadingBar(){
    if (isLoading == 1){
        $("#loadingBackground").hide();
        $("#loadingBar").hide();
        isLoading = 0;
    }
}