
$(function(){
	
	$("#skip-select li").each(function(i){
		$(this).click(function(){
			$("#skip-select li").each(function(){$(this).removeClass('select');});
			$("select#PostcodeSkip").val(i+1);
			$(this).addClass('select');
		});
		$(this).hover(
			function(){$(this).addClass('hover')},
			function(){$(this).removeClass('hover')}
		);
		$(this).css('cursor','pointer');
	});
	
	$("select#PostcodeSkip").change(function(){
		$("select#PostcodeSkip option:selected").each(function(){
			var skip_id = $(this).val();
			if(skip_id == '') $("#skip-select li").each(function(){$(this).removeClass('select');});
			$("#skip-select li.skip-"+skip_id).trigger('click');
		});
	});
	
	
	$("#PostcodeDateYear").after('<a href="#" id="date-pick">Pick date</a>');
	
	$("#permit_link").click(function(){
		$("#permit a").trigger('click');
		$("#permit").css('color','#C00');
		$("#permit a").click(function(){ $("#permit").css('color','#003979'); });
	});
	

	
	$('#date-pick').datePicker({
				createButton:false,
				startDate:'01/01/2005',
				endDate:'31/12/2010'
			}).bind('click', function(){
				updateSelects($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind('dateSelected', function(e, selectedDate, $td, state){
				updateSelects(selectedDate);
			}
		).bind('dpClosed', function(e, selected){
				updateSelects(selected[0]);
			}
		);
		
	var updateSelects = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		$('#PostcodeDateDay option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
		$('#PostcodeDateMonth option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
		$('#PostcodeDateYear option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
	}
	// listen for when the selects are changed and update the picker
	$('#PostcodeDateDay, #PostcodeDateMonth, #PostcodeDateYear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#PostcodeDateYear').val(),
							$('#PostcodeDateMonth').val()-1,
							$('#PostcodeDateDay').val()
						);
				$('#date-pick').dpSetSelected(d.asString());
			}
		);
	
	// default the position of the selects to today
	var today = new Date();
	updateSelects(today.getTime());
	
	// and update the datePicker to reflect it...
	$('#PostcodeDateDay').trigger('change');
	
	$('#faqs .faq').expandable({length:60});
	
	$("a[class^='diagram']").slimbox({
		loop: true,
		overlayOpacity: 0.6,
		resizeDuration: 200,
		overlayFadeDuration: 200,
		captionAnimationDuration: 200
		}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
	
});


jQuery.fn.expandable=function(options){
	options=$.extend({more:'more »',less:'« hide',length:20,greedy:true},options||{});return this.each(function(){var $this=$(this),text=$this.text(),truncated=text.substr(0,options.length),after=text.substr(options.length);if(!text){return;}
truncated=(options.greedy)?truncated+(after.match(/.*?[\s]/)||[''])[0]:truncated.match(/.*[\s]/)[0];truncated=truncated.substr(0,truncated.length-1);$this.data('expandable',{full:text,truncated:truncated});var $less=$('<a/>').text(options.less).attr('href','#less').addClass('expand').click(function(){$(this).appendTo('body').hide();$this.text($this.data('expandable').truncated+' ').append($more.show());return false;});var $more=$('<a/>').text(options.more).attr('href','#more').addClass('expand').click(function(){$(this).appendTo('body').hide();$this.text($this.data('expandable').full+' ').append($less.show());return false;});$less.click();});};
