$(document).ready(function(){
  
  // set up maps
  $('.coords').each(function(){
    var arLatLng = $(this).html().split(',');
    var myLatlng = new google.maps.LatLng($.trim(arLatLng[0]), $.trim(arLatLng[1]));
    var myOptions = {
      zoom: 14,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map($(this).get(0), myOptions);  
    var marker = new google.maps.Marker({  
      position: myLatlng,
      title: 'Waterfront Nursery',
      icon: '/assets/images/site/nursery.png',
      map: map  
    });
    var infowindow = new google.maps.InfoWindow({
      content:  '<div class="infowindow">'+
                '<p><strong>Waterfront Nursery</strong></p>'+
                '<p>Edinburgh’s Telford College<br/>'+
                '350 West Granton Road<br/>'+
                'Edinburgh<br/>'+
                'EH5 1QE</p>'+
                '</div>'
    });
    google.maps.event.addListener(marker,'click',function(){
      infowindow.open(map,marker)
    });
  });

  // embellish .more links
  $('.more a').append(' &raquo;');
  
  // embellish required form fields
  $('label:contains(" *")').parent('li').addClass('required');
  
  // enquiry form validation
  $('form.enquiry').listenForChange().submit(function(e){
    has_error = false;
    $(this).find('.required input, .required textarea').each(function(e){
      value = $.trim($(this).val());
      if (value == '') {
        has_error = true;
        $(this).parents('.required').addClass('error');
      }
      else if ($(this).attr('name').match(/email/) && !value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)) {
        has_error = true;
        $(this).parents('.required').addClass('error');
      }
      else  {
        $(this).parents('.required').removeClass('error');
      }
    })
    
    if (has_error) {
      $(this).find('p.information').html('We\'ve highlighted errors in the form below, please correct these to send the form.</p>').addClass('error');
    }
    
    return !has_error;
  });
  

  // apply nosub as required
  if ($('#subnavigation li').size() == 0) {
    $('#stage').addClass('nosub');
  }
  
  // apply rounded mask to #welcome .panel
  $('#welcome .panel').prepend('<div class="roundedmask"></div>');
  
  // replace img icons with backgrounds
  $('.listing img.icon').each(function(){
    src = $(this).attr('src');
    alt = $(this).attr('alt');
    $(this).replaceWith('<div class="icon" style="background:url(\''+
      src+
      '\') center no-repeat">'+
      alt+
      '</div>'
    );
  })

  // replace sidebar gallery imgs with backgrounds
  $('#context .images.panel img.image').load(function(){
    src = $(this).attr('src');
    alt = $(this).attr('alt');
    width = $(this).width();
    height = $(this).height();
    $(this).replaceWith('<div class="image" style="width:'+
      width+'px;height:'+
      height+'px;background:url(\''+
      src+
      '\') center no-repeat">'+
      alt+
      '</div>'
    );
  })

});
