var defaultFontSize = 78;

var counter = 0;
var requestMonth = 0;
var requestYear = 0;

var calendar;
var yearMonth;

dojo.addOnLoad(function() {
	font.init();
	//user_weather_zip.init();

	dojo.require('dijit._Calendar');
	dojo.require('dojo.date.locale');
	calendar = new dijit._Calendar({
			onValueSelected: function(value) {
				var today = new Date(value);
				var month = today.getMonth()+1;
				var year = today.getYear();
				var day = today.getDate();
				if (day < 10) day = '0' + day;
				if (month < 10) month = '0' + month;
				if (year < 1000) year += 1900;
				var strDate = year + '-' + month + '-' + day;

				window.location.href = config.baseurl + 'calendar/list/date/' + strDate;
			},
			isDisabledDate: function(date) {
				return false;
			},
			getClassForDate: function(date) {
				var date = new Date(date);
				var day = date.getDate();
				var month = date.getMonth()+1;
				var year = date.getYear();
				if (year < 1000) year += 1900;

				if (counter == 0) {
					if (day > 1) {
						if (month < 12) {
							month++;
						} else {
							month = 1;
							year++;
						}
					}
					
					var yearMonth = year.toString() + month.toString();

					if (typeof arrEventDays[yearMonth] == "undefined") {
						arrEventDays[yearMonth] = new Array;
						getEventDays(year, month);
						requestMonth = month;
						requestYear = year;
					}
				}

				if (counter == 41) counter = 0;
				else counter++;
				
				var yearMonth = year.toString() + month.toString();
				
				if (typeof arrEventDays[yearMonth] != "undefined" && typeof arrEventDays[yearMonth][day] != "undefined") {
					return 'hasEvent';
				}

			}
		}, dojo.byId("calendar_div"));
	
	
});

function getEventDays(year, month) {
	dojo.xhrGet({
		url: config.baseurl + 'calendar/ajaxcall/type/event_days/date/' + year + '-' + month,
		content: '',
		handleAs: 'json',
		handle: function(data,ioArgs){
			var date = requestYear.toString() + requestMonth;
			arrEventDays[date] = data;
			calendar.setValue(new Date(requestYear, requestMonth-1, 1));
		},
		error: function(response, ioArgs) {
			alert('Error. HTTP status code: ', ioArgs.xhr.status);
			return response;
		}
	});
}

var user_weather_zip = {
	strLeer: 'PLZ eingeben...',
	objInputZip: null,
	
	init: function() {
			user_weather_zip.objInputZip = dojo.byId('user_zip');
			dojo.connect(user_weather_zip.objInputZip, 'onfocus', user_weather_zip.input_focus);
			dojo.connect(user_weather_zip.objInputZip, 'onblur', user_weather_zip.input_blur);
			dojo.connect(dojo.byId('user_zip_form'), 'onsubmit', user_weather_zip.save_zip);
			
			dojo.require('dijit.Tooltip');
			
			var node = document.createElement('span');
			node.innerHTML = "Geben Sie hier bitte Ihre PLZ ein und best&auml;tigen Sie mit der Enter-Taste.";
			var tooltip = new dijit.Tooltip({ connectId: ['user_zip'] }, node);
			tooltip.startup();
		},

	input_focus: function() {
			if (user_weather_zip.objInputZip.value == user_weather_zip.strLeer) {
					user_weather_zip.objInputZip.value = '';
			}
		},
	
	input_blur: function() {
			if (user_weather_zip.objInputZip.value == '') {
					user_weather_zip.objInputZip.value = user_weather_zip.strLeer;
			}
		},
		
	save_zip: function() {
			createCookie('adress_zip', user_weather_zip.objInputZip.value, 365);
		}
};

//Fontsize
var font = {	
	currentSize: null,
	
	init: function() {
		
			var cookie = readCookie("fontSize");
			font.currentSize = cookie?cookie:defaultFontSize;
			font.setSize(font.currentSize);
		},
		
	changeSize: function(sizeDifference) {
			font.currentSize = parseInt(font.currentSize) + parseInt(sizeDifference * 5);

			if(font.currentSize > 100){
				font.currentSize = 100;
			} else if (font.currentSize < 70) {
				font.currentSize = 70;
			}
			font.setSize(font.currentSize);
		
			createCookie("fontSize", font.currentSize, 365);
		},
		
	setSize: function(fontSize) {
			document.body.style.fontSize = fontSize + '%';
		},
	
	revertSize: function() {
		font.currentSize = defaultFontSize;
		font.changeSize(0);
	}
	
}

//Cookie

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];

		while (c.charAt(0)==' ')
			c = c.substring(1,c.length);

		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}