/*-------------
menu
-------------*/
function menu()
{
    $(".sub_menu a").hover(function ()
    {
        var img = $(this).attr("rel");
        var titolo = $(this).html();

        $("#previewFoto img").attr("src", img);
        $("#previewFoto p").html(titolo);
    });
}



/*--------------------------------------------------
bgTrans
--------------------------------------------------*/
function bgTrans(){
	// $("body.HP_en").append('<div class="bg_trans"></div>');	
	$(".bg_trans").css({"width":$(window).width()+"px", "height":$(window).height()+"px"});
	$(window).resize(function(){
		$(".bg_trans").css({"width":$(window).width()+"px", "height":$(window).height()+"px"});
	});
}

/*--------------------------------------------------
show sub menu
--------------------------------------------------*/
function showSubMenu()
{
    $("#menu .menu a").hover(function ()
    {
        $(this).parent().next().show();
        $(this).addClass('on fondino');
    }, function ()
    {
        $(this).parent().next().hide();
        $(this).removeClass('on fondino');
    });

    $(".sub_menu").hover(function ()
    {
        $(this).show();
        $(this).prev().find("a").addClass('on fondino');
    }, function ()
    {
        $(this).hide();
        $(this).prev().find("a").removeClass('on fondino');
    });


}

/*--------------------------------------------------
aggiustamenti vari
--------------------------------------------------*/
function Aggiustamenti()
{
    $("#menu ul li:last-child a").addClass('prn');
}

/*-------------------------------------------------
ALIGN THUMB
-------------------------------------------------*/
function alignThumb()
{
    $.each(jQuery.browser, function (i, val)
    {
        if (i == "msie" && $.browser.version.split(".")[0] < 8)
        {
            $(".scrollable img").each(function ()
            {
                $(this).css({ "margin": ((100 - $(this).height()) / 2) + " 0px 0px 0px", "position": "static" });
            });
            $("#foto_media img").each(function ()
            {
                $(this).animate({ "margin-top": ((530 - $(this).height()) / 2) + "px" }, 500);
            });
        }
    });
}

/*-------------------------------------------------
select transform
-------------------------------------------------*/

function beautifySelect() {
    $("div.select").each(function() {
        $(this).append('<span class="select close"><span style="overflow: hidden">' + $(this).find("option:selected").html() + '</span></span>')
                                   .find("select")
                                       .css({ opacity: 0 })
                                       .change(function() {
                                           $(this).parent().find("span.select span").html($(this).find(':selected').html());
                                       });
    })
};




/*-------------------------------------------------
ALIGN PROD TEXT
-------------------------------------------------
function alignProdTxt() {
if ($(".prod_text").size()) {
$("#contenuti").height(354);
$(".prod_text").css({"position":"absolute", "top":"207px"});
$("#gallery").css({"position":"absolute", "bottom":"0px"});
if ($(".corpo").height() > 200) {
$(".prod_text").css({"top": + (410 - $(".prod_text").height()) + "px"});
//alert($(".prod_text").height() + "-208=" + ($(".prod_text").height()-208))
}
if ($(".prod_text").height() > 400) {
$(".prod_text").css({"top":"10px","overflow-y":"auto"});
$(".corpo").css({"height":"365px"});
}
}
}
*/

