var CALENDAR_MONTHS = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
var DAYS_OF_WEEK = ["Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"];
var DAYS_OF_MONTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var SHORT_DAYS = [];
var CALENDARS = {};

/*  TIME UNIT'S  */
var SECOND = 1;
var MINUTE = 60 * SECOND;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;
var MONTH = 30 * DAY;
var YEAR = 365 * DAY;

Date.prototype.clone = function()
{
	return new Date(this.getTime());
};

(function($){
	
	var r = function(s, v, r){ return s.split("%" + v).join(r); };
	var z = function(d){ return (d < 10) ? "0" + d : d.toString(); };
	
	var BurnCalendar = function(src)
	{
		var c = src;
		var id = c.attr("id");
		var month = c.find(".month");
		var week = month.find(".week");
		var days = month.find(".days");
		var control = c.find(".control");
		var control_prev = control.find("a.prev");
		var control_next = control.find("a.next");
		var control_month = control.find("strong");
		
		var day_class = "dia";	
		var null_class = "Vazio";
		var day_event_class = "diaCheio";
		var today_class = "diaCorrente";
		
		var defaultPatternDate = "([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})";
		
		var TEMPLATES = {};
		TEMPLATES.week_day = "<td abbr='%d' title='%d'>%sd</td>";
		TEMPLATES.null_day = "<td id='%id' class='%c'>&nbsp;</td>";
		TEMPLATES.month_name = "%m";
		TEMPLATES.day = "<a href='%link' day='d%d' title='%ext'>%d</a>";
		TEMPLATES.link = "#%d/%m/%y";
		
		return {
		
			init: function(d)
			{
				this.id = id;
				this.day_class = day_class;
				this.null_class = null_class;
				this.day_event_class = day_event_class;
				this.today_class = today_class;
				
				this.TEMPLATES = TEMPLATES;
				
				this.event_days = [];
				
				this.now = new Date();
				
				$(DAYS_OF_WEEK).each(function(){ SHORT_DAYS.push(this.substr(0, 1)); });
								
				this.setDate(d);
				
				control_next.attr("id", this.id + "_next").click(function(e){
					
					var id = $(this).attr("id").split("_")[0];
					
					CALENDARS[id].next();
					
					return false;
					
				});
				
				control_prev.attr("id", this.id + "_prev").click(function(e){
					
					var id = $(this).attr("id").split("_")[0];
					
					CALENDARS[id].prev();
					
					return false;
					
				});
				
				CALENDARS[this.id] = this;
				
				return this;
				
			},
			
			create: function()
			{
				this.createWeek();
				this.createMonth();
				
				return this;
			},
			
			configure: function(c)
			{
				for(var i in c)
				{
					if(typeof c != "object")
					{
						this[i] = c;
					}
					else
					{
						this[i] = jQuery.extend(this[i], c[i]);
					}
				}
				
				return this;
			},
			
			get: function(){ return c; },
			
			setEventsDays: function(d, p)
			{
				if(typeof p == "undefined")
				{
					p = defaultPatternDate;
				}
				
				this.event_days = {};
				
				for(var i=0; i<d.length; i++)
				{
					var tempDate = this.baseDate.clone();
					var di = new RegExp(p, "gi").exec(d[i]);
					
					tempDate.setDate(di[1]);
					tempDate.setMonth(new Number(di[2]) - 1);
					tempDate.setFullYear(di[3]);

					this.event_days[this.getDate(tempDate)] = tempDate;
					
					tempDate = null;
				}
				
				this.checkEvents();
				
				return this;
			},
			
			setDate: function(d)
			{
				if(typeof d == "undefined" || d == null || !d)
				{
					this.date = new Date();
				}
				else if(typeof d == "string")
				{
					d = d.split("/");
					this.date = new Date(d[2], new Number(d[1])-1, d[0]);
				}
				else
				{
					this.date = new Date(d[2], new Number(d[1])-1, d[0]);
				}
				
				this.baseDate = this.date.clone();
				
				this.checkEvents();
				
				return this;
			},
			
			getDate: function(d)
			{
				var d = (d || this.date);
				return [z(d.getDate()), z(d.getMonth()+1), d.getFullYear()].join("/");
			},
			
			next: function()
			{
				this.date.setTime(this.date.getTime()+(MONTH*1000));
				this.createMonth();
			},
			
			prev: function()
			{
				this.date.setTime(this.date.getTime()-(MONTH*1000));
				this.createMonth();
			},
			
			checkEvents: function()
			{
				var days_month = days.find("tr td:not(." + this.null_class + ")");
				var tempDate = this.date.clone();
				
				var ed = this.event_days, d = this.getDate;
				days_month.each(function(i){					tempDate.setDate($(this).find("a").html());
					if(typeof ed[d(tempDate)] != "undefined")
					{
						$(this).addClass(day_event_class);
					}
				});

				return this;
			},
			
			createMonth: function()
			{
				this.createCalendar();
				
				control_month.html( r(this.TEMPLATES.month_name, "m", CALENDAR_MONTHS[ this.date.getMonth() ]) );
				
				var temp_date = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
				var total_days = DAYS_OF_MONTHS[temp_date.getMonth()];
				
				if(temp_date.getMonth() == 1)
				{
					total_days = (temp_date.getFullYear() % 4 == 0) ? 29 : 28;
				}
				
				for(var init=temp_date.getDay(), i=init, dm=1; i<(init+total_days); dm++, i++)
				{
					var d = $("#" + this.id + "_d" + i);
					d.removeClass(this.null_class).addClass(this.day_class);
					
					temp_date.setDate(dm);
					
					var link = r(this.TEMPLATES.link, "d", dm);
					link = r(link, "m", temp_date.getMonth()+1);
					link = r(link, "y", temp_date.getFullYear());
					
					var dt = r(this.TEMPLATES.day, "d", dm);
					dt = r(dt, "link", link);
					dt = r(dt, "ext", this.getDate(temp_date));	
					
					if(dm == this.baseDate.getDate() && temp_date.getMonth() == this.baseDate.getMonth() && temp_date.getFullYear() == this.baseDate.getFullYear())
					{
						d.addClass(today_class);
					}
					
					d.html(dt);
				}
				
				this.checkEvents();
			},
			
			createWeek: function()
			{
				var tr = week.html("<tr></tr>").find("tr");
				
				var week_day = this.TEMPLATES.week_day;
				$(SHORT_DAYS).each(function(index){
					var w = r(week_day, "d", DAYS_OF_WEEK[index]);
					w = r(w, "sd", this);
					
					tr.append(w);
				});
			},
			
			createCalendar: function()
			{
				if(days.html() != "")
				{
					days.find("tr td").html("")
						.removeClass(this.today_class)
						.removeClass(this.day_class)
						.removeClass(this.day_event_class)
						.addClass(this.null_class);
						
					return false;
				}
				
				days.html("");
				
				for(var i=0, c=0, tr=null; i<(7*6); c++, i++)
				{
					if(tr == null)
					{
						tr = $("<tr></tr>");
					}

					var d = r(this.TEMPLATES.null_day, "id", this.id + "_d" + i);
					d = r(d, "c", this.null_class);
					tr.append(d);
					
					if(c >= 6)
					{
						days.append(tr);
						tr = null;
						c = -1;
					}
				}
			}
			
		};
		
	};
	
	$.fn.burnCalendar = function(d)
	{
		var id = this.attr("id");
		if(typeof CALENDARS[id] == "undefined")
		{
			var __calendar = new BurnCalendar(this, d);
			__calendar.init();
		}
		else
		{
			var __calendar = CALENDARS[id];
		}
		
		return __calendar;
	};
	
})(jQuery);
/* GZIPED */