// _hack
document.write('<style type="text/css">.bv-sep,img.gelm{visibility:hidden}.searchform{visibility:hidden;}</style>');
var isIe7=false;
var isIe6 =(!window.XMLHttpRequest)?true:false;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){var ieversion=new Number(RegExp.$1);if (ieversion>=7)  isIe7=true;}


var jq=jQuery.noConflict(),status='close',isHP;

jq(function(){
  isHP=(jq('#contenitoreFlash').length==0);  
  
  
  // box photogallery
  
  photogallery = jq('.photoGalley');
  photogallery.each(function(i,val){
    var thisSize=5;
 	if(jq(photogallery).hasClass('wemese')) thisSize=2;
     jq( photogallery[i]).scrollable({
      vertical:false,
      size:thisSize,
      speed:200,
      prev:'.prev',
      next:'.next',
      disabledClass:'off',
	  circular:'true'
    }).navigator('.balls');
  });
  
  
  
  
  // scorrables
  jq('.scrollable').each(function(i,val){
    var act=1;
    var direction=true;
    var nitem=jq('.item',val);
    if (jq(val).hasClass('s-twitter')) {
      jq(nitem).each(function(m,el){
        // var patternLinkRegexp = /[#]+[A-Za-z0-9-_]+/gi;
        // var patternDiesisRegexp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
        // value.replace(patternLinkRegexp,"<a href='$1'>$1</a>");
        // value.replace(patternDiesisRegexp,"<a href='http://search.twitter.com/search?q=$1'>$1</a>");
        var pretokens;
        if (jq(el).html().indexOf('<BR>')!=-1) pretokens=jq(el).html().split('<BR>');
        else if (jq(el).html().indexOf('<BR/>')!=-1) pretokens=jq(el).html().split('<BR/>');
        else if (jq(el).html().indexOf('<br>')!=-1) pretokens=jq(el).html().split('<br>');
        else if (jq(el).html().indexOf('<br/>')!=-1) pretokens=jq(el).html().split('<br/>');
        var tokens=pretokens[1].split(' ');
        var newcontent="";
        for(var i=0;i<tokens.length;i++){
          if(tokens[i].indexOf('#')!=-1){
            var goUser=(tokens[i].split('#'))[1],dp='';
            if(goUser.indexOf(':')!=-1){dp=':';goUser=(goUser.split(':'))[0]}
            tokens[i]='<a href="http://search.twitter.com/search?q='+goUser+'">#'+goUser+'</a>'+dp;
          }else if(tokens[i].indexOf('://')!=-1){
            tokens[i]='<a href="'+tokens[i]+'">'+tokens[i]+'</a>';
          }else{}
          newcontent+=tokens[i]+" ";
        }
        jq(el).html(pretokens[0]+'<br/>'+newcontent);
      });
      act=2;
      if(nitem.length<3)jq(val).next('.navi').hide();
    } else if (jq(val).hasClass('s-flashdown')) {
      act=3;
      if(nitem.length<4)jq(val).next('.navi').hide();
      direction=false;
    } else /*if (jq(val).hasClass('s-vacanze'))*/ {
      act=2;
      if(nitem.length<3)jq(val).next('.navi').hide();
    }
    jq(val).scrollable({
      vertical:direction,
      size:act,
      speed:200,
      prev:'.prev',
      next:'.next',
      disabledClass:'off'
    }).mousewheel(200).navigator('.balls');
  });
  jq('ul.tabs').tabs('.tab',{effect:'fade'});
  /*_hack*/ jq('.bv-sep').css('visibility','visible');
  jq('.next, .prev, .balls a, ul.tabs li').css('cursor','pointer').click(function(){return false;});
  jq('.box-vacanze-top ul.tabs li').css('cursor','pointer').click(function(){
  brand=jq(this).attr('id');
  var oldLink=jq('#vediOfferte').attr('href');
	var newLink=oldLink.substring(0,(oldLink.indexOf('pacchetti-vacanza/')+18))
  jq('#vediOfferte').attr('href',newLink+brand+'/');
 return false;});
  
  // print accordion
  jq('#print .scheda_txt .closeMyAcc').hide();
  // jq('.jeans-content2').hide();
  // jq('#print .scheda_txt h3.jeans2').click(function(){
    // if (jq(this).hasClass('hopen')){jq(this).removeClass('hopen').next('.jeans-content2').slideUp(400);}
    // else{jq(this).addClass('hopen').next('.jeans-content2').slideDown(400);}
    // return false;
  // });
  
  // internal accordion
  jq('.jeans-content').hide();
  jq('#print .scheda_txt h4.jeans').click(function(){
    if (jq(this).hasClass('hopen')){jq(this).removeClass('hopen').next('.jeans-content').slideUp(400);}
    else{jq(this).addClass('hopen').next('.jeans-content').slideDown(400);}
    return false;
  });
  
  
  // faq accordion
  jq('.faq-content').hide();
  jq('h2.faq').click(function(){
    if (jq(this).hasClass('hopen')){jq(this).removeClass('hopen').next('.faq-content').slideUp(400);}
    else{jq(this).addClass('hopen').next('.faq-content').slideDown(400);}
    return false;
  });
  
  
  // external accordion / ehtabs
  jq('.ehtabs').each(function(m,el){
    jq('.ehtab-content0,.ehtab-content1',el).hide();
    jq('.ehtab-voice0,.ehtab-voice1',el).click(function(event){
      var which=parseInt((jq(this).attr('class').split('ehtab-voice')[1]).substr(0,1));
      if (jq(this).hasClass('ehopen')){
        jq(this).removeClass('ehopen');
        jq('.ehtab-content'+which+'',el).slideUp(400);
      }else{
        jq('.ehtab-content0,.ehtab-content1',el).hide();
        jq('.ehtab-voice0,.ehtab-voice1',el).removeClass('ehopen');
        jq(this).addClass('ehopen');
        jq('.ehtab-content'+which+'',el).slideDown(400).children('iframe').attr('src',jq(this).attr('rel'));
      }
      event.preventDefault();
    });
  });
  
  
  // column sx accordion
  jq('.listPaneCont').show();
  var lps=jq('.listPaneCont');
  jq('.boxAlpi a.listPaneOpen').each(function(i,val){jq(val).click(function(){
    if (jq(val).hasClass('lpoopen')){jq(val).removeClass('lpoopen');jq(lps[i]).slideUp(400);}
    else{jq(val).addClass('lpoopen');jq(lps[i]).slideDown(400);}
    return false;
  })});
  
  
  // header accordion
  jq('.accordion h3').mouseover(function() {
    jq(this).parent().children('div').hide();
    jq(this).parent().children('h3').show();
    jq(this).hide();
    jq(this).next().toggle();
    return false;
  }).next().hide();
  jq('.accordion').children('h3:first-child').hide();
  if( jq('.accordionBox').parent().hasClass('boxAlpi') ) jq('.accordion').children('h3:first-child').next().toggle();
  jq('input[type=hidden]').each(function(i,val){
    if(jq(val).attr('class')==jq(val).attr('rel')) {jq(val).next().hide(); jq(val).next().next().toggle();}
  });
  
  
  // special button to close accordion from bottom
  jq('.closeMyAcc').click(function(){
    var ref = jq(this).parent();
    ref.slideUp(400);
    try{ref.prev().removeClass('lpoopen');}catch(e){};
    try{ref.prev().removeClass('iaopen');}catch(e){};
    try{ref.prev().removeClass('hopen');}catch(e){};
    return false;
  });
  
  
  // map overlay
  jq('a[title=MAPPA]').overlay({
    onBeforeLoad:function(){jq('#showcase-mappa iframe').attr('src',this.getTrigger().attr('href'));},
    target:'#showcase-mappa',expose:'#999',closeOnClick:false
  });
  
  
  // photogallery overlay
  jq('a.aPhotogallery').overlay({
    onBeforeLoad:function(){jq('#showcase-photogallery iframe').attr('src',this.getTrigger().attr('href'));},
    target:'#showcase-photogallery',expose:'#999',closeOnClick:false
  });
  
  
  // menu a tendina
  jq('.menu ul ul').css('visibility','hidden');
  jq('.menu li').hover(function(){
    jq('ul:first',this).css('visibility','visible');
	},function(){
		jq('ul:first',this).css('visibility','hidden');
	});
  
  
  // last minute destinazione
  try{var ips=jq('img.img-pacchetto').attr('src').toString().split('.jpg');
  var dest=jq('a.listDest').text().toString().split(' ');
  var h2t=jq('.boxAlpi2Large.borderLeft h2.titleBox span');
  var dests='';
  for (var i=0;i<dest.length;i++) {dests+=dest[i]; h2t.text(h2t.text()+' '+dest[i]);}
  //jq('img.img-pacchetto').attr('src',ips[0]+'_'+dests+'.jpg');
  jq('.pathAlpi').append(' &raquo; '+dests);
  jq('a.listDest').click(function(){return false;});
  }catch(e){};
  
  
  // callcenter
  if(!isHP) jq('#callcenter').css('top','40px');
  jq('#openCallCenter,.openCallCenter').click(function(){ jq('#callcenter').slideDown(); });
  jq('#closeCallCenter').click(function(){ jq('#callcenter').slideUp(); });
  
  
  // flash header | tooltip
  if(!isHP){
    jq('.alpitourPAGE').css('margin-top','40px');
    jq('a.stepAllInclusive').click(function() {thisMovie('headerfla').swfShowVideo(2);});
    jq('a.stepVantaggi').click(function() {thisMovie('headerfla').swfShowVideo(1);});
    
    jq('span.regrel').each(function(i,val){
      jq(val).easyTooltip({useElement:'reg'+i+''});
    });
  }else{
    jq('span.stepAllInclusive').easyTooltip({tooltipId:'alpitip',content:"Il prezzo che vedi a fianco e' quello 'tutto compreso' e include:<br/>VOLO A/R, <br/>TRASFERIMENTI IN LOCO DA E PER L'AEROPORTO, <br/>SOGGIORNO<br/>ASSISTENZA ALPITOURWORLD IN AEROPORTO IN ARRIVO E PARTENZA E IN DESTINAZIONE, <br/>ALPITOUR WORLD AMICO: UN CALL CENTER CHE TI ASSISTE PRIMA, DURANTE E DOPO LA VACANZA, <br/>QUOTA DI ISCRIZIONE <br/>ASSICURAZIONE TRAVEL BASIC"});
    jq('span.stepVantaggi').easyTooltip({tooltipId:'alpitip',content:'Prezzo tutto compreso'});
  }
  
  
  // idee-regalo tabs
  jq('.ir-tabs ul').tabs('.ir-tab',{effect:'fade',current:'ir-tab-active'});
  
  
  // gallery escursioni
  jq('.gelgs').tabs('img.gelm',{effect:'fade'}).click(function(){return false;});
  jq('img.gelm').css('visibility','visible');
  
  
  // searchforms
  //jq('.searchform').css('visibility','visible');
  //jq('.searchforms').tabs('.searchform',{effect:'ajax',history:true});
  
  
  // twitter button
  // jq('#follow-twitterapi iframe').contents().find('.twitter-follow-btn *:contains("@")').each(function(i,el){
    // var tokens=jq(el).html().split(' '),newcontent="";
    // for(var i=0;i<tokens.length;i++){
      // if(tokens[i].indexOf('#')!=-1)tokens[i]='';
      // newcontent+=tokens[i]+" ";
    // }
    // jq(el).html(newcontent);
  // });
});