/*-------------------------------------------------
PHOTOGALLERY
-------------------------------------------------*/
function photogallery()
{
    if ($("#galleria_foto").size())
    {
        var winW = $(window).width();
        var galL = Math.floor($("#galleria_foto").offset().left);
        var galW = $("#galleria_foto").width();
        var galH = $("#galleria_foto").height();
        var newGalW = (winW - galL);
        var index = 0;
        size = $("#galleria_foto img").size();
        $("#galleria_foto").css({ "width": newGalW + "px", "overflow": "hidden", "height": "400px", "overflow": "hidden" });
        $("#galleria_foto img").each(function ()
        {
            $(this).wrap("<div class='scroll' title='" + $(this).attr("alt") + "' style='background:url(" + $(this).attr("src") + ") no-repeat left top'></div>");
            $(this).remove();
        });
        $("#galleria_foto .scroll").css({ "float": "left", "width": galW + "px", "height": galH + "px" }).wrapAll("<div class='wrap'></div>");
        $("#galleria_foto .wrap").css({ "width": (galW * size) + "px", "height": galH + "px" });

        $("#galleria_foto .scroll:gt(0)").css({ "opacity": 0.3 });

        $("#paginazione a.nextPage").click(function ()
        {
            if (index < size - 1)
            {
                muovi(++index);
            }
            return false;
        });

        $("#paginazione a.prevPage").click(function ()
        {
            if (index > 0)
            {
                muovi(--index);
            }
            return false;
        });

        // mousewheel
        $('#galleria_foto').bind('mousewheel', function (event, delta)
        {
            if (delta > 0)
            {
                $("#paginazione a.prevPage").click();
            } else
            {
                $("#paginazione a.nextPage").click();
            }
            return false;
        });

        function muovi(i)
        {
            $("#galleria_foto .wrap").stop().animate({ "margin-left": "-" + (galW * i) + "px" }, 1000);
            $("#galleria_foto .scroll").stop().animate({ "opacity": 0.3 }, 1000);
            $("#galleria_foto .scroll:eq(" + i + ")").stop().animate({ "opacity": 1 }, 1000);
            $("#dida_testo").html($("#galleria_foto .scroll:eq(" + i + ")").attr("title"));
        }

        $("#galleria_foto .scroll").css({ "cursor": "pointer" }).click(function ()
        {
            muovi($("#galleria_foto .scroll").index($(this)));
            return false;
        });

        muovi(0);
    }
}


/*-------------------------------------------------
ZOOM
-------------------------------------------------*/
var winH = $(window).height();
function zoom()
{
    if ((winH + 50) > 900)
    {
        winH = 900;
    } else
    {
        winH = winH;
    }


    function overlay(img)
    {
        $img = img;
        $img2 = img;
        $('<div id="overlay">&nbsp;</div>').css({ "opacity": 0.6, "height": $(document).height() + "px" }).appendTo("body").fadeIn(500);
        $('<div id="zoomPic"><div id="closeZoomPic"><a href="#">close</a></div><div class="mapwrapper"><div id="viewport2" style="height:' + winH + 'px"><div style="background:url(' + $img + ') no-repeat center center transparent; width:900px; height:900px;"></div><div style="height:2000px; width:2000px;"><img alt="" src="' + $img2 + '"><div class="mapcontent"></div></div></div><div class="map-control"><a class="left" href="#left">Left</a> <a class="right" href="#right">Right</a> <a class="up" href="#up">Up</a> <a class="down" href="#down">Down</a> <a class="zoom" href="#zoom">Zoom</a> <a class="back" href="#zoom_out">Back</a></div></div></div>').css({ "height": (winH - 50) + "px" }).appendTo("body").fadeIn(500);
        $("#viewport2").mapbox({
            mousewheel: true,
            layerSplit: 3
        });
        jQuery(".map-control a").click(function ()
        {//control panel 
            var viewport = $("#viewport2");
            if (this.className == "zoom" || this.className == "back")
            {
                viewport.mapbox(this.className, 2); //step twice 
            }
            else
            {
                viewport.mapbox(this.className);
            }
            return false;
        });

        $("#overlay, #closeZoomPic a").click(function ()
        {
            $("#overlay, #zoomPic, #closeZoomPic a").fadeOut(500, function ()
            {
                $(this).remove();
                return false;
            });
        });
    }

    $("#zoom a").click(function ()
    {
        overlay($(this).attr("href"));
        return false;
    });
}


/*--------------------------------------------------
SCROLLABLE
--------------------------------------------------*/
function scrollable()
{
    try
    {
        url = location.href.split("?co_id=")[1];
        $(".scrollable a.active").removeClass("active");
        var seek = ($(".scrollable a[href$=" + url + "]").size()) ? $(".scrollable a").index($(".scrollable a[href$=" + url + "]")) : 0;
        $(".scrollable a[href$=" + url + "]").addClass("active");
        $(".scrollable").scrollable({ mousewheel: true });
        var api = $(".scrollable").data("scrollable");
        if (api != null)
            api.seekTo(seek - 1);
    } catch (e)
    {
    }
}


/*--------------------------------------------------
CAMBIO LINGUA
--------------------------------------------------*/
function cambioLingua()
{
    var lang_id;
    $("#lang_combo").change(function ()
    {
        lang_id = $("#lang_combo option:selected").val();
        $("#lang_id").val(lang_id);
        document.frm_langSelect.submit();
        return false;
    });
}


