var sfwHomeIndex=0;

var map;
var markers = [];
var latlng;
var addressesLatLng = []
var infowindow;
var geocoder;

$(function()
{
  $('h1, h2, h3').each(function(){$(this).html($(this).html().replace('®', '<sup>&reg;</sup>'));});

  $('div.title h1').after('<div class="h1side"></div>');
  $('div.title div.h1side').css('height',$('div.title div.h1side').siblings('h1').outerHeight(true)+'px');

  $('#search-top').submit(function(){if($('#search-top input').val()!=''){return true;}else{return false;};})
  $('#btn-search').click(function(){$('#search-top').submit();return false;});

  $('#country').qtip({
        content: $('#countries'),
        position: {corner: {target: 'bottomMiddle',tooltip: 'topMiddle'}},
        style: {name: 'light', tip:{corner: 'topMiddle', size: {x: 12, y: 8}}, fontSize: '12px', padding: '0px', border: {width: 1}, width: 480, background: '#F5F5F5'},
        show: {when: {event: 'click'}, delay: 0},
        hide: {when: {event: 'unfocus'}}
  });

  $('div.flash').each(function(){
    var url=$(this).text();
    $(this).text('');
    if(url!='')
    {
      $(this).flash({
          src: url,
          width: $(this).width(),
          height: $(this).height()
      });
    }
  });

  if($('a.img-zoom').size()>0)
    {$('a.img-zoom').fancybox({'overlayOpacity': 0.8, 'overlayColor': '#000000'});}

  if($('#detail.c240, #detail.c340, #detail.c136, #detail.c175').size()>0)
  {
    var maxH=0;
    $('#detail h2').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
    var maxH=0;
    $('#detail div.text').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
    var maxH=0;
    $('#detail p.link').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');

    var maxH=0;
    $('#detail li').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
  }

  if($('#photo-slider-big').size()>0)
  {
    $('#photo-slider-big').cycle({ fx: 'fade', speed: 500, before: function(currSlideElement,nextSlideElement){$('#photo-slider-description div').text($(nextSlideElement).children('img').attr('alt'))} }).cycle('pause');
    $('#photo-slider-big li img').click(function(){$('#photo-slider-big').cycle('next');});
    $('#photo-slider-tn li img').each(function(i){$(this).click(function(){$('#photo-slider-big').cycle(i);})});
  }

  if($('#photo-slider-big-2').size()>0)
  {
    $('#photo-slider-big-2').cycle({ fx: 'fade', speed: 500, before: function(currSlideElement,nextSlideElement){$('#photo-slider-description-2 div').text($(nextSlideElement).children('img').attr('alt'))} }).cycle('pause');
    $('#photo-slider-big-2 li img').click(function(){$('#photo-slider-big-2').cycle('next');});
    $('#photo-slider-tn-2 li img').each(function(i){$(this).click(function(){$('#photo-slider-big-2').cycle(i);})});
  }


  if($('#dBody').hasClass('dove_siamo'))
  {
    $('#content h2').click(showAddress);
    showMap();
  }
  else if($('#dBody').hasClass('home'))
  {
    sfwHomeIndex=Math.floor(Math.random()*swfs.length);

    $('#flash-home').flash({
        src: swfs[sfwHomeIndex][0],
        width: $('#flash-home').width(),
        height: $('#flash-home').height()
    });

    setTimeout(switchHomeFlash,swfs[sfwHomeIndex][1]*1000);
  }
  else if($('#dBody').hasClass('porte_tutto_vetro'))
  {
    $('#glass-door-slider').cycle({ fx: 'scrollHorz', speed: 1000, next: '#slider-next', prev: '#slider-prev' }).cycle('pause');

    $('#glass-door-slider li a').click(
      function()
      {
        $('#jqzoom-box div.col-l').append('<a href=""><img src="" alt="" /></a><div class="descr"></div>');
        $('#jqzoom-box div.col-l a img').attr('src',$(this).find('img').attr('src'));
        $('#jqzoom-box div.col-l a').attr('href',$(this).find('span.hr-img').text());
        $('#jqzoom-box div.col-l div.descr').text($(this).find('span.descr').text());
        $('#jqzoom-box div.col-l a, #jqzoom-box div.col-l a img').css({ 'width': $(this).find('img').width()+'px', 'height': $(this).find('img').height()+'px' });
        if(!$.browser.msie)
          {$('#jqzoom-box div.col-l a').jqzoom({ alwaysOn: true, zoomWidth: 480, zoomHeight: 480, yOffset: -50, xOffset: 20+(180-$(this).find('img').width())/2, title: false });}
      }
    );

    $('#glass-door-slider li a').fancybox({overlayOpacity: 0.8, overlayColor: '#000000', onCleanup: function (){$('#jqzoom-box div.col-l').empty();$('.jqZoomWindow, .jqZoomPup, .jqZoomTitle, .preload').remove();}, onComplete: function(){if($.browser.msie){$('#jqzoom-box div.col-l a').jqzoom({ alwaysOn: true, zoomWidth: 480, zoomHeight: 480, yOffset: -50, xOffset: 20+(180-$('#jqzoom-box div.col-l a img').width())/2, title: false });}} });
  }
  else if($('#dBody').hasClass('download'))
  {
    $('h2').click(downloadCatClick);
  }
  else if($('#dBody').hasClass('contatti'))
  {
    $('#form-contatti').submit(checkFormContatti);
  }
  else if($('#dBody').hasClass('press_area_registrazione'))
  {
    $('#form-press-area-reg').submit(checkFormPressAreaReg);
  }
  else if($('#dBody').hasClass('supporto_tecnico'))
  {
    var maxH=0;
    $('#content ul h2').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
    var maxH=0;
    $('#content ul div.text').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');

    var maxH=0;
    $('#content ul li').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
  }
  else if($('#dBody').hasClass('video_elenco'))
  {
    $('ul.videos a.img').click(function(){var p=$(this).parent();$(this).siblings().css('width','300px');$(this).remove();p.prepend('<iframe id="video-box" width="450" height="300" frameborder="0" src="'+p.find('div.video-url').text()+'" allowfullscreen></iframe>');return false;});
  }
  else if($('#dBody').hasClass('controtelaio'))
  {
    $('div.video div.subtitle h2').after('<div class="h2side"></div>');
    $('div.video a.video-small').click(function(){$('div.video div.video-big').removeClass('sel');$('#vb-'+this.id.substr(3)).addClass('sel');return false;});
    $('div.video-big a.img').click(function(){$(this).empty().append('<iframe id="video-box" width="271" height="168" frameborder="0" src="'+$(this).siblings('div.video-url').text()+'" allowfullscreen></iframe>');return false;});
  }
  else if($('#dBody').hasClass('dettagli_costruttivi'))
  {
    if($('ul.approfondimenti.int span').size()>0)
    {
      var maxH=0;
      $('ul.approfondimenti.int span').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
    }

    if($('ul.approfondimenti.ext span').size()>0)
    {
      var maxH=0;
      $('ul.approfondimenti.ext span').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
    }
  }
  else if($('#dBody').hasClass('rete_vendita_stato') || $('#dBody').hasClass('rete_vendita'))
  {
    if($('#map-distribution').size()>0)
    {
      geocoder=new google.maps.Geocoder();
      latlng=new google.maps.LatLng(41.442726135253906,12.392578125);

      var myOptions={
        zoom: 1,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map=new google.maps.Map(document.getElementById('map-distribution'),myOptions);

      infowindow=new google.maps.InfoWindow({content: ''});

      var maxH=0;
      $('#map-location-list li').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');

      showMarkers();

      google.maps.event.addListener(map, 'zoom_changed', showAddresses);
      google.maps.event.addListener(map, 'bounds_changed', showAddresses);
      google.maps.event.addListener(map, 'center_changed', showAddresses);
    }
  }

  $('#addthis').append('<script type="text/javascript" async="async" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4d7f47ab7ef560a3"></script>');
});

function showAddress()
{
  if(!$(this).hasClass('sel'))
  {
    $('div.sel').slideUp('slow',function(){$(this).removeClass('sel');showMap();});
    $('h2.sel').removeClass('sel');
    $('#address-content-'+this.id.substr(14)).slideDown('slow').addClass('sel');
    $(this).addClass('sel');
  }
}

function showMap()
{
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(41.442726135253906,12.392578125);
  var myOptions = {
    zoom: 15,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map=new google.maps.Map(document.getElementById('map'),myOptions);

  geocoder.geocode( { 'address': $('div.address.sel div.google-address').text()}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    }
  });
}