// print SEO
function riscriviStampa(quale){window.open(quale+"&stampa=true",'stampa','width=640,height=600,location=0,menubar=0,personalbar=0,resizable=0,toolbar=0,status=0,scrollbars=yes');
return false;}


// photogallery support
function closePhotogallery(){jq('a.aPhotogallery').each(function(i,val){jq(this).overlay().close();});}


// insert flash
function thisMovie(movieName){
  if (navigator.appName.indexOf("Microsoft")!=-1){return window[movieName];}
  else{return document[movieName];}
}
function showVideo() {
  jq('#accordionHeader').animate({top:'0'});
  jq('.alpitourPAGE').animate({marginTop:'340px'});
  jq('#callcenter').css('top','340px');
}
function hideVideo(){
  jq('#accordionHeader').animate({top:'-300px'});
  jq('.alpitourPAGE').animate({marginTop:'40px'});
  jq('#callcenter').css('top','40px');
}
function insertFlash(swf_id,width,height,port,flashVars) {
  var pt=(port!='')?':'+port:'';
  if(AC_FL_RunContent==0){alert("Questa pagina richiede AC_RunActiveContent.js.");}
  else {
    AC_FL_RunContent(
			'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
			'width',''+width+'',
			'height',''+height+'',
			'src','http://'+document.domain+pt+'/vacanze/swf/'+swf_id+'',
			'id',''+swf_id+'',
			'name',''+swf_id+'',
			'movie','http://'+document.domain+pt+'/vacanze/swf/'+swf_id+'',
			'quality','high',
			'pluginspage','http://www.macromedia.com/go/getflashplayer',
			'align','middle',
			'play','true',
			'loop','true',
			'scale','showall',
			'wmode','transparent',
			'devicefont','false',
			'bgcolor','#ffffff',
			'menu','true',
			'allowFullScreen','true',
			'allowScriptAccess','sameDomain',
			'salign','',
      'FlashVars',flashVars
    ); //end AC code
  }
}// _hack

