/**
 * @author roman
 */
// check dependencies
if(typeof iConcerts == "undefined"){
	alert("ERROR: iConcerts.js must be loaded prior to this File");
}

iConcerts.UI = {
	idCounter: 0,
	scrollBars: new Array(),
	getId: function(){
		return this.idCounter++;
	},
	
	updateScrollBars: function(){
		$A(this.scrollBars).each(function(sb){
			sb.update();
		});
	}
};
iConcerts.UI.ScreenFader = Class.create();
iConcerts.UI.ScreenFader.prototype = {
	initialize: function(element){
		this.element = $(element);
		this._animating = false;
		if(this.element == null){
			//TODO: Error handling
		}
		
		var offs = Position.cumulativeOffset(this.element);
		var wi = this.element.getWidth();
		var he = this.element.getHeight();
		
		var placeholder = document.createElement('div');
		placeholder.setAttribute('id','flashPlayerEmpty');
		
		this.element.up().insertBefore(placeholder,this.element);
		
		this.element.setStyle({
			position:'absolute',
			top:offs[1] + 'px',
			left:offs[0] + 'px',
			zIndex:'3'
		});
	},
	
	fadeIn: function(callback){
		if(!this._animating){
			this._animating = true;
			var node = $(document.createElement('div'));
			this.node = node;
			node.className = 'screenFade';
			var body =  $$('body')[0];
			body.appendChild(node);
			
			new Effect.Fade(node, {from:0, to:0.8, afterFinish:function(effect){
				this._animating = false;
				node.observe('click', this.fadeOut.bind(this));
				if(typeof callback == 'function'){
					this.callback = callback;
					callback(true);
				} else if(typeof this.callback == 'function'){
					this.callback(true);
				}
			}.bind(this)});
		}
	},
	
	fadeOut: function(callback){
		if(!this._animating){
			this._animating = true;
			var node = this.node;
			
			new Effect.Fade(node, {from:0.8, to:0, afterFinish:function(effect){
				this._animating = false;
				node.remove();
				if(typeof callback == 'function'){
					this.callback = callback;
					callback(false);
				} else if(typeof this.callback == 'function'){
					this.callback(false);
				}
			}.bind(this)});
		}
	}
}