function downloadCatClick()
{
  c=$('#dl-c-'+$(this).attr('id').substr(5));
  if(c.is(':hidden'))
    {c.slideDown(function(){var maxH=0;$('#content div.model').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');});}
  else
    {c.slideUp();}
}

function switchHomeFlash()
{
  sfwHomeIndex=(sfwHomeIndex+1)%swfs.length;

  $('#flash-home').empty().flash({
      src: swfs[sfwHomeIndex][0],
      width: $('#flash-home').width(),
      height: $('#flash-home').height()
  });

  setTimeout(switchHomeFlash,swfs[sfwHomeIndex][1]*1000);
}

function mapAutoCenter()
{
  if(markers.length>0)
  {
    var bounds=new google.maps.LatLngBounds();
    $.each(markers, function (index, marker) {bounds.extend(marker.position);});
    map.fitBounds(bounds);
    if(markers.length==1)
      {map.setZoom(10);}
  }
  else
  {
    latlng=new google.maps.LatLng(41.442726135253906,12.392578125);
    map.setCenter(latlng);
    map.setZoom(6);
  }
}

function showMarkers()
{
  $('#map-location-list li:not(.hidden)').each(function(){
    var im=parseInt($(this).find('.marker').text());
    if(!(im>=0))
      {im=0;}
    var img=new google.maps.MarkerImage('http://'+document.location.host+'/design/eclisse_it/images/markers/marker-'+im+'.png');

    markers.push(new google.maps.Marker({
        position: new google.maps.LatLng($(this).find('.latitude').text(),$(this).find('.longitude').text()),
        map: map,
        title: $(this).find('.name').text(),
        info: '<h2>'+$(this).find('.name').text()+'</h2>'+$(this).find('.address').html(),
        icon: img
    }));

    google.maps.event.addListener(markers[markers.length-1],'click',function(){infowindow.setContent(this.info);infowindow.open(map,this);});
  });

  mapAutoCenter();
}

function showAddresses()
{
  $('#map-location-list li').each(function(){
    var latlng =  new google.maps.LatLng($(this).find('.latitude').text(),$(this).find('.longitude').text());
    if( map.getBounds().contains(latlng))
      {$(this).show();}
    else
      {$(this).hide();}
  });
}

