dojo.provide("Z2h.VotingWidget");

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

dojo.declare("Z2h.VotingWidget", [dijit._Widget, dijit._Templated], {
	templateString: null,
	templatePath: dojo.moduleUrl("Z2h","templates/VotingWidget.html"),
	widgetsInTemplate : false,

	/**
	 * Private variables
	 */
	
	_params: {
		"wmode": "transparent",
		"allowFullScreen": "true",
		"allowscriptaccess":"always"
	},
	_attributes: {},
	_flashvars: {},
	_swf: null,
	_flashID: null,
	_width: "520",
	_height: "140",
	_flashVersion: "9.0.0",
	_expressInstall: "/js/expressInstall.swf",
	
	/**
	 * Public variables
	 */
	sid: "",
	blind: "0",
	vid: "",
	remoteURL: "",
	category: "",
	type: "voting",
	
	
	/**
	 * Methods
	 */
	
	postCreate: function(){
		this.inherited("postCreate",arguments);

		this._flashvars = {
			sid: this.sid,
			blind: this.blind,
			vid: this.vid,
			remoteURL: this.remoteURL,
			category: this.category
		};

		var swfPath = "/flash/contest/";
		
		if ( this.type == "creator" ){
			swfPath = swfPath + "CreatorWidget.swf";
		} else if ( this.type == "nominee" ){
			this._height = "108";
			dojo.style(this.widgetContainer,'height','108px');
			swfPath = swfPath + "NomineeWidget.swf";
		} else {
			swfPath = swfPath + "VotingWidget.swf";
		}
		
		this._flashID = this.flashWidget.id;
		this._swf = swfPath;
		
		swfobject.embedSWF(
				this._swf,
				this._flashID,
				this._width, 
				this._height,
				this._flashVersion,
				this._expressInstall,
				this._flashvars,
				this._params,
				this._attributes
			);
		
		dojo.subscribe("/flash/voting/reload/", dojo.hitch(this,'reloadWidget'));
	},
	
	reloadWidget: function(){
		console.log("widget id: ", this._flashID);
		
		dojo.destroy(this._flashID);
		var div = dojo.create("div");
		dojo.attr(div,'id',this._flashID);
		
		dojo.place(div,this.widgetContainer,'last');
		
		swfobject.embedSWF(
				this._swf,
				this._flashID,
				this._width, 
				this._height,
				this._flashVersion,
				this._expressInstall,
				this._flashvars,
				this._params,
				this._attributes
			);
		
		
	}
});