/*--------------------------------------------------
INIZIALIZZA CONTROLLI
--------------------------------------------------*/

function inizializza_controlli()
{
    $('.regioniPuntiVendita').hide('');
}



/*--------------------------------------------------
SEARCH
--------------------------------------------------*/
function clean_search()
{
    $("#text_to_search").click(function ()
    {
        $("#text_to_search").val("")
    });
}


function do_search()
{
    var text_to_search;

    $("input[id$='button_search']").click(function ()
    {
        text_to_search = $("#text_to_search").val()

        if (text_to_search.length > 0)
        {
            $("body").removeAttr("onunload");
            $("#fake_text_to_search").val(text_to_search);
            $("#frm_Search").submit();
        }

    });


    $("#text_to_search").keyup(function (event)
    {
        if (event.keyCode == 13)
        {
            text_to_search = $("#text_to_search").val()

            if (text_to_search.length > 0)
            {
                $("#button_search").click();
            }
        }
    });

}





function tabella_punti_vendita()
{
    var nazione_id;
    var id_regione;
    var curPage;
    var NavDirection;


    curPage = 1;
    NavDirection = "next";

    nazione_id = $("#id_selected_nation").val();
    regione_id = $("#id_selected_regioni").val();


    if (nazione_id == 114)
    {
        $('.regioniPuntiVendita').show('slow');
    } else
    {
        $('.regioniPuntiVendita').hide('slow');
        regione_id = 0;
    }


    if ((nazione_id == 114) && (regione_id != 0))
    {

        $('#lista_PuntiVendita').hide('slow');

        $this = $(this);

        //alert (nazione_id + ' - ' + regione_id );

        $.ajax({
            type: "POST",
            url: "/ws/wsGetContent.asmx/GetTableNazioni",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + NavDirection + "'}",
            dataType: "json",
            success: function (data)
            {
                $("#lista_PuntiVendita").hide();
                $("#lista_PuntiVendita").html(data.d.HTML);
                $('#lista_PuntiVendita').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#lista_PuntiVendita").html(data.d.ErrMessage);
            }
        });

    } else
    {

        $('#lista_PuntiVendita').hide('slow');

    }



    if ((nazione_id != 114) && (regione_id == 0))
    {

        $("#id_selected_regioni").val(0);


        $('#lista_PuntiVendita').hide('slow');

        $this = $(this);


        $.ajax({
            type: "POST",
            url: "/ws/wsGetContent.asmx/GetTableNazioni",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + NavDirection + "'}",
            dataType: "json",
            success: function (data)
            {
                $("#lista_PuntiVendita").hide();
                $("#lista_PuntiVendita").html(data.d.HTML);
                $('#lista_PuntiVendita').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#lista_PuntiVendita").html(data.d.ErrMessage);
            }
        });

    }


}







function tabella_dealer()
{
    var nazione_id;
    var id_regione;
    var curPage;
    var NavDirection;

    curPage = 1;
    NavDirection = "next";

    nazione_id = $("#id_selected_nation").val();
    regione_id = $("#id_selected_regioni").val();

    if (nazione_id == 114)
    {
        $('.regioniPuntiVendita').show('slow');
    } else
    {
        $('.regioniPuntiVendita').hide('slow');
        regione_id = 0;
    }

    if ((nazione_id == 114) && (regione_id != 0))
    {

        $('#dealer_list').hide('slow');

        $this = $(this);

        //alert (nazione_id + ' - ' + regione_id );

        $.ajax({
            type: "POST",
            url: "/ws/wsGetContent.asmx/GetDealerList",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + NavDirection + "'}",
            dataType: "json",
            success: function (data)
            {
                //alert(data.d.HTML);
                $("#dealer_list").html(data.d.HTML);
                $('#dealer_list').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#dealer_list").html(data.d.ErrMessage);
            }
        });

    } else
    {

        $('#dealer_list').hide('slow');

    }



    if ((nazione_id != 114) && (regione_id == 0))
    {

        $("#id_selected_regioni").val(0);


        $('#dealer_list').hide('slow');

        $this = $(this);


        $.ajax({
            type: "POST",
            url: "/ws/wsGetContent.asmx/GetDealerList",
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + NavDirection + "'}",
            dataType: "json",
            success: function (data)
            {
                $("#dealer_list").html(data.d.HTML);
                $('#dealer_list').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#dealer_list").html(data.d.ErrMessage);
            }
        });

    }


}





