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


// Global Variables
IPH_DAY = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
IPH_MONTHS = ["January","February","March","April","May","June","July","August","September","October","November","December"];
IPH_DATE = [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];
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_day_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_month_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_date_from_array_with_shift = function(array, index, shift){
	var i = get_month_from_array_with_shift(array, index, shift);
	return normalize(i);
}

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 = function(date){
	return (date < 10 ? ("0" + date.toString()) : date.toString());
}

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" });

// Month
  values.push({ name:"Month", type:"ComboBox", value:("January," + IPH_MONTHS.join(",")) });

// Date
  values.push({ name:"Date", type:"ComboBox", value:("1," + IPH_DATE.join(",")) });

// 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 month = values[1].value.split(",")[0];
  var month_index = get_index_of_array(IPH_MONTHS, month);

  var date = parseInt(values[2].value.split(",")[0]);
  var hour = parseInt(values[3].value.split(",")[0]);
  var min = parseInt(values[4].value.split(",")[0]);
  var meridiem = values[5].value.split(",")[0].toString();

  var day = IPH_DAY[new Date(new Date().getFullYear(), month_index, date).getDay()];
  var day_index = get_index_of_array(IPH_DAY, day);


  if (current){
	var d = new Date();
	date = d.getDate();

	day_index = d.getDay();
	day = IPH_DAY[day_index];

	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();

	month_index = d.getMonth();
	month = IPH_MONTHS[month_index];

	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 date_index = date - 1;
  var hour_index = get_index_of_array(IPH_HOURS, hour);
  var min_index = get_index_of_array(IPH_MINUTES, min);

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

Widget.GetObjectByName("1 (active)1").textChars = (day.substring(0,3));
Widget.GetObjectByName("1 (active)2").textChars = (month.substring(0,3));
Widget.GetObjectByName("1 (active)3").textChars = normalize(date);
		
Widget.GetObjectByName("2 (active)").textChars = hour.toString();
Widget.GetObjectByName("3 (active)").textChars = normalize(min);

Widget.GetObjectByName("1 (column1)1").textChars = (get_day_from_array_with_shift(IPH_DAY, day_index, -2).substring(0,3));
Widget.GetObjectByName("2 (column1)1").textChars = (get_day_from_array_with_shift(IPH_DAY, day_index, -1).substring(0,3));
Widget.GetObjectByName("3 (column1)1").textChars = (get_day_from_array_with_shift(IPH_DAY, day_index, 1).substring(0,3));
Widget.GetObjectByName("4 (column1)1").textChars = (get_day_from_array_with_shift(IPH_DAY, day_index, 2).substring(0,3));

Widget.GetObjectByName("1 (column1)2").textChars = (month.substring(0,3));
Widget.GetObjectByName("2 (column1)2").textChars = (month.substring(0,3));
Widget.GetObjectByName("3 (column1)2").textChars = (month.substring(0,3));
Widget.GetObjectByName("4 (column1)2").textChars = (month.substring(0,3));

Widget.GetObjectByName("1 (column1)3").textChars = get_date_from_array_with_shift(IPH_DATE, date_index, -2);
Widget.GetObjectByName("2 (column1)3").textChars = get_date_from_array_with_shift(IPH_DATE, date_index, -1);
Widget.GetObjectByName("3 (column1)3").textChars = get_date_from_array_with_shift(IPH_DATE, date_index, 1);
Widget.GetObjectByName("4 (column1)3").textChars = get_date_from_array_with_shift(IPH_DATE, date_index, 2);

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

Widget.GetObjectByName("1 (column3)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, -2);
Widget.GetObjectByName("2 (column3)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, -1);
Widget.GetObjectByName("3 (column3)").textChars = get_minutes_from_array_with_shift(IPH_MINUTES, min_index, 1);
Widget.GetObjectByName("4 (column3)").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;
}