// Matrix Javascript using Mootools framework
// By David Rugendyke

var frag_bits = [0,0];

// The initialising events when the page loads
// -------------------------------------------
window.addEvent('load', function() {
	// Auto select the default parent menu
	try {
		var default_pid = $('default_pid').getValue();
		var default_div = $('default_div').getValue();
		var default_col = $('default_col').getValue();
		var load_open   = $('load_open').getValue();
		
		// Need to check the frag ident here and change cat if need be???
		// #1758:4462
		// cat_1758.click();
	
		var reg_valid_frag = /^\d+:\d+$/
		
		if (reg_valid_frag.test(location.hash.substr(1))) {
			frag_bits = location.hash.substr(1).split(':');
			if ($chk($('cat_'+frag_bits[0]))) {
				matrix_load_cat_data(frag_bits[0], 'cat_'+frag_bits[0], default_col, true, load_open);
			} else {
				matrix_load_cat_data(default_pid, default_div, default_col, false, load_open);
			}
		} else {
			matrix_load_cat_data(default_pid, default_div, default_col, false, load_open);
		}
		
	} catch(e) {}
	
	var Page_Load;
});
// -------------------------------------------
//var winRef;


// When a parent category is clicked, load the data and change the bg colour
function matrix_load_cat_data(cat_id, cat_div, cat_colour, load_tab, load_open) {

	// Store the id of the current parent id
	$('pid').setProperty('value', cat_id);
	var mid = $('mid').getValue();

	$('matrix_course_data').setHTML('<div id="page_loading">Please wait while the course list is loading...</div>');
	
	// Load category header text
	if (load_tab) {
		var query = 'action=baloo&command=matrix_load_cat_header&pid='+cat_id;
		new Ajax('index.php', {method: 'post', data: query, update: $('category_header') }).request();
	}

	// Now load the data
	var query = 'action=matrix&command=matrix_load_cat_data&cat_colour='+cat_colour+'&mid='+mid+'&pid='+cat_id;
	if (cat_id == '') {
		query += '&page_request=offer-guarantee-courses';
	}
	
	try { Page_Load.cancel(); } catch(e) {}
	
	Page_Load = new Ajax('index.php', {method: 'post', data: query,  onComplete: function(resp) {
																			$('matrix_course_data').setHTML(resp);
																			if (load_open != 'open') {
																				$$('.matrix_unit_cat_div').each(function(item) { item.setStyle('display', 'none'); });
																				$$('.disp_grp').each(function(item) { item.setStyle('display', 'none'); });
																				$$('.matrix_loc').each(function(item) { item.setStyle('display', 'none'); });
																			}
																			// Get second part only and try that
																			if(frag_bits[1] > 0) {
																				matrix_open_unit_cat(frag_bits[1]);
																			}
																		}
																}).request();

	// Get the menu element
	var menuEl = $(cat_div);

	// Now get all those elements so we can select between them and turn some off/on etc
	var menuElements = $$(".matrix_parent_cat");
	// Loop through them
	menuElements.each(function(menuItem, i) {

			if (menuEl == menuItem) {
				colour = menuItem.getProperty('rel');
				new Fx.Styles(menuItem, {duration:500, wait:false}).start({'background-color': colour});
				new Fx.Styles($('page_loading'), {duration:500, wait:false}).start({'background-color': colour});
				menuItem.setStyles({'border-bottom': 'none', 'color': '#234a71'});
			} else {
				new Fx.Styles(menuItem, {duration:500, wait:false}).start({'background-color': '#EFF5FA'});
				menuItem.setStyles({'border-bottom': '0px solid #9FB8D0', 'color': '#306396'});
			}

	 });
}