function ChangePage(curPage, direction, WsMethod)
{
    var wsurl;
    var nazione_id;
    var id_regione;

    wsurl = "/ws/wsGetContent.asmx/" + WsMethod;


    if (WsMethod == 'GetListNews')
    {

        $('#press').hide('slow');
        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function (data)
            {
                $('#news_list').hide();
                $("#news_list").html(data.d.HTML);
                $('#news_list').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#news_list").html(data.d.ErrMessage);
            }
        });
    }



    if (WsMethod == 'GetListRassegnaStampa')
    {

        $('#press').hide('slow');
        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function (data)
            {
                $("#press").hide();
                $("#press").html(data.d.HTML);
                $('#press').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#press").html(data.d.ErrMessage);
            }
        });
    }






    if (WsMethod == 'GetTableNazioni')
    {
        nazione_id = $("#id_selected_nation").val();
        regione_id = $("#id_selected_regioni").val();
        $('#lista_PuntiVendita').hide('slow');
        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function (data)
            {
                $("#lista_PuntiVendita").hide();
                $("#lista_PuntiVendita").html(data.d.HTML);
                $('#lista_PuntiVendita').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#lista_PuntiVendita").html(data.d.ErrMessage);
            }
        });

    }



    if (WsMethod == 'GetBoutiquesList')
    {
        //nazione_id= $("#id_selected_nation").val();
        //regione_id= $("#id_selected_regioni").val();
        nazione_id = 0;
        regione_id = 0;

        $('#botiques_list').hide('slow');
        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function (data)
            {
                $('#botiques_list').hide();
                $('#botiques_list').html(data.d.HTML);
                $('#botiques_list').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#botiques_list").html(data.d.ErrMessage);
            }
        });

    }




    if (WsMethod == 'GetDealerList')
    {
        nazione_id = $("#id_selected_nation").val();
        regione_id = $("#id_selected_regioni").val();
        $('#dealer_list').hide('slow');
        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{nazione_id: " + nazione_id + ", regione_id:" + regione_id + ", curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function (data)
            {
                $("#dealer_list").hide();
                $("#dealer_list").html(data.d.HTML);
                $('#dealer_list').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#dealer_list").html(data.d.ErrMessage);
            }
        });

    }





    if (WsMethod == 'GetSearchResult')
    {
        $('#search_site_result').hide('slow');
        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",

            success: function (data)
            {
                $("#search_site_result").hide();
                $("#search_site_result").html(data.d.HTML);
                setClassLast();
                $('#search_site_result').show('slow');
            },
            error: function (data)
            {
                if (data.d != null)
                    $("#search_site_result").html(data.d.ErrMessage);
            }
        });

    }

}




function OpenPageFlip()
{

    $("a.show_map_fake").fancybox({
        'width': '90%',
        'height': '90%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe',
        'titlePosition': 'inside'
    });




    $('a.show_map').live('click', function ()
    {
        // NON USATO
        par = $(this).attr('rel')
        new_url = "http://toywatch.websolute.it/ShowPageFlip.aspx?par=" + par
        //Sostituisco l'url con quello corretto	
        $("a#fake_map_url").attr('href', new_url);

        //Apro il fancy box con i parametri corretti
        $("a.show_map_fake").click();

        return false;
    });

}


