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

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

    $('#children img').each(function(){if($(this).width()>($(this).parent().parent().width())){$(this).parent().parent().css('width',$(this).width()+'px');}})
  }

  if($('#detail').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');

    $('#detail img').each(function(){if($(this).width()!=($(this).parent().width()) && $(this).width()>0){$(this).parent().css('width',$(this).width()+'px');}})

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

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


  if($('#dBody').hasClass('home'))
  {
    if($('#flash').size()>0)
    {
      $('#flash').flash({
          src: flashUrl,
          width: 588,
          height: 480
      });
    }
  }
  else if($('#dBody').hasClass('choosing_right_size'))
  {
    $('ul li:nth-child(1) p, ul li:nth-child(2) p').css('height','170px');
  }
  else if($('#dBody').hasClass('links'))
  {
    var maxH=0;
    $('#product-range-full li').each(function(){if($(this).height()>maxH){maxH=$(this).height();}}).css('height',maxH+'px');
  }
  else if($('#dBody').hasClass('handles'))
  {
    $('table a').fancybox({'overlayOpacity': 0.8, 'overlayColor': '#000000'});
  }
  else if($('#dBody').hasClass('glass_door'))
  {
    $('#glass-door-slider li a').click(
      function()
      {
        $('#main-img img').attr('src',$(this).find('.hr-img').attr('src'));
        $('#glass-door-slider li a').removeClass('sel');
        $(this).addClass('sel');
        gdScrollOrigin=this;
        $.scrollTo('#main-img',10);
        return false;
      }
    );

    $('#main-img').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',$('#glass-door-slider li a.sel').find('.pattern-img').attr('src'));
        $('#jqzoom-box div.col-l a').attr('href',$('#glass-door-slider li a.sel').find('span.pattern-hr-img').text());
        $('#jqzoom-box div.col-l div.descr').text($('#glass-door-slider li a.sel').find('span.descr').text());
        $('#jqzoom-box div.col-l a, #jqzoom-box div.col-l a img').css({ 'width': $('#glass-door-slider li a.sel').find('.pattern-img').width()+'px', 'height': $('#glass-door-slider li a.sel').find('.pattern-img').height()+'px' });
        if(!$.browser.msie)
          {$('#jqzoom-box div.col-l a').jqzoom({ alwaysOn: true, zoomWidth: 480, zoomHeight: 480, yOffset: -50, xOffset: 20+(180-$('#glass-door-slider li a.sel').find('.pattern-img').width())/2, title: false });}
      }
    );

    $('#main-img').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 });}} });

    $('#back-to-patterns').click(function(){if(gdScrollOrigin){$.scrollTo(gdScrollOrigin,10);}});
  }
  else if($('#dBody').hasClass('distributors'))
  {
    geocoder=new google.maps.Geocoder();
    latlng=new google.maps.LatLng(51.5,0);

    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);
  }
});

var gdScrollOrigin;

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(51.5,0);
    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_co_uk/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();}
  });
}
