$(document).ready(function() {
	$('#ad').cycle({
		fx: 'fade', 
		speed:    2500, 
		timeout:  6000
	});
	$("a.zoom2").fancybox({
		'zoomSpeedIn'		:	500,
		'zoomSpeedOut'		:	500,
		'overlayColor'		:	'#000'
	});
	$('#dropdown').hide();
	$('#showdropdown').click(function () {
		if ($('#dropdown').is(':hidden')) {
			$('#dropdown').slideDown('slow');
		} else {
			$('#dropdown').slideUp('fast');
		}
	});
	$('#customquestion').click(function() {
		if ($('#addquestion').val() != '')
		{
			var question_id = $('#customvalue').val();
			var content = $('#addquestion').val();
			var new_content = '<li id="delquestionc' + question_id + '"><div class="delete"><a href="#" class="deselectit" id="deselectc' + question_id + '">X</a></div><span id="questioncontentc' + question_id + '"><a href="#">' + content + '<input type="hidden" name="custom[]" value="' + content + '" /></a></span></li>';
			$('#selectedquestions').append(new_content);
			$('#addquestion').val('')
			$('#customvalue').val(parseInt(question_id) + 1)
		}
		else
		{
			alert('You did not enter any question to add!');
		}
		return false;
	});
	$('.listselection .selectit').each(function() {this.checked = false;});
	$('.listselection .tickall').click(function() {
		var allquestions = $('#allquestions tr').size();
		var selectedquestions = $('#selectedquestions li').size();
//		if (selectedquestions + allquestions > 20) {
//			alert('You can only chose a maximum of 20 questions.');
//			return false;
//		}
		$('.listselection tr').each(function() {
			var question_id = $(this).attr('id').substring(11);
			$('#select' + question_id).each(function() {this.checked = false;});
			var content = $('#questioncontent' + question_id).html();
			var new_content = '<li id="delquestion' + question_id + '"><div class="delete"><a href="#" class="deselectit" id="deselect' + question_id + '">X</a></div><span id="questioncontent' + question_id + '"><a href="#">' + content + '</a></span></li>';
			$('#selectedquestions').append(new_content);
			$(this).remove();
		});
	});
	$('.listselection .selectit').live('click', function() {
		var questions = $('#selectedquestions li').size();
//		if (questions + 1 > 20) {
//			alert('You can only chose a maximum of 20 questions.');
//			return false;
//		}
		var questionid = $(this).val();
		$('#addquestion' + questionid).toggleClass('selected');
		$('.listselection .selected').each(function() {
			var question_id = $(this).attr('id').substring(11);
			$('#select' + question_id).each(function() {this.checked = false;});
			var content = $('#questioncontent' + question_id).html();
			var new_content = '<li id="delquestion' + question_id + '"><div class="delete"><a href="#" class="deselectit" id="deselect' + question_id + '">X</a></div><span id="questioncontent' + question_id + '"><a href="#">' + content + '</a></span></li>';
			$('#selectedquestions').append(new_content);
			$(this).remove();
		});
		return false;
	});
	$('.listselected .deselectit').live('click', function() {
		var questionid = $(this).attr('id').substring(8);
		$('#delquestion' + questionid).toggleClass('selected');
		$('.listselected .selected').each(function() {
			var question_id = $(this).attr('id').substring(11);
			var content = $('#questioncontent' + question_id + ' a').html();
			var new_content = '<tr id="addquestion' + questionid + '"><td><input type="checkbox" class="selectit" name="select' + questionid + '" id="select' + questionid + '" value="' + questionid + '" /></td><td><a href="#"><span id="questioncontent' + question_id + '">' + content + '</span></a></td></tr>';
			$('#allquestions').append(new_content);
			$(this).remove();
		});
		return false;
	});
	$('#saveforlater').click(function() {
		var questions = '';
		$('.listselected li').each(function() {
			var question_id = $(this).attr('id').substring(8);
			if (questions == '') 
				questions += question_id;
			else
				questions += ',' + question_id;
		});
		$('#questions').value(questions);
	});
	$('#preparethelist').click(function() {
		var questions = '';
		$('.listselected li').each(function() {
			var question_id = $(this).attr('id').substring(11);
			if (questions == '') 
				questions += question_id;
			else
				questions += ',' + question_id;
		});
		$('#questions').val(questions);
	});
	$('#pdfpreview').click(function () {
		$.ajax({
			type: 'POST',
			url: '/includes/preview.php',
			data: $('#questions_to_answer').serialize(),
			success: function() {
				var preview = window.open('/preview.htm');
				if (!preview)
				{
					alert('Your browser has blocked the PDF Preview popup. You may need to allow popups for this site to view the Preview.');
				}
				else
				{
					alert('Dependant on the number of questions and the number of images uploaded, it can take a few moments for the preview to load.');
				}
			}
		});
		return false;
	});
});

