/**
 * @author Kozicki Jakub
 * @copyright Kozicki Jakub <kuba.kozicki@gmail.com>
 */

jQuery.noConflict();

var reDate = /(\d+)\-(\d+)\-(\d+)/;

jQuery(document).ready(function(){
		createDataPicker("#input-date-from", "maxDate", "#input-date-to", "#button-date-from");
		createDataPicker("#input-date-to", "minDate", "#input-date-from", "#button-date-to");
	});

function createDataPicker(input, bound, boundContainer, button) {
	var input = jQuery(input);

	input.datepicker();

	input.datepicker('option',
			jQuery.extend({showMonthAfterYear: false},
			jQuery.datepicker.regional['pl'])
	);
	
	input.datepicker('option', {dateFormat: 'yy-mm-dd',
		beforeShow: function(input) {
			var dateMatch = reDate.exec(jQuery(boundContainer).val());
			var date = new Date(dateMatch[1], parseInt(dateMatch[2].replace(/^0+/g)) - 1, parseInt(dateMatch[3].replace(/^0+/g)));
			switch(bound) {
				case "minDate" :
					jQuery(input).datepicker('option', {minDate: date});
					break;
				case "maxDate" :
					jQuery(input).datepicker('option', {maxDate: date});
					break;
			}
		}
	});

	jQuery(button).click(function () { 
		input.datepicker('show');
    });
}

function Calendar(year, month, previous, next, bodyMonth, bodyYear, bodyHead, body, bodyWaiting, urlTemplate) {
	this.months = ["Styczeń", "Luty", "Marzec",  "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"];
	this.year = year;
	this.month = month;
	this.bodyMonth = jQuery(bodyMonth);
	this.bodyYear = jQuery(bodyYear);
	this.bodyHead = jQuery(bodyHead);
	this.body = body;
	this.bodyWaiting = jQuery(bodyWaiting);
	this.urlTemplate = urlTemplate;
	
	jQuery(previous).bind("click", function(event) {
		self.getPreviousMonth();
		event.stopPropagation();
	});
	jQuery(next).bind("click", function(event) {
		self.getNextMonth();
		event.stopPropagation();
	});
	
	var self = this;
}

Calendar.prototype = {
	getNextMonth : function() {
		if(++this.month == 13) {
			this.year++;
			this.month = 1;
		}
		this.getMonth();
	},
	
	getPreviousMonth : function() {
		if(--this.month == 0) {
			this.month = 12;
			this.year--;
		}
		this.getMonth();
	},
	
	getMonth : function() {
		this.waiting(true);
		var self = this;
		jQuery.get(this.urlTemplate.interpolate({year: this.year, month : this.month}), {}, function(html) {
			self.waiting(false);
			self.bodyMonth.text(self.months[self.month - 1]);
			self.bodyYear.text(" " + self.year);
			self.bodyHead.parent().append(html);
		}, 'html');
	},
	
	waiting : function(waiting) {
		if(waiting) {
			this.bodyHead.hide();
			jQuery(this.body).remove();
			this.bodyWaiting.show();
		} else {
			this.bodyWaiting.hide();
			this.bodyHead.show();
		}
	}
}

String.prototype.interpolate = function(object) {
	var str = this;
	for(property in object) {
		str = str.replace('#{'+ property +'}', object[property]);
	}
	return str;
}