dojo.provide("Z2h.Slide.Contest");

dojo.require("dojo.fx");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("Z2h.Slide");

dojo.declare("Z2h.Slide.Contest", [Z2h.Slide], {
	templateString: null,
	templatePath: dojo.moduleUrl("Z2h","Slide/templates/Contest.html"),
	widgetsInTemplate : true,
	
	/**
	 * Public variables
	 */
	author_avatar: '',
	author_url: '',
	author_username: '',
	author_level: '',
	author_title: '',
	author_last_online: '',
	comment: '',
	video_points: '',
	video_url: '',
	video_title: '',
	video_file: '',
	video_thumb: '/flash/history/history_vid.jpg',
	position: '',
	
	/**
	 * Private variables
	 */
	_currentlyShown: false,
	_swfPlayer: null,
	_videoPlayerId: '',
	_slideshow: null,

	/**
	 * Methods
	 */
	postCreate: function(){
		if ( this.position == '1' ){
			dojo.attr(this.videoWinner,'innerHTML',"Winner: ");
		} else {
			dojo.attr(this.videoWinner,'innerHTML',"Runner Up: ");
		}
	},
	
	show: function(slideshow){
		this._videoPlayerId = this.videoPlayerContainer.id;
		if ( swfobject.hasFlashPlayerVersion("9.0.0") ){
			var attributes = { data: "/flash/player.swf", width: "520", height: "312" };
			var parameters = { 
					flashvars: "file="+this.video_file[0]+"&bufferlength=1&image="+this.video_thumb+"&name="+this._videoPlayerId,
					"wmode" : "opaque",
					"allowFullScreen" : "true",
					"allowscriptaccess" : "always",
					"swliveconnect":"true"
			};
			this._swfPlayer = swfobject.createSWF(attributes,parameters,this._videoPlayerId);
			setTimeout(dojo.hitch(this,'playerReady'),10);
			this._currentlyShown = true;
		}
	},

    playerStarted: function(obj){
            setTimeout(dojo.hitch(this,'checkPlayerRunning'),50);
    },

    checkPlayerRunning: function(){
            var player = dojo.byId(this._videoPlayerId);
            if ( player.getConfig && player.getConfig().state != "IDLE" ){
                    this._slideshow.clearIntervalToNext();
            }
    },

	
	playerReady: function(){
		player = dojo.byId(this._videoPlayerId);
		if ( player.addViewListener ){
			dojo.subscribe("Z2h.slideshow.playerStarted", dojo.hitch(this,'playerStarted'));
			player.addViewListener("PLAY","Z2h.slideshow.playerStarted");
		} else {
			setTimeout(dojo.hitch(this,'playerReady'),10);
		}
	},
	
	hide: function(){
		if ( this._swfPlayer ) {
			this._swfPlayer = null;
			this._currentlyShown = false;
			swfobject.removeSWF(this._videoPlayerId);
			var playerContainer = dojo.create('div');
			dojo.attr(playerContainer,'id',this._videoPlayerId);
			var imgPlaceholder = dojo.create('img');
			dojo.attr(imgPlaceholder,'src',this.video_thumb);
			dojo.attr(imgPlaceholder,'alt','Hot Pink Shorts');
			dojo.place(imgPlaceholder,playerContainer,'last');
			dojo.place(playerContainer,this.videoPlayerDiv,'last');
		}
	}
	
});