function ClearSearchData()
{
    $.ajax({
        async: false,
        type: "POST",
        url: "/ws/wsGetContent.asmx/ClearSearchData",
        cache: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}



function retrieveNazioni(sender)
{
    // SE ISRAELE, DIRETTAMENTE LA RICERCA
    var idArea = $(sender).val();
    var tip = $("#IdTipologia").val();

    $('#botiques_list').hide();
    $("#lista_PuntiVendita").hide();
    $("#punti_vendita").hide();

    $("#cap").parent().remove();
    $("#citta").parent().remove();
    $("#regioni").parent().remove();
    $("#nazioni").parent().remove();
    $("#contee").parent().remove();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetNazioni",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{'idArea':" + idArea + ", 'tip':" + tip + "}",
        dataType: "json",
        success: function (data)
        {
            $(sender).parent().after(data.d);

            if (idArea == 9)
            {
                $("#nazioni").val("113");
                $("#nazioni").hide();

                if (tip == 8)
                    retrieveBoutiques($("#nazioni"));
                else if (tip == 9)
                    retrievePuntiVendita($("#nazioni"));
                else
                    retrieveDealer($("#nazioni"));
            }
            else
            {
                beautifySelect();

                if (tip == 8)
                {
                    // boutiques
                    $("#nazioni").change(function () { retrieveBoutiques(this); });
                }
                else if (tip == 9)
                {
                    // punti vendita
                    $("#nazioni").change(function ()
                    {
                        var nazId = $(this).val();
                        if (nazId == 114)
                        {
                            retrieveRegioni(this);
                        }
                        else
                        {
                            retrieveContee(this);
                        }
                        /*
                        else if (nazId == 95)
                        {
                        retrieveContee(this);
                        }
                        else
                        {
                        retrievePuntiVendita(this);
                        }*/

                    });
                }
                else
                {
                    // dealer
                    //$("#nazioni").change(function () { retrieveDealer(this); });
                    $("#nazioni").change(function ()
                    {
                        var nazId = $(this).val();
                        if (nazId == 114)
                        {
                            retrieveRegioni(this);
                        }
                        else
                        {
                            retrieveDealer(this);
                        }
                    });
                }
            }
        },
        error: function (data)
        {
            alert(data.d);
        }
    });
}


