// <![CDATA[

var PlayerWidget = Class.create(
{
	initialize: function()
	{

		if(typeof swfobject == 'undefined')
		{
			return;
		}
		else
		{
			var bg     = this.createBg();
			var videos = $$('a.video');
			this.redraw();

			//this.player = swfobject.createSWF(att, par, pane.id);


//			swfobject.embedSWF("/flash/player_flv_maxi.swf", pane.id, "320", "240", "9.0.0", "/scripts/lib/swfobject/expressInstall.swf", null, {movie: '/flash/player_flv_maxi.swf', wmode: 'transparent'});
			videos.each(function(v)
			{
				v.observe('click', function(event){
					event.stop();

					if(this.bg.visible())
					{
						this.stopFLV();
					}
					else
					{
						this.playFLV(v.href);
					}
				}.bind(this));
			}.bind(this));
		}
	},

	togglePlayer: function()
	{
		if(this.bg.visible())
		{
			this.bg.fade({duration:.2});
		}
		else
		{
			this.bg.appear({duration: .2, from:0, to: .8});
		}
	},

	createBg: function()
	{
		this.bg = new Element('div', {id: 'ACOFI-player_bg'});

		$(document.body).insert({bottom: this.bg.hide()});

		this.bg.observe('window:resize', this.redraw.bind(this));
		this.bg.observe('click', this.stopFLV.bind(this));

		return this.bg;
	},

	createPane: function()
	{
		this.pane = new Element('div', {id: 'ACOFI-player_pane'});
		this.anim = new Element('div', {id: 'ACOFI-player_anim'});
		this.anim.update('<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>');

		$(document.body).insert({bottom: this.pane.insert(this.anim)});
		this.anim.setStyle({width: '480px', height:'360px'});
		this.redraw();

		return this.anim;
	},

	removePane: function()
	{
		this.pane.remove();
	},

	redraw: function()
	{
		var scr = document.viewport.getDimensions();

		this.bg.setStyle({position: 'fixed', zIndex: 10000, top: '0px', left: '0px', width: scr.width + 'px', height: scr.height + 'px', backgroundColor: '#000000', opacity: .8});
		if(this.pane)
		{
			this.pane.setStyle({position: 'fixed', zIndex: 10001, width: '480px', height: '360px', left: ((scr.width-480)/2) + 'px', top: ((scr.height-360)/2) + 'px'});
		}
	},

	playFLV: function(filename)
	{
        var rand = new Date();
        var att  = {id: 'playerMovie', name: 'playerMovie'};
        var par  = {swliveconnect: 'true', wmode: 'transparent'};
        var vars = {
        	flv: filename + '?time=' + rand.getTime(),
        	width: 480,
        	height: 360,
        	autoplay: 1,
        	autoload: 1,
        	buffermessage: '',
        	showvolume: 1,
        	showtime: 1,
        	showplayer: 'autohide',
        	showfullscreen: 1
        };

        new Ajax.Request('/stats/add-view', {
            parameters: {
                filename: filename
            },
            onSuccess: function(transport){

                var success = transport.responseJSON.success || false;

                if(true == success)
                {
                    this.createPane();

                    this.togglePlayer();

                    swfobject.embedSWF("/flash/player_flv_maxi.swf", this.anim.id, "480", "360", "9.0.16.60", "/scripts/lib/swfobject/expressInstall.swf", vars, par, att);
                }

            }.bind(this)
        });
	},

	stopFLV: function()
	{
		swfobject.removeSWF(this.anim.id);
		this.removePane();
		this.togglePlayer();
	}
});

var VideoInfoWidget = Class.create({
   initialize: function()
   {
        var infos  = $$('#demarquage-playlist-infos div.video-info');
            infos.invoke('hide');

        var videos   = $$('#demarquage-playlist-videos a.video');
        var updating = false;

        videos.each(function(v, i){
            v.observe('mouseover', function(){

                for(j=0, len=infos.length;j<len; j++)
                {
                    if(j == i && !infos[j].visible())
                    {
                        pos = v.cumulativeOffset();
                        infos[j].setStyle({
                            'top' : pos.top  - 138 + 'px',
                            'left': pos.left - 49  + 'px'
                        }).appear({duration:.5});
                    }
                }

           });

           v.observe('mouseout', function(){

                infos.invoke('hide');

           });

        });
   }
});

var EditoWidget = Class.create({
	initialize: function()
	{
		if(!$('edito-content'))
		{
			return false;
		}

		var preview   = $('edito-preview');
		var content   = $('edito-content').hide();
		var open_cmd  = $('edito-cmd-open');
		var close_cmd = $('edito-cmd-close').hide();

		open_cmd.observe('click', function(event){
			event.stop();

			content.blindDown({
				duration   :.5,
				fps: 50,
				beforeStart: function(){
					open_cmd.hide();
					close_cmd.show();
				}
			});
		});

		close_cmd.observe('click', function(event){
			event.stop();
			$('topContainer').scrollTo();
			content.hide();
			close_cmd.hide();
			open_cmd.show();
			/*
			content.blindUp({
				duration:.25,
				fps:50,
				beforeStart: function(){
					$('topContainer').scrollTo();
				},
				afterFinish: function(){
					close_cmd.hide();
					open_cmd.show();
				}
			});
			*/
		});
	}
});

document.observe('dom:loaded', function()
{
	new EditoWidget();
	new PlayerWidget();
        new VideoInfoWidget();

	var vars = {
			flv: 'http://www.acofi.com/medias/anims/anim_cia.swf'
	};
		var att  = {id: 'PEA-immobilier', name: 'PEA-immobilier'};
		var par  = {swliveconnect: 'true', wmode: 'transparent'};

	swfobject.embedSWF("/medias/anims/anim_cia.swf", "PEA-immobilier", "196", "136", "9.0.16.60", "/scripts/lib/swfobject/expressInstall.swf", vars, par, att);
});

// ]]>

