$(document).ready(function(){
  $('#quicksearch_form a.go').click(function(){
    $(this).closest('form').submit();
    return false;
  });

  $("a.submit").click(
    function(){
      $(this).closest('form').submit();
      return false;
    }
  );

  $("#postcode").focus(function(){
    if(this.value=='- postcode -')
    {
      this.value = '';
    }
  });

  $("#postcode").blur(function(){
    if(this.value=='')
    {
      this.value = '- postcode -';
    }
  });

  $('.showreviewform').click(function(){
		$('#reviewform').toggle();
		$.scrollTo('#reviewform');
    return false;
	});

  
  // need a youtube image if there isn't one
  
  if ($('.youtubeimg').length > 0) {
    
    $('.youtubeimg').each(function(){
      var strhref = $(this).closest('a').attr('href');
      
      ytVideoID = getYTid(strhref);
      
      imgUrl = getYTScreen(ytVideoID);
      
      $(this).append('<img src="'+imgUrl+'" width="472" height="310" alt="" />');
      
    });
    
  }
  
  
  $('.videostartlink').click(function(){

    var strhref = $(this).attr('href');
    
    ytVideoID = getYTid(strhref);
    
    var videoDivId = $(this).parents('.videocontainer').children('.videocontent:first').attr('id');
    
    //console.log('vidid' + ytVideoID);
    //console.log('videoDivId' + videoDivId);
    
    $(this).parents('.videocontainer').children('.videostartlink').hide();
    
    if(ytVideoID != ''){
      var atts = {};
      atts.id = videoDivId;
      atts.play = true;
      swfobject.embedSWF('http://www.youtube.com/v/'+ytVideoID+"&hl=en&fs=1&rel=0&color1=0x000000&color2=0x2d2d2d&enablejsapi=1&playerapiid=ytplayer",
          videoDivId, "472px", "310px", "8", null, null, atts);
    }
    
    return false;
  });
  
});


var RecaptchaOptions = {
    		theme : 'clean'
 		};

/**
 * For getting the YouTube video id from a url
 * @param strhref string, a youtube url
 * @returns string, the youtube video id
 */
function getYTid(strhref) 
{
  return strhref.match(/^[^v]+v.(.{11}).*/)[1];
}

/**
 * Returns the URL for a YouTube thumbnail from the url
 * @param url string
 * @param size string (optional 'small', defaults to 'big')
 * @returns {String}
 */
function getYTScreen(url, size) 
{
  if(url === null){ return ""; }

  size = (size === null) ? "big" : size;
  var vid;
  var results;

  results = url.match("[\\?&]v=([^&#]*)");

  vid = ( results === null ) ? url : results[1];

  if(size == "small"){
    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
  }else {
    return "http://img.youtube.com/vi/"+vid+"/0.jpg";
  }
}

