/* 
	main.js
	High level javscript functionality
*/

/* Essentially the page load function */
$(document).ready( function() {
	setup_login();
	error_message();
});

// word_count()
// Tests a textbox for a wordcount, displays that wordcount, and disables a submit button if the wordcount is exceeded
// textbox - the id of the textbox you're testing the length for
// display - the id of the element where to display the length
// max_char - the max length of the text
// submit_button - the button to disable if the max_char is exceeded
function word_count(textbox, display, max_char, submit_button) {
	var length = $('#' + textbox).val().length;
	var display_string = length + "/" + max_char;
	if(length > max_char){
		$('#' + submit_button).attr("disabled", "disabled");
		display_string = '<font color="red">' + display_string + '</font>';
	}
	else {
		$('#' + submit_button).removeAttr("disabled");
	}
	$('#' + display).html(display_string);
}

// DATEPICKER
// Used if you ever need to have a selector with a datetime, you can use this functionality to manage what it shows (pretty cool, aight?)
// config works like this:
// config = {
//   minYear: int for start year to display
//   maxYear: int for end year to display
//   yearSel: id of year selector on this page
//   monthSel: id of month selector on this page
//   daySel: id of day selector on this page
//   timeSel: id of the time selector on this page
//   defaultDateTime: the unix timestamp for the datetime to display
// }
var DATEPICKER = {};
function datePicker(config){
  // build years array
  for(var i = config.minYear, allYears = []; i <= config.maxYear; ++i) allYears.push(i);

  DATEPICKER = {
    yearSel: $('#' + config.yearSel),
    monthSel: $('#' + config.monthSel),
    daySel: $('#' + config.daySel),
    timeSel: $('#' + config.timeSel),
    years: allYears,
    months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
    times: {
      min: 8, // 8AM
      max: 22, // 10PM
      denominations: .5 // :30 min intervals
    },
    
    // Initialization function for linking fields with the datepicker and populating what can be populated
    initialize: function(){
      // Add years
      var years_append = "";
      for(var i = 0; i < this.years.length; ++i){
        years_append += this.optionTag(this.years[i], this.years[i]);
      }
      this.yearSel.append(years_append);
      
      // Add months
      var months_append = "";
      for(var i = 0; i < this.months.length; ++i){
        months_append  += this.optionTag(i+1,this.months[i]); 
      }
      this.monthSel.append(months_append);
      
      // Add times
      var times_append = "";
      for(var i = this.times.min; i <= this.times.max; ++i){
        for(var j = 0.0; j < 1.0; j += this.times.denominations){
          var minutes = (j * 60 == 0 ? '00' : j * 60).toString();
          times_append += this.optionTag(
            i.toString() + ':' + minutes,
            (i-12 < 1 ? i : i-12).toString() + ':' + minutes + ' ' + (i-12 < 0 ? 'AM' : 'PM')
          );
        }
      }
      this.timeSel.append(times_append);
      
      // Convert config.defaultDateTime to object
      var use_date = new Date();
      if(config.defaultDateTime) use_date = new Date(config.defaultDateTime * 1000);
      
      this.yearSel.val(use_date.getFullYear());
      this.monthSel.val(use_date.getMonth() + 1);
      
      this.populateDays();
      
      this.daySel.val(use_date.getDate());
      
      var built_time = use_date.getHours().toString() + ':' + (use_date.getMinutes() < 10 ? '0':'') + use_date.getMinutes().toString();
      this.timeSel.val(built_time);
      
      // Add events
      this.yearSel.change(this.populateDays);
      this.monthSel.change(this.populateDays);
    },
    
    // option tag builder
    optionTag: function(value,display_val){
      return '<option value="' + value + '">' + display_val + '</option>'; 
    },
    
    // Repopulates days based on month/year change
    populateDays: function(){
      var prev_date = DATEPICKER.daySel.val(); // Get previous selected date
      DATEPICKER.daySel.html(''); // Reset days
      var total_days = 32 - new Date(DATEPICKER.yearSel.val(),DATEPICKER.monthSel.val()-1,32).getDate(); // Get days in month, yes this is weird oh well.
      if(total_days < prev_date) prev_date = total_days; // Don't select illegal date
      
      // Append
      var days_append = ""
      for(var i = 0; i < total_days; ++i){
        days_append += DATEPICKER.optionTag(i+1,i+1);
      }
      DATEPICKER.daySel.append(days_append);
      
      DATEPICKER.daySel.val(prev_date); // Reselect previously selected date
    }
  }
  
  DATEPICKER.initialize();
}


// Comment admin management

// comment_mouseover($id, $type)
// $id is the id of the comment (in the comment table)
// $type is the type of comment (news, cal_item)
// on mouseover event:
function comment_mouseover($id, $type){

  var button = '<div style="float: right"></div>';

  var comment_div = $('#comment_' + $id.toString())[0];
  
}

