SITE_LANGUAGE = window.SITE_LANGUAGE || 'IT'; // equivale a SITE_LANGUAGE = SITE_LANGUAGE ? SITE_LANGUAGE : = 'ita';
MENU_SEL = window.MENU_SEL || 'home'
BASE_URL = window.BASE_URL || ''

$(document).ready(function() {

    /* search field setup */
    var default_search_strings = { "IT": "Cerca nel sito...", "EN": "Search the site...", "DE": "Suchen..." }
    $('#clicksearch').click(function() { $('#searchform').submit(); });

    var sf = $('#searchfield');
    var defaultText = default_search_strings[SITE_LANGUAGE]
    sf.click(function() { if (sf.val() == defaultText) sf.val(""); });
    sf.blur(function() { if (sf.val() == defaultText | sf.val() == "") sf.val(defaultText); });
    sf.trigger("blur");

    /* menu selezione prodotti */
    initMenuSelezioneProdotti();

    /* dialogo modale avvisi  */
    //$.blockUI.defaults.css = {};
    var wb = $('.warningmsgbox');
    if (wb.length) $.blockUI({
        message: wb,
        fadeIn: 0
    });
});

function safeSubmit(box) {
    var el = '#' + (box || 'pagina');
    $(el).block({
        overlayCSS: { backgroundColor: '#fff' },
        css: { cursor: 'wait', border: 0 },
        message: '',
        fadeIn: 0
    });
}

function closeDialog() {
    $.unblockUI({
        fadeOut: 0
    });
}

function initMenuSelezioneProdotti() {
    $('ul.menu_selezione_prodotti ul').hide();
    $.each($('ul.menu_selezione_prodotti'), function() {
        var cookie = $.cookie(this.id);
        if (cookie === null || String(cookie).length < 1) {
            $('#' + this.id + '.expandfirst ul:first').show();
        }
        else { $('#' + this.id + ' .' + cookie).next().show(); }
    });
    $('ul.menu_selezione_prodotti li a').click(
		function() {
		    var checkElement = $(this).next();
		    var parent = this.parentNode.parentNode.id;
		    if ($('#' + parent).hasClass('noaccordion')) {
		        if ((String(parent).length > 0) && (String(this.className).length > 0)) {
		            if ($(this).next().is(':visible')) { $.cookie(parent, null); }
		            else { $.cookie(parent, this.className); }
		            $(this).next().slideToggle('normal');
		        }
		    }
		    if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		        if ($('#' + parent).hasClass('collapsible')) {
		            $('#' + parent + ' ul:visible').slideUp('normal');
		        }
		        return false;
		    }
		    if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		        $('#' + parent + ' ul:visible').slideUp('normal');
		        if ((String(parent).length > 0) && (String(this.className).length > 0)) {
		            $.cookie(parent, this.className);
		        }
		        checkElement.slideDown('normal');
		        return false;
		    }
		}
	);
}

