var query = new Object();
var Tags = new Array();
var TagRef = new Array();
var NameRef = new Array();
var Headers = new Array();
var Courses = new Array();
var CourseID = new Array();
var Files = new Array();
var xmlPath = '/ohcms/cores/files/xml.php';
var gTag = '';
var loc = document.location.hash.split(/\//g);

function _checkAll()
{
	$('#Tags').find('.field input').each(function() {
		$(this).attr('checked', 'checked');
	});
}

function _uncheckAll()
{
	$('#Tags').find('.field input').each(function() {
		$(this).attr('checked', '');
	});
}
function createLayout()
{
	
	Headers.push("Course Title","Semester","Year","Type","Pages","Instructor","Download");
	$('#Exams').html('<thead></thead><tbody></tbody><tfoot></tfoot>');
	txt = '<tr>';
	for(var i=0;i<Headers.length;i++)
	{
		txt += '<th>' + Headers[i] + '</th>';
	}
	txt += '</tr>';
	$('#Exams thead').html(txt);
}

function _ajaxGetTags()
{
	clear();
	query.method = 'listDirectory';
	query.path = '/Documents/Exam Registry';

	$.ajax({
		success: function(xml) {
			$(xml).find('Directory Directory').each(function() {
				tag = new Object;
				tag.Name = $(this).attr('Name');
				tag.Id = $(this).attr('Id');
				Tags.push(tag);
				
			});
		},
		complete: function() {
			txt = '';
			c = 0;
			col = '';
			colHeight = 15;
			for(var i=0;i<Tags.length;i++)
			{
				txt += (i%colHeight == 0) ? '<ol>' : '';
				txt += '<li><a class="exam-'+Tags[i].Id + col +'" href="#" onclick="_ajaxLoadExams('+Tags[i].Id+');return false;">'+Tags[i].Name+'</a></li>';
				txt += (i%colHeight == colHeight-1) ? '</ol>' : '';
				TagRef[Tags[i].Id] = Tags[i].Name;
				c++;
			}
			$('#Tags').html(txt);
		}
	});
}

function _ajaxLoadAllExams()
{
	clear();
	$('#Tags').find('.field input:checked').each(function() {
		CourseID.push($(this).val());
	});
	if(CourseID != '') {
		_ajaxLoadExams(CourseID);
	} else {
		txt = 'No Course Selected';
		$('#Exams tbody').html(txt);
	}
	
}


function _ajaxLoadExams(CourseID)
{
	loc = document.location.hash.split(/\//g);
	if(loc[1] == TagRef[CourseID]) {
		return;
	} else {
		$('#Exams tbody').fadeOut(200);
		clear();
		$('#Tags').find('li a.active').removeClass('active');
		
		$('.exam-'+CourseID).addClass('active');
		query.id = CourseID;
			$.ajax({
				async: false,
				success: function(xml) {
					$(xml).find('Directory Directory').each(function() {
						course = new Object();
						course.Name = $(this).attr('Name');
						course.Id = $(this).attr('Id');
						Courses.push(course);
					});
				}
			});
		
		_ajaxBuildNumSelect();
		gTag = TagRef[CourseID];
		//$('#currentCourse').html(gTag);
		
		if(Courses[0] == undefined) {
			txt = 'No Courses Found';
			$('#Exams tbody').html(txt);
			$('#Exams tbody').fadeIn(0);
			return false;
		} else {
			_ajaxLoadExamContent(Courses[0].Id)
		}
	}
}

function _ajaxBuildNumSelect() {
	txt = '';
	for(var i=0;i<Courses.length;i++)
	{
		txt += '<option val="'+Courses[i].Id+'">' + Courses[i].Name + '</option>';	
	}
	$('#courseNumSelect').html(txt);
}

function _ajaxLoadExamContent(dirID)
{
	clear();
	if(dirID == undefined){
		query.id = $('#courseNumSelect option:selected').attr('val');
	} else {
		query.id = dirID;
	}
	
	document.location = '#/' + gTag + '/' + $('#courseNumSelect option:selected').val();
	
	$.ajax({
		async: false,
		success: function(xml) {
			$(xml).find('File').each(function() {
				file = new Object();
				file.Name = $(this).attr('Name');
				file.Id = $(this).attr('Id');
				file.Link = $(this).find('Link').text();
				Files.push(file);
			});
		},
		complete: function() {
			txt = '';
			var o = 0;
			for(var i=0;i<Files.length;i++)
			{
				
				if(o==1) {
					odd = 'odd';
					o = 0;
				} else {
					odd = '';
					o++;
				}
				
				txt += '<tr class="'+odd+'">';
				txt += '<td>'+Files[i].Name+'</td>';
				txt += '<td></td><td></td><td></td><td></td><td></td>';
				txt += '<td><a target="_blank" href="'+Files[i].Link +'">'+ Files[i].Name +'</a></td>';
				txt += '</tr>';
				
			}
			
			$('#Exams tbody').html(txt);
			$('#Exams tbody').fadeIn(200);
		}
		
	});

}


function clear() {
	CourseID = [];
	Files = [];
	Courses = [];
}

