﻿// JS Document

/* inizializzazione jquery */
$(document).ready(function () {

    /*

    if ($("div.align_right_zoom").size() > 0) {
    $("div.align_right_zoom").each(function() { 
    var imageName = $("img", this).attr("src");
    var indice1 = imageName.lastIndexOf("_");
    var indice2 = imageName.lastIndexOf(".");
    var lung = imageName.length;
    var newName = imageName.substring(0, indice1) + imageName.substring(indice2, (lung));
    $("img", this).wrap("<a href=\""+newName+"\" rel=\"prettyPhoto\" />");
    });
    }
    if ($("p.align_right_zoom").size() > 0) {
    $("p.align_right_zoom").each(function() { 
    var imageName = $(this).attr("src");
    var indice1 = imageName.lastIndexOf("_");
    var indice2 = imageName.lastIndexOf(".");
    var lung = imageName.length;
    var newName = imageName.substring(0, indice1) + imageName.substring(indice2, (lung));
    $(this).wrap("<a href=\""+newName+"\" rel=\"prettyPhoto\" />");
    });
    }
    if ($(".align_right_zoom").size() > 0) {
    $(".align_right_zoom").each(function () { 
    var imageName = $(this).attr("src");
    var indice1 = imageName.lastIndexOf("_");
    var indice2 = imageName.lastIndexOf(".");
    var lung = imageName.length;
    var newName = imageName.substring(0, indice1) + imageName.substring(indice2, (lung));
    $(this).wrap("<a href=\""+newName+"\" rel=\"prettyPhoto\" />");
    });
    }

    if ($(".align_right_zoom").size() > 0) {
    $(".align_right_zoom").each(function () {
    var imageName = $(this).attr("src");
    var indice1 = imageName.lastIndexOf("_");
    var indice2 = imageName.lastIndexOf(".");
    var lung = imageName.length;
    var newName = imageName.substring(0, indice1) + imageName.substring(indice2, (lung));
    $(this).wrap("<a href=\"" + newName + "\" rel=\"prettyPhoto\" />");
    });
    }

    if ($(".image_zoom").size() > 0) {
    $(".image_zoom").each(function () {
    var imageName = $(this).attr("src");
    var indice1 = imageName.lastIndexOf("_");
    var indice2 = imageName.lastIndexOf(".");
    var lung = imageName.length;
    var newName = imageName.substring(0, indice1) + imageName.substring(indice2, (lung));
    $(this).wrap("<a href=\"" + newName + "\" rel=\"prettyPhoto\" />");
    });
    }

    */

    if ($(".scrollable").size() > 0) {
        $(".scrollable").scrollable({ circular: false });
    }
    if ($("#browsable").size() > 0) {
        $("#browsable").scrollable({ speed: 900, circular: true }).autoscroll({ interval: 8000 }).navigator();
    }
    if ($("a[rel^='prettyPhoto']").size() > 0) {
        $("a[rel^='prettyPhoto']").prettyPhoto({ overlay_gallery: false });
    }
    if ($(".CatalogCategory").size() > 0) {
        $(".CatalogCategory").columnize({ columns: 2, balance: true, dontsplit: "li" });
    }
});

function OpenDialog(messaggio) {
    OpenDialogFull('Attenzione!', messaggio, 300, 200);
}


function OpenDialogFull(titolo, messaggio, w, h) {
    if (messaggio == '') {
        messaggio = 'Questa funzione sarà disponibile a breve!<br /><br />Nel frattempo, potete utilizzare la funzione “<strong>Richiedi Info</strong>” per informazioni o prenotazioni sulle Schede Prodotto, oppure potete contattarci o venirci a trovare agli indirizzi indicati nella pagina “Contattaci”.<br /><br />Vi ringraziamo per il vostro interesse!';
    }

    if ($("#dialogUI").size() > 0) {
        $("#dialogUI").html('<br/><strong>' + messaggio + '</strong>');
        $("#dialogUI").dialog({ closeOnEscape: true,
            title: titolo,
            dialogClass: 'alert',
            draggable: false,
            width: w,
            height: h,
            modal: true,
            resizable: false,
            zIndex: 3999,
            stack: true
        });
    }
    else {
        alert(messaggio);
    }
}



/* FUNZIONI PER IL SEARCH XSLT */

function GoSearch() {
    var field = document.getElementById('searchField');
    var searchType = "";
    if (document.getElementById("rdbSearchTypeCat").checked)
        searchType = "c";
    else
        searchType = "s";

    if (field != null) {
        var testo = document.getElementById('searchField').value;
        if (testo != '') {
            if (testo.length >= 3) {
                window.location.href = '/ricerca/' + '?search=' + encodeURIComponent(testo) + "&type=" + searchType;
            }
            else {
                OpenDialog('<br/><strong>Si prega di specificare una parola di almeno 3 caratteri.</strong>');
            }
        }
    }
    return true;
}

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function setPreviousSearchValue() {
    var prevSearchString = gup('search');
    var prevSearchType = gup('type');
    if (prevSearchString != '') {
        var field = document.getElementById('searchField');
        if (field != null) {
            //document.getElementById('searchField').value = escape(unescape(prevSearchString));
            document.getElementById('searchField').value = decodeURIComponent(prevSearchString);
        }
        else {
            alert('Error: search field not available!');
        }
    }
    if (prevSearchType != '') {
        if (prevSearchType == "c")
            document.getElementById("rdbSearchTypeCat").checked = true;
        else
            document.getElementById("rdbSearchTypeSite").checked = true;
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
addLoadEvent(setPreviousSearchValue);

function disableEnterKey(e) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //firefox     

    if (key == 13) {
        GoSearch();
    }
    return (key != 13);
}

function SetMaxCharsNumber() {
    $("#dialog").html('<br/><strong>Limite di caratteri massimi raggiunto!</strong>');
    $("#dialog").dialog({ closeOnEscape: true,
        title: 'Attenzione!',
        dialogClass: 'alert',
        draggable: false,
        width: 300,
        height: 120,
        modal: true,
        resizable: false,
        zIndex: 3999,
        stack: true
    });
}


/* END FUNZIONI PER IL SEARCH */


