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

$(function()
{
  $.extend($.expr[':'], {
    'containsi': function(elem, i, match, array)
    {
      return (elem.textContent || elem.innerText || '').toLowerCase()
      .indexOf((match[3] || "").toLowerCase()) >= 0;
    }
  });

  $('#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}, 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($('#body').hasClass('c-prodotti') || $('#body').hasClass('c-collezioni'))
  {
    $('#collezioni-accordion li').hover(function(){if(!$(this).hasClass('open')){$('#collezioni-accordion li.open').animate({width: '40px'}, { queue:false, duration:400 }).removeClass('open');$(this).animate({width: '368px'}, { queue:false, duration:400}).addClass('open');}});
  }
  else if($('#body').hasClass('c-prodotto'))
  {
    if($('#product-photos').size()>0)
    {
      $('#product-photos-big').cycle({ fx: 'fade', speed: 500}).cycle('pause');
      $('#product-photos-big li img').click(function(){$('#product-photos-big').cycle('next');});
      $('#product-photos-tn li img').each(function(i){$(this).click(function(){$('#product-photos-big').cycle(i);})});
    }

    $('#product-features img').qtip({
          position: {corner: {target: 'topMiddle',tooltip: 'bottomMiddle'}},
          style: {name: 'light', tip:{corner: 'bottomMiddle', size: {x: 12, y: 8}}, fontSize: '12px', padding: '5px', border: {width: 1}, background: '#F5F5F5', width: 200},
          show: {when: {event: 'mouseover'}},
          hide: {when: {event: 'mouseout'}}
    });


    $('#product-variants img').qtip({
          position: {corner: {target: 'topMiddle',tooltip: 'bottomMiddle'}},
          style: {name: 'light', tip:{corner: 'bottomMiddle', size: {x: 12, y: 8}}, fontSize: '12px', padding: '5px', border: {width: 1}, background: '#F5F5F5', width: 200},
          show: {when: {event: 'mouseover'}},
          hide: {when: {event: 'mouseout'}}
    });

    if($('#product-photos-tn li').size()>3)
    {
      $('#product-photos-tn-box').jScrollPane();
    }

    $('#menu-left-products-slider').jScrollPane();
  }
  else if($('#body').hasClass('c-gallery'))
  {
    $('#product-list a').fancybox({'overlayOpacity': 0.8, 'overlayColor': '#000000'});
  }
  else if($('#body').hasClass('c-prodotti_ricerca'))
  {
    $('#product-search-text').keyup(productFilter);
  }
  else if($('#body').hasClass('c-prodotti_alfabetica'))
  {
    $('#product-letter-index a').click(showProductLetter);
  }
  else if($('#body').hasClass('c-prodotti_tipologia'))
  {
    $('#product-type-index a').click(showProductType);
  }
  else if($('#body').hasClass('c-contatti') || $('#body').hasClass('c-contatti_assistenza'))
  {
    $('#form-contatti').submit(checkFormContatti);
  }
  else if($('#body').hasClass('c-news_dettaglio'))
  {
    if($('#news-photos').size()>0)
    {
      $('#news-photos').cycle({ fx: 'fade', speed: 500}).cycle('pause');
      $('#news-photos-prev').click(function(){$('#news-photos').cycle('prev');});
      $('#news-photos-next, #news-photos img').click(function(){$('#news-photos').cycle('next');});
    }
  }
  else if($('#body').hasClass('c-advertising') || $('#body').hasClass('c-comunicazione'))
  {
    if($('#adv-photos').size()>0)
    {
      $('#adv-photos').cycle({ fx: 'fade', speed: 500}).cycle('pause');
      $('#adv-photos-prev').click(function(){$('#adv-photos').cycle('prev');});
      $('#adv-photos-next, #adv-photos img').click(function(){$('#adv-photos').cycle('next');});
    }
  }
  else if($('#body').hasClass('c-prodotti_vedi_tutte'))
  {
    $('#products-view-all li').hover(productsViewAllMouseEnter,productsViewAllMouseExit);
    $('#products-view-all-close').click(function(){history.back();$('#products-view-all-box').hide();});
  }
  else if($('#body').hasClass('c-mappa_distribuzione') || $('#body').hasClass('c-distribuzione'))
  {
    geocoder=new google.maps.Geocoder();
    latlng=new google.maps.LatLng(41.442726135253906,12.392578125);

    var myOptions={
      zoom: 6,
      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');

    $('#location-search-text').keyup(locationFilter);

    showMarkers();
  }
});

function productFilter()
{
  $('#product-list li').addClass('hidden');

  if(this.value!='')
  {
    $('#product-list li:containsi("'+this.value+'")').removeClass('hidden');
  }
}

function showProductLetter()
{
  $('#product-alphabetic ul').addClass('hidden');
  $('#product-list-'+$(this).text()).removeClass('hidden');

  $('#product-letter-index a').removeClass('selected');
  $(this).addClass('selected');

  return false;
}

function showProductType()
{
  $('#product-type ul').addClass('hidden');
  $('#product-list-'+$(this).text()).removeClass('hidden');

  $('#product-type-index a').removeClass('selected');
  $(this).addClass('selected');

  return false;
}

function productsViewAllMouseEnter()
{
  $(this).css({'z-index' : '10'});
  $(this).addClass('hover').find('img').stop().css('border-color',$(this).attr('rel'))
    .animate({
      marginTop: '-154px',
      marginLeft: '-154px',
      top: '50%',
      left: '50%',
      width: '300px',
      height: '300px'
    }, 200);
}

function productsViewAllMouseExit()
{
  $(this).css({'z-index' : '0'});
  $(this).removeClass('hover').find('img').stop().css('border-color','#CCCCCC')
    .animate({
      marginTop: '0',
      marginLeft: '0',
      top: '0',
      left: '0',
      width: '150px',
      height: '150px'
    }, 200);
}

function locationFilter()
{
  if(this.value!='')
  {
    $('#map-location-list li').addClass('hidden');
    $('#map-location-list li:containsi("'+this.value+'")').removeClass('hidden');
  }
  else
  {
    $('#map-location-list li').removeClass('hidden');
  }

  clearMarkers();
  showMarkers();
}

function clearMarkers()
{
  if(markers)
  {
    for(var i=0;i<markers.length;i++)
      {markers[i].setMap(null);}
  }

  markers=[];
}

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(){
    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: $(this).find('.address').html()
    }));

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

  mapAutoCenter();
}