function retrieveRegioni(sender)
{
    $('#botiques_list').hide();
    $("#lista_PuntiVendita").hide();
    $("#punti_vendita").hide();

    $("#cap").parent().remove();
    $("#citta").parent().remove();

    var tip = $("#IdTipologia").val();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetRegioni",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{ tip: " + tip + "}",
        dataType: "json",
        success: function (data)
        {
            $(sender).parent().after(data.d);
            beautifySelect();
            $("#regioni").change(function ()
            {
                //alert(tip);
                if (tip == 10)
                    retrieveDealer($("#nazioni"));
                else if (tip == 9)
                {
                    retrieveCitta(this);
                }
            });
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}


function retrieveContee(sender)
{
    var idNazione = $(sender).val();

    $("#lista_PuntiVendita").hide();
    $("#citta").parent().remove();
    $("#cap").parent().remove();
    $("#regioni").parent().remove();
    $("#contee").parent().remove();

    var tip = $("#IdTipologia").val();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetContee",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: JSON2.stringify({ 'idNazione': idNazione }),
        dataType: "json",
        success: function (data)
        {
            if (data.d == "")
            {
                retrievePuntiVendita(sender);
            }
            else
            {
                $(sender).parent().after(data.d);
                beautifySelect();
                $("#contee").change(function ()
                {
                    //alert(tip);
                    if (tip == 9)
                    {
                        retrievePuntiVendita(sender);
                    }
                });
            }
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}

function retrieveCitta(sender)
{
    $("#lista_PuntiVendita").hide();
    $("#punti_vendita").hide();

    $("#citta").parent().remove();
    $("#contee").parent().remove();

    var regione = $(sender).val();
    var tip = $("#IdTipologia").val();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetCitta",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: JSON2.stringify({ 'idRegione': regione }),
        dataType: "json",
        success: function (data)
        {
            $(sender).parent().after(data.d);
            beautifySelect();
            retrieveCAP(sender);

            $("#citta").change(function ()
            {
                //alert(tip);
                if (tip == 10)
                    retrieveDealer($("#nazioni"));
                else if (tip == 9)
                {
                    $("#cap").val("-1");
                    beautifySelect();
                    retrievePuntiVenditaCitta($("#nazioni"));
                }
            });
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}


function retrieveCAP(sender)
{
    var regione = $(sender).val();
    var tip = $("#IdTipologia").val();

    $("#cap").parent().remove();
    $("#contee").parent().remove();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetCAP",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: JSON2.stringify({ 'idRegione': regione }),
        dataType: "json",
        success: function (data)
        {
            $("#citta").parent().after(data.d);
            beautifySelect();
            $("#cap").change(function ()
            {
                if (tip == 10)
                    retrieveDealer($("#nazioni"));
                else if (tip == 9)
                {
                    $("#citta").val("-1");
                    beautifySelect();
                    retrievePuntiVenditaCAP($("#nazioni"));
                }
            });
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}


function retrieveBoutiques(sender)
{
    var idNazione = $(sender).val();
    var idRegione = 0;
    
    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetBoutiquesList",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{nazione_id: " + idNazione + ", regione_id:" + idRegione + ", curPage: 1, NavDirection:'next'}",
        dataType: "json",
        success: function (data)
        {
            $('#botiques_list').html(data.d.HTML);
            $('#botiques_list').show('slow');
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}

function retrieveBoutiquesByUT(ut_id)
{
    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetBoutiquesListByUT",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{'ut_id':" + ut_id + "}",
        dataType: "json",
        success: function (data)
        {
            var v = data.d.coord;
            var s = v.split(",");

            $("#areeNazioni").val(s[0]);
            retrieveNazioniAndSet($("#areeNazioni"), s[1]);

            $("#botiques_list").html(data.d.markup);
            $("#botiques_list").show();
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}



function retrieveNazioniAndSet(sender, v)
{
    var idArea = $(sender).val();
    var tip = $("#IdTipologia").val();

    $("#citta").parent().remove();
    $("#regioni").parent().remove();
    $("#nazioni").parent().remove();
    $("#contee").parent().remove();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetNazioni",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{'idArea':" + idArea + ", 'tip':" + tip + "}",
        dataType: "json",
        success: function (data)
        {
            $(sender).parent().after(data.d);
            $("#nazioni").val(v);
            beautifySelect();
            if (tip == 8)
            {
                // boutiques
                $("#nazioni").change(function () { retrieveBoutiques(this); });
            }
            else if (tip == 9)
            {
                // punti vendita
                $("#nazioni").change(function ()
                {
                    var nazId = $(this).val();
                    if (nazId == 114)
                    {
                        retrieveRegioni(this);
                    }
                    else if (nazId == 95)
                    {
                        retrieveContee(this);
                    }
                    else
                    {
                        retrievePuntiVendita(this);
                    }
                });
            }
            else
            {
                // dealer
                //$("#nazioni").change(function () { retrieveDealer(this); });
                $("#nazioni").change(function ()
                {
                    var nazId = $(this).val();
                    if (nazId == 114)
                    {
                        retrieveRegioni(this);
                    }
                    else
                    {
                        retrieveDealer(this);
                    }
                });
            }
        },
        error: function (data)
        {
            alert(data.d);
        }
    });
}



function retrieveDealer(sender)
{
    var idRegione = 0;
    var citta = "";

    var idNazione = $(sender).val();
    if (idNazione == 114)
    {
        idRegione = $("#regioni").val();
        citta = $("citta").val();
    }
    else
    {
        $("#regioni").parent().remove();
        $("#citta").parent().remove();
    }

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetDealerList",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{nazione_id: " + idNazione + ", regione_id:" + idRegione + ", citta:'" + citta + "', curPage: 1, NavDirection:'next'}",
        dataType: "json",
        success: function (data)
        {
            $('#delaer_list').html(data.d.HTML);
            $('#delaer_list').show('slow');
        },
        error: function (data)
        {
            if (data.d != null)
                $("#delaer_list").html(data.d.ErrMessage);
        }
    });
}


function retrieveDealersByUT(ut_id)
{
    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetBoutiquesListByUT",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{'ut_id':" + ut_id + "}",
        dataType: "json",
        success: function (data)
        {
            var v = data.d;
            var s = v.split(",");

            $("#areeNazioni").val(s[0]);
            retrieveNazioni($("#areeNazioni"));

            setTimeout("setNazione2(" + s[1] + ");", 500);
        },
        error: function (data)
        {
            if (data.d != null)
                $("#botiques_list").html(data.d.ErrMessage);
        }
    });
}



function retrievePuntiVendita(sender)
{
    var contea = "";
    var idRegione = 0;
    var idNazione = $(sender).val();
    idRegione = $("#regioni").val();
    if ($("#contee:visible").size())
    {
        contea = $("#contee").val();
        wsMethod = "GetPuntiVenditaListUK";
        d = "{nazione_id: " + idNazione + ", contea:'" + contea + "', curPage: 1, NavDirection:'next'}";
    }
    else
    {
        $("#contee").parent().remove();
        $("#regioni").parent().remove();
        $("#citta").parent().remove();
        $("#cap").parent().remove();

        wsMethod = "GetPuntiVenditaList";
        d = "{nazione_id: " + idNazione + ", regione_id:0, cap:'', citta:'', curPage: 1, NavDirection:'next'}";
    }

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/" + wsMethod,
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: d,
        dataType: "json",
        success: function (data)
        {
            $("#lista_PuntiVendita").fadeOut(400, function ()
            {
                $('#lista_PuntiVendita').html(data.d.HTML);
                $('#lista_PuntiVendita').fadeIn(650);
            });
        },
        error: function (data)
        {
            if (data.d != null)
                $("#lista_PuntiVendita").html(data.d.ErrMessage);
        }
    });
}




