var aacl = {
	surveyForm :
	{
		init : function() {
			dialogLinks(); // hijack links in the dialog
			disableEnterKey(); // disable enter key
			$(".modalLink","#modalBtns").click(function(event) {
				event.preventDefault(); 
				var $dialog = $('<div id="dialogBox"><p style="padding:10px">Loading ...</p></div>');
				$dialog.load($(this).attr('href'),{modal:true}).dialog({
					modal:true,
					minHeight:400,
					minwidth:600,
					width:600,
					buttons: {
						Next: function() {
							var bValid = true;
							var $qu = $('#surveyForm input[name="qu"]').val();
							if($('#surveyForm').serialize().indexOf($qu)) {
								bValid = false;
								$('#surveyForm div.error').remove();
								$('#surveyForm .question').after('<div class="error"><p>Please select an option below.</p></div>');
							}
							if($('#surveyForm').serialize().indexOf($qu+'b') == 0) {
								bValid = false;
								$('#surveyForm div.error').remove();
								$('#surveyForm .question').after('<div class="error"><p>Please select an option below.</p></div>');
							}
							if($('#surveyForm select').val() == '') {
								bValid = false;
								$('#surveyForm div.error').remove();
								$('#surveyForm .question').after('<div class="error"><p>Please select an option below.</p></div>');
							}
							if($('#surveyForm #other:visible input[type="text"]').val() == '') {
								bValid = false;
								$('#surveyForm div.error').remove();
								$('#surveyForm .question').after('<div class="error"><p>Please enter the other treatment.</p></div>');
							}
							if (bValid) {
								$dialog.load('/question.php',$('#surveyForm').serialize(), function() {
									$(this).effect('slide',{direction:"right"},300);
								});
							}
							else {
								$dialog.parent('div').effect('shake',{distance:10,times:2},100);
							}
						},
						'Submit Survey': function() {
							$dialog.load('/question.php',{modal:true,complete:true}, function() {
								$(this).effect('slide',{direction:"right"},300);
							});
						},
						Save: function() {
							// open save window
							$dialog.load('/survey/save/',{modal:true,save:true}, function() {
								$(this).effect('slide',{direction:"right"},300);
							});
						},
						Back: function() {
							$dialog.load('/question.php?go=back',{modal:true}, function() {
								$(this).effect('slide',{direction:"left"},300);
							});
						},
						Submit: function() {
							var bValid = true;
							if (bValid) {
								$dialog.load('/index.php',$('#saveForm').serializeArray(), function() {
									$(this).effect('slide',{direction:"right"},300);
								});
							}
						},
						'Return to survey': function() {
							$dialog.load('/question.php',{modal:true}, function() {
								$(this).effect('slide',{direction:"left"},300);
							});
						}
					},
					open: function() {
						// init buttons
						$('.ui-dialog-buttonpane button:contains(Submit)').hide();
						$('.ui-dialog-buttonpane button:contains(Save)').hide();
						$('.ui-dialog-buttonpane button:contains(Next)').hide();
						$('.ui-dialog-buttonpane button:contains(Back)').css({'float':'left','margin-left':'10px'}).hide();
						$('.ui-dialog-buttonpane button:contains("Return to survey")').css({'float':'left','margin-left':'10px'}).hide();
						$('.ui-dialog-buttonpane button:contains("Submit Survey")').hide();
					},
					close: function() {
						// unset modal flag in php
						$.post("/question.php",{modal:false});
						// remove content
						$dialog.empty();
						$dialog.dialog("destroy");
						$dialog.remove();
					}
				});
			});
		}
	},
	researchList :
	{
		init : function() {
			// collapsing dd's
			var $dd = $('dl.collapse dd','#researchList');
			var $dt = $('dl.collapse dt','#researchList');
			$dd.hide();
			$('#expandDD').toggle(function() {
				$dt.addClass('open');
				$dd.show();
				$(this).addClass('collapse').text('Collapse all');
			}, function() {
				$dt.removeClass('open');
				$dd.hide();
				$(this).removeClass('collapse').text('Expand all');
			});
			$dt.click(function() {
				$(this).hasClass('open') ? $(this).removeClass('open').next('dd').hide() : $(this).addClass('open').next('dd').show();
			});
		}
	}
}

$(document).ready(function() {
	// textfield and textarea placeholders
	$.placeholder.backwardsCompatibility();
	
	// scrolling for anchors
	$('a[href^=#][href!=#]','#content').live('click',function(e){
		$('html,body').animate({'scrollTop': $($(this).attr('href')).offset().top+'px'}, 1500, 'easeOutQuad'); 
		e.preventDefault();
	});
	
	// add feedback form
	$('#feedback').contactable({
		name: 'Name',
		email: 'Email',
		message : 'Message',
		subject : 'Feedback',
		recievedMsg : 'Thankyou for your feedback',
		notRecievedMsg : 'Sorry, your message could not be sent, try again later',
		disclaimer: 'Please feel free to get in touch, we value your feedback',
		hideOnSubmit: true
 	});
});

(function($){
	// multiple select checkboxes
	multipleCheckbox = function() {
		$('ul.multipleCheckbox li','#surveyForm').addClass('closed').children('ul').hide();
		$('ul.multipleCheckbox li.collapse span','#surveyForm').toggle(function() {
			$(this).parent('li').removeClass('closed');
			$(this).next('ul').show();
		}, function() {
			$(this).parent('li').addClass('closed');
			$(this).next('ul').hide();
		});
	}
	// handle the 'other' field
	optionalField = function() {	
		var $last = $('#surveyForm ul.radioList li:last input');
		var $any = $('#surveyForm ul.radioList input');
		$last.attr('checked') ? $('#other').css('display','block') : $('#other').css('display','none');
		$any.change(function() {
			$last.attr('checked') ? $('#other').css('display','block') : $('#other').css('display','none');
		});
	}
	// open survey links in modal screen
	dialogLinks = function() {
		$('a','#dialogBox').live('click', function(event) {
			event.preventDefault();
			var slideDirection = "right";
			if($(this).attr('rel').length) slideDirection = $(this).attr('rel');
			$('#dialogBox').load($(this).attr('href'), {modal:true}, function() {
				$(this).effect('slide',{direction:slideDirection},300);
			});
		});
	}
	disableEnterKey = function() {
		$("#dialogBox form").live('keypress', function(event) {
			if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
				event.preventDefault();
			}
		});
	}
})(jQuery);
