$(document).ready(function() {

	// Datepickers
	if(typeof $.fn.datePicker == 'function')
		$('input.date').datePicker({clickInput:true}).bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				// Set any blank dates to same date
				$('input.date[value=]').val($(this).val());
			}
		);



	// Timepickers
	if(typeof $.fn.timepicker == 'function')
		$('input.time').timepicker();

	// Add 'link to page' link
	$('#linkhtml').text('<p><a target="_blank" href="' + location.href + '"><img title="Find us on MyKidsCan - the parents directory" src="http://www.mykidscan.co.uk/resources/my-kids-can-button.jpg" style="border: 0" /></a></p>');
	$('#linkhtml').hide();
	$('#link a').click(function() {
		$('#linkhtml').slideToggle(function() {
    	$('#linkhtml').select();
		});

		return false;
	})


	// Make postcode search form clear on focus
	$('input[name=postcode]').focus(function() {
		if($(this).val() == 'postcode')
			$(this).val('');
	})

	// Charcounter for textareas
	if($.fn.charCounter) {
	  $('#listing-description').charCounter(500, {container : '<p style="margin-left: 150px;"></p>'});
	  $('#listing-additional_information').charCounter(255, {container : '<p style="margin-left: 150px;"></p>'});
  }



	// Gallery
	if(typeof($.fn.lightGallery) == 'function') {
		$('ul.lightGallery').lightGallery({loadingIcon	:	'assets/ajax-loader-circle.gif'});
  }
  
  // Make logo clickable aswell - only if wrapped in a link to the larger image
  $('a img.logo').each(function() {
		var a  = $(this).parent();
		var ul = $('<ul class="lightGallery" style="margin: 0 10px 5px 0;"><li style="margin: 0; padding: 0"></li></ul>');
		$(a).after(ul);
		$(ul).children('li').append(a);
		$(ul).lightGallery({loadingIcon	:	'assets/ajax-loader-circle.gif'});
	})

	// Auto-submit select boxes
	$('select.autosubmit').change(function() {
		$(this).parents('form').trigger('submit');
	})


	// Remove blank gallery images
	$('ul.lightGallery').find('img').each(function() {
		
		if($(this).attr('src').match('//') != null)
			$(this).parents('li').remove();

		//if($(this).attr('src').match(/[0-9]+\/[0-9]+(\/[0-9]+)?/) === null)
		//	$(this).parents('li').remove();
	})


	// Remove blank logos
	$('img.logo').each(function() {
		if($(this).attr('src').match('/0/0/50') !== null)
			$(this).remove();
	})

	// Expandable listings
	$('div.listing').not('.fullpage').each(function() {
		var listing_div = this;


		var descr   = $(this).find('div.description');

		// Hide the description
		$(descr).hide();

		// Make a shorter, abbreviated summary
		var summary = $('<p class="description"></p>');
		$(descr).after(summary);

		// Character count
		$(summary).text($(descr).find('p').text().substr(0, 60).replace(/ [^ ]+$/, '') + '...');




		$(this).find('h3').attr('title',	'Click for full details');

		// Have the more button toggle the visibility of the full description
		$(this).find('img.more,h3').click(function() {
			
			var heading  = $(this).parents('div.listing').find('h3');

			var img_more = $(this).parents('div.listing').find('img.more');
			$(summary).hide();


      $(listing_div).addClass('expanded');


			// Toggle visibility of description
			$(descr).slideToggle('normal', function() {

				// Show summary
				if(!$(descr).is(':visible')) {
					$(summary).show();
					$(listing_div).removeClass('expanded');
				}

				// Show more/less icon as appropriate
				$(img_more).attr('src', $(descr).is(':visible') ? 'assets/less.gif' : 'assets/more.gif');

 				// Scroll the window
				$().scrollTo($(heading).position().top - 20, {duration: 'normal'});

			})
		})

	})


		// Ensure listing link starts with http://
		$('a.website, div.event p.contact a').each(function() {
			if($(this).attr('href').match(/^http:\/\//i) == null)
				$(this).attr('href', 'http://' + $(this).attr('href'));
		});


})