
SEAT.mapAddrPos = 1;
SEAT.lang = 'it';
SEAT.singleRouteUrl = '';

SEAT.isTabSchedaVisited = false;
SEAT.isTabSediVisited = false;
SEAT.isTabVideoVisited = false;

// taglio testo in scheda azienda
function leggiTutto()
{
	
	$('#cut-text').click(function(event){
	$('#cutted-txt').slideDown();
	$(this).hide();
	event.preventDefault();
	});
}

// js per slide mappa in scheda azienda
function mapToggle()
{
	
	$("#map-show").click(function(e){
		$("#map-box").slideToggle();
		e.preventDefault();
	});
}

function getLangCode( langStr)
{
	var langCod = 1;
	
	switch( langStr)
	{
		case 'it': langCod = 1; break;
		case 'en': langCod = 2; break;
		case 'de': langCod = 3; break;
		case 'fr': langCod = 4; break;
		case 'es': langCod = 5; break;
	}
	
	return langCod;
}


function openRouteUrl()
{
	var routeUrl = '';
	
	if( SEAT.singleRouteUrl == '')
	{
		// console.log( 'SEAT.singleRouteUrl vuota');
		// console.log( 'openRouteUrl');
		// http://www.tuttocitta.it/tcol/percorsi?dv2=Cagliano frazione di Colle Brianza (LC)&cx=7.67082&cy=45.09068&z=1&zd=0.6&
		// http://www.tuttocitta.it/tcol/percorsi?dv2=Torino%20(TO)&ind2=Corso%20Mortara,%2034&cx=7.67082&cy=45.09068&z=1&zd=0.6&
		var nodo = $("#accord-visible-indir div.accordion-parent[ ind = '" + SEAT.mapAddrPos + "' ]");
		var posX = nodo.attr('x');
		var posY = nodo.attr('y');
		var posIndir = nodo.find("span.street-address").text();
		var posLoc = nodo.find("span.locality").text();
		// console.log( ' SEAT.mapAddrPos = ' + SEAT.mapAddrPos + ' x = ' + posX + '  y = ' + posY + '  indir = ' + posIndir);
		routeUrl  = 'http://www.tuttocitta.it/tcol/percorsi?';
		routeUrl += 'cx=' + posX + '&cy=' + posY + '&z=1&zd=0.6&dv2=' + posLoc + '&ind2=' + posIndir;
	}
	else
	{
		routeUrl  = SEAT.singleRouteUrl;
		// console.log( 'SEAT.singleRouteUrl NON vuota');
	}
	
	routeUrl += '&l=' + getLangCode( SEAT.lang);
	
	window.open( routeUrl);
}


//funzioni per video tab
function singleVideoOnClick(event,videoObj){
	globalStatistics('','', '', 'video/'+videoObj.video, '');
};

// questa funzione dovrebbe essere migliorata, piu' selettiva
// non c'e' sempre bisogno di chiamare tutte le funzioni all'interno
var tabClickFirstTime = true;
function tabClick(event, tabIndex) // non funziona se invece scrivo: tabClick( event, tabIndex, this)
{
	// console.log( 'function tabClick');
	// deduco il nome-id della scheda
	var sectName = 'scheda';
	var tabObj = $('#tabid-'+tabIndex);
	var sect = tabObj.attr('sectid');
	if(tabIndex > 0){
		sectName = tabObj.attr('sectname');
	}
	
	//log statistiche
	//tolgo prima chiamata loggata da pagina
	if(!tabClickFirstTime){
		//ora la PAG di Shiny e' identica alla url per RedSherif
		globalStatistics('','0', sectName, sectName, sect);
	}else{
		tabClickFirstTime = false;
	}
	
	if( sectName == 'scheda' && !SEAT.isTabSchedaVisited)
	{
		initMap();
		initPagineMultiIndir();
		pageSelectIndir( 0);
		initImages();
		leggiTutto();
		mapToggle();
		SEAT.isTabSchedaVisited = true;
	}

	if( sectName == 'sedi' && !SEAT.isTabSediVisited)
	{
		initPagineRegioniSedi();
		pageSelectSedi( 0);
		SEAT.isTabSediVisited = true;
	}

	if( sectName == 'video' && !SEAT.isTabVideoVisited)
	{
		initVideo();
		SEAT.isTabVideoVisited = true;
	}

	//stop video
	if( sectName != 'video')
	{
		stopVideo();
	}
};

function initImages()
{
	//console.log( 'initImages()');
	// js per dialog click su img in scheda azienda
	var triggers = $(".img-overlay").overlay({mask: "#444"});
	
	$(".img-overlay").click(function(){
		// evita il double click al re-open scroll
		$(".dialog-scrollable").scrollable({circular: true, speed: 200});
		var api = $(".dialog-scrollable").data("scrollable");
		api.seekTo(0,1);
		var url = $(this).attr("src");
		$(".items-cont img").each(function(i){
			var src = $(this).attr("src");
			var item = $(".items");
			if(src == url){
				$(this).parent().prependTo(item);
				//item.css({'left':'0px'});
			}
		});
	});
	// end js per dialog click su img in scheda azienda
};

