var CONSORTIUM = new Class({
	initialize: function(container, images, accordions, currentAccordion, currentCompany) {
		var _self = this;
		this.imageDir = './images/content/';
		
		if(typeof container == 'object') this.container = container;
		else this.container = $(container.toString());
		this.images = images;
		this.accordions = accordions;
		this.accordion = null;
		this.currentAccordion = currentAccordion;
		this.currentCompany = currentCompany
		
		this.contentWrapper = null;
		this.contentWrapperID = 'consortiumContent';
		this.consortiumContentMinHeight = 430;
		this.imageWrapper = null;
		this.imageWrapperID = 'imageWrapper';
		this.imagePointer = new Array();
		this.imageMap = null;
		this.accordionWrapper = null;
		this.accordionWrapperID = 'accordionWrapper';
		this.detailsWrapper = null;
		this.detailsWrapperID = 'detailsWrapper';
		this.detailsHeadline = null;
		this.detailsHeadlineID = 'detailsHeadline';
		this.buttonClose = null;
		this.detailsText = null;
		this.detailsTextID = 'detailsText';
		
		this.imageFx = null;
		this.detailsFx = null;
		this.detailsFxFadeInTime = 1000;
		this.detailsFxFadeOutTime = 500;
		
		this.contentWrapper = new Element('div', {'id':this.contentWrapperID});
		this.detailsWrapper = new Element('div', {'id':this.detailsWrapperID});
		this.detailsHeadlineWrapper = new Element('div');
		this.detailsHeadline = new Element('div', {'id':this.detailsHeadlineID});
		this.buttonClose = new Element('img', {'src':this.imageDir + 'button-close-slider-off.gif'}).setStyles({
			'cursor':'pointer',
			'height':30,
			'width':30
		});
		this.buttonClose.addEvent('mouseover', function() {
			this.src = _self.imageDir + 'button-close-slider-on.gif';
		});
		this.buttonClose.addEvent('mouseout', function() {
			this.src = _self.imageDir + 'button-close-slider-off.gif';
		});
		this.buttonClose.addEvent('click', function() {
			_self.detailsFx = new Fx.Morph(_self.detailsWrapper, {
				duration:_self.detailsFxFadeOutTime
			}).start({'left':-435});
			(function() { _self.contentWrapper.setStyle('height', _self.consortiumContentMinHeight); }).delay(_self.detailsFxFadeOutTime);
		});
		this.detailsHeadlineWrapper.grab(new Element('div').setStyles({'padding':'10px', 'float':'right'}).grab(this.buttonClose)).grab(this.detailsHeadline)
		this.detailsText = new Element('div', {'id':this.detailsTextID});
		this.detailsWrapper.grab(this.detailsHeadlineWrapper).grab(this.detailsText);
		this.contentWrapper.grab(this.detailsWrapper);
		
		var leftEdge = new Element('div').setStyles({
			'float':'left',
			'height':450,
			'width':12,
			'z-index':20
		}).grab(
			new Element('img', {'src':this.imageDir + 'consortium-details-left-edge.gif'}).setStyles({
				'position':'absolute',
				'z-index':20
			})
		);
		this.contentWrapper.grab(leftEdge);
		
		this.imageWrapper = new Element('div', {'id':this.imageWrapperID});
		this.imageMap = new Element('map', {'name':'consortium-ImageMap'});
		
		for(var i = 0; i < this.images.length; i++) {
			this.imagePointer[i] = new Element('img', {'class':'consortiumImage'}).setStyle('opacity', 0).set('usemap', '#consortium-ImageMap');
			this.imageWrapper.grab(this.imagePointer[i]);
			var area = new Element('area');
			area.set('shape', this.images[i].shape);
			area.set('name', 'areaPolygon');
			area.set('coords', this.images[i].coords);
			area.set('href', '#');
			area.addEvent('click', this.showAccordion.bind(this, [i]));
			this.imageMap.grab(area);
		}
		this.imageWrapper.grab(this.imageMap);
		this.contentWrapper.grab(this.imageWrapper);
		
		this.accordionWrapper = new Element('div', {'id':this.accordionWrapperID});
		for(var i = 0; i < this.accordions.length; i++) {
			//var toggler = new Element('h2', {'id':i, 'class':'toggler'}).set('html', this.accordions[i].headline);
			var toggler = new Element('div', {'id':i, 'class':'toggler'}).grab(new Element('img', {'alt':this.accordions[i].headline, 'src':this.imageDir + 'accordion-headline-' + this.accordions[i].headline.toLowerCase() + '.gif', 'title':this.accordions[i].headline}));
			var elem = new Element('div', {'class':'element'}).setStyle('height', '0px');
			var unorderedList = new Element('ul');
			for(var j = 0; j < this.accordions[i].companies.length; j++) {
				var li = new Element('li').set('html', this.accordions[i].companies[j].headline);
				if(this.accordions[i].companies[j].text != '') {
					li.set('class', 'li-level1-on');
					li.addEvent('mouseover', function() {
						this.set('class', 'li-level1-over');
					});
					li.addEvent('mouseout', function() {
						this.set('class', 'li-level1-on');
					});
					li.addEvent('click', this.showDetails.bind(this, [this.accordions[i].companies[j]]));
				} else {
					li.set('class', 'li-level1-off');
				}
				unorderedList.grab(li);
			}
			elem.grab(unorderedList);
			this.accordionWrapper.grab(toggler).grab(elem);
		}
		
		this.contentWrapper.grab(this.accordionWrapper);
		this.container.grab(this.contentWrapper);
		
		var images = new Array();
		for(var i = 0; i < this.images.length; i++) images.push(this.images[i].src);
		
		this.accordion = new Accordion($$('.toggler'), $$('.element'), {
			duration:500,
			onActive: function(toggler, element) {
				_self.showImage(toggler.id.toInt());
			},
			show:-1
		});
		
		new Asset.images(images, {
			onComplete: function() {
				_self.accordion.display(_self.currentAccordion);
				if(_self.currentCompany != null && 
					_self.accordions[_self.currentAccordion].companies[_self.currentCompany] && 
					_self.accordions[_self.currentAccordion].companies[_self.currentCompany].text != '') {
					(function() {
						_self.showDetails(_self.accordions[_self.currentAccordion].companies[_self.currentCompany]);
					}).delay(500);
				}
			}
		});
	},
	
	showAccordion: function(indx) {
		if(this.currentAccordion != indx) {
			this.accordion.display(indx);
		}
	},
	
	showImage: function(indx) {
		var _self = this;
		this.currentAccordion = indx;
		
		for(var i = 0; i < this.imagePointer.length; i++) {
			if(i == indx) this.imagePointer[i].setStyle('z-index', 10);
			else this.imagePointer[i].setStyle('z-index', 5);
		}
		this.imagePointer[indx].src = this.images[indx].src;
		if(this.imageFx != null) this.imageFx.cancel();
		this.imageFx = new Fx.Morph(this.imagePointer[indx], {duration:500}).start({'opacity':[0, 1]});
		(function() {
			for(var i = 0; i < _self.imagePointer.length; i++) {
				if(i != indx) _self.imagePointer[i].setStyle('opacity', 0);
			}
		}).delay(500);
	},
	
	showDetails: function(company) {
		var _self = this;
		var delayTime = 0;
		if(this.detailsFx != null) this.detailsFx.cancel();
		if(this.detailsWrapper.getStyle('left').toInt() != '-435') {
			delayTime = this.detailsFxFadeOutTime;
			this.detailsFx = new Fx.Morph(this.detailsWrapper, {
				duration:_self.detailsFxFadeOutTime
			}).start({'left':-435});
		}
		
		(function() {
			_self.detailsHeadline.set('html', company.headline);
			_self.detailsText.set('html', company.text);
			_self.contentWrapper.setStyle('height', '');
			var height = _self.detailsWrapper.getHeight();
			if(height > _self.consortiumContentMinHeight) {
				_self.contentWrapper.setStyle('height', height);
			} else {
				_self.contentWrapper.setStyle('height', _self.consortiumContentMinHeight);
				if(Browser.Engine.trident4) _self.detailsWrapper.setStyle('height', _self.consortiumContentMinHeight);
			}
			_self.detailsFx = new Fx.Morph(_self.detailsWrapper, {
				duration:_self.detailsFxFadeInTime
			}).start({'left':12});
		}).delay(delayTime);
	}
});