
	/*
	* Title: Winterton JS Objects
	* Author: Simon Smith - http://www.blink-design.net
	* Site: http://www.wintertonvalley.co.uk
	* Requires jQuery 1.4+
	*/
	
	$('html').addClass('js');
	
	// Only call methods common to all pages here
	$(document).ready(function() {
		$('a[rel=highslide]').attr('title', 'Click to expand');
	});
	
	
	var WIN = WIN || {}; // Namespace
	WIN.constructors = {}; // Constructor objects
	WIN.util = {};
	
	
	WIN.clearInput = function() {
		
		var textarea = $.extend(
			{},
			WIN.constructors.inputTextClear({
				inputBox : '#extra-info',
				text : 'Any additional details..'
			})
		);

		textarea.emptyOnClick();
		
	}
	
	WIN.moreInfo = function() {
		
		var readMore; 		// Element that will show moreDetails when clicked
		var moreDetails; 	// Reference to hidden elements

		moreDetails = $('#more-details');	
		
		readMore = $('<p></p>', {
			id : 'more',
			html : '<a href="#">Show me more information</a>'
		});
		
		moreDetails.before(readMore);
		
		readMore.find('a').click(function() {
			moreDetails.slideDown();
			readMore.remove();
			return false;
		});
		
	}
	
	WIN.bookingFormDates = function() {
		
		var selectableRows; 				// Table rows that CAN be selected
		var fromDateInput;					
		var toDateInput; 					
		var startDates 						// Collection of all start dates from table
		var endDates 						
		var fromDateSelectElement; 			// Select element that is constructed and appended to DOM
		var toDateSelectElement; 			
		var dateSelectObj; 					// Will be instance of multiple objects
		
		fromDateInput = $('#from-date');
		toDateInput = $('#to-date');
		startDates = $('#bookings tr').not('.booked').not(':first').find('td:first');
		endDates = $('#bookings tr').not('.booked').not(':first').find('td:eq(1)');
		selectableRows = $('#bookings').find('tr.ui-selectee');
		
		// Create new object	
		dateSelectObj = $.extend(
			{}, 
			WIN.constructors.filterTableRow(),
			WIN.constructors.dateSelect(),
			WIN.constructors.compareSelectIndex()
		);
		
		// Create select lists 
		fromDateSelectElement = dateSelectObj.buildSelect('from-date', startDates); 
		toDateSelectElement = dateSelectObj.buildSelect('to-date', endDates); 

		// Add lists to form
		dateSelectObj.addToForm(fromDateInput, fromDateSelectElement);
		dateSelectObj.addToForm(toDateInput, toDateSelectElement);
		
		selectableRows.click(function() {
			var selectedRows = $('#bookings').find('tr.ui-selected');
			
			var firstTableRowIndex = dateSelectObj.getRowIndex({ // Index of the first selected row in the set
				selectedRow : selectedRows,
				allRows : selectableRows,
				position : ':first' 
			});
				
			var lastTableRowIndex = dateSelectObj.getRowIndex({ // Index of the last selected row in the set
				selectedRow : selectedRows, 
				allRows : selectableRows,
				position : ':last' 
			});
	
			dateSelectObj.setOptionToSelected(fromDateSelectElement, firstTableRowIndex); // Sets the from date option to the one matching the index from above method call
			dateSelectObj.setOptionToSelected(toDateSelectElement, lastTableRowIndex);  // Same as above for to date
		});
	
		$('#from-date, #to-date').change(function() {
			var siblingSelect = dateSelectObj.getSiblingSelect($(this));
			if (this.id === 'from-date') {
				if (siblingSelect.indexOfCurrentOption > siblingSelect.siblingIndex) { // If the index of the chosen option is greater than the one in the sibling list, switch sibling list to same index as this one
					dateSelectObj.setOptionToSelected(siblingSelect.sibling, siblingSelect.indexOfCurrentOption);
				}
			} else {
				if (siblingSelect.indexOfCurrentOption < siblingSelect.siblingIndex) { // Same as above but in reverse
					dateSelectObj.setOptionToSelected(siblingSelect.sibling, siblingSelect.indexOfCurrentOption);
				}
			}
			$('#bookings').find('tr.ui-selected').removeClass('ui-selected');
		});
			
	}
	
	// Object constructors ------------------ //
	
	WIN.constructors.compareSelectIndex = function() {
		
		var optionIndex = function(select) {
			WIN.util.strict( [Object], arguments );
			var index = select.find('option:selected').index();
			return index;
		}
		
		return {
			
			getSiblingSelect : function(select) {
				WIN.util.strict( [Object], arguments );
				var siblingSelect = select.parent().siblings('li').find('select');
				var selectObject = {
					indexOfCurrentOption : optionIndex(select),
					sibling : siblingSelect,
					siblingIndex : optionIndex(siblingSelect) 
				}
				return selectObject;
			}
			
		}
		
	}
	
	WIN.constructors.filterTableRow = function() {
		
		return {
			
			getRowIndex : function(opt) { 
				WIN.util.strict( [Object], arguments );
				var filterSelected = opt.selectedRow.filter(opt.position); // Filter all chosen (i.e clicked) elements to return one specified in filter
				var indexOfSelected = opt.allRows.index(filterSelected); // Look through newly filtered elements and find index of the specified one
				return indexOfSelected;
			}
			
		}
		
	}
	
	WIN.constructors.dateSelect = function() {
		
		var buildOptions = function(dates) {
			WIN.util.strict( [Object], arguments );
			var options = '';
			dates.each(function() {
				var text = this.innerHTML;
				options += '<option value="' + text + '">' + text + '</option>';
			});
			return options;
		}
		
		var getId = function(elem) {
			var id = elem.attr('id');
			return '#' + id;
		}
		
		return {
			
			buildSelect : function(id, dates) {
				WIN.util.strict( [String, Object], arguments );
				var optionElements = buildOptions(dates);
				var newList = $('<select></select>', {
					name : id,	
					id : id
				});
				newList.html(optionElements);
				return newList;
			},
			
			addToForm : function(input, list) {
				WIN.util.strict( [Object, Object], arguments );
				var label = input.prev();
				var id = getId(input);
				$(id).remove();
				label.after(list);
			},
			
			setOptionToSelected : function(list, i) {
				list.find('option').eq(i).attr('selected', 'selected');
			}
			
		}
		
	}
	
	WIN.constructors.inputTextClear = function(opt) {
		
		return {
				
			emptyOnClick : function() {
				$(opt.inputBox).attr('value', opt.text);

				$(opt.inputBox).focus(function() {
					if (this.value == opt.text) this.value = ''; 
				}).blur(function() {
					if (this.value == '') this.value = opt.text;
				});
			}
			
		}
		
	}
	
	// Utilties ---------------- //
	
	WIN.util.strict = function(types, args) {
		if (types.length != args.length) {
			throw 'Invalid number of arguments. Expected ' + types.length + ' but received ' + args.length + ' instead!';
		}
		
		for (var i = 0; i < args.length; i++) {
			if (args[i].constructor != types[i]) {
				throw "Invalid argument type. Expected " + types[i].name + ' but received ' + args.constructor.name + ' instead.';
			}
		}
	}
	
	
	// Highslide overrides
	hs.graphicsDir = 'images/highslide/graphics/';
	hs.showCredits = false;
	