var videoHandlerObj = null;
function stopVideo(){
	if(videoHandlerObj != null){
		videoHandlerObj.stopCurrentVideo();
	}
};

$(document).ready(function(){

    // js per show/hide popup+dialog in scheda azienda
    var width = $(document).width();
    var height = $(document).height();
    $("#overlay-div").css({'width': width,'height': height,'position': 'absolute','top': '0px','left': '0px'}).hide();
    $(".call-tooltip,#site-tooltip,#freecall-more").click(function(){
    	var position = $(this).position();
    	$("#overlay-div").fadeIn();
    	$(this).next().css({'left': '0px','top': + position.top}).fadeIn();
	});
    $("#overlay-div,.close-tooltip").click(function(){
    	$('#overlay-div').fadeOut();
    	$(".toolbox").fadeOut();
    });
    
    // js per tabs scheda azienda
	//$("ul.tabs").tabs(".tabs-scheda", {tabs: 'li'});
	// ste $("ul.tabs").tabs("div.panes > div", {tabs: 'li'});
	/* accordion */
	$.tools.tabs.addEffect("slide", function(tabIndex, done){
		var maxHeight = this.getPanes().parent().height();
		this.getPanes().parent().height(maxHeight);
		this.getPanes().slideUp(200);
		this.getPanes().eq(tabIndex).slideDown(200, done);
	});
	$("#accordion").tabs("#accordion .pane", {tabs: 'h2', effect: 'slide', initialIndex: null});
	// end js per tabs scheda azienda
	
	
	// js gestione tooltip scrollable video in scheda azienda
	$(".scrollable").scrollable().navigator();
	// end js gestione tooltip scrollable video in scheda azienda

	// js per dialog contatta
	$(".amail").overlay({
		target: '#contact-dialog',
		mask: "#020202", 
		opacity:0.5, 
		fixed: false,
		onBeforeLoad: function(){			
			var wrap = this.getOverlay().find(".contentWrap");
			wrap.load(this.getTrigger().attr("href"), function(){
				$("#riprova_link_contatta").click(function(){
					$('#msg_ko_contatta').hide();
					$('#contatta-captcha').val('').blur();
					new_freecap('contatta');
					$('#form_contatta').show();
				});
				
				$("#nuova_segn_link_contatta").click(function(){
					resetFormMail('form_contatta');
					$('#msg_ok_contatta').hide();
					new_freecap('contatta');
					$('#form_contatta').show();
					if($("#andata-box").hasClass("inactived")){
						$("#andata-box").click();
					}
				});
				//tooltip cos'e'
				$(".whatis").mouseover(function(){
					$('.whatis-box').fadeIn();
				});
				$(".whatis").mouseleave(function(){
					$('.whatis-box').fadeOut();
				});

				
				// show-hide input header
				setExample('#inform-box input[type="text"]');
				setExample('#inform-box textarea');
				// js per slide in dialog contatta
				$("#andata-box").click(function(event){
					$(this).toggleClass("inactived");
					if($(this).hasClass("inactived")){
						var altezza_exposeMask = ($("#exposeMask").height() + 250);
						$("#exposeMask").css({height: altezza_exposeMask});
						$(".footer").css({"top": "250px"});
					}
					$("#anadata-show").slideToggle(300);
					if(!$(this).hasClass("inactived")){
						var altezza_exposeMask = ($("#exposeMask").height() - 250);
						setTimeout(function(){
							$("#exposeMask").css({height: altezza_exposeMask});
							$(".footer").css({"top": "0px"});
						},300);
					}
					event.preventDefault();
				});
			});
		},
		onLoad: function(){
			$('body,html').animate({
				scrollTop: 0
			}, 150);
			
			$("#contact-dialog .dialog-close").click(function(){
				$(".amail").overlay().close();
			});
			// end js per slide in dialog contatta
			stopVideo();
			//statistiche
			globalStatistics('','0','contatta','contatta', '3');
		},
		onClose: function(){
			$(".footer").css({"top": "0px"});
		}
	});
	// end js per dialog contatta
	
	// js per dialog segnala ad amico
	bindSegnalaOverlay("#friend-dialog","scheda");
	// end js per dialog segnala ad amico
	
	// js per dialog chiama gratis
	$(".freecall").overlay({
		target: '#call-dialog',
		mask: "#020202", 
		opacity:0.5, 
		fixed: false,
		onBeforeLoad: function(){
			var wrap = this.getOverlay().find(".contentWrap");
			wrap.load(this.getTrigger().attr("href"));
		},
		onLoad: function(){
			$('body,html').animate({
				scrollTop: 0
			}, 150);
			stopVideo();
			//statistiche
			globalStatistics('','0','chiamagratis', 'chiama gratis', '14');
		}
	});
	// end js per dialog chiama gratis

});	
