// JavaScript Document

//on window load set cookie with closest full hour of client time
// eg: client time = 17:34, cookie will be set to 17:00

  

//function to grab client time
function setClientTime(type){
	
	var d = new Date();
	if( type.toLowerCase() == 'utc' ){
		var curr_day = d.getUTCDate();
		var curr_month = d.getUTCMonth() + 1;
		var curr_year = d.getUTCFullYear();
		var curr_hour = d.getUTCHours();
		var curr_min = d.getUTCMinutes();
	}else{
		var curr_day = d.getDate();
		var curr_month = d.getMonth();
		var curr_year = d.getFullYear();
		var curr_hour = d.getHours();
		var curr_min = d.getMinutes();
	}
	
	//var clientTime = curr_month + '/' + curr_day + '/' + curr_year + ' ' + curr_hour + ':00';
	var clientTime = curr_year + '-' + curr_month + '-' + curr_day + ' ' + curr_hour + ':00';
	var offset = d.getTimezoneOffset()/60;	
	var clientUTCTime = document.getElementById( 'clientUTCTime' );
	var clientOffset = document.getElementById( 'clientOffset' );
	
	if( clientUTCTime ) {
		clientUTCTime.value = clientTime;
	}
	if( clientOffset ) {
		clientOffset.value = offset;
	}
	selTime = clientTime;
	
}

//only set the cookie if its empty or if old cookie value != current lower hour
function setCookie(name,value,hours) {
	
	if( readCookie(name) != value ) {
		createCookie(name,value,hours);		
	}
	
}

function createCookie(name,value,hours) {
	
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
}

function readCookie(name) {
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
	
}

function ToTime( $hour ) {
		
	if( $hour < 0 ) $hour += 24;
	if( $hour > 24 ) $hour -= 24;
	if( $hour == 24 ) $hour = 0;
	
	return $hour;
}

var $originalTime = '';

function timeScroll( $o ) {
	
	$offSet = $o.id;
	$time = "";
	//$time: currently selected time (in 24 hour format)
	if( document.uiForm ) $time = document.uiForm.selectedTime.value;
	if( $time == '' && document.uiFormComplete ) $time = document.uiFormComplete.selectedTime.value;
	//set time to noon if no time given
	if( $time == '' ) $time = 12;
	
	if( document.uiForm && $originalTime == '' ) $originalTime = document.uiForm.selectedTime.value;
	
	//offSet is passed by the slider
	$selectedTime = Math.round($time) + Math.round($offSet);
	$dir = 'up';
	if( $offSet < 0 ) $dir = 'down';
	
	//move the date if we rotating around midnight
	if( $selectedTime < 0 ) {
		moveDateSelector( $dir );
	}
	if( $selectedTime > 23 ) {
		moveDateSelector( $dir );
	}
	$selectedTime = ToTime( $selectedTime );
	
	//alert( 'selected Time = ' + $selectedTime );
	//update global $time with the new time
	if( document.uiForm) document.uiForm.selectedTime.value = $selectedTime;
	//if user clicks on timeonly slider update originalTime
	if( $o.parentNode.id == 'slideSelectDiv-list' ) $originalTime = $selectedTime;
	if( document.uiFormComplete) document.uiFormComplete.selectedTime.value = $selectedTime;
	
	//get all the <a> tags and redistribute the offset
	$parent = $o.parentNode;
	$tags = $parent.childNodes;
	for(i=0;i<$tags.length;i++) {
		if( $tags[i].nodeType == 1 ) {
			//reset the class for each tag
			$tags[i].className = '';
			//new offset = old offset + (selectedoffset*-1)
			$newOffset = Math.round($tags[i].id) + (Math.round($offSet) * -1);
			$tags[i].id = $newOffset;
		}
	}
	
	//set selected time's class
	if( $o.parentNode.id == 'slideSelectDiv-list' ) {
		$o.className = "loadingLink";
	}
	else {
		$o.className = 'on';
	}
	
}

function timeScroll2( $o ) {
	
	$offSet = $o.id;
	$time = "";
	//$time: currently selected time (in 24 hour format)
	if( document.uiFormComplete ) $time = document.uiFormComplete.selectedTime.value;
	//set time to noon if no time given
	if( $time == '' ) $time = 12;
	
	//offSet is passed by the slider
	$selectedTime = Math.round($time) + Math.round($offSet);
	$dir = 'up';
	if( $offSet < 0 ) $dir = 'down';
	
	//move the date if we rotating around midnight
	if( $selectedTime < 0 ) {
		moveDateSelector( $dir );
	}
	if( $selectedTime > 23 ) {
		moveDateSelector( $dir );
	}
	$selectedTime = ToTime( $selectedTime );
	
	//alert( 'selected Time = ' + $selectedTime );
	//update global $time with the new time
	//if user clicks on timeonly slider update originalTime
	if( document.uiFormComplete) document.uiFormComplete.selectedTime.value = $selectedTime;
	
	//get all the <a> tags and redistribute the offset
	$parent = $o.parentNode;
	$tags = $parent.childNodes;
	for(i=0;i<$tags.length;i++) {
		if( $tags[i].nodeType == 1 ) {
			//reset the class for each tag
			$tags[i].className = '';
			//new offset = old offset + (selectedoffset*-1)
			$newOffset = Math.round($tags[i].id) + (Math.round($offSet) * -1);
			$tags[i].id = $newOffset;
		}
	}
	
	//set selected time's class
	$o.className = 'on';
	
}

function moveDateSelector(d) {
	o1 = document.uiForm;
	o2 = document.uiFormComplete;
	if( o1 ) {
		o = o1.selectedDate;
		x = o.options.selectedIndex+1;
		if( d == 'down' ) x = o.options.selectedIndex-1;
		if( x< 0 ) x = 0;
		o.options[x].selected=true;
	}
	if( o2 ) {
		o = o2.selectedDate;
		x = o.options.selectedIndex+1;
		if( d == 'down' ) x = o.options.selectedIndex-1;
		if( x< 0 ) x = 0;
		o.options[x].selected=true;
	}
		
}


function resetForm() {
	document.forms.uiForm.reset();
	if( $originalTime != '' ) {
		if( document.uiForm) document.uiForm.selectedTime.value = $originalTime;
	}
}	

	
function _12to24( $t ) {
		//turn am/pm into 24 hour
		$p = $t.indexOf('am');
		if( $p > -1 ) {	
			$t = parseInt($t);
			if( $t == 12 ) $t -= 12;
		} else {
			$t = parseInt( $t );
			$t += 12;
			//if( $t != 12) $t += $t;
		}
		return $t;
		
}