
/*
	Copyright (c) JB Interactive Pty. Ltd.
	All Rights Reserved
	http://www.jbinteractive.com.au/
*/

(function ($) {

var parseValue = function (value) {
	if (value == 'auto')
		return 0;
	else
		return parseInt(value);
};

var cookie = {
	name:    'campion_notice',
	options: {
		path:    '/',
		expires: 365
	}
};

$(document).ready (function () {
	
	// Display holiday message if not already viewed
	var viewed = $.cookie(cookie.name);
	
	// Default text on search fields
	$('.program_code').focus (function () {
		this.value = ''; 
		$(this).css ('color', '#000000');
	})
	.blur (function () {
		if (this.value == '') {
			this.value = 'Enter program code'; 
			$(this).css ('color', '#ababab');
		} else {
			this.value = this.value;
		}
	});
	
	// Clear form fields on 'Clear' button click
	$('#clear-button').click (function () {		
		var form =$('form');
		$('input', form).attr ('value', "");
	})
	
	// Add the table zebra stripe and header
	var header = $('tr:first-child');
	header.addClass ('header');
	$('tr:odd').addClass ('odd');
	$('tr:even').not (header).addClass ('even');
	
	// Slight safari-specific styling
	if ($.browser.safari) {
		$('.pagination form').css ('margin-top', '-35px');
	}
	
	// Yucky fix for bookweb pagination - the previous page button wants to be
	// in the results-start.html file, but we want it laid out in the
	// results-end.html file, so let's move it here.
	$('.previous-page')
		.prependTo ('.pagination')
		.removeClass ('hidden');
		
	// Mouseovers for pagination
	$('.pagination input[type="submit"]').hover (
		function () {
			$(this).css ('color', '#ae8272');
			$('div', $(this).parent ()).css ('backgroundColor', '#ae8272');
		},
		function () {
			$(this).css ('color', '#c5a89d');
			$('div', $(this).parent ()).css ('backgroundColor', '#c5a89d');
		});
		
	// Header search 
	$('.header-search form').bind('submit', function (e) {
		e.preventDefault();
		
		var self = this,
			$this = $(this), 
			selectBox = $this.find('select[name=STYPE]'),
			searchTerm = $this.find('input[name=STEXT]');
		
		if (selectBox.val() == 'PROGRAM_CODE') {
			var sendTo = $this.attr('action').replace('search', 'subject') 
					   + '?CAMPUS=' + $this.find('input[name=CAMPUS]').val()
					   + '&YEAR=' + $this.find('input[name=YEAR]').val()
					   + '&SEM=' + $this.find('input[name=SEM]').val()
					   + '&SUBJECT1=' + searchTerm.val();

			window.location.href = sendTo;
		} else {
			self.submit();
		}
	});
	
	// Header search default text
	$('.header-search select').bind('change', function () {
		var self = this,
			$this = $(this),
			searchTerm = $this.parents('ul').find('input[name=STEXT]');
			
		searchTerm.css('color', '#ABABAB').val($this.find(':selected').attr('title'));
	});
	
	$('.search_word').bind('focus', function () {
		$(this).css('color', 'black').val('');
	});
	
	// Ensure updating of shipping method by submitting form on change
	var pathPieces = window.location.pathname.split('/');
	var currentPage = pathPieces[pathPieces.length - 1];
	
	if (currentPage == 'viewlist.cgi') {
		$('form select[name=ZONE]').bind('change', function () {
			$('input[type=submit]').attr('disabled', true);
			$(this).parents('form').submit();
		});
	}	
});

})(jQuery);