$(function() {
	var default_postcode = 'Enter postcode';
	$('#location-search-form').submit(function() {
		var postcode = $('#postcode').val();
		if (!postcode || postcode == default_postcode) {
			alert("Please enter a postcode");
			return false;
		}		
	});
	$('#postcode').blur(function() {
		if ($(this).val() == '') {
			$(this).val(default_postcode);
		}
	});
	$('#postcode').focus(function() {
		if ($(this).val() == default_postcode) {
			$(this).val('');
		}
	});
	
	$('a.new-search').click(function() {
		$(this).fadeOut('fast');
		$('form#location-search-form').slideDown();
		return false;
	});
	
	$('a.pop').click(function() {
		var href = $(this).attr('href');
		window.open(href,'popup','width=500,height=500,scrollbars=yes');
		return false;
	});
	
});

function togglePostcode(t,postcode) {
	$('#postcode').val(postcode);
	$('.postcode_toggle').css('color','rgb(128, 128, 128)');
	t.css('color','#000');
	return false;
}

$(document).ready(function(){
  $('ul.accordion').accordion({ 
      autoheight: false,
      header: ".opener",
      active: '.selected',
      selectedClass: 'active',
      alwaysOpen: false,
      event: "click"
  });
});

/* Manage the "Helpful/Not helpful" buttons:*/

$(function() { 
	$('div.vote a.opinion').click(function() {

		var href = $(this).attr('href');
		var parameters = href.substr(1);
		var p = parameters.split('_');
		
		var inputs = new Object;
		inputs['review'] = p[0];
		inputs['helpful'] = p[1];
		
		target = $(this).parents('div.vote');
		
		$.ajax({
			data: inputs,
			url: "/ajax/helpful.php",
			type: "POST",
			timeout: 2000,
			error: function() {
				console.log("Failed to submit, or the response could not be read");
			},
			success: function(xml) { 
				var status = $("status",xml).text();	
				var message = $("message",xml).text();								
				var html = $("html",xml).text();		
				if (status != 0) {
					target.html('<strong>Thank you for your feedback</strong>');
				}		
				else {
					console.log("Zero status: " + message);
				}
			}
		});		
		return false;
	});
});

/* Allow search results to be nested: */

$(function() {
	$('ul.results li').not('.has_children').click(function() {
		// Also needs to follow 'onclick="document.location=\'/locations/location.php?o='.$outlet['ID'].'\'; return false"'
		// Where /locations/location.php?o='.$outlet['ID'] is also the href attr of the child 'a' element
		var location = ($(this).children('a').attr('href'));
		document.location = location;
		return false;
	});
	
	$('ul.results li.has_children').click(function() {
		// This li has children -- loop through each sibling [technically, they're not children in the markup], and show any immediate children that are hidden
		loop = 1;
//		$(this).siblings('li').each(function (i) {
//		id = $(this).attr('id');
//		alert ('#'+id+" ~ li");
//		$('#'+id+" ~ li").each(function (i) {
		$(this).nextAll('li').each(function (i) {
			// I don't think 'this' is a jquery object...
			// Aha: can we use $("#" + this)? 
			// No -- BUT look into using .each instead here...
			var classname = this.className;
			if (classname.match('nested') != null) {
				this.style.display = 'block';	
				// alert (classname.match('nested'));
			}
			else {
				// alert("none");
				return false;
			}
    });
		return false;
	});
	
	$('ul.results li a').click(function() {
		// Just cancel the original a tag; we leave it in place as it holds location, above
		// We should really trigger the parent li click event here, but we can tidy this up later
		return false;
	});
	
});

// Centre various panels

$(document).ready(function() {
	$(".centred").centerInClient();
	setTimeout(function() {
		$(".centred").centerInClient();
   }, 100);
});
$(window).scroll(function() {
	$(".centred").centerInClient();
});
$(window).resize(function () {
	$(".centred").centerInClient();
});
