/**
 * @author kleppe@ubilabs.net
 */

Object.extend(Array.prototype, {
	shuffle: function(){
		return this.sort(function(){return 0.5 - Math.random();}); // pseudo sorting 
	}
});

Object.extend(Event, ly.ical.prototyp.util.message);

/*
 * Search for all .platform classes and assign Actor class.
 */
var cafissimo = {};
cafissimo.Page = Class.create();
Object.extend(cafissimo.Page.prototype, {
	initialize: function(){
		$$('#stage li .platform').shuffle().each(function(element, index){
			if (typeof disableGrow == "undefined" || !disableGrow){
				new Effect.Grow(element, {direction: 'center', duration: 0.3, delay: index*0.1});
			}
			if(!element.parentNode.hasClassName('teaser')) {
				var ratio = String(
					element.classNames().grep(/ratio_/)
				).match(/ratio_(\d)_(\d)/);
				new Actor(element, ratio ? {x: ratio[1], y: ratio[2]} : null);		
			}
		});		
	}
});
Event.observe(window, 'load', function(){ new cafissimo.Page();});

/*
 * This will replace an existing div with a new SWF.
 * 
 * @param div: Element to replace with Flash movie.
 * @param url: Target source reference.
 * @param id: ID for flash movie.
 */
cafissimo.Flash = Class.create();
Object.extend(cafissimo.Flash.prototype, {
	initialize: function(div, url, id){
		this.div = div;
		var swf = new SWFObject(url, id, "100%", "100%", "8");
		swf.addParam("align", "middle");
		swf.addParam("wmode", "transparent");
		this.div.hide();
		new Insertion.Before(this.div, '<div class="flash">' + swf.getSWFHTML() + '</div>');
	},
	remove: function(){
		this.div.show();
		this.div.previous(".flash").remove();		
	}
});

var Actor = Class.create();
Object.extend(Actor.prototype, {
	speed: 0.7,
	initialize:function(element, ratio){
		this.element = element;
		this.parent = this.element.parentNode;
		this.actor = this.element.down('.actor');
		this.link = this.element.down('.click');
		this.image = this.element.down("img");
		
		this.ratio = ratio || {x:2,y:2};
		if (typeof homeGrow == "undefined" || !homeGrow){
			this.ratio.from = this.link.getDimensions();
			this.ratio.from.width = 140;
			this.ratio.from.height = 80;
		} else {
			this.ratio.from = this.link.getDimensions();
			this.ratio.from.width = 140;
			this.ratio.from.height = 80;
		}
		
		this.ratio.to = {
			width: this.ratio.x * (this.ratio.from.width + 2) - 2,
			height: this.ratio.y * (this.ratio.from.height + 2) - 2
		};
		/*
		this.link.observe("click", function(event){
			Event.stop(event);
			Event.element(event).blur();
			this.stopEffect();
			this.act();
		}.bind(this));
		*/
		Event.responders.register({
			onActor_start: function(msg, id){
				if(id != this.parent.id && !this.collapsed){
					this.stopEffect();	
					this.act();
				}
			}.bind(this)
		});
		
		this.collapsed = true;
		this.prepare(element);
	},
	prepare: function(element){
		// override in subclasses
	},
	stopEffect: function(){
		if (this.effect){
			this.effect.cancel();
			this.effect = null;
		}		
	},
	act: function(){
		
		if (!!this.element.classNames().grep(/noscale/).reduce()){
			 if (this.collapsed){
			 	this.grow();
				this.top();	
			 }else {
				this.shrink();
				this.bottom();
			 }
			 this.collapsed = !this.collapsed;
			 return;
		}
		
		var options = {
			queue: {position:'end', scope: 'size_' + this.parent.id, limit:1},
			duration: this.speed
		};
		if (this.collapsed){
			options = Object.extend(options, {
				afterFinish: this.top.bind(this), 
				beforeStart: this.grow.bind(this),
				style: {
					height: this.ratio.to.height + "px", 
					width: this.ratio.to.width + "px"
				}
			});
		} else {
			options = Object.extend(options, {
				beforeStart: this.shrink.bind(this), 
				afterFinish: this.bottom.bind(this),
				style: {
					height: this.ratio.from.height + "px", 
					width: this.ratio.from.width + "px"
				} 
			});
		}
		this.effect = new Effect.Morph(this.actor, options);
		this.collapsed = !this.collapsed;
	},
	depth: function(depth){
		this.parent.setStyle({zIndex:depth});	
	},
	bottom: function(){
		this.depth(100);
		//if (parent.audio) {
		//	parent.audio.location.reload();
		//}
	},
	shrink: function(){
		this.link.show();		
		this.element.removeClassName('ontop');
		this.depth(200);		
		if (this.flash){
			this.flash.remove();
		}	
		Event.send('actor_stop', this.parent.id);
	},
	grow: function(){
		this.link.hide();
		this.element.addClassName('ontop');
		this.depth(300);		
		Event.send('actor_start', this.parent.id);
	},
	top: function(){
		this.link.show();
		this.depth(400);
		if (this.link.rel.match(/\.swf/)){
			this.flash = new cafissimo.Flash(this.image, this.link.rel, "movieplayer_" + this.actor.id);
		}		
	}
});