// When a unit category is clicked
function matrix_open_unit_cat(cat_unit_id, force) {

	var cat_unit_div = 'unit_cat_'+cat_unit_id;
	var cat_loc_div = 'matrix_loc_'+cat_unit_id;
	var label = 'school_location_hdr_'+cat_unit_id;
	var display = $(cat_unit_div).getStyle('display');

	// This toggles the properties of the expanded cell
	if(force != 'shrink' && (display == 'none' || force == 'grow')) {
		
		if (force != 'grow') {
			$$('.matrix_unit_cat_div').each(function(el) { el.setStyle('display', 'none'); } );
			$$('.matrix_loc').each(function(el) { el.setStyle('display', 'none'); } );
			$$('.school_location_hdr').each(function(el) { el.setStyles({
				fontWeight: 'normal',
				height: 'auto',
				paddingBottom: 0
			}); } );
		}
		
		$(cat_unit_div).setStyles({display: 'block'});
		$(cat_loc_div).setStyles({display: 'block'});
		$(label).setStyles({
			fontWeight: 'bold',
			height: 25,
			paddingBottom: 3
		});
	} else if (display != 'none' || force == 'shrink') {
		// Close it
		$(cat_unit_div).setStyles({display: 'none'});
		$(cat_loc_div).setStyles({display: 'none'});
		$(label).setStyles({
			fontWeight: 'normal',
			height: 'auto',
			paddingBottom: 0
		});
	}

}
// When a unit category is clicked
function matrix_open_fac_cat(fac_id, force) {

	var school_id;
	var schools = $$('#cat_units_titles_'+fac_id+' .matrix_subcat_block');
	//if (schools.length == 0) { alert('Need to open other cat'); }
	schools.each(function(sch_cat, i) { 
						  	school_id = sch_cat.id.substr(11); //course_cat_4456
							matrix_open_unit_cat(school_id, force);
						  });
	
	var forcexored = (force == 'grow') ? 'shrink' : 'grow';
	$('fac_link_'+fac_id).setProperty('href', 'javascript:matrix_open_fac_cat('+fac_id+', \''+forcexored+'\')');
}

// When a display group is clicked
function matrix_open_disp_grp(disp_grp_id) {

	var disp_grp_div = 'disp_grp_'+disp_grp_id;
	var label = 'course_disp_label_'+disp_grp_id;
	var display = $(disp_grp_div).getStyle('display');

	// This toggles the properties of the expanded cell
	if(display == 'none') {
		
		$$('.disp_grp').each(function(el) { el.setStyles({
			display: 'none',
			marginBottom: 0 
		}); } );
		$$('.course_disp_label').each(function(el) { el.setStyles({
			fontWeight: 'normal',
			height: 'auto',
			paddingBottom: 0
		}); } );
		
		$(disp_grp_div).setStyles({
			display: 'block',
			marginBottom: 15
		});
		$(label).setStyles({
			fontWeight: 'bold',
			height: 'auto',
			paddingTop: 8
		});
	}else{
		// Close it
		$(disp_grp_div).setStyles({
			display: 'none',
			marginBottom: 0
		});
		$(label).setStyles({
			fontWeight: 'normal',
			height: 'auto',
			paddingTop: 0
		});
	}

}

function toggle(el) {
	
	var disp = ($(el).getStyle('display') == 'block') ? 'none' : 'block';
	$(el).setStyle('display', disp);
}

function save_units(checker) {
	if (checker.getProperty('checked')) {
		var value = '';
		// Set value
		$$('.saved-unit').each(function(el) {
					value += el.getProperty('id');
				});
		Cookie.set('SCU_fees_units', value, { duration: 30 });
	}
	else { 
		if (confirm('Are you sure you no longer want to have your saved units available for your next visit?')) {
			Cookie.remove('SCU_fees_units');
		} else {
			checker.setProperty('checked', true);
		}
	}
}

// Matrix temp load live
/*function matrix_temp_load_live(cid, spk_no, spk_ver_no, school_id, faculty_id) {

	try { winRef.close(); } catch(e) {}
	winRef = window.open('?action=matrix&command=matrix_temp_load&cid='+cid+'&spk_no='+spk_no+'&spk_ver_no='+spk_ver_no+'&school_id='+school_id+'&faculty_id='+faculty_id, 'Live_Win', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');

}*/