function slideDownSafe(quale){
	if(isIe7){
		var data = jq(quale).html();
		jq(quale).hide().html(data).slideDown().show(function(){jq(this).html(data).show();});
	}else{jq(quale).slideDown(600);}
}

function popUpCenter(pageURL,title,w,h) {
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 


/* Gestione dei Cookie */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var xmlhttp;

function loadDestinazioni(dest) {

	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		document.getElementById("dest_" + dest).innerHTML = '<select id="keyd_weekend" name="keyd_weekend" title="Scegli destinazione" class="tg11" style="width:250px;font-weight:bold;"><option value="">Attendere prego...</option></select>';
		if(ajaxRequest.readyState == 4){
			document.getElementById("dest_" + dest).innerHTML = ajaxRequest.responseText;
		}
	}

	if (dest != null) {
		var tema = document.getElementById("temi_" + dest).selectedIndex;

		if (tema != 0) {
			if (dest == "italia") {
				document.getElementById("err_tema_italia").style.display = "none";
			} else if (dest == "estero") { 
				document.getElementById("err_tema_estero").style.display = "none";
			}
		}
			
		var elrCar0 = 'CATA';
		var elrKar0 = '';
		if (dest == "italia") {
			elrKar0 = 'PIT';
		} else if (dest == "estero") {
			elrKar0 = 'EUR';
		}
		var elrCar1 = 'CARA';
		var elrKar1 = '';
		if (tema == 1) { 
			elrKar1 = 'PASSIONE';
		} else if (tema == 2)  {
			elrKar1 = 'CULTURA';		
		} else if (tema == 3)  {
			elrKar1 = 'BENESSERE';		
		} else if (tema == 4)  {
			elrKar1 = 'INSOLITO';		
		} else if (tema == 5)  {
			elrKar1 = 'ENOGASTRON';		
		}
	
	    var url="/vacanze/week-end/destinazioni.jsp";
	    url+="?elrCar0="+elrCar0;
	    url+="&elrKar0="+elrKar0;
	    url+="&elrCar1="+elrCar1;
	    url+="&elrKar1="+elrKar1;
	    url+="&dest="+dest;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null); 
	}
}

