// create cookie
if (Cookie.read('size') == null) {
	Cookie.write('size', 'full', { duration: false });
}

var FlashInterface = new Class({

	initialize: function() {
		
		// get header div
		this.header = $('header');
		
		// create and append elements
		this.btnResize  = new Element('a', {
			'id': 'btnResize',
			'html': 'speichern &amp; einklappen'
		});
		this.btnResize.addEvent('click', this.resizeFlash.bind(this));
		this.btnSound  = new Element('a', {
			'id': 'btnSound',
			'html': 'Ton ausschalten'
		});
		this.btnSound.addEvent('click', this.toggleSound.bind(this));
		this.btnSound.inject(this.header);
		if (Cookie.read('size') != null) this.btnResize.inject(this.header);
		if (Cookie.read('size') == null) this.btnSound.setStyle('right','20px');

		// set flash vars
		if (Cookie.read('size') == 'small') this.headerState = 1;
		if (Cookie.read('size') == 'full') this.headerState = 0;
		
		// make functions accessible for flash. feels dirty :'(
		window.resizeFlash = this.resizeFlash.bind(this);
		window.switchSound = this.switchSound.bind(this);
		window.toggleSoundBtn = this.toggleSoundBtn.bind(this);
		window.flashVariables = this.flashVariables.bind(this);
		window.flashData = this.flashData.bind(this);

	},
	
	prepare: function() {
		
		// create morph FX
		this.headerTween = new Fx.Tween($('flashHeader'), {
			duration: 400,
			transition: Fx.Transitions.Sine.easeOut
		});
		
		// set initial header state
		if (Cookie.read('size') == 'small') {
			if (Browser.Engine.trident) $('flashHeader').style.height = '130px';
			else this.headerTween.set('height','130px');
			this.header.addClass('small');
			this.btnResize.addClass('active');
			this.btnResize.set('html','ausklappen');
		}

	},
	
	resizeFlash: function() {

		if (this.header.hasClass('small')) {
			if (Browser.Engine.trident) $('flashHeader').style.height = '330px';
			else this.headerTween.start('height','330px');
			this.btnResize.set('html','speichern &amp; einklappen');
			Cookie.write('size', 'full');
			this.headerState = 0;
		}
		 
		if (!this.header.hasClass('small')) {
			if (Browser.Engine.trident) $('flashHeader').style.height = '130px';
			else this.headerTween.start('height','130px');
			this.btnResize.set('html','ausklappen');
			Cookie.write('size', 'small');
			this.headerState = 1;
		}
		
		this.btnResize.toggleClass('active');
		this.header.toggleClass('small');
		
		// send header state to flash movie
		$('flashHeader').toggleHeader(this.headerState);

	},

	toggleSound: function() {
		
		if (this.btnSound.hasClass('active')) {
			this.btnSound.set('html','Ton ausschalten');
			this.soundState = 0;
		}
	
		if (!this.btnSound.hasClass('active')) {
			this.btnSound.set('html','Ton einschalten');
			this.soundState = 1;
		}
	
		this.btnSound.toggleClass('active');
		
		// send sound state to flash movie
		$('flashHeader').toggleSound(this.soundState);

	},
	
	switchSound: function(sw) {
		
		if (sw == 'off') {
			this.btnSound.set('html','Ton einschalten');
			this.soundState = 1;
			this.btnSound.addClass('active');
		}
		
		if (sw == 'on') {
			this.btnSound.set('html','Ton ausschalten');
			this.soundState = 0;
			this.btnSound.removeClass('active');
		}
		
		// send sound state to flash movie
		$('flashHeader').toggleSound(this.soundState);
		
	},
	
	toggleSoundBtn: function(soundStateFromFlash) {
		
		if (soundStateFromFlash == 0) {
			this.btnSound.set('html','Ton ausschalten');
			this.btnSound.removeClass('active');
		}
		
		if (soundStateFromFlash == 1) {
			this.btnSound.set('html','Ton einschalten');
			this.btnSound.addClass('active');
		}

	},
	
	flashData: function(data) {
		
		var externalModuleLinks = $$('.sendToLightbox.data');

		// save initial href values
		if (!this.initialLinks) {
			this.initialLinks = [];
			for (var i=0; i < externalModuleLinks.length; i++) {
				this.initialLinks.push(externalModuleLinks[i].href);
			};
		}

		// add data from flash to href
		for (var i=0; i < externalModuleLinks.length; i++) {
			externalModuleLinks[i].href = this.initialLinks[i] + data;
		};

	},
			
	flashVariables: function() {
		return { 'headerState': this.headerState };
	}

});