var Questions = Class.create();
Questions.prototype = {
	
	initialize: function(ratingID) {
		this.questionRef = {"id":ratingID};
		ajaxRequestor('loadPaneHTML', 'page=questions&rating_id='+ratingID, this.handleGetQuestions.bind(this), this.handleGetQuestions.bind(this));
	},
	
	handleGetQuestions: function(response) {
		$("mid_pane").innerHTML = response.paneHTML;
		Jq("#question_list").show();
		// load answers after question had been loaded
		this.loadAnswerHTML(this.questionRef.id);
		
		if(response.jsrsp) {
			for(x=0;x<response.jsrsp.length;x++) {
				qStr = 'question_id='+response.jsrsp[x];
				ajaxRequestor('getChartData', qStr, this.handleShowChart.bind(this), this.handleShowChart.bind(this));
			}
		}	
		this.registerListeners();
	},
	
	handleShowChart: function(response) {
		if(response.j[0] && response.chartResponse == 'true') {
			YAHOO.widget.Chart.SWFURL = "http://yui.yahooapis.com/2.6.0/build//charts/assets/charts.swf";
			var myDataSource = new YAHOO.util.DataSource( response.j );
			myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
			myDataSource.responseSchema =
			{
				fields: Object.keys(response.j[0])
			};
			var seriesDef = [];
			var keys = Object.keys(response.j[0]);
			for(x=0;x<keys.length;x++) {
				if(keys[x] !== 'date') {
					seriesDef.push({displayName: keys[x], yField: keys[x]});
				}
			}
			var Axis = new YAHOO.widget.NumericAxis();
			Axis.minimum = 0;
		
			var mychart = new YAHOO.widget.LineChart( "chart_"+response.questionID, myDataSource,
			{
				series: seriesDef,
				xField: "date",
				yAxis: Axis,
				//dataTipFunction: getDataTipText,
				//only needed for flash player express install
				expressInstall: "assets/expressinstall.swf",
				style:
				{
					padding:20,
					legend:
					{
						display:"bottom",
						padding: 10,
						spacing: 5,
						font:
						{
							family: "Arial",
							size: 13
						}
					}
				}
			});
			
		} else { 
			$('chart_'+response.questionID).setStyle({height:'20px'});
		}
		
	},
	
	registerListeners: function() {
		Jq("#question_option_backLink").click(this.handleLinkClick.bind(this));	
		Jq(".question_option").click(this.handleLinkClick.bind(this));	
		Jq("#new_question").jqm({onShow:this.registerModalListeners.bind(this)});
		Jq("#create_question_link").click(function(e) {
			Jq("#new_question").jqmShow();
		});
		Jq("#error_window").jqm();
		Jq(".answerInput").focus(this.handleLinkClick.bind(this));
		Jq(".answerInput").blur(this.handleLinkClick.bind(this));
		Jq(".answerInput").mouseup(this.handleLinkClick.bind(this));
	},
	
	registerModalListeners: function(hash) {
		hash.w.css('opacity',1).show(); 
		Jq("#question_option_new").click(this.handleLinkClick.bind(this));
	},
	
	handleLinkClick: function(e) {
		var action = e.target.id.replace("question_option_", "");
		var questionID = action.split("_");
		action = questionID[0];
		questionID = questionID[1];
		this.clickedObj = {"id":questionID,"action":action};
		switch(action) {
			case 'backLink':
				loadPage('ratings');
			break;
			
			case 'new':
				if(Jq("#question_name_label_input").attr('value') == "") {
					Jq("#question_name_info").text("Required");
				}
				else {
					Jq("#new_question").jqmHide();
					ajaxRequestor('newQuestion', 'name='+Jq("#question_name_label_input").attr('value')+'&rating_id='+this.questionRef.id, this.handleNewQuestion.bind(this), this.handleNewQuestion.bind(this));
				}
			break;
			
			case 'newAnswerInput':
				switch(e.type) {
					case 'focus':
						$(e.target.id).select();
					break;
					
					case 'blur':
						if(Jq("#"+e.target.id).attr('value') == 'Add answer here...') {
							
						}
						else {
							// ajax request add 
							ajaxRequestor('newAnswer', 'name='+Jq("#"+e.target.id).attr('value')+'&question_id='+this.clickedObj.id, this.handleNewAnswer.bind(this), this.handleNewAnswer.bind(this));
							Jq("#"+e.target.id).attr('value', "Add answer here...");
						}
					break;
					
					case 'mouseup':
						return false;
						break;
				}
			break;	
			
			case 'enableRealTime':
				// turn on periodical executer for results
				if(this.answerUpdater) {
					this.answerUpdater.stop();
					delete this.answerUpdater;
					Jq("#"+e.target.id).html('Activate Real-time');
				} else {
					Jq("#"+e.target.id).html('Deactivate Real-time');
					this.answerUpdater = new PeriodicalExecuter(this.pollAnswers.bind(this), 10);
				}
			break;
		}
	},
	
	pollAnswers: function() {
		//reload answer HTML
		this.loadAnswerHTML(this.clickedObj.id);
	},
	
	handleNewQuestion: function(response) {
		if(response.response.match(/true/)) {
			new Questions(this.questionRef.id);
		}
		else {
			Jq("#error_window").text("To add additional questions, please upgrade your account.  Thanks!");
			Jq("#error_window").jqmShow();
		}
	},
	
	handleNewAnswer: function(response) {
		if(response.response.match(/true/)) {
			this.loadAnswerHTML(this.clickedObj.id);
		}
	},
	
	loadAnswerHTML: function(questionID) {
		ajaxRequestor('loadPaneHTML', 'page=answers&question_id='+questionID, this.handleLoadAnswerHTML.bind(this), this.handleLoadAnswerHTML.bind(this));
	},
	
	handleLoadAnswerHTML: function(response) {
		if(response.response.match(/true/)) {
			// append child
			$('inner_answers_'+this.questionRef.id).innerHTML = response.paneHTML;
		}
	}
}
