function changeVideo(sid,vid) {
	$('#videolinks li a').removeClass('sel');
	$('#vlink'+vid).addClass('sel');
	$.ajax({
		url: "/inc/ajax/subject_video.php",
		global: false,
		type: "GET",
		dataType: "html",
		data: "sid=" + sid + "&vid=" + vid,
		success: function(msg){
			$('#mainvideo').html(msg);
		}
	});	
}

function totTicked(group) {
	var total = 0;
	$('#' + group + ' input:checked').each(function(){ 
		total ++;
	});
	return total;
}

function totremaining(group,total,target) {
	var remaining = target - total;
	var remaintext = remaining + ' Choice';
	if (remaining != 1) remaintext += 's';
	remaintext += ' remaining';

	if (remaining >= 0) {
		$('#' + group + '_remain').html(remaintext);
	}
	return remaining;
}

function selectOption(group,option) {	
	if (group == "special") {
		remain = totremaining(group,totTicked(group),5);
		if (option == "ict" || option == "business" || option == "art") {
			if ($('#' + group + '_' + option + ' input').is(':checked')) {
				$('#supple_' + option).addClass('locked');
				$('#supple_' + option + ' input[type="checkbox"]').attr('checked', false).attr('disabled', true);
			} else {
				$('#supple_' + option).removeClass('locked');
				$('#supple_' + option + ' input[type="checkbox"]').attr('disabled', false);
			}
		}	
		if (remain < 0) {
			$('#' + group + '_' + option + ' input[type="checkbox"]').attr('checked', false).attr('disabled', false);
			$('#supple_' + option).removeClass('locked');
			$('#supple_' + option + ' input[type="checkbox"]').attr('disabled', false);
			alert('Please select a maximum of 5 choices');
		}
	} else if (group == "lang") {
		remain = totremaining(group,totTicked(group),5);
		if (option == "french") {
			if ($('#' + group + '_' + option + ' input').is(':checked')) {
				$('#supple_' + option).addClass('locked');
				$('#supple_' + option + ' input[type="checkbox"]').attr('checked', false).attr('disabled', true);
			} else {
				$('#' + group + '_' + option + ' input').attr('checked', false);
				$('#supple_' + option).removeClass('locked');
				$('#supple_' + option + ' input[type="checkbox"]').attr('disabled', false);
			}
		} else if (option == "btecsport") {
			if ($('#' + group + '_' + option + ' input').is(':checked')) {
				$('#supple_pe').addClass('locked');
				$('#supple_pe input').attr('checked[type="checkbox"]', false).attr('disabled', true);
			} else {
				$('#' + group + '_' + option + ' input').attr('checked', false);
				$('#supple_pe').removeClass('locked');
				$('#supple_pe input[type="checkbox"]').attr('disabled', false);
			}
		}
		if (remain < 0) {
			$('#' + group + '_' + option + ' input[type="checkbox"]').attr('checked', false).attr('disabled', false);
			$('#supple_pe').removeClass('locked');
			$('#supple_pe input[type="checkbox"]').attr('disabled', false);
			alert('Please select a maximum of 5 choices');
		}
	} else if (group == "supple") {
		remain = totremaining(group,totTicked(group),6);
		if (option == "ict" || option == "business" || option == "art" || option == "french") {
			if ($('#' + group + '_' + option + ' input').is(':checked')) {
				$('#special_' + option).addClass('locked');
				$('#special_' + option + ' input[type="checkbox"]').attr('checked', false).attr('disabled', true);
			} else {
				$('#' + group + '_' + option + ' input').attr('checked', false);
				$('#special_' + option).removeClass('locked');
				$('#special_' + option + ' input[type="checkbox"]').attr('disabled', false);
			}
		} else if (option == "pe") {
			if ($('#' + group + '_' + option + ' input').is(':checked')) {
				$('#lang_btecsport').addClass('locked');
				$('#lang_btecsport input').attr('checked[type="checkbox"]', false).attr('disabled', true);
			} else {
				$('#' + group + '_' + option + ' input').attr('checked', false);
				$('#lang_btecsport').removeClass('locked');
				$('#lang_btecsport input[type="checkbox"]').attr('disabled', false);
			}
		}
		if (remain < 0) {
			$('#' + group + '_' + option + ' input').attr('checked', false);
			$('#special_' + option).removeClass('locked');
			$('#special_' + option + ' input[type="checkbox"]').attr('disabled', false);
			alert('Please select a maximum of 6 choices');
		}
	}	
	if (remain >= 0) {
		if ($('#' + group + '_' + option + ' input').is(':checked')) {
			$('#' + group + '_' + option + '').addClass('selected');
		} else {
			$('#' + group + '_' + option + '').removeClass('selected');
		}
	}
}



$(document).ready(function() {
	$('#choose').submit(function() {
		var err = false;
		var msg = "";
		
		remain = totremaining('special',totTicked('special'),5);
		if (remain > 0) {
			err = true;
			msg += "Specialism Options has " + remain + " remaing choice";
			if (remain > 1) msg += "s";
			msg += "\n";
		}
		
		remain = totremaining('lang',totTicked('lang'),5);
		if (remain > 0) {
			err = true;
			msg += "Languages has " + remain + " remaing choice";
			if (remain > 1) msg += "s";
			msg += "\n";
		}
		
		remain = totremaining('supple',totTicked('supple'),6);
		if (remain > 0) {
			err = true;
			msg += "Supplementary Options has " + remain + " remaing choice";
			if (remain > 1) msg += "s";
			msg += "\n";
		}
		
		if (err) {
			alert("Error:\n"+msg);
			return false;
		} else {
			if ($('#submitto').is(":checked")) {
				if (confirm("Are you sure you want to submit your choices?")) { return true; } else { return false; }
			} else {
				return true;
			}
		}
	});
	
	$("ol.selector").sortable({
		placeholder: 'highlight',
		handle: 'a.drag',
		axis: 'y'
	});
	$("ol.selector").disableSelection();
	
	$("a[rel='lightbox']").colorbox({transition:"fade"});

	$('a.tooltip').tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - "
	});


});
