// kate: indent-mode cstyle; space-indent false; indent-width 4;

// Course Schedule Creator
// Copyright (C) 2009  Roman Shtylman <shtylman@gmail.com>
//
// Licensed under the GNU Affero General Public License
// http://www.fsf.org/licensing/licenses/agpl-3.0.html

/// Color ///
function Color(r, g, b)
{
	this.r = r;
	this.g = g;
	this.b = b;
}

Color.prototype.toHexString = function()
{ with(this) {
	var pre = "#";

	//the tostring function doesn't pad, so we need to pad the leading 'r'
	if (r == 0)
	{
		pre += "00";
	}
	else if (r < 16)
	{
		pre += "0";
	}
	
	return pre + (r<<16 | g<<8 | b).toString(16);
}}

/// Major ///
function Major(id, abbr, n)
{
	this.id = id;
	this.abbr = abbr;
	this.name = n;
	
	this.courses = new Array();
	this.coursesByNumber = new Array();
}

/// Course ///
function Course(id, major, number, name)
{
	this.id = id;
	this.major = major;
	this.number = number;
	this.name = name;
	this.sections = new Array();
	
	//color
	this.color = new Color(
		Math.floor(Math.random() * 255),
		Math.floor(Math.random() * 255),
		Math.floor(Math.random() * 255)
	)
}

Course.prototype.toString = function()
{
	return this.number + " " + this.name;
}

/// Section ///
function Section(id, srn, name, credits, campus)
{
	this.id = id;
	this.srn = srn;
	this.name = name;
	this.credits = credits;
	this.campus = campus;
	this.gpa = 0;
	
	this.timeslots = new Array();
	this.instructor;
	
	this.visible = false;
	this.checked = false;
}

Section.prototype.toString = function()
{
	return this.srn + " " + this.name;
}

Section.prototype.timesString = function()
{
	if (this.timeslots.length == 0)
	{
		return "TBA";
	}
	
	var last = this.timeslots[0];
	var string = last.day;
	
	//assume timeslots are in sorted by matching start/end times and days
	//loop timeslots
	//when change from previous timeslot, add break and new day(s)
	for (var i=1; i<this.timeslots.length ; ++i)
	{
		var ts = this.timeslots[i];
		
		if (ts.start == last.start && ts.end == last.end)
		{
			string += ts.day;
		}
		else
		{
			string += " " + last.startString() + " - " + last.endString();
			string += "<br />" + ts.day;
			last = ts;
		}
	}
	
	string += " " + last.startString() + " - " + last.endString();
	
	return string;
}

Section.prototype.timeGroups = function()
{
	var groups = new Array();
	
	var last = null;
	var group = null;
	
	//assume timeslots are in sorted by matching start/end times and days
	//loop timeslots
	//when change from previous timeslot, add break and new day(s)
	for (var i=0; i<this.timeslots.length ; ++i)
	{
		var ts = this.timeslots[i];
		
		if (last != null && ts.start == last.start && ts.end == last.end)
		{
			group.push(ts);
		}
		else
		{
			group = new Array(ts);
			groups.push(group);
			last = ts;
		}
	}
	
	return groups;
}

/// Timeslot ///
function Timeslot(type, day, location, start, end)
{
	this.type = type;
	this.day = day;
	this.location = location;
	this.start = start;
	this.end = end;
	
	this.displayDiv;
}

//make a timestring out of the start and end time
Timeslot.prototype.timeString = function(time)
{
	var h = Math.floor(time / 60);
	var m = time % 60;
	var ampm = " am";
	
	if (h >= 12)
	{
		ampm = " pm";
		
		if (h != 12)
		{
			h -= 12;
		}
	}
	
	if (m < 10)
	{
		m = "0" + m;
	}
	
	return h + ":" + m + ampm;
}

Timeslot.prototype.startString = function()
{
	return this.timeString(this.start);
}

Timeslot.prototype.endString = function()
{
	return this.timeString(this.end);
}

/// Instructor ///
function Instructor(id, fname, lname)
{
	this.id = id;
	this.fname = fname;
	this.lname = lname;
	
	//email
}