// @author Jan Hronec
var Sky = {

    action: function (params) {
        $.get('/', params, function (data, textStatus) {
            if (textStatus == "success") {
                alert(data);
            } else {
                alert("An error occurred, please try again.");
            }
        });
    },

    downloadSamplePackage: function () {
        window.location.href = "/resources/data/sky-textures-sample.zip";
    },
    
    addTextureToLightbox: function (lid, tid, num) {
         $.get("/", {"lightbox-action": "add-texture", "lightbox-id": lid, "texture-id": tid}, function (data, textStatus) {
            if (textStatus == "success") {

                if (data.status == true) {
                    var top = $("#lightbox-counter-top-" + num);
                    var body = $(".lightbox-counter-body-" + num);
                    if (top && body) {
                        $(top).text(parseInt($(top).text()) + 1);
                        $(body).each(function () {
                            $(this).text(parseInt($(this).text()) + 1);
                        });
                    }
                    
                }
                
                if (data.message != "OK") {
                    alert(data.message);
                }

            } else {
                alert("An error occurred, please try again.");
            }
        },"json");
    },

    rejectTextureFromLightbox: function (lid, tid, num, item) {
         $.get("/", {"lightbox-action": "drop-texture", "lightbox-id": lid, "texture-id": tid}, function (data, textStatus) {
            if (textStatus == "success") {

                if (data.status == true) {
                    var top = $("#lightbox-counter-top-" + num);
                    if (top) {
                        $(top).text(parseInt($(top).text()) - 1);
                        $(item).parent().parent().remove();
                        if ($("#lightbox-gallery > *").length == 0) {
                            $("#lightbox-gallery").html("<strong>Lightbox is empty.</strong>");
                        }
                    }

                }
                
                if (data.message != "OK") {
                    alert(data.message);
                }
                
            } else {
                alert("An error occurred, please try again.");
            }
        },"json");
    },

    freeLightbox: function (lid, num) {
        if (confirm("Are you sure you want to empty the lightbox?")) {
            $.get("/", {"lightbox-action": "free-lightbox", "lightbox-id": lid}, function (data, textStatus) {
                if (textStatus == "success") {

                    if (data.status == true) {
                        $("#lightbox-counter-top-" + num).text("0");
                        $("#lightbox-gallery").html("<strong>Lightbox is empty.</strong>");
                    }

                    if (data.message != "OK") {
                        alert(data.message);
                    }

                } else {
                    alert("An error occurred, please try again.");
                }
            }, "json");
        }
    },

    renameLightbox: function (lid, name, num) {
        $.get("/", {"lightbox-action": "rename-lightbox", "lightbox-id": lid, "lightbox-name": name}, function (data, textStatus) {
            if (textStatus == "success") {

                if (data.status == true) {
                    $(".lightbox-name-" + num).each(function () {
                        $(this).text(name);
                    });
                }

                if (data.message != "OK") {
                    alert(data.message);
                }
                
            } else {
                alert("An error occurred, please try again.");
            }
        }, "json");
    },

    download: function (url, error, credit) {
        if (error.length > 0) {
            alert(error);
            if (error.indexOf("You must be logged in") != -1) {
                window.location.href = "/login";
            }
            return false;
        } 
        if (credit > 0 && !confirm("Amount deducted: - $" + credit)) {
            return false;  
        }
        if (url.length > 5) {
            window.location.href = url;
            if (url.indexOf("/small/") == -1) {
                var text = $("#account-downloads-num").text();
                if (text.length > 0) {
                    text = text.split("/", 2);
                    $("#account-downloads-num").text((parseInt(text[0]) - 1) + "/" + text[1]);
                }
            }
            
            return true;
        }
        return false;
    },

    sendLightbox: function (email, message, lightbox) {
        if (!/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test(email)) {
            alert("Invalid email address");
            return false;
        }
        var ids = [];
        $("input.download-item").each(function () {
            ids.push(this.value);
        });
        if (ids.length > 0) {
            $.post("/download/send-lightbox", {"ids[]": ids, email: email, message: message, lightbox: lightbox}, function (data) {
                if (data.status == true) {
                    var text = $("#account-downloads-num").text();
                    if (text.length > 0) {
                        text = text.split("/", 2);
                        var limit = parseInt(text[0]) - ids.length;
                        if (limit >= 0) {
                            $("#account-downloads-num").text(limit + "/" + text[1]);
                        }
                    }
                }
                alert(data.message);
            }, "json");
            return true;
        }
        return false;
    },

    downloadAll: function () {
        var ids = [];
        $("input.download-item").each(function () {
            ids.push(this.value);
        });
        if (ids.length > 0) {
            window.location.href = "/download/package/" + ids.join("-");
        } else {
            alert("Empty lightbox");
        }
    },

    downloadSelected: function () {
        var ids = [];
        $("input.download-item:checked").each(function () {
            ids.push(this.value);
        });
        if (ids.length > 0) {
            window.location.href = "/download/package/" + ids.join("-");
        } else {
            alert("No texture selected");
        }
    },

    downloadAllAsPdf: function (lightbox) {
        var ids = [];
        $("input.download-item").each(function () {
            ids.push(this.value);
        });
        if (ids.length > 0) {
            window.location.href = "/download/pdf/" + ids.join("-") + "/" + lightbox;
        } else {
            alert("Empty lightbox");
        }
    }
};