iConcerts.UI.ScrollBar = Class.create();
iConcerts.UI.ScrollBar.prototype = {
	initialize: function(element, options){
		this.options = Object.extend({
			trackClass: 'scrollTrack',
			handleClass: 'scrollHandle',
			handleActive: 'scrollHandleActive',
			direction: 'vertical',
			position: 'inside',
			handleMinSize: 10,
			width:null,
			height:null,
			minSize: 20,
			preserveOuterDimensions: false
		}, options || {});
		
		this.element = $(element);
		
		this.scrollbar = $(document.createElement('div'));
		this.scrollbar.className = 'scrollTrack';
		this.scrollbar.setAttribute('id', 'scrollBar' + iConcerts.UI.getId());
		this.handle = $(document.createElement('div'));
		this.handle.className = 'scrollHandle';
		this.handle.setAttribute('id', 'scrollHandle' + iConcerts.UI.getId());
		this.inner = $(document.createElement('div'));
		
		this.inner.innerHTML = this.element.innerHTML;
		this.element.innerHTML = '';
		this.element.appendChild(this.inner);
		this.scrollbar.appendChild(this.handle);
		this.element.appendChild(this.scrollbar);
		this.inner.makePositioned();
		this.element.makePositioned();
		
		this.owi = this.element.getWidth();
		this.ohe = this.element.getHeight();
		this.iwi = this.inner.getWidth();
		this.ihe = this.inner.getHeight();
		this.scwi = this.scrollbar.getWidth();
		
		if(this.options.width != null){
			this.element.setStyle({width: this.options.width + 'px'});
			this.owi = this.options.width;
		}
		if(this.options.height != null){
			this.element.setStyle({height: this.options.height + 'px'});
			this.ohe = this.options.height;
		}
		this.eventObj = this.doWheel.bindAsEventListener(this);
		this.drawScrollbar();
		iConcerts.UI.scrollBars.push(this);
		
	},
	
	drawScrollbar: function(){
		if(this.options.direction == 'vertical'){
			if(this.ihe > this.ohe || this.ohe < this.options.minSize){
				this.scrollbar.show();
				var hsize = Math.round(this.ohe / this.ihe * this.ohe);
				hsize = hsize < this.options.handleMinSize ? this.options.handleMinSize : hsize;
				
				this.scrollbar.setStyle({
					height: this.ohe + 'px',
					cssFloat: 'right'
				});
				
				if(this.options.position == 'inside'){
					this.inner.setStyle({ width: this.owi - this.scwi -10 + "px", cssFloat:'left' });
				} else {
					this.element.setStyle({ width: this.owi + this.scwi + "px" });
					this.inner.setStyle({ width: this.owi + "px", cssFloat:'left'});
				}
				this.handle.setStyle({ height: hsize + 'px '});
				var gap = this.ihe - this.ohe;
				this.scrolltick = 1 / (gap/4);
				
				var pos = 0;
				if(this.slider){
					pos = this.slider.value;
					this.slider.dispose();
				}
				this.slider = new Control.Slider
						(this.handle.id, this.scrollbar.id, {axis:'vertical', minimum:0,maximum:this.ohe});
						
				this.slider.options.onChange = function(val){
					var ptop = Math.round(gap * val);
					this.inner.setStyle({top: -ptop + 'px'});
					this.handle.className = this.options.handleClass;
				}.bind(this);
				
				this.slider.options.onSlide = function(val){
					var ptop = Math.round(gap * val);
					this.inner.setStyle({top: -ptop + 'px'});
					this.handle.className = this.options.handleActive;
				}.bind(this);
				this.slider.setValue(pos);
				Event.observe(this.element, 'mousewheel', this.eventObj);
				Event.observe(this.element, 'DOMMouseScroll', this.eventObj);
			} else {
				this.scrollbar.hide();
				
				if(this.slider){
					this.slider.setValue(0);
					Event.stopObserving(this.element, 'mousewheel',this.eventObj);
					Event.stopObserving(this.element, 'DOMMouseScroll',this.eventObj);
					this.slider.dispose();
					delete this.slider;
				}
			}
		} else {
			if(this.iwi > this.owi || this.owi < this.options.minSize){
				this.scrollbar.show();
				var hsize = Math.round(this.owi / this.iwi * this.owi);
				hsize = hsize < this.options.handleMinSize ? this.options.handleMinSize : hsize;
				this.scrollbar.setStyle({
					height: this.scwi + 'px',
					width: this.owi + 'px',
					left:'0px'
				});
				
				if(this.options.position == 'inside'){
					this.inner.setStyle({ height: this.ohe - this.scwi + "px" });
				} else {
					this.element.setStyle({ height: this.ohe + this.scwi + "px" });
					this.inner.setStyle({ height: this.ohe + "px", overflow:'hidden' });
				}
				this.handle.setStyle({ width: hsize + 'px ', height: this.scwi + 'px'});
				var gap = (this.iwi - this.owi);
				this.scrolltick = 1 / (gap/4);
				
				var pos = 0;
				if(this.slider){
					pos = this.slider.value;
					this.slider.dispose();
				}
				this.slider = new Control.Slider
						(this.handle.id, this.scrollbar.id, {axis:'horizontal', minimum:0,maximum:this.owi});
						
				this.slider.options.onChange = function(val){
					var pleft = Math.round(gap * val);
					this.inner.setStyle({left: -pleft + 'px'});
					this.handle.className = this.options.handleClass;
				}.bind(this);
				
				this.slider.options.onSlide = function(val){
					var pleft = Math.round(gap * val);
					this.inner.setStyle({left: -pleft + 'px'});
					this.handle.className = this.options.handleActive;
				}.bind(this);
				this.slider.setValue(pos);
				Event.observe(this.element, 'mousewheel', this.eventObj);
				Event.observe(this.element, 'DOMMouseScroll', this.eventObj);
			} else {
				this.scrollbar.hide();
				
				if(this.slider){
					this.slider.setValue(0);
					Event.stopObserving(this.element, 'mousewheel', this.eventObj);
					Event.stopObserving(this.element, 'DOMMouseScroll', this.eventObj);
					this.slider.dispose();
					delete this.slider;
				}
			}
		}
	},
	
	update: function(){
		var newiwi = this.inner.getWidth();
		var newihe = this.inner.getHeight();
		var newowi = this.element.getWidth();
		var newohe = this.element.getHeight();
		if(newiwi != this.iwi || newihe != this.ihe || newowi != this.owi || newohe != this.ohe){
			if(newowi < this.options.minSize || newohe < this.options.minSize)
				return;
			
			this.iwi = newiwi;
			this.ihe = newihe;
			if(!this.options.preserveOuterDimensions){
				this.ohe = newohe;
				this.owi = newowi;
			}
			this.drawScrollbar();
		}
	},	
	
	doWheel: function(event){
		Event.stop(event);
		var speed = Event.wheel(event);
		var pos = this.slider.value;
		this.slider.setValue( pos - speed * this.scrolltick);
	},
	
	getInner: function(){
		return this.inner;
	}
		
}