/*
 * CourseView class, updates the course view
 *
 * @package classes
 * @copyright Copyright 2009, Lastbyte VOF
 * @version 1.0
 */
var CourseView = new Class(
{
	Implements: [Options, Events],

	options:
	{
		coursesPerPage: [10]
	},

	/*
	 * Constructor
	 *
	 * @param Element element
	 */
	initialize: function (element, options)
	{
		this.setOptions(options);
	
		this.element = null;
		if ($type(element) == 'element')
			this.element = element;
			
		this.list = null;
		this.pages = 1;
		this.currentPage = 1;
		this.courseLimit = this.options.coursesPerPage[0];
	},
	
	/*
	 * Shows the courses in the viewport
	 */
	show: function ()
	{
		if (!this.list)
			return;
			
		// List all courses
		var count = 0;
		
		var html = '';
		
		html += '<div class="product_box_header">';
		html += 	'<div class="courseDate"><strong>Starting date <span>(dd/mm/yy)</span></strong></div>';
		html += 	'<div class="courseCountry"><strong>Country</strong></div>';
		html += 	'<div class="courseLocation"><strong>Location</strong></div>';
		html += 	'<div class="courseTrainingType"><strong>Training</strong></div>';
		html += '</div>';
		
		if (this.list.getLength() > 0)
		{
			this.list.getCourses().each(function (course)
			{
				if (count >= (this.currentPage - 1) * this.courseLimit && count < (this.currentPage * this.courseLimit))
				{
					var date;
					date = course.getAttribute('start')['date'];

					html += '<div class="product_box"><a href="courses.php?c=' + course.getAttribute('id') + '">';
					html += 	'<div class="courseDate">' + date + '</div>';
					html += 	'<div class="courseCountry">' + course.getAttribute('Country') + '</div>';
					html += 	'<div class="courseLocation">' + course.getAttribute('location') + '</div>';
					html += 	'<div class="courseTrainingType">' + course.getAttribute('Training Type') + '</div>';
					html += '</a></div>';
					//course.getLabel()
					//alert(course.getAttribute('Training Type'));
					//alert(course.getAttribute('Country'));
				}
				count++;
			}.bind(this));
			html += '';
		}
		else
			html = '<span>No courses found matching these criteria.</span>';
			
		// Replace the HTML contents with a fading animation
		this.element.set('tween', {duration: 300});
		this.element.tween('opacity', 0);
		
		(function ()
		{
			this.element.set('html', html);
			this.element.tween('opacity', 1);
		}.bind(this)).delay(300);
	},
	
	/*
	 * Sets the thumbnail limit
	 *
	 * @param integer limit
	 */
	setThumbnailLimit: function (limit)
	{
		if (this.options.thumbsPerPage.indexOf(limit) > -1)
		{
			this.thumbsLimit = limit;
			if (this.list == null || this.list.getLength() == 0)
				this.pages = 1;
			else
				this.pages = Math.ceil(this.list.getLength() / this.coursesLimit); // Calculate the number of pages
				
			if (this.currentPage > this.pages)
				this.currentPage = this.pages;

			// Update viewport
			this.fireEvent('onUpdate');
			this.show();
		}
	},
	
	/*
	 * Sets the viewed page
	 *
	 * @param integer page
	 */
	setPage: function (page)
	{
		if (page > 0 && page <= this.pages && this.currentPage != page)
		{
			this.currentPage = page;
			
			// Update viewport
			this.fireEvent('onUpdate');
			this.show();
		}
	},
	
	/*
	 * Returns the current page
	 *
	 * @return integer
	 */
	getCurrentPage: function ()
	{
		return this.currentPage;
	},
	
	/*
	 * Returns the total number of pages
	 *
	 * @return integer
	 */
	getTotalPages: function ()
	{
		return this.pages;
	},
	
	/*
	 * Sets the page one back
	 */
	prevPage: function ()
	{
		this.setPage(this.getCurrentPage() - 1);
	},
	
	/*
	 * Sets the page one further
	 */
	nextPage: function ()
	{
		this.setPage(this.getCurrentPage() + 1);
	},
	
	/*
	 * Updates the view based on the specified argument
	 *
	 * @param mixed arg
	 */
	update: function (arg)
	{
		if (!this.element)
			return false;
			
		// Check whether an argument was specified
		if (!arg)
			return false;
			
		// Update view
		if ($type(arg) == 'object')
		{
			this.list = arg; // Store the list
			if (this.list.getLength() == 0)
				this.pages = 1;
			else
				this.pages = Math.ceil(this.list.getLength() / this.courseLimit); // Calculate the number of pages
			
			if (this.currentPage > this.pages)
				this.currentPage = this.pages;

			// Update viewport
			this.fireEvent('onUpdate');
			this.show();
		}
	}

});
