// coursepage.js

$(document).ready(function(){

	$("#savethis a").click( function(e) {
		
		// get the id of this course and current contents of the course basket
		var courseid = $.query.get('id');
		
		// if no d was passed in the query, see if it was set on the page
		if ( courseid == '' ) {
			courseid = thiscourse;
		}
		
		var coursebasket = "";
		if ( $.cookie('coursebasket') ) {
			var coursebasket = $.cookie('coursebasket');
		}		
		// split the course basket up ito individual courses and check to see 
		// if this course it already in there
		var courselist = new Array();
		for ( var i = 0; i < coursebasket.split(":").length; i++ ) {
			courselist[coursebasket.split(":")[i]] = 1;
		}
		
		if ( courselist[courseid] != 1 ) {
			// OK, so this course id wasn't in the coursebasket so put it in, 
			// adding a colon as delimiter in necessary, then set the cookie
			if ( coursebasket != '' ) { coursebasket += ":"; }
			coursebasket += courseid;
			$.cookie('coursebasket', coursebasket, { expires: 1, path: '/', domain: 'hud.ac.uk' } );
			$("#savethis a").html("Course saved");
			$("#savethis").addClass("saved");
			$("#viewbasket").addClass("yescomp");
		}
				
		// update the display showing the number of courses saved
		$("#contentBox #miniinfo p.nb").html("You have <a href='coursebasket.php'>"+coursebasket.split(':').length+" courses saved for comparison</a>.");
		
		// return false to prevent default behaviour of clicking the link
		e.preventDefault();
		
		
		
	});

	$("#basketsnitch").click( function() {
		alert( $.cookie('coursebasket') );
	});
	$("#basketclear").click( function() {
		$.cookie('coursebasket', null, { expires: 1, path: '/', domain: 'hud.ac.uk' });
		window.location.reload();
		return false;
	});
	
	$("#contentBox h2.narrative").toggle( function() {
		
		var narrsection = $(this).attr('id');
		$(this).addClass('expanded');
		$("#"+narrsection+"text").slideDown(500, 'swing');	
			
	}, function() {
		var narrsection = $(this).attr('id');	
		$(this).removeClass('expanded');
		$("#"+narrsection+"text").slideUp(400, 'swing', function() {

//			$("#"+narrsection+"text").addClass('expanded');

		});		
	});
	
	// hide all the narrative text boxes
	$("#contentBox div.narrative").hide();

	$("#aim").click();
	
	// make it obvious that the narrative h2 are clickable with hand cursor
	$("#contentBox h2.narrative").css("cursor", "pointer");
	$("#contentBox h2.narrative").hover( function() {

		$(this).addClass('narhover');

	}, function() {

		$(this).removeClass('narhover');

	});

});


