window.addEvent('siteready', function() {
	s.startVideo( document );
} );


siteEngine.prototype.startVideo = function( obj )
{
	var thisObj = this;

	if (!thisObj.popupVideoOverlay)
		thisObj.createPopupVideo();
	
	var objs = obj.getElements('.promo-video');
	for (var i=0; i<objs.length; i++)
	{
		objs[i].importProps();
		objs[i].addEvent('click', thisObj.putVideo.pass(objs[i].get('href'), thisObj) );
		objs[i].addEvent('click', function(e) { new Event(e).stop(); } );
		if (objs[i].hasClass('autoplay'))
			thisObj.putVideo( objs[i].get('href') );
	}
	objs = obj.getElements('.video');
	for (var j=i; j<objs.length; j++)
	{
		objs[j].importProps();
		objs[j].addEvent('click', thisObj.putVideo.pass(objs[j].get('href'), thisObj) );
		objs[j].addEvent('click', function(e) { new Event(e).stop(); } );
		if (objs[j].hasClass('autoplay'))
			thisObj.putVideo( objs[j].get('href') );
	}	
}

siteEngine.prototype.putVideo = function( href )
{
	this.xhr = new Request.HTML().get( href + '?nocache=' + Math.floor(Math.random()*100) );
	this.xhr.addEvent('onComplete', function(response) {
		this.popupVideo.content.setHTML( this.xhr.response.text );
		this.writeVideo( );
	}.bind(this) );

	this.popupVideoOverlay.setStyle('display', 'block');
	this.popupVideo.setStyle('display', 'block');
	
	var scroll = document.getScroll();
	this.popupVideoOverlay.fx.start(.7);
	this.popupVideoOverlay.setStyle('width', document.getScrollSize().x + scroll.x);
	this.popupVideoOverlay.setStyle('height', document.getScrollSize().y + scroll.y);
	this.popupVideo.fx.start(0, 1);
}

siteEngine.prototype.writeVideo = function( )
{
	var box = $('videoplayer');
	var s1 = new SWFObject("../images/mediaplayer.swf","mediaplayer","463","390","7");
	s1.addParam("allowfullscreen","true");
	s1.addVariable("width","463");
	s1.addVariable("height","390");
	s1.addVariable("autostart","true");
	s1.addVariable("file", box.getHTML() );
	//s1.addVariable("image","video.jpg");
	s1.write("videoplayer");
	
	this.startVideo( this.popupVideo.content );
}

siteEngine.prototype.createPopupVideo = function( )
{
	this.popupVideoOverlay = new Element('div', {'class': 'popup-video-overlay'}).setStyles({'position':'absolute', 'overflow': 'hidden', 'left': 0, 'top': 0, 'z-index': 10000, 'display': 'none'}).inject(document.body, 'top');
	this.popupVideo = new Element('div', {'class': 'popup-video'}).setStyles({'position':'absolute', 'z-index': 10001, 'display': 'none'}).inject(document.body, 'top');
	this.popupVideoTop = new Element('div', {'class': 'popup-video-top'}).inject( this.popupVideo );
	this.popupVideoBottom = new Element('div', {'class': 'popup-video-bottom'}).inject( this.popupVideoTop );
	this.popupVideo.content = new Element('div', {'class': 'popup-video-bg'}).inject( this.popupVideoBottom );
	this.popupVideo.close = new Element('a', {'href':'javascript:;', 'class': 'popup-video-close'}).set('html', 'chiudi <strong>X</strong>').inject(this.popupVideoBottom, 'top');
	this.popupVideo.close.addEvent('click', this.closePopupVideo.bind(this) );
	
	this.popupVideoOverlay.fx = new Fx.Tween( this.popupVideoOverlay, 'opacity').set(0);
	this.popupVideo.fx = new Fx.Tween( this.popupVideo, 'opacity').set(0);
	
	if ( (Browser.Engine.name == 'trident') && (Browser.Engine.version < 5) )
	{
		this.popupVideo.iframe = new Element('iframe')
		.setStyles({'display': 'block', 'position':'absolute', 'top':0, 'left':0, 'z-index':-1, 'width':3000, 'height': 3000, 'filter': 'mask()'})
		.inject(this.popupVideoOverlay);
	}
}

siteEngine.prototype.closePopupVideo = function( )
{
	this.popupVideo.fx.start(1,0).chain( function() { this.popupVideoOverlay.fx.start(0); this.popupVideo.content.set('html', ''); }.bind(this) );
	this.wait(0);
}