/**
 * @author		Patrick Schröder
 * @copyright	2010 DI.NET AG
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 */
function CalendarEditor() {
	this.datenow				= new Date();
	this.month					= this.datenow.getMonth() + 1;
	this.year					= this.datenow.getFullYear();

	this.calendarTypeDefault	= 1;
	this.calendarStartMonth		= 0;
	this.calendarSheetActive	= 0;
	this.calendarSheetScroll	= 0;



	this.init = function() {
		this.setVars();
		this.switchCalendarType(this.calendarTypeDefault);
		this.setMonthScrollButtons();
		this.getSheet();
		this.getPersonalization();
	}


	this.setVars = function() {
		// set 'calendarStartMonth'
		if(this.month >= 12) this.calendarStartMonth = 1;
		else this.calendarStartMonth = (this.month + 1);

		if($("#calendarType").val() >= 1) this.calendarTypeDefault = $("#calendarType").val();
	}


	this.setMonthScrollButtons = function() {
		if(this.calendarSheetScroll >= 12) $('#buttonSwitchSheetMonthNext').css('display', 'none');
		else $('#buttonSwitchSheetMonthNext').css('display', 'block');

		if(this.calendarSheetScroll <= 0) $('#buttonSwitchSheetMonthLast').css('display', 'none');
		else $('#buttonSwitchSheetMonthLast').css('display', 'block');
	}


	this.switchCalendarType = function(type) {
		$("#calendarType-1").css('display', 'none');
		$("#calendarType-2").css('display', 'none');
		$("#calendarType-1_dimensions").css('display', 'none');
		$("#calendarType-2_dimensions").css('display', 'none');

		$("#calendarType-" + type).css('display', 'block');
		$("#calendarType-" + type + "_dimensions").css('display', 'inline');
	}


	this.switchSheetMonth = function(opt) {
		if(opt == 'next') {
			if(this.calendarSheetScroll >= 12) return;

			this.calendarSheetScroll++;
			this.calendarSheetActive++;

			if(this.calendarSheetActive >= 13) this.calendarSheetActive = 1;
			if(this.calendarSheetScroll == 1) this.calendarSheetActive = this.calendarStartMonth;
		}
		if(opt == 'last') {
			if(this.calendarSheetScroll <= 0) return;
			
			this.calendarSheetScroll--;
			this.calendarSheetActive--;

			if(this.calendarSheetActive == 0 && this.calendarSheetScroll != 0) this.calendarSheetActive = 12;
			if(this.calendarSheetScroll == 0) this.calendarSheetActive = 0;
		}

		this.setMonthScrollButtons();
		this.getPersonalization();
		this.getSheet();
	}


	this.getPersonalization = function() {
		// use timestamp for image personalization to avoid cache
		var timestamp = new Date().getTime();

		$(".personalizationImage").attr('src', RELATIVE_KAPPOO_DIR + 'images/sheet/pLoad.jpg');
		$(".personalizationImage").attr('src', RELATIVE_KAPPOO_DIR + "index.php?action=GetImagePersonalization&p=" + this.calendarSheetActive + '&jstime=' + timestamp + SID_ARG_2ND);
	}


	this.getSheet = function() {
		var sheet = "";
		var sheetyear = "";

		// when month is december, then take next year
		if(this.month == 12 && this.calendarStartMonth == 1) {
			sheetyear = (this.year + 1);
		} else
		if(this.calendarSheetActive <= this.month) {
			sheetyear = (this.year + 1);
		} else {
			sheetyear = this.year;
		}

		if(this.calendarSheetActive == 0) {
			if(this.month == 12) {
				sheet = (this.year + 1) + "cover";
			} else {
				sheet = this.year + "-" + (this.year + 1) + "cover";
			}
		} else
		if(this.calendarSheetActive <= 9) {
			sheet = sheetyear + '' + "0" + this.calendarSheetActive;
		} else {
			sheet = sheetyear + '' + this.calendarSheetActive;
		}

		$("#calendarType-1_calendarSheet").css('background-image', 'url(' + RELATIVE_KAPPOO_DIR + 'images/sheet/desk_' + sheet +'.gif)');
		$("#calendarType-2_calendarSheet").css('background-image', 'url(' + RELATIVE_KAPPOO_DIR + 'images/sheet/wall_' + sheet +'.gif)');
	}


	this.submitPreview = function() {
		if($("#forename").val() == "" && $("#surname").val() == "") {
			alert('Bitte Vor- und Nachnamen angeben!');
			return;
		}

		$.ajax({
			async:		false,
			cache:		false,
			type:		'POST',
			url:		RELATIVE_KAPPOO_DIR + 'index.php',
			data:		'form=Preview&forename=' + $("#forename").val() + '&surname=' + $("#surname").val() + SID_ARG_2ND,
			dataType:	'json',
			global:		'false',
			success: function(data) {
				if(data['status'] == true) {
					calendareditor.getPersonalization();
					//window.location.reload();
				}
			},
			error: function() {
				alert('Es ist ein Fehler aufgetreten. Das tut uns leid! :-(');
			}
		});
	}
}

var calendareditor = new CalendarEditor();

$(document).ready(function() {
	calendareditor.init();
});
