var TimelineController = new Class({

	initialize: function()
	{
		this._init();
	},
	
	_init: function()
	{
		this.isOpen = false;
		this.swfInited = false;
		
		Dumper.log("[timeline] _init");
		this.body = document.getElement("body");
		
		this.container = {};
		this.container.width = 0;
		this.container.height = 0;
		this.container.element = new Element("div", { id: "timeline" }).inject(this.body);
		
		this.barcont = {}
		this.barcont.isUp = false;
		this.barcont.element = new Element("div").inject(this.container.element);
		this.barcont.element.addClass("barcont");
		this.barcont.element.set('tween', { duration: 'short' });

		this.bar = {}
		this.bar.element = new Element("div").inject(this.barcont.element);
		this.bar.element.setOpacity(.9);
		this.bar.element.addClass("bar");
				
		this.button = {}
		this.button.element = new Element("a").inject(this.barcont.element);
		this.button.element.addClass("button");
		this.button.element.addEvent("click", this.open.bind(this));
		
		this.swfcont = {}
		this.swfcont.element = new Element("div").inject(this.container.element);
		this.swfcont.element.addClass("swfcont");
		
		window.addEvent("resize", this._onWindowResize.bind(this));
		window.addEvent("mousemove", this._onMouseMove.bind(this));
		
		this._refreshContainerStyles();	
		this._checkDocumentHash.delay(2000, this);
		//this._checkDocumentHash();
	},
	
	_checkDocumentHash: function() 
	{
		if(document.location.hash.indexOf("timeline")>=0)
			this.open();
		//else if (document.location.hash.indexOf("timeline")==-1 && this.isOpen == true)
		//	this.close();
	},
	
	_onMouseMove: function(e)
	{
		if(this.isOpen) return;
		var pos = parseInt(this.barcont.element.getStyle("top"));
		var enabled = pos == -79 || pos == -50;
		if(!enabled) return;
		var event = new Event(e);
		if((this.container.height - event.client.y)<150) 
		{
			this.barcont.element.tween("top", -79);	
		} else if(pos == -79) {
			this.barcont.element.tween("top", -50);	
		}
	},
	
	_initSwf: function()
	{
		Dumper.log("[timeline] _initSwf");
		this.swf = new Swiff('/be/public/inc/swf/timeline.swf', {
		    id: 'timelineswf',
		    name: 'timelinename',
		    container: this.swfcont.element,
		    width: "100%",
		    height: "100%",
		    params: {
		    	wMode: "opaque",
		        bgcolor: '#131313'
		    },
		    vars: {
		        xml: "/be/servizi/flash_timeline.cache.xml.php",
		        overlay: true
		    }
		});
		this.swfInited = true;
	},
	
	_onWindowResize: function()
	{
		Dumper.log("[timeline] _onWindowResize");
		this._refreshContainerStyles();	
	},
	
	_refreshContainerStyles: function()
	{
		Dumper.log("[timeline] _refreshContainerSize");	
		var size = window.getSize();
		this.container.width = size.x;
		this.container.height = size.y;
		this.swfcont.element.setStyle("height", size.y);
		if(this.isOpen == false) this.container.element.setStyle("top", this.container.height);
		this.container.element.setStyle("height", this.container.height);
	},
	
	_onTweenComplete: function()
	{
		if(this.isOpen) 
		{
			if(this.swfInited) 	$("timelineswf").openTimeline();
			else 				this._initSwf();
		}
	},
	
	open: function()
	{
		if(this.isOpen) return true;
		Dumper.log("[timeline] open");
		this.isOpen = true;
		//document.location.hash = '/timeline/';
		this.container.element.set("tween", { onComplete: this._onTweenComplete.bind(this) } );
		this.container.element.tween("top", 0);
		window.removeEvent("mousemove", this._onMouseMove.bind(this));
		return true;
	},
	
	close: function()
	{
		if(!this.isOpen) return true;
		Dumper.log("[timeline] close");
		this.isOpen = false;
		//document.location.hash = '';
		this.barcont.element.setStyle("top", -50);
		this.container.element.tween("top", this.container.height);
		window.addEvent("mousemove", this._onMouseMove.bind(this));
		return true;
	},
	
	getFlash: function()
	{
		return $("timelineswf");
	}
	
});

SWFAddress['_swfChange'] = function() 
{
	console.log("la mia");
	var flash = document.timeline.getFlash();
	var value = SWFAddress.getValue();
	flash.setSWFAddressValue(value);
}


var Dumper = {
	containerId : "dump",
	enabled : false,

	log : function() {
		if (Dumper.enabled) {
			try {
				var subj = arguments.length == 1 ? arguments[0] : arguments;
				console.log(subj);
			} catch (err) {
			}
		}
	},

	dir : function() {
		if (Dumper.enabled) {
			try {
				var subj = arguments.length == 1 ? arguments[0] : arguments;
				console.dir(subj);
			} catch (err) {
			}
		}
	},

	add : function($var) {
		try {
			var p = new Element("p");
			p.setText($var);
			$(this.containerId).appendChild(p);
		} catch (err) {
		}
	}
}
