/**
* SX project specific:
----------------------------------------------------------------------------------------------------*/
var SxSwiff = new Class({
	Implements: Options,
	options: {
		version: null,
		container: 'sxSwiff'
	},
	initialize: function(path,options){
		this.setOptions(options);
		var anchor = window.location.hash.substring(1);
		if(anchor) this.options.vars['anchor'] = anchor;
		this.path = path;
		this.options.container = document.id(this.options.container);
		if(!this.checkVersion()) return this.showAlternative();
		this.options.container.addClass('loading');
		this.inject();
	},
	showAlternative: function(){
		var el = this.getAlternativeContent();
		el.getElement('.js').setStyle('display','none');
		el.inject(this.options.container)
		return true;
	},
	inject: function(){
		this.swiff = new Swiff(this.path, this.options);
		var header = document.id('header');
		header.setStyle('backgroundImage','none');
	},
	checkVersion: function(){
		return this.options.version <= Browser.Plugins.Flash.version
	},
	getAlternativeContent: function(){
		if (!this.alternativeContent) this.alternativeContent = new Element('div',{'html':this.options.container.getElement('noscript').get('text')});
		return this.alternativeContent;
	}
});
/**
* used in application form
*/
var Calendar = new Class({
	Implements: Options,
	globalVars: {
		'titleformat':['j-dt-sp-F-sp-Y'], // 3. März 2010
		'cellformat':['j-dt-sp-F-sp-Y'],
		'finalopacity':80,
		'bespokeTitles':{ // configure official holidays (?)
			'****0101':'Neujahr',
			'****0106':'Heilige Drei Könige',
			'****0501':'Staatsfeiertag',
			'****1026':'Nationalfeiertag',
			'****1225':'Christtag',
			'****1226':'Stephanitag'
		}
	},
	options: {
		'statusFormat':'j-dt-sp-F-sp-Y',
		//'inputFormat':'d-ds-m-ds-Y', // division 4
		'inputFormat':'Y-ds-m-ds-d', // rest of the world
		'showWeeks': true,
		'noFadeEffect':true,
		'rangeLow': false, // yyyymmdd
		'rangeHigh': false, // yyyymmdd
		'notBefore': false, // str elementId
		'notAfter': false // str elementId
	},
	initialize: function(id,options){
		if(typeof datePickerController != 'object') {
			alert('datePickerController is not loaded');
			return;
		}
		if(!datePickerController.globalVarsSet) this.setGlobalVars();
		this.setOptions(options);
		this.element = $(id);
		this.setPickerOptions();
		datePickerController.createDatePicker(this.pickerOptions);
	},
	setGlobalVars: function(){
		$(document.body).addEvent('click',function(){datePickerController.cleanUp();});
		datePickerController.setGlobalVars(this.globalVars);
		datePickerController.globalVarsSet = true;
	},
	setPickerOptions: function(){
		var formElements = {};
		formElements[this.element.id] = this.options.inputFormat;
		var options = {formElements: formElements};
		if(this.options.rangeLow) options['rangeLow'] = this.options.rangeLow;
		if(this.options.rangeHigh) options['rangeHigh'] = this.options.rangeHigh;
		if(this.options.notBefore) {
			var linkedElement = $(this.options.notBefore);
			linkedElement.addEvent('blur',this.setNewRangeLow.bindWithEvent(this));
		}
		if(this.options.notAfter) {
			options['rangeHigh'] = this.getDateString(this.options.notAfter);
			var linkedElement = $(this.options.notAfter);
			linkedElement.addEvent('blur',this.setNewRangeHigh.bindWithEvent(this));
		}
		if(this.options.notBefore || this.options.notAfter) {
			window.addEvent('load',function(){
				linkedElement.fireEvent('blur');
				this.element.fireEvent('blur');
			}.bindWithEvent(this))
		}
		this.pickerOptions = options;
	},
	setNewRangeLow: function(e){
		//console.log('set date range low for ' + this.element.id + ' ' + this.getDateString(this.options.notBefore))
		datePickerController.setRangeLow(this.element.id,this.getDateString(this.options.notBefore));
	},
	setNewRangeHigh: function(e){
		//console.log('set date range high for ' + this.element.id + ' ' + this.getDateString(this.options.notAfter))
		datePickerController.setRangeHigh(this.element.id,this.getDateString(this.options.notAfter));
	},
	getDateString: function(id){
		var dateObj = datePickerController.getSelectedDate(id);
		var dateStr = datePickerController.printFormattedDate(dateObj, 'Y-m-d', 0);
		return dateStr;
	}
});

/**
* used in application form
*/
var WorkPermitToggler = new Class({
	Implements: Options,
	options: {
		toggler: 'person-CITIZENSHIP_ID',
		slidesClass: 'ifNotAt'
	},
	initialize: function(formId,options){
		this.setOptions(options);
		this.form = $(formId);
		this.toggler = this.form.getElement('#' + this.options.toggler);
		this.slides = this.form.getElements('.' + this.options.slidesClass);
		this.initToggler();
		this.toggler.fireEvent('change');
	},
	initToggler: function(){
		this.toggler.addEvent('change',this.toggleSlides.bindWithEvent(this));
	},
	toggleSlides: function(){
		var i = this.toggler.options['selectedIndex'];
		if($(this.toggler.options[i]).hasClass('slideOut')) this.hide();
		else this.show();
	},
	show: function(){
		this.slides.removeClass('hide');
	},
	hide: function(){
		this.slides.addClass('hide');
	}
});
