
	/* ------------------------------------------------------------------

	Copyright:	© 2009 Designition Ltd
	Author:		Jonic Linley - Designition Ltd - www.designition.co.uk
	Date:		2009-06-19
	Site:		EDGE Services - www.edgeservices.co.uk

	

	------------------------------------------------------------------ */



	//	We pseduo-namespaced this city on rock and roll..

	var Des = Des || {};



	//	Let's get some damn functions in there

	Des.booking_form = function() {

		//	check if courses table exists
		if ($('form.booking_form')[0]) {

			$('#delegates').change(function() {

				discount = false;
				del_count = $(this).val();

				if ($('.discount_row')[0]) $('.discount_row').remove();

				if (del_count > 1) discount = true;

				price_per_del = $('span.price').text();
				vat_rate      = $('span.vat_rate').text();

				sub_total   = price_per_del * del_count;

				if (discount) {
					discount_total = (sub_total / 100) * 5;
					sub_total      = sub_total - discount_total;

					$('<tr class="discount_row"><th>Discount:</th><td>&pound;' + discount_total.toFixed(2) + '</span></td></tr>').insertBefore('.sub_total_row');

				}

				vat_total   = (sub_total / 100) * vat_rate;
				final_total = sub_total + vat_total;

				$('.del_count').text(del_count);
				$('.sub_total').text(sub_total.toFixed(2));
				$('.vat_total').text(vat_total.toFixed(2));
				$('.final_total').text(final_total.toFixed(2));

			}).change();

		}

	}

	Des.courses = function() {

		//	check if courses table exists
		if ($('table.side_courses')[0]) {

			$('table.side_courses').wrap('<div class="side_courses_wrap"></div>');

			var courses = $('table.side_courses tbody tr');

			display_show = !+"\v1" ? 'block' : 'table-row';

			if (courses.length > 5) {

				//	define actions for showing and hiding courses
				function pagination_init() {

					show_city = $('form.city_select').find('select').val();

					if (show_city != 'All Cities') {
						$('.side_courses_wrap').css('height', 'auto');

						$('table.side_courses tbody tr').removeClass('odd');

						$('.courses_pagination').empty().css('display', 'none');

						$.each($('table.side_courses tbody tr td.city'), function() {
							display_val = $(this).text() == show_city ? display_show : 'none';

							$(this).parent().css('display', display_val);
						});

						$('table.side_courses tbody tr:visible:even').addClass('odd');
					} else {
						$('.side_courses_wrap').css('height', 202);

						$('.courses_pagination').pagination(courses.length, {
							callback            : pagination_show,
							items_per_page      : 5,
							num_display_entries : 5
						}).css('display', 'block');
					}

					$('.showing_courses').find('span').text(show_city);

				}	//	function pagination_init()


				function pagination_show(page_index) {

					$('table.side_courses tbody tr').removeClass('odd');

					var items_per_page = 5;
					var max_elem       = Math.min((page_index + 1) * items_per_page, courses.length);

					$('table.side_courses tbody tr').css({ display : 'none' });

					for (var i = page_index * items_per_page; i < max_elem; i++) {
						$('table.side_courses tbody tr:eq(' + i + ')').css('display', display_show);
					}

					$('table.side_courses tbody tr:visible:even').addClass('odd');

					return false;

				}	//	function pagination_show()


				var cities = [];

				//	count number of unique cities
				$.each($('table.side_courses tbody tr'), function() {
					city = $(this).find('td.city').text();

					if (!in_array(city, cities)) cities.push(city);
				});

				sort(cities);


				if (cities.length > 1) {

					$('<form class="clearfix city_select"><fieldset><label for="city">Showing dates in:</label> <select id="city"><option>All Cities</option></select></label></fieldset></form>').insertBefore('.side_courses_wrap');

					$(cities).each(function(index) {
						$('<option>' + cities[index] + '</option>').appendTo('.city_select select');
					});

					$('form.city_select select').change(function() {
						$('form.city_select').submit();
					});

					$('form.city_select').submit(function() {
						pagination_init();

						return false;
					});

//					$('<div class="showing_courses">Showing dates in <span>All Cities</span>:</div>').insertBefore('table.side_courses');

				}	//	if (all_cities.length > 1)

				$('<div class="courses_pagination"></div>').insertAfter('.side_courses_wrap');

				pagination_init();

			}	//	if (num_courses > 5)

		}	//	if ($('table.side_courses')

	}	//	Des.courses = function()

	//	That's my functions done and flippin' dusted.. Now let's get any third party funcs in there an' all


	//	This is from PHP.js (www.phpjs.org)
	function in_array(needle, haystack, argStrict) {
		var key = '', strict = !!argStrict;

		if (strict) {
			for (key in haystack) {
				if (haystack[key] === needle) return true;
			}
		} else {
			for (key in haystack) {
				if (haystack[key] == needle) return true;
			}
		}

		return false;
	}

	//	This is from PHP.js (www.phpjs.org)
	function sort(inputArr, sort_flags) {
		var valArr = [], keyArr=[];
		var k = '', i = 0, sorter = false, that = this;
	
		for (k in inputArr) { // Get key and value arrays
			valArr.push(inputArr[k]);
			delete inputArr[k];
		}

		sorter = function (a, b) {
			if (a > b) return 1;
			if (a < b) return -1;
			return 0;
		};

		valArr.sort(sorter);

		for (i = 0; i < valArr.length; i++) { // Repopulate the old array
			inputArr[i] = valArr[i];
		}

		return true;
	}

	//	Functions and I are done, professionally..



	$(function() {	//	Make rocket go now..

		jQuery.fn.log = function(msg) {
			console.log("%s: %o", msg, this);
			return this;
		};

		$('body').addClass('js');

		if ($.isFunction(window.loadFirebugConsole)) window.loadFirebugConsole();

		if ($('.boxy a')[0]) {
			$('.boxy')
				.css('cursor', 'pointer')
				.click(function() {
					target_href = $(this).find('a').attr('href');
					window.location = target_href;
				})
				.hover(
					function() { $(this).addClass('hover'); },
					function() { $(this).removeClass('hover'); }
				);
		}

		Des.booking_form();
		Des.courses();

		//	That's the anthem, get your damn hands up!

	});
