VS.HomeSales = { 
	VocabLevel:1,
	Score:0,
	Points:{ correct:5,incorrect:0 },
	CurrentQuestion:{},
	nextQuestion:null,
	previousWords:[],
	NextQTxn:null,
	ConsecutiveRight:0, 
	NumberCorrect:0,
	NumberWrong:0,
	ConsecutiveIncorrect:0,
	Counter:1,
	ShowQuestionOnload:null, 
	LastQuestionCorrect:null,
	TxnErrCount:{ LoadQ:0 },
	List:12,
	Name:'',
	Grade:'',
	TopTen:[],
	HintRemove:[],
	HintUsed:0,
	init:function(){ 
		this.CurrentQuestion = VS.Init.initData.CurrentQuestion; 
		this.previousWords[0] = this.CurrentQuestion.id; 
		this.previousWords[1] = this.CurrentQuestion.OptionIDs[0]; 
		this.previousWords[2] = this.CurrentQuestion.OptionIDs[1]; 
		this.previousWords[3] = this.CurrentQuestion.OptionIDs[2]; 
		this.previousWords[4] = this.CurrentQuestion.OptionIDs[3]; 
		E.addListener($('StartGame'),'click',this.Listeners.StartGame);
		E.addListener($('HintLink'),'click',this.Listeners.onClickHint);
		E.addListener(Dom.getElementsByClassName('optionlink'),'click',this.Listeners.CheckAnswer); 	
		Dom.getElementsByClassName('responseflag','img','ProgressSpace',function(el){ Dom.setStyle(el,'opacity',0); Dom.setStyle(el,'display',''); }); 
		this.GetNextQuestions();		
	},
	GetNextQuestions:function(){ 
		var Load = function(o){ 
			try{ 
				var R = YAHOO.lang.JSON.parse(o.responseText);
				if(R.ok){ 
					VS.HomeSales.TxnErrCount.LoadQ=0; 
					VS.HomeSales.nextQuestion = { correct:R.response.onCorrect, incorrect:R.response.onWrong }; 
					if(VS.HomeSales.ShowQuestionOnload != null){ 
						VS.HomeSales.ShowQuestion(VS.HomeSales.ShowQuestionOnload); 
						VS.HomeSales.ShowQuestionOnload = null; 
					} 
				} 
			} catch (err) { 
				VS.HomeSales.TxnErrCount.LoadQ++; 
				if(VS.HomeSales.TxnErrCount.LoadQ == 6){ 
//					VS.Alert('info','Busy busy busy!','VocabSushi is popular and we couldn\'t get a new question for you right now. We\'ll be back shortly!'); 
				} else { 
					VS.HomeSales.GetNextQuestions(); 
				} 
			}
		} 
		var Fail = function(o){ 
			VS.HomeSales.TxnErrCount.LoadQ++; 
			if(VS.HomeSales.TxnErrCount.LoadQ == 6){ 
	//			VS.Alert('info','Busy busy busy!','VocabSushi is popular and we couldn\'t get a new question for you right now. We\'ll be back shortly!'); 
			} else { 
				VS.HomeSales.GetNextQuestions(); 
			} 
		}
		if(!YAHOO.util.Connect.isCallInProgress(this.NextQTxn)){ 
			this.NextQTxn = YAHOO.util.Connect.asyncRequest('POST','/home/getNewSamplerQuestion',
																											{success:Load,failure:Fail},
																											'level='+this.VocabLevel
																											+'&list='+this.List
																											+'&numcorrect='+this.NumberCorrect
																											+'&numwrong='+this.NumberWrong
																											+'&qcount='+this.Counter
																											+'&consecutiveright='+this.ConsecutiveRight
																											+'&previouswords='+this.previousWords.join(',')); 
		}
	},	
	ShowQuestion:function(which){ 
		var Question = this.nextQuestion[which];
		$('Question').innerHTML = Question.Q;
		$('Answer_0').innerHTML = Question.Options[0]; 
		$('Answer_1').innerHTML = Question.Options[1]; 
		$('Answer_2').innerHTML = Question.Options[2]; 
		$('Answer_3').innerHTML = Question.Options[3];
		$('Answer_4').innerHTML = Question.Options[4];

		Dom.getElementsByClassName('optionlink','a','AnswerOptions',function(el){ Dom.setStyle(el,'opacity',1); }); 

		this.Counter = this.Counter+1;
		this.previousWords[this.previousWords.length] = Question.id; 
		this.previousWords[this.previousWords.length] = Question.OptionIDs[0]; 
		this.previousWords[this.previousWords.length] = Question.OptionIDs[1]; 
		this.previousWords[this.previousWords.length] = Question.OptionIDs[2]; 
		this.previousWords[this.previousWords.length] = Question.OptionIDs[3]; 
		this.previousWords[this.previousWords.length] = Question.OptionIDs[4]; 
		
		$('QCount').innerHTML = this.Counter; 
		
		switch(Question.type){ 
			case 'sentence': 
				$('QuestionType').innerHTML = 'Complete This Sentence'; 
				Question.QClass = 'sentence';
				break; 
			case 'definition': 
				$('QuestionType').innerHTML = 'Choose the Correct Definition'; 
				Question.QClass = 'definition';
				break; 
			case 'word': 
				$('QuestionType').innerHTML = 'Choose the Word That Matches This Definition'; 
				Question.QClass = 'word';
				break; 
		}  
		if(Question.QClass == 'sentence'){ 			
			$('ArticleHeadline').firstChild.href=Question.article.url
			$('ArticleHeadline').firstChild.innerHTML=Question.article.headline;
			$('Source').innerHTML=Question.article.source+' | '+Question.article.date; 
			Dom.setStyle($('Attribution'),'opacity',1);
		} else { 
			Dom.setStyle($('Attribution'),'opacity',0);
		} 
		Dom.replaceClass($('Question'),this.CurrentQuestion.QClass,Question.QClass); 
		
		this.IncreaseLevel(Question.Level);
		
		this.CurrentQuestion = Question; 
		this.nextQuestion = null; 
		this.GetNextQuestions(); 
	},
	ShowHint:function(){ 
		var not = VS.HomeSales.CurrentQuestion.CorrectPosition; 
		var select,selected = new Array(); 
		do{ 
			select = Math.floor(Math.random()*5);
			if(select!=not && !selected.inArray(select)){ 
				selected[selected.length] = select; 
			} 
		}while( selected.length<3); 
		
		this.HintUsed++; 
		
		for(var i=0;i<3;i++){ 
			Dom.setStyle('Answer_'+selected[i],'opacity',0); 
		} 
	},
	UpdateTodaysProgress:function(){ 


	},
	AdjustScore:function(whichway){ 
		var points,random;
		if(whichway=='correct'){ 
			points = this.Points.correct*this.VocabLevel;
			if(this.HintUsed==1){ 
				points = Math.floor((points/2)); 
			} else if(this.HintUsed==2) { 
				points = 0; 
			} 
			this.HintUsed = 0; 
			random = Math.floor(Math.random()*5);
			var anim3 = new YAHOO.util.Anim($('CorrectFlag'+random),{opacity:{to:1}},0.15); 
			anim3.animate(); 
			setTimeout(function(){ 
			var anim3 = new YAHOO.util.Anim($('CorrectFlag'+random),{opacity:{to:0}},0.25); 
			anim3.animate(); },2000); 
		} else { 
			points = this.Points.incorrect; 
			random = Math.floor(Math.random()*3);
			var anim3 = new YAHOO.util.Anim($('IncorrectFlag'+random),{opacity:{to:1}},0.15); 
			anim3.animate(); 
			setTimeout(function(){ 
			var anim3 = new YAHOO.util.Anim($('IncorrectFlag'+random),{opacity:{to:0}},0.25); 
			anim3.animate(); },2000); 
		} 
		this.Score = this.Score + points; 
		$('GameScore').innerHTML = this.Score; 
	}, 
	IncreaseLevel:function(toLevel){ 
		$('GameLevel').innerHTML = toLevel; 
		this.VocabLevel = toLevel; 
	},
	DoDone:function(){ 
				// 
				// var Txn = YAHOO.util.Connect.asyncRequest('POST','/home/registerTopTenHomeQuiz',
				// 																								{success:function(){},failure:function(){}},
				// 																								'list='+this.List
				// 																								+'&grade='+this.Grade
				// 																								+'&name='+this.Name
				// 																								+'&score='+this.Score
				// 																							); 
				// 
		//var TopTen = new Array(); 
		var TopTen = this.TopTen;
		var reg = false; 
		var graders; 
		
		switch(this.Grade){ 
			case 'middle':
				graders = "Middle Schoolers"; 
				break; 
			case 'high':
				graders = 'High Schoolers'; 
				break; 
			case 'college': 
				graders = "College Students"; 
				break; 
			case 'graduate': 
				graders = "College Graduates"; 
				break;
			case 'professional': 
				graders = "Professionals"; 
				break;
		} 
		// for(var i=0;i<10;i++){ 
		// 	if(TopTen.length<10){ 
		// 		if(!reg && this.Score>this.TopTen[i][1]){ 
		// 			reg = true; 
		// 			TopTen[TopTen.length] = [this.Name,this.Score]; 
		// 		}
		// 		TopTen[TopTen.length] = this.TopTen[i]; 
		// 	}
		// } 
		
		var h1,timg,fbimg,h3,button,p,table,tbody,tr,tdrank,tdname,tdscore; 
		
		h1 = document.createElement('h1'); 
		h1.innerHTML = "Your score is "+this.Score+' <a name="fb_share" type="button" href="http://www.facebook.com/sharer.php&u=http://vocabsushi.com&t=I got a Vocab Skill Score of '+this.Score+' at VocabSushi.com!" style="vertical-align:middle">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script> <a href="http://twitter.com/share" class="twitter-share-button" data-text="I got a Vocab Skill Score of '+this.Score+' at VocabSushi.com! Just try to beat that!" data-count="none" data-via="vocab_sushi" style="vertical-align:middle">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>'; 

		button = document.createElement('input'); 
		button.type = 'button'; 
		button.className='new_standard_button'; 
		button.value = 'Sign up Now!'; 
		
		p = document.createElement('p'); 
		var ptext = 'Sign up to save your score on the leaderboard!'; 
		// var ptext = (reg)?'You placed in the top ten '+graders+' taking the '+$('wordlist').options[$('wordlist').selectedIndex].text+'!':"A little more practice with VocabSushi could get you in the top ten!"; 
		// ptext+=' You got to Vocab Level '+this.VocabLevel+'. <a href="/signup?l='+this.VocabLevel+'">Sign up now starting at this level</a> to keep building your vocab skills.'; 
		p.innerHTML = ptext; 
		
		h3 = document.createElement('h3'); 
		h3.innerHTML = 'Top Ten / '+graders+' / '+$('wordlist').options[$('wordlist').selectedIndex].text; 
		
		table = document.createElement('table'); 
		tbody = document.createElement('tbody'); 
		for(var i=0,il=TopTen.length;i<il;i++){ 
			tr = document.createElement('tr'); 
			tdrank = document.createElement('td'); 
			tdrank.innerHTML = (i+1); 
			tdname = document.createElement('td'); 
			tdname.innerHTML = TopTen[i][0]; 
			tdscore = document.createElement('td'); 
			tdscore.innerHTML = TopTen[i][1]; 
			tr.appendChild(tdrank); 
			tr.appendChild(tdname); 
			tr.appendChild(tdscore); 
			tbody.appendChild(tr); 
		} 
		table.appendChild(tbody); 
		
		$('DoneSpace').appendChild(h1); 
		$('DoneSpace').appendChild(button); 
		$('DoneSpace').appendChild(p); 
		$('DoneSpace').appendChild(h3); 
		$('DoneSpace').appendChild(table); 

		E.addListener(button,'click',function(){ window.location.href='/signup?l='+VS.HomeSales.VocabLevel+'&w='+VS.HomeSales.List+'&g='+VS.HomeSales.Grade+'&t=1&p='+VS.HomeSales.Score; }); 

		Dom.setStyle($('DoneSpace'),'opacity',0); 
		Dom.setStyle($('DoneSpace'),'display',''); 
		var anim1 = new YAHOO.util.Anim($('GameSpace'),{opacity:{to:0}},0.4); 
		anim1.onComplete.subscribe(function(){ Dom.setStyle($('GameSpace'),'display','none'); }); 
		anim1.animate(); 
		var anim3 = new YAHOO.util.Anim($('DoneSpace'),{opacity:{to:1}},0.4); 
		anim3.animate(); 
	},
	GetTopTen:function(){ 
		var Load = function(o){ 
				var R = YAHOO.lang.JSON.parse(o.responseText);
				if(R.ok){ 
					VS.HomeSales.TopTen = R.response.topten; 
				} 
		} 
		var Fail = function(o){ 
		}
		var Txn = YAHOO.util.Connect.asyncRequest('POST','/home/getTopTenHomeQuiz',
																										{success:Load,failure:Fail},
																										'list='+this.List
																										+'&grade='+this.Grade
																									); 
	}, 
	Listeners:{ 
		onClickHint:function(){ 
			VS.HomeSales.ShowHint(); 
		},
		StartGame:function(){ 
			if($('wordlist').value=='' || $('grade').value==''){ // || $('name').value==''){ 
				VS.Alert('info','Missing Info',"To try our demo game, please enter a name for the High Scores chart, select your grade in school and what word list to work with."); 
			} else { 
				VS.HomeSales.List = $('wordlist').value; 
				// VS.HomeSales.Name = $('name').value;
				VS.HomeSales.Grade = $('grade').value;
				Dom.setStyle('GameStart','display','none'); 
				Dom.setStyle('GameOn','display',''); 
				VS.HomeSales.GetTopTen(); 
			}
		},
		CheckAnswer:function(){ 
			var Answer = this.id.split('_')[1]; 
			var MaxCount = 20; 
			if(Answer == VS.HomeSales.CurrentQuestion.CorrectPosition){ 
				VS.HomeSales.LastQuestionCorrect = true; 
				VS.HomeSales.ConsecutiveRight = VS.HomeSales.ConsecutiveRight+1; 
				VS.HomeSales.NumberCorrect++; 
				VS.HomeSales.UpdateTodaysProgress(); 
				VS.HomeSales.AdjustScore('correct'); 
				if(VS.HomeSales.Counter==MaxCount){ 
					VS.HomeSales.DoDone(); 
				} else { 
					if(VS.HomeSales.nextQuestion == null){ 
						VS.HomeSales.GetNextQuestions(); 
						VS.HomeSales.ShowQuestionOnload = 'correct'; 
						$('QuestionType').innerHTML = ' '; 
						$('Question').innerHTML = '<div class="loadingmsg"><img src="/js/yui/assets/skins/sam/wait.gif" width="18" height="18" alt="" /> loading next question</div>'; 
					} else { 
						VS.HomeSales.ShowQuestion('correct'); 
					} 
				}
			} else { 
				VS.HomeSales.LastQuestionCorrect = false; 
				VS.HomeSales.ConsecutiveRight = 0; 
				VS.HomeSales.NumberWrong++; 
								
				VS.HomeSales.AdjustScore('incorrect'); 
				VS.HomeSales.UpdateTodaysProgress(); 
				if(VS.HomeSales.Counter==MaxCount){ 
					VS.HomeSales.DoDone(); 
				} else { 
					if(VS.HomeSales.nextQuestion == null){ 
						VS.HomeSales.GetNextQuestions(); 
						VS.HomeSales.ShowQuestionOnload = 'incorrect'; 
						$('QuestionType').innerHTML = ' '; 
						$('Question').innerHTML = '<div class="loadingmsg"><img src="/js/yui/assets/skins/sam/wait.gif" width="18" height="18" alt="" /> loading next question</div>'; 
					} else { 
						VS.HomeSales.ShowQuestion('incorrect'); 
					} 	
				}
			}
		} 
		
	}
} 
VS.Init.initThese[VS.Init.initThese.length] = 'HomeSales';

