/*--------------------------------------------------
	MENU
--------------------------------------------------*/
function menu() {
	
	// funzione da richiamare al setTimeout
	function chiudi(el){
		el.fadeOut("normal");
		el.removeClass("on");
	}
	
	// nascondo tutti gli <ul li ul>
	$("#menu ul li ul").fadeOut(1);
	$("#menu ul li ul li a.on").parent().parent().fadeIn("normal");
	$("#menu ul li a[href='#']").click(function(){return false;});

	// al rollover su <ul li a> show del sottomenu
	$("#menu ul li a").not($("#menu ul li ul li a")).mouseover(function(){
		$(this).next().addClass("on").fadeIn("normal");
		return false;
	});
	
	// al rollout su <ul li a> timeout della chiusura del sottomenu
	$("#menu ul li a").not($("#menu ul li ul li a")).mouseout(function(){
		$(this).next().removeClass("on");
		var myElA = $(this).next();
//		var myElA = $(this).parent().childrens().not($(this));
		aTimer = setTimeout(
					function(){
						if (myElA.attr("class")!="on"){
							chiudi(myElA);
						}
					},200); 
	});
	
	// al rollover su <ul li ul> aggiungo classe on
	$("#menu ul li ul").mouseover(function(){
		$(this).addClass("on");
	});
	
	// al mousout tolgo la classe on e timeout della chiusura
	$("#menu ul li ul").mouseout(function(){
		$(this).removeClass("on");
		var myElUl = $(this);
		ulTimer = setTimeout(
					function(){
						if (myElUl.attr("class")!="on"){ // se non c'� classe on (che appare al rollover) lo chiudo
							chiudi(myElUl);
						}
					},1000); 
	});
	
	// riallineo il menu lingue
	$("#menu ul li ul:eq(1)").css({"margin-top":"-37px"});
}


/*-------------------------------------------------
	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);
			});
		}
	});
}


/*-------------------------------------------------
	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");
	    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(){
    $('#lista_regioni').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)  {
    $('#lista_regioni').show('slow');
} else {
    $('#lista_regioni').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").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").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)  {
    $('#lista_regioni').show('slow');
} else {
    $('#lista_regioni').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").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").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").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').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").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)
        { 
           //alert(data.d.HTML);
           $("#search_site_result").html(data.d.HTML);
           $('#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() {
		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"
    });
}




/*--------------------------------------------------
	INIT
--------------------------------------------------*/

$(function(){
    inizializza_controlli();
    clean_search();
    do_search();
	menu();
	zoom();
	cambioLingua();
	alignProdTxt();
	photogallery();
	scrollable();
	OpenPageFlip();
});


/*--------------------------------------------------
	WINDOW RESIZE
--------------------------------------------------*/
$(window).resize(function(){
	winH = $(window).height();
});


/*--------------------------------------------------
	WINDOW LOAD
--------------------------------------------------*/
$(window).load(function(){
	alignThumb();
});
