/**
 * jCalendar 0.5
 *
 * Some code based on jQuery Date Picker (http://kelvinluck.com/assets/jquery/datePicker/)
 *
 * Copyright (c) 2007 Theodore Serbinski (http://tedserbinski.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */ 
 
jQuery.jcalendar = function() {
	var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	var days = ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'];
	var navLinks = {p:'Anterior', n:'Próximo', t:'Hoje'};
	var _firstDayOfWeek;
	var _firstDate;
	var _lastDate;
	var _selectedDate;
	
// preencher o primeiro vetor abaixo com a data, no formato dd/m/aaaa e o segundo com o link correspondente (na mesma posição, a primeira data vai ter o primeiro link, a segunda vai ter o segundo link, etc...)

//ficar atento à sintaxe: dados entre aspas simples, virgula entre dados, não colocar virgula no fim do último dado
	
	
var eventos = [
	
'15/2/2011', //1
'16/2/2011', //2

'29/3/2011', //3
'30/3/2011', //4
'31/3/2011', //5

//'1/4/2011', //6

'4/4/2011', //7
'5/4/2011', //8
'6/4/2011', //9
'7/4/2011', //10
'8/4/2011', //11

'25/4/2011', //14
'26/4/2011', //15
//'27/4/2011', //16
//'28/4/2011', //17
//'29/4/2011', //18

//'1/5/2011',  //19

//'2/5/2011',  //20

'9/5/2011',  //21
'10/5/2011', //22
'11/5/2011', //23
'12/5/2011', //24
'13/5/2011', //25

'16/5/2011', //26
'17/5/2011', //27
'18/5/2011', //28
'19/5/2011', //29
'20/5/2011', //30

'24/5/2011', //31
'25/5/2011', //32

'31/5/2011', //33  
'1/6/2011',  //34
'2/6/2011',  //35
'3/6/2011',  //36
'4/6/2011',  //37
'5/6/2011',  //38

//'13/6/2011', //39
'14/6/2011', //40
'15/6/2011', //41
'16/6/2011', //42

'22/6/2011', //43
'23/6/2011', //44
'24/6/2011', //45

'27/6/2011', //46
'28/6/2011', //47
'29/6/2011', //48
'30/6/2011', //49
'1/7/2011',  //50

//'6/7/2011', //51
'7/7/2011', //52

//'14/7/2011', //53
//'15/7/2011', //54

'25/7/2011', //55
'26/7/2011', //56
'27/7/2011', //57
'28/7/2011', //58
'29/7/2011', //59

//'1/8/2011', //60

'9/8/2011', //61
'10/8/2011', //62
'11/8/2011', //63

//'1/9/2011', //64

'22/9/2011', //65
'23/9/2011', //66

//'1/10/2011', //67
//'2/10/2011', //68

'18/10/2011', //69
'19/10/2011', //70
'20/10/2011', //71

'24/10/2011', //72

//'1/11/2011', //73

'23/11/2011', //74
'24/11/2011', //75
'25/11/2011', //76

//'1/12/2011',  //77

'15/12/2011' //78

				   ];
				   
	var links = [
	
/*


*/	
	
'http://www.incit.com.br/incit/Pagina.do?idSecao=250', //1
'http://www.incit.com.br/incit/Pagina.do?idSecao=250', //2

'http://www.incit.com.br/incit/Pagina.do?idSecao=251', //3
'http://www.incit.com.br/incit/Pagina.do?idSecao=251', //4
'http://www.incit.com.br/incit/Pagina.do?idSecao=251', //5

//'http://www.incit.com.br/incit/Pagina.do?idSecao=253', //6

'http://www.incit.com.br/incit/Pagina.do?idSecao=252', //7
'http://www.incit.com.br/incit/Pagina.do?idSecao=252', //8
'http://www.incit.com.br/incit/Pagina.do?idSecao=252', //9
'http://www.incit.com.br/incit/Pagina.do?idSecao=252', //10
'http://www.incit.com.br/incit/Pagina.do?idSecao=252', //11

'http://www.incit.com.br/incit/Pagina.do?idSecao=253', //14
'http://www.incit.com.br/incit/Pagina.do?idSecao=253', //15

//'http://www.incit.com.br/incit/Pagina.do?idSecao=254', //16
//'http://www.incit.com.br/incit/Pagina.do?idSecao=254', //17
//'http://www.incit.com.br/incit/Pagina.do?idSecao=254', //18

//'http://www.incit.com.br/incit/Pagina.do?idSecao=258', //19

//'http://www.incit.com.br/incit/Pagina.do?idSecao=259', //20

'http://www.incit.com.br/incit/Pagina.do?idSecao=255', //21
'http://www.incit.com.br/incit/Pagina.do?idSecao=255', //22
'http://www.incit.com.br/incit/Pagina.do?idSecao=255', //23
'http://www.incit.com.br/incit/Pagina.do?idSecao=255', //24
'http://www.incit.com.br/incit/Pagina.do?idSecao=255', //25

'http://www.incit.com.br/incit/Pagina.do?idSecao=256', //26
'http://www.incit.com.br/incit/Pagina.do?idSecao=256', //27
'http://www.incit.com.br/incit/Pagina.do?idSecao=256', //28
'http://www.incit.com.br/incit/Pagina.do?idSecao=256', //29
'http://www.incit.com.br/incit/Pagina.do?idSecao=256', //30

'http://www.incit.com.br/incit/Pagina.do?idSecao=258', //31
'http://www.incit.com.br/incit/Pagina.do?idSecao=258', //32

'http://www.incit.com.br/incit/Pagina.do?idSecao=257', //33
'http://www.incit.com.br/incit/Pagina.do?idSecao=257', //34
'http://www.incit.com.br/incit/Pagina.do?idSecao=257', //35
'http://www.incit.com.br/incit/Pagina.do?idSecao=257', //36
'http://www.incit.com.br/incit/Pagina.do?idSecao=257', //37
'http://www.incit.com.br/incit/Pagina.do?idSecao=257', //38

//'http://www.incit.com.br/incit/Pagina.do?idSecao=260', //39
'http://www.incit.com.br/incit/Pagina.do?idSecao=259', //40
'http://www.incit.com.br/incit/Pagina.do?idSecao=259', //41
'http://www.incit.com.br/incit/Pagina.do?idSecao=259', //42

'http://www.incit.com.br/incit/Pagina.do?idSecao=260', //43
'http://www.incit.com.br/incit/Pagina.do?idSecao=260', //44
'http://www.incit.com.br/incit/Pagina.do?idSecao=260', //45

'http://www.incit.com.br/incit/Pagina.do?idSecao=261', //46
'http://www.incit.com.br/incit/Pagina.do?idSecao=261', //47
'http://www.incit.com.br/incit/Pagina.do?idSecao=261', //48
'http://www.incit.com.br/incit/Pagina.do?idSecao=261', //49
'http://www.incit.com.br/incit/Pagina.do?idSecao=261', //50

//'http://www.incit.com.br/incit/Pagina.do?idSecao=263', //51
'http://www.incit.com.br/incit/Pagina.do?idSecao=264', //52

//'http://www.incit.com.br/incit/Pagina.do?idSecao=263', //53
//'http://www.incit.com.br/incit/Pagina.do?idSecao=263', //54

'http://www.incit.com.br/incit/Pagina.do?idSecao=262', //55
'http://www.incit.com.br/incit/Pagina.do?idSecao=262', //56
'http://www.incit.com.br/incit/Pagina.do?idSecao=262', //57
'http://www.incit.com.br/incit/Pagina.do?idSecao=263', //58
'http://www.incit.com.br/incit/Pagina.do?idSecao=263', //59

//'http://www.incit.com.br/incit/Pagina.do?idSecao=265', //60

'http://www.incit.com.br/incit/Pagina.do?idSecao=265', //61
'http://www.incit.com.br/incit/Pagina.do?idSecao=265', //62
'http://www.incit.com.br/incit/Pagina.do?idSecao=265', //63

//'http://www.incit.com.br/incit/Pagina.do?idSecao=266', //64

'http://www.incit.com.br/incit/Pagina.do?idSecao=266', //65
'http://www.incit.com.br/incit/Pagina.do?idSecao=266', //66

//'http://www.incit.com.br/incit/Pagina.do?idSecao=267', //67
//'http://www.incit.com.br/incit/Pagina.do?idSecao=268', //68

'http://www.incit.com.br/incit/Pagina.do?idSecao=267', //69
'http://www.incit.com.br/incit/Pagina.do?idSecao=267', //70
'http://www.incit.com.br/incit/Pagina.do?idSecao=267', //71

'http://www.incit.com.br/incit/Pagina.do?idSecao=268', //72

//'http://www.incit.com.br/incit/Pagina.do?idSecao=269', //73

'http://www.incit.com.br/incit/Pagina.do?idSecao=269', //74
'http://www.incit.com.br/incit/Pagina.do?idSecao=269', //75
'http://www.incit.com.br/incit/Pagina.do?idSecao=269', //76

//'http://www.incit.com.br/incit/Pagina.do?idSecao=270', //77

'http://www.incit.com.br/incit/Pagina.do?idSecao=270' //78

/*

*/
				 ]

	var _drawCalendar = function(dateIn, a, day, month, year) {
	  var today = new Date();
	  var d;

		if (dateIn == undefined) {
			// start from this month.
			d = new Date(today.getFullYear(), today.getMonth(), 1);
			year.val(today.getFullYear());
			month.val(today.getMonth()+1);
			day.val(today.getDate());
		}
		else {
			// start from the passed in date
			d = dateIn;
		  d.setDate(1);
		}

		// check that date is within allowed limits
		if ((d.getMonth() < _firstDate.getMonth() && d.getFullYear() == _firstDate.getFullYear()) || d.getFullYear() < _firstDate.getFullYear()) {
			d = new Date(_firstDate.getFullYear(), _firstDate.getMonth(), 1);
		}
		else if ((d.getMonth() > _lastDate.getMonth() && d.getFullYear() == _lastDate.getFullYear()) || d.getFullYear() > _lastDate.getFullYear()) {
			d = new Date(_lastDate.getFullYear(), _lastDate.getMonth(), 1);
		}

		var firstMonth = true;
		var firstDate = _firstDate.getDate();

		// create prev and next links
		if (!(d.getMonth() == _firstDate.getMonth() && d.getFullYear() == _firstDate.getFullYear())) {
			// not in first display month so show a previous link
			firstMonth = false;
			var lastMonth = d.getMonth() == 0 ? new Date(d.getFullYear()-1, 11, 1) : new Date(d.getFullYear(), d.getMonth()-1, 1);
			var prevLink = jQuery('<a href="#1" class="link-prev">&lsaquo; '+ navLinks.p +'</a>').click(function() {
				jQuery.jcalendar.changeMonth(lastMonth, this, day, month, year);
				colocarlinks();			
			});
		}

		var finalMonth = true;
		var lastDate = _lastDate.getDate();

		if (!(d.getMonth() == _lastDate.getMonth() && d.getFullYear() == _lastDate.getFullYear())) {
			// in the last month - no next link
			finalMonth = false;
			var nextMonth = new Date(d.getFullYear(), d.getMonth()+1, 1);
			var nextLink = jQuery('<a href="#1" class="link-next">'+ navLinks.n +' &rsaquo;</a>').click(function() {
				jQuery.jcalendar.changeMonth(nextMonth, this, day, month, year);
				colocarlinks();
			});
		}

		var todayLink = jQuery('<a href="#1" class="link-today">'+ navLinks.t +'</a>').click(function() {
			day.val(today.getDate());
			jQuery.jcalendar.changeMonth(today, this, day, month, year);
			colocarlinks();
		});

    // update the year and month select boxes
  	year.val(d.getFullYear());
  	month.val(d.getMonth()+1);

		var headRow = jQuery("<tr></tr>");
		for (var i=_firstDayOfWeek; i<_firstDayOfWeek+7; i++) {
			var weekday = i%7;
			var wordday = days[weekday];
			headRow.append('<th scope="col" abbr="'+ wordday +'" title="'+ wordday +'" class="'+ (weekday == 0 || weekday == 6 ? 'weekend' : 'weekday') +'">'+ wordday +'</th>');
		}
		headRow = jQuery("<thead></thead>").append(headRow);

		var tBody = jQuery("<tbody></tbody>");
		var lastDay = (new Date(d.getFullYear(), d.getMonth()+1, 0)).getDate();
		var curDay = _firstDayOfWeek - d.getDay();
		if (curDay > 0) curDay -= 7;

		var todayDate = today.getDate();
		var thisMonth = d.getMonth() == today.getMonth() && d.getFullYear() == today.getFullYear();

    // render calendar
		do {
 		  var thisRow = jQuery("<tr></tr>");
  		for (var i=0; i<7; i++) {
  			var weekday = (_firstDayOfWeek + i) % 7;
  			var atts = {'class':(weekday == 0 || weekday == 6 ? 'weekend ' : 'weekday ')};

  			if (curDay < 0 || curDay >= lastDay) {
  				dayStr = ' ';
  			}
  			else if (firstMonth && curDay < firstDate-1) {
  				dayStr = curDay+1;
  				atts['class'] += 'inactive';
  			}
  			else if (finalMonth && curDay > lastDate-1) {
  				dayStr = curDay+1;
  				atts['class'] += 'inactive';
  			}
  			else {
				
				d.setDate(curDay+1);

  				// attach a click handler to every day to select it if clicked
  				// we use the rel attribute to keep track of the day that is being clicked
				
  				dayStr = jQuery('<a href="" rel="'+ d +'">'+ (curDay+1) +'</a>').click(function(e) {
					if (_selectedDate) {
					   _selectedDate.removeClass('selected');
					}
						_selectedDate = jQuery(this);
						_selectedDate.addClass('selected');
					day.val(new Date(_selectedDate.attr('rel')).getDate());
			
  				});

  				// highlight the current selected day
  				if (day.val() == d.getDate()) {
  				  _selectedDate = dayStr;
  				  _selectedDate.addClass('selected');
  				}
  				
  				for (h=0; h<= eventos.length -1 ; h++) {
  					dataaux= eventos[h].split("/");
					if ( dataaux[0] == d.getDate() && dataaux[1] == d.getMonth()+1 && dataaux[2] == d.getFullYear() ) {
						jQuery(dayStr).attr('href',links[h]+'');
						jQuery(dayStr).addClass('evento');
					}else{
					
						if ( jQuery(dayStr).attr('href') == '' ) {
						
							jQuery(dayStr).attr('href','#1');
							
						}
					}
				}
				
  			}

  			if (thisMonth && curDay+1 == todayDate) {
  				atts['class'] += 'today';
  			}
  			thisRow.append(jQuery("<td></td>").attr(atts).append(dayStr));
  			curDay++;
      }

			tBody.append(thisRow);
		} while (curDay < lastDay);

		jQuery('div.jcalendar').html('<table cellspacing="1"></table><div class="jcalendar-links"></div>');
		jQuery('div.jcalendar table').append(headRow, tBody);
		jQuery('div.jcalendar > div.jcalendar-links').append(prevLink, todayLink, nextLink);
	};

	return {
		show: function(a, day, month, year) {
 			_firstDate = a._startDate;
			_lastDate = a._endDate;
			_firstDayOfWeek = a._firstDayOfWeek;

			// pass in the selected form date if one was set
			var selected;
			if (year.val() > 0 && month.val() > 0 && day.val() > 0) {
			  selected = new Date(year.val(), month.val()-1, day.val());
			}
			else {
			  selected = null;
			}
			_drawCalendar(selected, a, day, month, year);
		},
		changeMonth: function(d, e, day, month, year) {
			_drawCalendar(d, e, day, month, year);
		},
		/**
		* Function: setLanguageStrings
		*
		* Allows you to localise the calendar by passing in relevant text for the english strings in the plugin.
		*
		* Arguments:
		* days		-	Array, e.g. ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
		* months	-	Array, e.g. ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
		* navLinks	-	Object, e.g. {p:'Prev', n:'Next', c:'Close', b:'Choose date'}
		**/
		setLanguageStrings: function(aDays, aMonths, aNavLinks) {
			days = aDays;
			months = aMonths;
			navLinks = aNavLinks;
		},
		/**
		* Function: setDateWindow
		*
		* Used internally to set the start and end dates for a given date select
		*
		* Arguments:
		* i			-	The id of the INPUT element this date window is for
		* w			-	The date window - an object containing startDate and endDate properties
		*				e.g. {startDate:'24-11-1981', endDate:'25-12-2012}
		**/
		setDateWindow: function(i, w, year) {
			if (w == undefined) w = {};
			if (w.startDate == undefined) {
				// set the minimum browseable date equal to January of the min year in the select box
				// don't get the first option because that is an empty year

				// note we can't do this: year.find('option:eq(1)').val()
				// it doesn't work in 1.0 since find() is destructive
				// so we copy the object to a new var
				i._startDate = new Date(jQuery(year).find('option:eq(1)').val(), 0, 1);
			}
			else {
  			dateParts = w.startDate.split('-');
  			i._startDate = new Date(dateParts[2], Number(dateParts[1])-1, Number(dateParts[0]));
			}
			if (w.endDate == undefined) {
			  // set the maximum browseable date equal to December of the max year in the select box

			  // note we can't do this: year.find('option:last').val()
				// it doesn't work in 1.0 since find() is destructive
				// so we copy the object to a new var
				i._endDate = new Date(jQuery(year).find('option:last').val(), 11, 1);
			}
			else {
  			dateParts = w.endDate.split('-');
  			i._endDate = new Date(dateParts[2], Number(dateParts[1])-1, Number(dateParts[0]));
			}
			i._firstDayOfWeek = w.firstDayOfWeek == undefined ? 0 : w.firstDayOfWeek;
		}
	};
}();

jQuery.fn.jcalendar = function(a) {
	this.each(function() {
    var day = jQuery(this).find('select.jcalendar-select-day');
    var month = jQuery(this).find('select.jcalendar-select-month');
    var year = jQuery(this).find('select.jcalendar-select-year');
    jQuery('div.jcalendar-selects').after('<div class="jcalendar"></div>');
		jQuery.jcalendar.setDateWindow(this, a, year);
		jQuery.jcalendar.show(this, day, month, year);

		day.change(function() {
		  // only if a valid day is selected
		  if (this.value > 0) {
		    d = new Date(year.val(), month.val()-1, this.value);
  	    jQuery.jcalendar.changeMonth(d, a, day, month, year);
  	  }
		});

		month.change(function() {
		  // only if a valid month is selected
		  if (this.value > 0) {
		    d = new Date(year.val(), this.value-1, 1);
  	    jQuery.jcalendar.changeMonth(d, a, day, month, year);
  	  }
		});

		year.change(function() {
		  // only if a valid year is selected
		  if (this.value > 0) {
  		  d = new Date(this.value, month.val()-1, 1);
    	  jQuery.jcalendar.changeMonth(d, a, day, month, year);
    	}
		});

	});
	return this;
};
