﻿var doNothing = true;

$(document).ready(function () {
    $("#decori").animate({
        opacity: 0.1
    }, 1500);
});

function printRiepilogo() {
    $("#printPage").hide();
    window.print();
}

function toggleMisure(elem) {
    var x = '#' + 'D'+ elem.id;
    $(x).slideToggle("slow");
}

function mv(elem, carat) {
    $("#newImg").find('IMG').each(function () {
        if (this.id == carat) {
            this.src = elem;
        }
    });
}

function rp(elem, carat) {
    if (doNothing == false || carat != 'LAS') {
        $("#newImg").find('IMG').each(function () {
            if (this.id == carat) {
                var miaStringa = this.src;
                var trovaFine = miaStringa.indexOf(".png");
                var trovaInizio = miaStringa.lastIndexOf('/');
                var stringToReplace = miaStringa.substring(trovaInizio + 1, trovaFine);
                var stringNew = stringToReplace.substring(0, stringToReplace.length - 1);
                stringNew = stringNew + elem;
                miaStringa = miaStringa.replace(stringToReplace, stringNew);
                this.src = miaStringa;
            }
        });
    }
}

function addImg(elem) {
    if (doNothing) {
        var rel = elem.longDesc;
        rel = rel.replace(elem.id, elem.id + ($('#IDPANV').val() == undefined ? '' : $('#IDPANV').val()));
        var alt = elem.alt;
        var value = elem.title;
        mv(rel, 'IMG' + alt);
        $('#ID' + alt).val(elem.id);
        if ($('#FINDecori')[0].textContent === undefined) {
            var set = $('#FINDecori')[0].innerText.split(':');
            $('#FINDecori')[0].innerText = set[0] + ": " + value;
        }
        else {
            var set = $('#FINDecori')[0].textContent.split(':');
            $('#FINDecori')[0].textContent = set[0] + ": " + value;
        }
    }

}

$(function () {

    $(".stampaInArt").click(function () {

        var valid = true;
        var message = '';
        $("#divMessage").css("color", "#C00000");
        $("#divMessage").text('');
        $("#frmtoSubmit").find('input').each(function () {
            if ($(this)[0].name != "FIN") {
                if ($(this).val() == '') {
                    message += $("#divMessage").attr('rel') + ' ' + this.title + ' | ';
                }
            }
        });
        if (message != '') {
            $("#divMessage").text(message);
            return false;
        }

    });

    $(".addIMG").click(function () {
        var rel = $(this).attr('rel');
        var alt = $(this).attr('alt');
        mv(rel, 'IMG' + alt);
    });



    $(".imgPrint").click(function () {
        if (doNothing == false) {
            var rel = $(this).attr('rel');
            mv(rel, 'IMGLAS');
            $("#divLAS").fadeOut(0);
            $("#divLAS").fadeIn(625);
        }
    });

    $(".resizeImg").click(function () {
        if (doNothing == false) {
            var rel = $(this).attr('rel');
            rp(rel, 'IMGLAS');
        }
    });

    $('.checkBoxOptions').click(function () {
        selected($(this));
    });

    $('.portraitImg').click(function () {
        $('#' + $(this).attr('alt')).find('IMG').each(function (index) { setOpt($('#' + this.id)); });
        selected($(this));
    });

    function setOpt(elem) {
        var x = elem[0].className;
        if (elem.hasClass("portraitImgBorderOn")) {
            elem.removeClass("portraitImgBorderOn");
            elem.addClass("portraitImg");
        }

    }

    function selected(elem) {
        if (elem.hasClass("portraitImg")) {
            elem.removeClass("portraitImg");
            elem.addClass("portraitImgBorderOn");
            $('#ID' + elem.attr('alt')).val(elem.attr('id'));
            toggleStatus(elem.attr('alt'), elem.attr('id'), elem.attr('rel'));
        }

        if (elem.hasClass("checkBoxOptions")) {
            var x = $('#ID' + elem.attr('alt')).val(elem.attr('id'));
            toggleCloseDiv(elem.attr('alt'), elem.attr('id'), elem.attr('value'));
        }
        else {
            var x = $('#ID' + elem.attr('alt')).val(elem.attr('id'));
            toggleDiv(elem.attr('alt'), elem.attr('id'), elem.attr('title'));
        }
    }

    function toggleStatus(opt, value, rel) {

        
        if (opt == 'PANV') {
            if (value == "0") {
                doNothing = false;
                $("#xxxx").animate({
                    opacity: 0.1
                }, 1500);
            }
            else {
                doNothing = true;
                $("#xxxx").animate({
                    opacity: 0.9
                }, 1500);
            }

            $("#newImg").find('IMG').each(function () {
                if (this.id == "IMGFIN") {
                    this.src = "http://media.novellini.com/NOV10/Photos/Images/clear.png";

                }
            });
        }

        if (opt == 'LAS') {
            if (value == "1") {
                doNothing = true;
                $("#xxxx").animate({
                    opacity: 0.9
                }, 1500);
            }
            else {
                doNothing = false;
                $("#xxxx").animate({
                    opacity: 0.1
                }, 1500);
            }

            $("#newImg").find('IMG').each(function () {
                if (this.id == "IMGFIN") {
                    this.src = "http://media.novellini.com/NOV10/Photos/Images/clear.png";

                }
            });
        }
    }

    function toggleCloseDiv(opt, value, rel) {
        $("#" + opt).find('div').each(function () {
            $("#" + this.id).slideToggle("slow");
        });

        $("#" + opt).find('a').each(function () {
            if (this.textContent === undefined) {
                var set = this.innerText.split(':');
                this.innerText = set[0] + ": " + rel;
            }
            else {
                var set = this.textContent.split(':');
                this.textContent = set[0] + ": " + rel;
            }
        });
    }

    function toggleDiv(opt, value, rel) {
        $("#" + opt).find('a').each(function () {
            if (this.textContent === undefined) {
                var set = this.innerText.split(':');
                this.innerText = set[0] + ": " + rel;
            }
            else {
                var set = this.textContent.split(':');
                this.textContent = set[0] + ": " + rel;
            }
        });
    }
});
