<!--
//---------------------------------+
//  CARPE  S l i d e r        1.5  |
//  2006 - 01 - 03                 |
//  By Tom Hermansson Snickars     |
//  Copyright CARPE Design         |
//  http://carpe.ambiprospect.com/ |
//  Contact for custom scripts     |
//  or implementation help.        |
//---------------------------------+

// Global vars. You don't need to make changes here to change your sliders.
// Changing the attributes in your (X)HTML file is enough.
var carpeDefaultSliderLength      = 100;
var carpeSliderClassName          = 'carpe_slider';
var carpeSliderDisplayClassName   = 'carpe_slider_display';

// carpeGetElementsByClass: Cross-browser function that returns
// an array with all elements that have a class attribute that
// contains className
function carpeGetElementsByClass(className) {
	var classElements = new Array();
	var els = document.getElementsByTagName("*");
	var elsLen = els.length;
	var pattern = new RegExp("\\b" + className + "\\b");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// carpeLeft: Cross-browser version of "element.style.left"
// Returns or sets the horizontal position of an element.
function carpeLeft(elmnt, pos) {
	if (!(elmnt = document.getElementById(elmnt))) return 0;
	if (elmnt.style && (typeof(elmnt.style.left) == 'string')) {
		if (typeof(pos) == 'number') {
			elmnt.style.left = pos + 'px';
		} else {
			pos = parseInt(elmnt.style.left);
			if (isNaN(pos)) pos = 0;
		}
	} else if (elmnt.style && elmnt.style.pixelLeft) {
		if (typeof(pos) == 'number') {
			elmnt.style.pixelLeft = pos;
		} else {
			pos = elmnt.style.pixelLeft;
		}
	}
	return pos;
}

function getTextDate(this_date) {
	if (this_date == "NOW")		return "Present";

	date_arr = this_date.split("_");
	month = date_arr[1] * 1 - 1;
	year = date_arr[0];

	// begin building text date
	display_date = monthArray[month];

	// if this is September 2001 then it's either "a" or "b"
	if (month == 8 && year == 2001) {
		if (date_arr[2] != null) {
			display_date += " (2/2)";
		} else {
			display_date += " (1/2)";
		}
	}

	// add year
	display_date += " " + year;

	return display_date;
}

// moveSlider: Handles slider and display while dragging
function moveSlider(evnt) {
	var evnt = (!evnt) ? window.event : evnt; // The mousemove event
	if (mouseover) { // Only if slider is dragged
		x = slider.startOffsetX + evnt.screenX; // Horizontal mouse position relative to allowed slider positions
		if (x > slider.xMax) x = slider.xMax; // Limit horizontal movement
		if (x < 0) x = 0; // Limit horizontal movement
		carpeLeft(slider.id, x);  // move slider to new horizontal position
		sliderPos = (slider.distance / display.valuecount) * Math.round(display.valuecount * x / slider.distance);
		v = Math.round((sliderPos * slider.scale + slider.from) * Math.pow(10, display.decimals)) / Math.pow(10, display.decimals); // calculate display value

		display.value = getTextDate(dateArray[v]); // put the new value in the slider display element
		changeDate( dateArray[v] );

		return false;
	}
	return;
}

// slide: Handles the start of a slider move.
function slide(evnt) {
	if (!evnt) evnt = window.event; // Get the mouse event causing the slider activation.
	slider = (evnt.target) ? evnt.target : evnt.srcElement; // Get the activated slider element.
	dist = parseInt(slider.getAttribute('distance')); // The allowed slider movement in pixels.
	slider.distance = dist ? dist : carpeDefaultSliderLength; // Default distance from global var.
	displayId = slider.getAttribute('display'); // ID of associated display element.
	display = document.getElementById(displayId); // Get the associated display element.
	display.sliderId = slider.id; // Associate the display with the correct slider.
	dec = parseInt(display.getAttribute('decimals')); // Number of decimals to be displayed.
	display.decimals = dec ? dec : 0; // Default number of decimals: 0.
	val = parseInt(display.getAttribute('valuecount'));  // Allowed number of values in the interval.
	display.valuecount = val ? val : slider.distance + 1; // Default number of values: the sliding distance.
	from = parseFloat(display.getAttribute('from')); // Min/start value for the display.
	from = from ? from : 0; // Default min/start value: 0.
	to = parseFloat(display.getAttribute('to')); // Max value for the display.
	to = to ? to : slider.distance; // Default number of values: the sliding distance.
	slider.scale = (to - from) / slider.distance; // Slider-display scale [value-change per pixel of movement].
	slider.from = from;
	slider.xMax = slider.distance;
	slider.startOffsetX = carpeLeft(slider.id) - evnt.screenX; // Slider-mouse horizontal offset at start of slide.
	mouseover = true;
	document.onmousemove = moveSlider; // Start the action if the mouse is dragged.
	document.onmouseup = sliderMouseUp; // Stop sliding.
	return false;
}

function getDateFromText(text_date) {
	if (text_date == "Present")		return "NOW";

	// turn display back into date
	date_arr = text_date.split(" ");
	month = date_arr[0];
	year = date_arr[1];
	if (year.substr(0, 1) == "(") {
		year = date_arr[2];
	}

	month = array_search(monthArray, month) + 1;

	if ( month < 10 ) {
		month = "0" + month;
	}

	if ( (month < 9 && year < 2002) || date_arr[1].substr(0, 2) == "(2" ) {
		month += "_";
	}
	
	this_date = year + "_" + month;
	return this_date;
}

// sliderMouseUp: Handles the mouseup event after moving a slider.
// Snaps the slider position to allowed/displayed value. 
function sliderMouseUp() {
	if (mouseover) {
		v = (display.value) ? display.value : dateArray[0]; // Find last display value.

		// get date format from this
		this_date = getDateFromText(v);

		// update the cloud
		changeDate( this_date );
		
		// now turn the value into its key
		v = array_search(dateArray, this_date);
		pos = (v - slider.from)/(slider.scale); // Calculate slider position (regardless of orientation).
		pos = (pos > slider.xMax) ? slider.xMax : pos;
		pos = (pos < 0) ? 0 : pos;
		carpeLeft(slider.id, pos); // Snap horizontal slider to corresponding display position.

		if (document.removeEventListener) { // Remove event listeners from 'document' (W3C).
			document.removeEventListener('mousemove', moveSlider, false);
			document.removeEventListener('mouseup', sliderMouseUp, false);
		} else if (document.detachEvent) { // Remove event listeners from 'document' (IE).
			document.detachEvent('onmousemove', moveSlider);
			document.detachEvent('onmouseup', sliderMouseUp);
		}
	}
	mouseover = false; // Stop the sliding.
}

function arrow_slider(thisObj, this_direction) {
	slider = document.getElementById('date_slider');
	display = document.getElementById('date_display');

	xMax = parseInt(slider.getAttribute('distance'));
	val = parseInt(display.getAttribute('valuecount'));
	movePx = Math.round( xMax / (val-1) );

	x = carpeLeft('date_slider') + (movePx * this_direction);
	if (x > xMax) x = xMax;
	if (x < 0) x = 0;

	carpeLeft(slider.id, x);

	v = (display.value) ? display.value : 0;
	if (v != 0) {
		// get date format from this
		this_date = getDateFromText(v);

		// now turn the value into its key and apply direction
		v = array_search(dateArray, this_date) + this_direction;
		
		if (v < 0)	v = 0;
		if (v >= dateArray.length)	v = dateArray.length - 1;
	}

	// having incremented v, create the date again
	this_date = dateArray[v];
	
	// get the value out and call changeDate() and reset the displayed date
	changeDate(this_date);
	display.value = getTextDate(dateArray[v]);

	// clear selection rectangle
	thisObj.blur();
}

function focusDisplay(evnt) {
	if (!evnt) evnt = window.event; // Get the mouse event causing the display activation.
	display = (evnt.target) ? evnt.target : evnt.srcElement; // Get the activated display element.
	lock = display.getAttribute('typelock'); // Is the user allowed to type into the display?
	if (lock == 'on') {
		display.blur();
	}
	return;
}


//-->