
/*
	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:    'mcgills_notice',
	options: {
		path:    '/',
		expires: 365
	}
};

$(document).ready (function () {

	// Default text on search fields
	$('.search_word').focus (function () {
		this.value = ''; 
		$(this).css ('color', '#000000');
	})
	.blur (function () {
		if (this.value == '') {
			this.value = 'Enter search word'; 
			$(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 css fix for firefox
	if ($.browser.mozilla) {
		$('.header-search')
			.css ('padding-top', '3px')
			.css ('height', '29px');
		$('.header-search form')
			.css ('margin-top', '-4px');
		$('.header-search button')
			.css ('margin-top', '1px');
	}
	
	// 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', '#98ab6d');
			$('div', $(this).parent ()).css ('backgroundColor', '#98ab6d');
		},
		function () {
			$(this).css ('color', '#b5c58f');
			$('div', $(this).parent ()).css ('backgroundColor', '#b5c58f');
		});
		
	// Exam certification searches
	if ($('#cert_searches').is('*')) {
		$('#cert_searches input[type=text]').bind('focus', function () {
			$(this).val('').css('color', 'black');
		});
		$('#microsoft_cert_search').bind('submit', function (e) {
			e.preventDefault();
			var keyword = $(this).find('input[name=STEXT]'), str = keyword.val();
			
			str = keyword.val();
			
			if (str.length >= 3 && str.substring(2,3) != '-') {
				keyword.val(
					str.substring(0,2) + '-' + str.substring(2)
				);
			}
			
			this.submit();
		});
		$('#cisco_cert_search').bind('submit', function (e) {
			e.preventDefault();
			var keyword = $(this).find('input[name=STEXT]'), str = keyword.val();
			
			str = keyword.val();
			
			if (str.length >= 4 && str.substring(3,4) != '-') {
				keyword.val(
					str.substring(0,3) + '-' + str.substring(3)
				);
			}
			
			this.submit();			
		});	
	}
	
	// Header search 
	$('.header-search button').click(function () {
		$(this).parents('form').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);
