HOMISSMELogoController = new Class({

	// DOM references


	initialize: function(version) {

		this.currentLogoType;

		this.logoTypes = {
			normal: {
				url: "img/logo/logo_HOMISSME.png",
				transitionOut: "img/logo/HOMISSME_exploding.gif",
				width: 462,
				height: 120,
				dismissed: false,
				dismissable: true
			},
			ivy: {
				url: "img/logo/logo_ivy.gif",
				width: 428,
				height: 200,
				dismissed: false,
				dismissable: false
			}
		};

		// setTimeout reference
		this.timer;
		this.HOMISSMELogo = $('HOMISSME');

		// Events
		this.HOMISSMELogo.getElement("img").addEvent('load', function() {
			this.HOMISSMELogo.show();

			// fade back up without the onComplete Handler
			if (!this.currentLogoType.dismissed) {
				this.HOMISSMELogoFX.start({
					opacity: [0, 1]
				});
			}

		}.bind(this));

		// add click event listeners
		this.HOMISSMELogo.getElement('img').addEvent('click',
			function(event) {
				if (!this.currentLogoType.dismissed) {
					this.transitionOut();
				} else {
					event.target.hide();
				}

				if (this.currentLogoType.dismissable) {
					this.currentLogoType.dismissed = true;
				}
			}.bind(this)
		);
		
		// prevent accidental dragging of HOMISSME logo
		this.HOMISSMELogo.getElement("img").addEvent('mousedown', function(event) {
			event.preventDefault();
		});

		// MooTools FX
		this.HOMISSMELogoFX = new Fx.Morph(this.HOMISSMELogo, {
			duration: 1000,
			frameSkip: true,
			transition: Fx.Transitions.Quart.easeOut,
			link: 'cancel'
		});

		// version control
		if (version == 'light') { }
	},

	/**
	 * Listen for scene changes from Director
	 *
	 * @param $scene
	 */

	// TODO: no need to pass in the entire scene, just the name / CONSTANT ?

	showInitial: function() {
		this.currentLogoType = this.logoTypes.normal;

		this.transitionIn();
	},

	PresentScene: function($scene) {
		switch ($scene.name) {

			case "Archive":
				// fade out existing logo
				this.fadeOut();

				break;

			case "Feature":
				// fade out existing logo
				this.fadeOut();

				break;

			case "Covers":
				// swap logo
				this.transitionLogos(this.logoTypes.ivy);

				break;

			case "Wall":
				// swap logo
				this.transitionLogos(this.logoTypes.normal);

				break;

			case "Post":
				// don't do anything with the logo
				break;

			case "Search":
				// fade out existing logo
				this.fadeOut();

				break;

			default:
				// fade out existing logo
				this.transitionLogos();

				break;
		}
	},

	/**
	 * This will transition out any logo currently on the DOM and fade in a new one (if one has been specified in this.currentLogoType)
	 * TODO: it may be worth having this receive an argument, rather than relying on the state of thiscurrentLogoType
	 */

	transitionLogos: function($newLogoType) {

		// TODO: make this DRY
		if ($newLogoType != this.currentLogoType &&
			$newLogoType != undefined) {

			// set logo type
			this.currentLogoType = $newLogoType;

			// check logo visibility
			if ( this.HOMISSMELogo.isDisplayed() ) {
				// fade it out and replace on complete
				this.HOMISSMELogoFX.start({
					'opacity':0.0
				}).chain(function() {
					this.transitionIn();
				}.bind(this));
			} else {
				// previous logo is removed from the DOM
				// transition in new logo (replace src - if necessary - and fade in)

				this.transitionIn();
			}

		} else {

			// the logo hasn't changed
//			this.transitionOut();

			this.HOMISSMELogo.setStyles({
				display: "block"
			});

			if (this.currentLogoType) {
				// lets fade it back in
				if (!this.currentLogoType.dismissed) {
					this.HOMISSMELogoFX.start({
						'opacity': 1.0
					});
				} else {
					this.HOMISSMELogo.setStyles({
						display: "none"
					});
				}
			}
		}
	},

	fadeOut: function() {
		// fade out logo and remove on complete
		this.HOMISSMELogoFX.start({
			'opacity':0.0
		}).chain(function() {
			this.HOMISSMELogo.hide();
		}.bind(this));
	},

	// TODO: only change src if we need to
	transitionIn: function() {

	//	console.log("TRANSITION INNNN");

		// clear previous timer
		clearTimeout(this.timer);

		// update img src if it's changed
		var targetImgSrc = this.currentLogoType.url;
//		var currentImgSrc = this.HOMISSMELogo.getElement("img").getProperty('src');

		this.HOMISSMELogo.getElement("img").setProperty('src', targetImgSrc);

	//	console.log(this.HOMISSMELogo);

		// Hide and center the logo
		this.HOMISSMELogo.hide();
		this.HOMISSMELogo.setStyles({
			opacity: 0.0
		});
		this.HOMISSMELogo.setStyles({
			'margin-left': - this.currentLogoType.width / 2,
			'margin-top': - this.currentLogoType.height / 2
		});
	},

	transitionOut: function() {
		switch(this.currentLogoType) {

			case (this.logoTypes.normal):

				// swap with exploding GIF
				this.HOMISSMELogo.getElement("img").setProperty('src', this.currentLogoType.transitionOut);

				// hide logo after 1 second (or the end of the GIF animation)
				this.timer = (function(){
					this.HOMISSMELogo.setStyles({
						display: "none"
					})
				}).delay(1000, this);

				break;

			case (this.logoTypes.ivy):
				// fade out logo and remove on complete
				this.HOMISSMELogoFX.start({
					'opacity':0.0
				}).chain(function() {
					this.HOMISSMELogo.hide();
				}.bind(this));

				break;

			default:
				break;
		}
	}
});
