/**
 * @author nd
 */
var Floor = new Class({

	// describe the state of the footer, true = footer is opened and emotion images are displayed
	floorState: true,
	
	// describe the state of the Atomticly hidden status of the footer
	autoHideState: false,
	
	// list of switcher effects
	switcherFX: [],
	switcherItems: [],
	
	// directory containing the toggle images
	switcherImageDir: 'fileadmin/templates/alexgastro/images/emotions/',

	// array containing toggle image files
	switcherImages: {
		'left' : {
			'up': 'toggle_left_up',
			'down': 'toggle_left_dn'
		},
		'right' : {
			'up': 'toggle_right_up',
			'down': 'toggle_right_dn'
		}
	},
 	
	// Use GIFs for IE and PNGs for all other Engines
	switcherImageExt: window.ie?'.gif':'.png',
	
	options: {
		onStart: Class.empty,
		onComplete: Class.empty,
		contentDiv: 'content_wrapper',
		floorDiv: 'floor',
		furnishingItemClass: 'item',
		switcherId: 'toggle'
	},

	initialize: function(element, options){
		this.element = element;
		this.setOptions(options);

		this.addSwitcher();
	},

	start: function() {
		this.fireEvent('onStart', this.element, 10);
		this.callChain();
	},

	finish: function() {
		this.fireEvent('onComplete', this.element, 10);
	},

	fetchFurnishingItems: function() {
		this.furnishingItems = $$('#' + this.options.floorDiv + ' .'+this.options.furnishingItemClass);
	},

	toogle: function(){
		if (this.floorState){
			this.chain( this.start );
			this.chain( this.hideSwitcher );
			this.chain( this.hideFurnishing );
			this.chain( function(){ this.scaleFx(-100); }.bind(this) );
			this.chain( function(){ this.floorState = false; this.callChain(); }.bind(this) );
			this.chain( this.showSwitcher );
			this.chain( this.finish );
		} else {
			this.chain( this.start );
			this.chain( this.hideSwitcher );
			this.chain( function(){ this.scaleFx(100); }.bind(this) );
			this.chain( this.showFurnishing );
			this.chain( function(){ this.floorState = true; this.callChain(); }.bind(this) );
			this.chain( this.showSwitcher );
			this.chain( this.finish );
		}
		// Call the first Chain Element
		this.callChain();

	},

	scaleFx: function(scale){

		
		var scaleContent = new Fx.Styles(this.options.contentDiv, {duration: 1000});
		var positionBottom = $(this.options.contentDiv).getStyle('bottom').toInt();

		var scaleFloor = new Fx.Styles(this.options.floorDiv, {duration: 1000});
		var floorHeight = $(this.options.floorDiv).getStyle('height').toInt();

		scaleContent.start({
	  		'bottom': [positionBottom, positionBottom+scale]
		}).chain(
			 function(){
				// if IE6 the height of the content_wrapper div must set explicit
			 	if (window.ie6){
					var ie6ContentWrapperHeight = $(this.options.contentDiv).getSize().size.y - scale;
					$(this.options.contentDiv).setStyle('height', ie6ContentWrapperHeight);	
				}
			 }.bind(this)
		);
		


		scaleFloor.start({
	  		'height': [floorHeight, floorHeight+scale]
		}).chain( function(){
			this.callChain();
		}.bind(this));

	},

	hideFurnishing: function() {
		this.fetchFurnishingItems();
		this.furnishingItems.forEach(
			function(el){
				var hide = new Fx.Style(el,'opacity',{'duration':500});
				hide.start(0).chain(
					function(){
						el.getParent().setStyle('display', 'none');
					}.bind(this)
				);
			}
		);
		this.callChain();
	},

	showFurnishing: function() {
		this.fetchFurnishingItems();
		this.furnishingItems.forEach(
			function(el){
				el.getParent().setStyle('display', 'block');
				var show = new Fx.Style(el,'opacity',{'duration':500,'wait':true});
				show.start(1);
			}
		);
		this.callChain();
	},

	addSwitcher: function() {

		var switchers = [];
		switchers[0] = new Element( 'a', {
			'class': this.options.switcherId,
			'href': '#'
		});
		var hideImage = new Element( 'img', {
			'src': this.switcherImageDir+this.switcherImages.left.down+this.switcherImageExt,
			'id': 'switch_floor_down_left',
			'width': '38',
			'height': '25',
			'alt': 'hide emotions',
			'title': 'hide emotions'
		});

		var showImage = new Element( 'img', {
			'src': this.switcherImageDir+this.switcherImages.left.up+this.switcherImageExt,
			'id': 'switch_floor_up_left',
			'width': '38',
			'height': '25',
			'alt': 'show emotions',
			'title': 'show emotions'
		});

		hideImage.setStyle('opacity', '0.5');
		hideImage.injectInside(switchers[0]);

		showImage.setStyle('opacity', '0.5');
		showImage.injectInside(switchers[0]);

		switchers[1] = switchers[0].clone();
		switchers[1].addClass('right');

		switchers[1].firstChild.setProperty( 'src', this.switcherImageDir+this.switcherImages.right.down+this.switcherImageExt );
		switchers[1].firstChild.setProperty( 'id', 'switch_floor_down_right' );
		switchers[1].firstChild.getNext().setProperty( 'src', this.switcherImageDir+this.switcherImages.right.up+this.switcherImageExt );
		switchers[1].firstChild.getNext().setProperty( 'id', 'switch_floor_up_right' );

		var i = 0;
		switchers.forEach(
			function(item,index) {
				item.injectInside($('floor'));
				item.addEvent('click', function(e) {
					e = new Event(e);
					this.toogle();
					e.stop();
				}.bind(this));
				this.switcherFX[i] = new Fx.Slide(item.firstChild,{'duration':250,'mode':'horizontal'});
				this.switcherItems[i] = item.firstChild;
				i++;
				this.switcherFX[i] = new Fx.Slide(item.firstChild.getNext(),{'duration':250,'mode':'horizontal'}).hide();
				this.switcherItems[i] = item.firstChild.getNext();
				i++;
			},
			this
		);

	},

	hideSwitcher: function () {
		this.switcherFX.forEach(
			function(item,index) {
				var mode = "";
				if (!this.floorState) {
					mode = 'up';
				} else {
					mode = 'down';
				}
				if (item.element.id.contains(mode)) {
					this.switcherItems[index].setStyle( "display", "block" );
					item.toggle();
				} else {
					this.switcherItems[index].setStyle( "display", "none" );
				}
			},
			this
		);
		this.callChain();
	},

	showSwitcher: function(){
		this.switcherFX.forEach(
			function(item,index) {
				var mode = "";
				if (!this.floorState) {
					mode = 'up';
				} else {
					mode = 'down';
				}
				if (item.element.id.contains(mode)) {
					this.switcherItems[index].setStyle( "display", "block" );
					item.toggle();
				} else {
					this.switcherItems[index].setStyle( "display", "none" );
				}
			},
			this
		);
		this.callChain();
	},

	getTimendEmotions: function () {
		// Check if at the Floor are TimendEmotions
		var timeZone = clientTimeZone.getZone();
		
		// Get all right timend imgage objects from all timend emotion objects
		var css_statement = '.' + par.furnishing.timendEmoClass + ' .' + timeZone;
		var timendEmoObj = $ES( css_statement, par.furnishingId );
		
		// Get Parameter if a Obj. was found
		if (timendEmoObj.length > 0) {
			timendEmoObj.each(function(timendObj){
				// Get content inside of the current timendObj
					var imgURL  = $E(".imgurl",     timendObj).getText();
					var linkURL = $E(".linkurl",    timendObj).getText();
					var posLeft = $E(".posleft",    timendObj).getText();
					var posTop  = $E(".postop",     timendObj).getText();
					var reflex  = $E(".reflection", timendObj).getText();
				// Create IMG out of Parameter
					this.createEmoObj(timendObj.getParent(), timeZone, "/"+imgURL, linkURL, posLeft, posTop, reflex);
			}.bind(this));
		}
	},

	createEmoObj: function(domHook, timeZone, imgURL, linkURL, posLeft, posTop, reflex) {
		
		// Clean old Parameter Content
		domHook.empty();
		// Add new Image
		var imgObj = new Asset.image(imgURL, {
			onload: function(){
				
				// Add Link into Parent
				var emoLink = new Element('a', {
					'href': linkURL,
					'class': 'emoLink'
				}).injectInside(domHook);
				
				// Add IMG into Link
				var emoIMG = new Element('img', {
					'src':   imgURL,
					'class': reflex,
					'alt':   par.furnishing.altText + timeZone ,
					'title': clientTimeZone.getTimedEmotionTitle()
				}).injectInside(emoLink);

				if (window.ie6 && imgURL.test("png")) {
					emoIMG = this.fixFooterPNG(emoIMG, imgURL, reflex, par.furnishing.altText + timeZone, clientTimeZone.getTimedEmotionTitle());
				}

				// Set Styles for IMG
				emoIMG.setStyles({
				   left: posLeft,
				   top: posTop
				});
				if (linkURL.test("void")) {
					emoLink.setStyle('cursor','default');
				}
				
			}.bind(this)
		});
	},
	
	fixFooterPNG: function(imgOBJ, imgURL, imgClass, imgALT, imgTITLE){
		var imgSizeWidth  = imgOBJ.offsetWidth;
		var imgSizeHeight = imgOBJ.offsetHeight;
		var imgParent = imgOBJ.getParent();
			imgParent.empty();
		imgOBJ = new Element('div', {
			'class': 'iePNGwrap',
			'styles': {
				'width':	  imgSizeWidth,
				'height': 	  imgSizeHeight,
				'background': 'none',
				'filter':  	  'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="crop", src="'+ imgURL + '")',
				'display':	  'block',
				'position':	  'absolute'
			}
		}).injectInside(imgParent);
		var dummyIMG = new Element('img', {
			'src':    '/clear.gif',
			'class':  imgClass,
			'alt':    imgALT,
			'title':  imgTITLE,
			'width':  imgSizeWidth,
			'height': imgSizeHeight
		}).injectInside(imgOBJ);
		return imgOBJ;
	},

	controlAutoHide: function() {
		// Check if the floor should be automaticly hidden now
		var ifAutoHide = ( $ES( '.autoHideObj', $(par.contentId) ).length > 0 );
		if (!ifAutoHide && this.getAutoHide()) {
			this.undoAutoHide();
		} else if (ifAutoHide && !this.getAutoHide()) {
			this.setAutoHide();
		}
/*
// If AutoHide found and viewed with FireFox with FireBug installed
if (ifAutoHide && window.gecko && typeof(console) == 'object') {
	console.info("AutoHide FCE found on this page!");
}
*/
	},

	getAutoHide: function() {
		return this.autoHideState;
	},


	setAutoHide: function() {
		// If Floor is Visible, hide it now
		if (!this.autoHideState) {
			// Hide the Floor
			this.toogle();
			// Set Autohide Flag
			this.autoHideState = true;
		}
	},

	undoAutoHide: function() {
		// If Floor is Hidden via the AutoHide Func. ReShowit
		if (this.autoHideState) {
			// Show the Floor
			this.toogle();
			// UnSet Autohide Flag
			this.autoHideState = false;
		}
	}

});

Floor.implement( new Events()  );
Floor.implement( new Options() );
Floor.implement( new Chain()   );