$(document).ready(function(){
	$(function() {
		$("#products_nav").accordion({ header: '.n_set h3', active: false, collapsible: true, autoHeight: false, animated: false});
	});
	
	//Init search autocomplete
	search.init();
	
	//On home
	if($("#slider").length > 0){
		$("#slider").easySlider({
			auto: true, 
			continuous: true,
			speed: 1000,
			pause: 3000,
			numeric: true
		});
	}
	
	//Colourbox on signup
	$(".popup").colorbox({
		html : "<div><p id='pleasewait'>Please wait</p><div id='results'></div></div>",
		innerWidth : "300",
		height : 145,
		onComplete:function(){
			var postcode = "";
			if($('#postcode').val() == ""){
				postcode = prompt("Please enter a postcode");
			}else{
				postcode = $('#postcode').val();
			}
			
			$.ajax({
				//url: "/json.php?postcode="+postcode,
				url : "/cgi-bin/paf_lookup.cgi?"+postcode,
				type: 'get',
				dataType: 'json',
				success : function(t) {
					if(t.length > 0){
						var html = "<select name='addr' id='addr'>";
						var name = "";
						for(x=0;x<t.length;x++){
							if(t[x].comp){
								name = t[x].comp;
							}else if(t[x].flat){
								name = t[x].flat;
							}else if(t[x].bnam){
								name = t[x].bnam+", ";
							}else if(t[x].hnum){
								name = t[x].hnum;
							}else{
								name = "";
							}
							name = name+" "+t[x].road+", "+t[x].loca;
							var sel_width = (name.length * 10) + 90;
							
							html += "<option value=\""+x+"\">"+name+"</option>";
						}
						html += "</select><br /><input id=\"selectaddress\" type=\"button\" value=\"Select Address\">";
						$('#results').html(html);
						$('#pleasewait').html("Please select your address");
						$('#selectaddress').bind("click",function(){
							var selectedAddr = t[$('#addr').val()];
							$('#hnum').val(selectedAddr.hnum);
							if(selectedAddr.comp){
								$('#comp').val(selectedAddr.comp);
							}
							$('#hnam').val(selectedAddr.bnam);
							$('#fnum').val(selectedAddr.flat);
							$('#road').val(selectedAddr.road);
							$('#loc').val(selectedAddr.loca);
							$('#twn').val(selectedAddr.town);
							$('#cty').val(selectedAddr.cnty);
							$('#postcode').val(selectedAddr.pcod);
							$.colorbox.close();
						});
						$.colorbox.resize({innerWidth: sel_width});
						return true;
					}else{
						$('#pleasewait').html("Unable to find address information from your postcode");
						return false;
					}
				},
				error : function () {
					return false;
				},
				timeout : function () {
					return false;
				},
				async : false
			});
		}
	});
});
