// JavaScript Document
var multipleSelect = {
	options : {
		parent		: null,
		parentID	: null,
		active 		: false,
		startObj 	: null,
		endObj		: null,
		useKey		: 83
	},
	go : function() {
		multipleSelect.options.active = true;
	},
	select : function() {
		startInt = ($(multipleSelect.options.startObj).prevAll()).length; // get number of elements before the first clicked object
		endInt = ($(multipleSelect.options.endObj).prevAll()).length; // get number of elements before the last clicked object
		_temp = endInt; //temporarily store the end int
		if ( startInt > endInt )
		{
			endInt = startInt;
			startInt = _temp;		
		}
		//put the function into in-active state to stop this function being re-called on the "click" trigger1
		multipleSelect.options.active = false;
		//select all children based on the parent of those selected, and take out just the ones we need
		selection = $(multipleSelect.options.parent).children().slice( Number(startInt)+1, endInt)
		//trigger the "click" event on the objects
		$(selection).trigger("click");
		//reset the multipleSelect object so that it can be re-used again
		multipleSelect.stop();
		//job well done :)
	},
	stop : function() {
		multipleSelect.options.parent = null;
		multipleSelect.options.parentID = null;
		multipleSelect.options.active = false;
		multipleSelect.options.startObj = null;
		multipleSelect.options.endObj = null;
	},
	init : function( options ) {
		$( options.clickable ).each( function(){ // for each clickable
			$(this).bind("click", function(){	// bind click event to each clickable object found
				$(this).toggleClass( options.toggleClass ); //toggle the colour of the object to visually show click event occured
				// change the checkbox status this div relates to
				if ( typeof( $(this).children().attr("checked") ) == "undefined" )
				{
					//if unchecked - "checked" attr is not set - so set it to true (seeing as they clicked on it!)
					$(this).children().attr("checked",true)
				} 
				else 
				{
					//"checked" attr is already defined, find out what it is and toggle it
					if ( $(this).children().attr("checked") == true ){
						$(this).children().attr("checked", false); // un-check the box if currently checked
					} else {
						$(this).children().attr("checked", true); // check the box if currently un-checked
					}
				}
				//check if user is single selecting, or wanting to multiple select
				if ( multipleSelect.options.active == true ) 
				{	// letter s is currently is pushed down - initiate / end multi select
					if ( multipleSelect.options.startObj == null)
					{	//if start div is currently null then set it to the clicked div, and the parent (so only siblings can be selected)
						multipleSelect.options.parentID = $(this).parent().attr("id");
						multipleSelect.options.startObj = $(this);
					}
					else 
					{	//start div is set, make the clicked div the end div selected - ONLY if it has the same parent!!!
						if ( multipleSelect.options.parentID == $(this).parent().attr("id") )
						{
							multipleSelect.options.parent = $(this).parent();
							multipleSelect.options.endObj = $(this);
							multipleSelect.select();
							// got start and end objects, get all between and trigger a "click"
						}
					}
				}
			});
		});
		
		if ( typeof(options.useKey) !== "undefinded" )
		{	//set custom key code
			multipleSelect.options.useKey = (options.useKey.toUpperCase()).charCodeAt(0);
		}
		
		$(window).keydown( function(event){
			switch (event.keyCode) {
		  		case multipleSelect.options.useKey: multipleSelect.go();
		  	}
		});
		$(window).keyup( function(event){
			switch (event.keyCode) {
		  		case multipleSelect.options.useKey: 
					multipleSelect.stop();
		  	}
		});
	}
};