function retrievePuntiVenditaCitta(sender)
{
    $("#contee").parent().remove();
    $("#cap").val("-1");
    var idRegione = 0;
    var citta = "";
    var idNazione = $(sender).val();
    idRegione = $("#regioni").val();
    citta = $("#citta").val();
    citta = citta.replace("'", "_");

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetPuntiVenditaList",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{nazione_id: " + idNazione + ", regione_id:" + idRegione + ", cap:'', citta:'" + citta + "', curPage: 1, NavDirection:'next'}",
        dataType: "json",
        success: function (data)
        {
            $("#lista_PuntiVendita").fadeOut(400, function ()
            {
                $('#lista_PuntiVendita').html(data.d.HTML);
                $('#lista_PuntiVendita').fadeIn(650);
            });
        },
        error: function (xhr, status, error)
        {
        }
    });
}


function retrievePuntiVenditaCAP(sender)
{
    var idRegione = 0;
    var cap = "";
    $("#citta").val("-1");

    var idNazione = $(sender).val();
    idRegione = $("#regioni").val();
    cap = $("#cap").val();

    $.ajax({
        type: "POST",
        url: "/WS/wsGetContent.asmx/GetPuntiVenditaList",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: "{nazione_id: " + idNazione + ", regione_id:" + idRegione + ", cap:'" + cap + "', citta:'', curPage: 1, NavDirection:'next'}",
        dataType: "json",
        success: function (data)
        {
            $("#lista_PuntiVendita").fadeOut(400, function ()
            {
                $('#lista_PuntiVendita').html(data.d.HTML);
                $('#lista_PuntiVendita').fadeIn(650);
            });
        },
        error: function (data)
        {
            if (data.d != null)
                $("#lista_PuntiVendita").html(data.d.ErrMessage);
        }
    });
}



function setNazione(id)
{

    if (!$("#nazioni").size())
    {
        setTimeout("setNazione(" + id + ");", 500);
    }
    else
    {
        $("#nazioni").val(id);
        retrieveBoutiques($("#nazioni"));
    }
}


function setNazione2(id)
{

    if (!$("#nazioni").size())
    {
        setTimeout("setNazione2(" + id + ");", 500);
    }
    else
    {
        $("#nazioni").val(id);
        retrieveDealer($("#nazioni"));
    }
}






/*--------------------------------------------------
WINDOW RESIZE
--------------------------------------------------*/
$(window).resize(function ()
{
    winH = $(window).height();
});


/*--------------------------------------------------
WINDOW LOAD
--------------------------------------------------*/
$(window).load(function ()
{
    alignThumb();
});

function changeLang(sender, url)
{
    var lngId = $(sender).val();
    location.href = url + "&lg=" + lngId;
}


/*--------------------
    set class 'last'
--------------------*/
function setClassLast()
{
    $("#search_site_result .row").each(function ()
    {
        $(this).find("dl:last").addClass("last");
    });
}



/*--------------------------------------------------
INIT
--------------------------------------------------*/

$(function ()
{
    $("#elencoNewsHome #news_list dl:last").css("margin-right", "0px");
    Aggiustamenti();
    beautifySelect();
    showSubMenu();
    inizializza_controlli();
    clean_search();
    do_search();
    menu();
    zoom();
    cambioLingua();
    //alignProdTxt();
    photogallery();
    scrollable();
    OpenPageFlip();
    initAreaRiservata();
    bgTrans();
    $("#areeNazioni").change(function () { retrieveNazioni(this); });
    if ($("#news_list a[href*='/toytoy']").size())
    {
        var dest = $("#news_list a[href*='/toytoy']").attr("href");
        $("#news_list a[href*='/toytoy']").first().parent().prev().find("a").attr("href", dest).attr("target", "_blank");
    }
    setClassLast();


    /*
    if ($("#welcomeUK").size())
    {
    $(".welcomeFancy").fancybox();
    $("#openWelcomeFancy").click();
    }*/

});