function initWeekend() {
	if (document.getElementById("frm_italia") != null) {
		document.getElementById("temi_italia").selectedIndex = 0;
		document["frm_italia"].keyd_weekend.selectedIndex = 0;
	} 
	if (document.getElementById("frm_estero") != null) {
		document.getElementById("temi_estero").selectedIndex = 0;
		document["frm_estero"].keyd_weekend.selectedIndex = 0;		
	}
}

function offerteWeekend(dest) {
	var indexTema = document.getElementById("temi_" + dest).selectedIndex;
	var indexDest = document["frm_"+dest].keyd_weekend.selectedIndex;
	if (indexTema == 0) {
		if (dest == "italia") { 
			document.getElementById("err_dest_italia").style.display = "none";
			$("#err_tema_italia").fadeIn("slow");
		} else if (dest == "estero") { 
			document.getElementById("err_dest_estero").style.display = "none";
			$("#err_tema_estero").fadeIn("slow");
		}
	} else if ((indexTema != 0) && (indexDest == 0)) {
		if (dest == "italia") { 
			document.getElementById("err_tema_italia").style.display = "none";
			$("#err_dest_italia").fadeIn("slow");
		} else if (dest == "estero") { 
			document.getElementById("err_tema_estero").style.display = "none";
			$("#err_dest_estero").fadeIn("slow");
		}		
	} else if ((indexTema != 0) && (indexDest != 0)) {
		var cata = "";
		if (dest == "italia") { 
			document.getElementById("err_dest_italia").style.display = "none";
			document.getElementById("err_tema_italia").style.display = "none";
			cata = "PIT";
		} else if (dest == "estero") { 
			document.getElementById("err_dest_estero").style.display = "none";
			document.getElementById("err_tema_estero").style.display = "none";
			cata = "EUR";
		}
		tema = document.getElementById("temi_" + dest).options[indexTema].value;
		if (tema == "Gusto") {
			tema = "Enogastron";
		} else if (tema == "Insolito ed esotico") {
			tema = "Insolito";
		}
		keydDest = document["frm_"+dest].keyd_weekend.options[indexDest].value;
		window.location = "/vacanze/viaggio/hotel.jsp?nCara=4&elrCar0=CARA&elrKar0="+tema.toUpperCase()+"&elrCar1=CATA&elrKar1="+cata+"&elrCar2=KEYD&elrKar2="+keydDest+"&elrCar3=CARA&elrKar3=SOLOSOGG&flusso=VHH&offerte=weekend";		
	}
}
