//==================================================================
// iPhone Wheel (Time Picker)
// Timur Prokopiev 2011
//==================================================================

// Global Variables
IPH_HOURS = [1,2,3,4,5,6,7,8,9,10,11,12];
IPH_MINUTES = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59];
IPH_TIMEZONE = ['AM', 'PM'];

get_index_of_array = function(array, value){
  for(var i = 0; i < array.length; i++){
    if (value === array[i]){
      return i;
    }
  }
  return null;
}

get_hours_from_array_with_shift = function(array, index, shift){
	var new_index = index + shift;
	if (new_index < 0) new_index = array.length + new_index;
	if (new_index > array.length - 1) new_index = new_index - array.length;
	return array[new_index];
}

get_minutes_from_array_with_shift = function(array, index, shift){
	var i = get_hours_from_array_with_shift(array, index, shift);
	return normalize_minutes(i);
}

normalize_minutes = function(min){
	return (min < 10 ? ("0" + min.toString()) : min.toString());
}

//==================================================================

function setDefaultValues() {
  var values = new Array();
  
// Enable Current
  values.push({ name:"Current", type:"Boolean",  value:"true" });

// Hour
  values.push({ name:"Hour", type:"ComboBox", value:("12," + IPH_HOURS.join(",")) });

// Minute
  values.push({ name:"Minute", type:"ComboBox", value:("30," + IPH_MINUTES.join(",")) });

// Meridiem
  values.push({ name:"Meridiem", type:"ComboBox", value:("PM," + IPH_TIMEZONE.join(","))});

Widget.elem.customData["currentValues"] = values;

}

function applyCurrentValues() {
  var values = Widget.elem.customData["currentValues"];
  var current = (values[0].value.toString()=="true");
  var hour = parseInt(values[1].value.split(",")[0]);
  var min = parseInt(values[2].value.split(",")[0]);
  var meridiem = values[3].value.split(",")[0].toString();

  if (current){
	var d = new Date();
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();

	if (curr_hour > 12){
		meridiem = "PM";
		curr_hour -= 12;
	} else{
		meridiem = "AM";
	}
	if (curr_hour == 0) curr_hour = 12;
	
	hour = curr_hour;
	min = curr_min;
  }

  var hour_index = get_index_of_array(IPH_HOURS, hour);
  var min_index = get_index_of_array(IPH_MINUTES, min);

//==================================================================
		
		
Widget.GetObjectByName("1 (active)").textChars = hour.toString();
Widget.GetObjectByName("2 (active)").textChars = normalize_minutes(min);

Widget.GetObjectByName("1 (column1)").textChars = get_hours_from_array_with_shift(IPH_HOURS, hour_index, -2);
Widget.GetObjectByName("2 (column1)").textChars = get_hours_from_array_with_shift(IPH_HOURS, hour_index, -1);
Widget.GetObjectByName("3 (column1)").textChars = get_hours_from_array_with_shift(IPH_HOURS, hour_index, 1);
Widget.GetObjectByName("4 (column1)").textChars = get_hours_from_array_with_shift(IPH_HOURS, hour_index, 2);

Widget.GetObjectByName("1 (column2)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, -2);
Widget.GetObjectByName("2 (column2)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, -1);
Widget.GetObjectByName("3 (column2)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, 1);
Widget.GetObjectByName("4 (column2)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, 2);

Widget.GetObjectByName("meridiem").textChars = meridiem;
Widget.GetObjectByName("am").visible = (meridiem == "PM");
Widget.GetObjectByName("pm").visible = (meridiem == "AM");

}

switch (Widget.opCode)
{
	case 1: setDefaultValues();   break;
	case 2: applyCurrentValues(); break;
}