/* Author: 

*/
jQuery(function($){
 function initialize() {
    var latlng = new google.maps.LatLng(51.499472,-0.113696);//51.4926406357673,-0.14077541993339
    var myOptions = {
      zoom: 14,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("google-map"),
        myOptions);

	var image = new google.maps.MarkerImage('img/map-icon.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(40, 57),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(20, 56));
      
	var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: image
    });
  }

  if($('body').attr('id') == 'contact') {
  	initialize();
  }
  
  if($('body').attr('id') != 'gallery') {
		//Hide all but first image
		$('#images img:not(":first")').hide();
		
		(function imageTransition(){
			setTimeout(function(){
				//Fade all images out
				$('#images img:visible').fadeOut();
				
				//Fade in images
				if($('#images img:visible').next('img').size() < 1)
				{
					$('#images img:first').fadeIn();
				}
				else
				{
					$('#images img:visible').next('img').fadeIn();
				}
				imageTransition();
			}, 5000);
		})();
	}

	//Checkbox
	$('input[type=checkbox]').checkbox();
	$('input[type=radio]').checkbox();

	//Form label values
	$('.form-field').each(function() {
		var fieldName = $(this).prev('label').text();
		$(this).val(fieldName);


		$(this).focus(function () {
			if ($(this).val() == fieldName) {
				$(this).val("");
			}
		}).blur(function () {
			if ($(this).val() == "") {
				$(this).val(fieldName);
			}
		});
	});

	$('.gallery a').click(function(e) {
		
		//Find source of large image
		var source = $(this).attr('href');

		//Add to source
		$('<img src="'+source+'" alt="The Horse Bar" style="display: none;" />').appendTo('.galleryImgDisplay');

		//Fade out first image and fade in new image
		$('.galleryImgDisplay img:first').fadeOut('slow');
		$('.galleryImgDisplay img:not(":first")').fadeIn('slow', function() {
			$('.galleryImgDisplay img:first').remove();
		});
		
		//galleryImgDisplay
		e.preventDefault();
	});

	//Twitter
	$('.twitter').cycle({
		fx: 'scrollDown', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout:  6000   // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});

	//Select fields
		// select element styling
		$('select.select').each(function(){
			var title = $(this).attr('title');
			if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
			$(this)
				.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
				.after('<span class="select">' + title + '</span>')
				.change(function(){
					val = $('option:selected',this).text();
					$(this).next().text(val);
					})
		});


	//Booking Form
	$('#booking-form').hide();

	var checked = false;

	$('.radio').click(function() {
		$('#booking-form').show();
		$('#booking-form-container').height(472);
		$('#bookingform').height(472);
	});
	
	
	if ($('#book-online p').children().hasClass('error') || $('#book-online p').hasClass('successText')){
		//show
		$('#booking-form').show();
		$('#booking-form-container').height(472);
		$('#bookingform').height(472);
	} else {
		//hide
		$('#booking-form').hide();
	}
	
	
  });
