// Drop-down Auto-submit  
function submitForm(elm,formName){
	if (elm.value != "" && elm.value != null){
		document.forms[formName].submit();
	} else{
		return false;
	}
}

// Google WebFont Loader
WebFontConfig = {
	google: { families: [ 'Anton' ] }
	};
(function() {
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
		'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
})();


// Featured Artists Slideshow
$(document).ready(function() {
	var onPause = false;
	$('#artistSlideshow').cycle({
		fx:     'scrollHorz',
		prev:   '#prevButton',
		next:   '#nextButton',
		timeout: 6000,
		speed: 2000
	});
	$('#pauseButton').click(function() { 
		if (onPause == false){
			$('#artistSlideshow').cycle('pause');
			$("#pauseButton").attr("src", "images/upcoming-controls-pause-on.png");
			onPause = true;
		} else {
			$('#artistSlideshow').cycle('resume', true); 
			$("#pauseButton").attr("src", "images/upcoming-controls-pause.png");
			onPause = false;
		}
	});
});

// these are labels for the days of the week
cal_days_labels = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];

// these are human-readable month name labels, in order
cal_months_labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

// these are the days of the week for each month, in order
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// this is the current date
var cal_current_date = new Date();

function Calendar(month, year) {
  this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
  this.year  = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
}

Calendar.prototype.generateHTML = function(){

  // get first day of month
  var firstDay = new Date(this.year, this.month, 1);
  var startingDay = firstDay.getDay();
  
  // find number of days in month
  var monthLength = cal_days_in_month[this.month];
  
  // compensate for leap year
  if (this.month == 1) { // February only!
    if((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
      monthLength = 29;
    }
  }

  // check if we are displaying the current month
  checkCurrentMonth = (cal_current_date.getMonth() == this.month && cal_current_date.getFullYear() == this.year) ? true : false;
  
  // do the header
  var monthName = cal_months_labels[this.month]
  var html = '<table class="calendar-table" cellpadding="1" cellspacing="1">';
  html += '<tr class="calendar-header-month">';
  html += '<td><a href="javascript:cal.prevMonth()">&#171;</a></td>';
  html += '<td colspan="5"><a href="events.php?month=' + (this.month + 1) + '&year=' + this.year + '">' + monthName + ' ' + this.year + '</a></td>';
  html += '<td><a href="javascript:cal.nextMonth()">&#187;</a></td>';
  html += '</tr>';
  html += '<tr class="calendar-header-day">';
  for(var i = 0; i <= 6; i++ ){
    html += '<td>' + cal_days_labels[i] + '</td>';
  }
  html += '</tr><tr>';

  // fill in the days
  var day = 1;
  // this loop is for is weeks (rows)
  for (var i = 0; i < 9; i++) {
    // this loop is for weekdays (cells)
    for (var j = 0; j <= 6; j++) { 
      html += '<td class="calendar-day">';
      if (day <= monthLength && (i > 0 || j >= startingDay)) {
        ts = this.generateTimestamp(day);
        if (checkCurrentMonth == true && day == cal_current_date.getDate())
          html += '<a class="today" href="events.php?date=' + ts + '">' + day + '</a>';
        else
          html += '<a href="events.php?date=' + ts + '">' + day + '</a>';
        day++;
      }
      html += '</td>';
    }
    // stop making rows if we've run out of days
    if (day > monthLength) {
      break;
    } else {
      html += '</tr><tr>';
    }
  }
  html += '</tr></table>';
  document.getElementById('calendar-holder').innerHTML = html;
}

Calendar.prototype.generateTimestamp = function(day) {
  month = this.month + 1;
  month_str = '' + month;
  if (month_str.length < 2) {
    month_str = '0' + month_str;
  }
  day_str = '' + day;
  if (day_str.length < 2) {
    day_str = '0' + day_str;
  }
  return this.year + "-" + month_str + "-" + day_str;
}

Calendar.prototype.prevMonth = function() {
  if (this.month == 0) {
    this.month = 12;
    this.year = (this.year - 1);
  }
  this.month = (this.month - 1);
  this.generateHTML();
}

Calendar.prototype.nextMonth = function() {
  if (this.month == 11) {
    this.month = -1;
    this.year = (this.year + 1);
  }
  this.month = (this.month + 1);
  this.generateHTML();
}
