jQuery(document).ready(function() {

	var $ = jQuery;
	
	function field_blur(field) {

		if(field.val() == '') { field.val(field.attr('title')); }
	}
	
	function field_focus(field) {

		if(field.val() == field.attr('title')) { field.val(''); }
	}
	
	function form_instruct(form) {
		
		$(form+' input[type="text"]').each(function() {
	
			field_blur($(this));
			$(this).focus(function() { field_focus($(this)) });
			$(this).blur(function() { field_blur($(this)) });
		});
		
		$(form).submit(function() {
	
			$(this).find('input[type="text"]').each(function() {
	
				field_focus($(this));
			});
		});
	
		$(form+' input[type="reset"]').click(function() {
	
			$(this).siblings('input[type="text"]').each(function() {
					
				$(this).val('');
				field_blur($(this));
			});
			return false;
		});	
	}

	form_instruct('#burial-search-box form');
	form_instruct('#postcode-search-box form');
	

	$(".colorbox").colorbox({iframe:true, width:'90%', height:'90%', title:false });
	$(document).bind('cbox_open', function () {
		
		$('html').css({ overflow: 'hidden' });
	}).bind('cbox_closed', function () {
		
		$('html').css({ overflow: 'auto' });
	});

	$("a.colorboximg").colorbox({maxWidth:'90%', maxHeight:'90%'});

	$('.item-page div.columns').columnize({ columns: 2 });
	$('#footer ul.columns li').addClass('dontsplit');
	$('#footer ul.columns').columnize({ columns: 3, lastNeverTallest: true });

	// Add pdf icons to pdf links
	$("a[href$='.pdf']").addClass("pdf");
	// Add email icons to email links
	$("a[href^='mailto:']").addClass("email");
	//Add external link icon to external links - 
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname && this.hostname !== location.hostname;
	  }).addClass("external");
	  
		
  $("#slides").slides({
	container: 'bannergroup1',
	effect: 'fade',
	crossfade: true,
	fadeSpeed: 1750,
	fadeEasing: "easeOutQuint",
	generatePagination: false,
	slideEasing: "easeInOutQuint",
	pagination: true,
	generatePagination: true,
	paginationClass: 'slides-pagination',
	play: 5000
  });

    $('#header ul.menu li ul li:first-child').addClass('first');
	$('#header ul.menu li ul li:last-child').addClass('last');

  $().UItoTop({ easingType: 'easeInOutQuad' });

	$('div.pagination ul li > *').each(function() {
		
		$(this).addClass('btn').wrapInner('<span />');
	});

	$('div.maplink-pdf').click(function() {

		var SiteID = $(this).children('span.siteid').html();
		var loader = $(this).children('span.maplink-pdf-load');
		loader.show();
		$.ajax({

			url: 'index.php?option=com_burials&view=results&format=map',
			dataType: 'json',
			data: 'id='+SiteID,
			success: function(msg) {

				if(msg.url != '') {

					window.location = msg.url;
					loader.hide();
				}
				else {

					loader.hide();
				}
			}
		});
	});
	
	$('span.maplink-pdf-small').click(function() {

		var SiteID = $(this).children('span.siteid').html();
		var loader = $(this).siblings('span.maplink-pdf-load-small');
		$(this).hide();
		loader.css('display','inline-block');
		$.ajax({

			url: 'index.php?option=com_burials&view=results&format=map',
			dataType: 'json',
			data: 'id='+SiteID,
			success: function(msg) {

				if(msg.url != '') {

					window.location = msg.url;
					loader.hide();
				}
				else {

					loader.hide();
				}
			}
		});
	});



});



