var showedmodalwarning=false;
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
$('post-question-button').addEvent('click',function (e) {
	if ($('questiontxt').value== 'Type your homework question here, like "Help me understand this chemistry problem..."') { StickyWin.alert("Enter a question", "Please enter a question!"); }
	else if ($('questiontxt').value.length < 20) { StickyWin.alert("Question Length", "Your question isn't long enough, it needs to be atleast 20 characters!");  }
	else if (check_question_text($('questiontxt').value) && !showedmodalwarning && !IE6
	) { 
		new StickyWinModal({
			content:StickyWin.ui('Hmmmmmm...',$('anticheating'+check_question_text($('questiontxt').value)).get('html'),{width:'600px'}),
			position:'center',width:'600px',height:'450px'});
			pageTracker._trackPageview('/goal/anti_cheating_modal');
			pageTracker._setVar('saw_anti_cheating_modal');
			showedmodalwarning=true;
	} 
	else {
		homepage_postquestion();
	}
	});

function homepage_postquestion() {
	new StickyWinModal({
	content: StickyWin.ui('Post Your Question!',$('askquestionmodal'),{width:'600px'}),
	position:'center',width: '600px',height:'450px'
	});
	$('askquestionmodal').style.display='inline';
	$('title').value=$('questiontxt').value.substr(0,40);
	$('questionionformtxt').value=$('questiontxt').value;	
	if ($('dueDate').value =='') { $('dueDate').value=new Date().increment('day',3).format('%m/%d/%Y'); }
	$('ques').addEvent('submit', function(e) {
	datearr=$('dueDate').value.split('/');
	$('dueMonth').value=datearr[0];
	$('dueDay').value=datearr[1];
	$('dueYear').value=datearr[2];   
	});
	loadAskValidators();
	var quesval= new FormValidator('ques');
	new DatePicker('dueDate', { additionalShowLinks:['dueDatecal']});
}

window.addEvent('domready',function() {
//if not ie6 or old browser
if (typeof document.body.style.maxHeight != "undefined") {
	$('questiontxt').style.width='80%';
}
$('htmltagcloud').style.display='none';
});

$('questiontxt').addEvent('click',function (e) {
	if (this.value == 'Type your homework question here, like "Help me understand this chemistry problem..."') {
	  this.value='';
	  this.style.color='#000000';
	}
});


function show_next_h(type,page) {
	if (type == 'popular') {
		$('pagenav').innerHTML='';
		$('queslist').innerHTML='';
		$('pagecount').innerHTML='';
		$('popsubj').addClass('active');
		$('unanstab').removeClass('active');
		$('recenttab').removeClass('active');
		$('htmltagcloud').style.display='block';
		return false;
	}
	$('htmltagcloud').style.display='none';

	if (!$('quesajaxloader')) {
		new Element('img',{'src':'/images/ajax-loader-small.gif','id':'quesajaxloader'}).injectInside('pagenav');
	}
	if (type == 'recent') {
		$('recenttab').addClass('active');
		$('unanstab').removeClass('active');
		$('popsubj').removeClass('active');
	} else {
		$('recenttab').removeClass('active');
		$('unanstab').addClass('active');
		$('popsubj').removeClass('active');
	}
	new Request.JSON({url:'/cgi/ajax_unans',method:'get',async:false,onSuccess:function  (o) {
	o.page=parseInt(o.page);
	o.questiontotalcount=parseInt(o.questiontotalcount);
	var max;
	if (o.page *6 + 6 > o.questiontotalcount) { max=o.questiontotalcount;} else {max=o.page*6+6; }
	$('pagecount').innerHTML=(o.page==1)?1:(parseInt(o.page)-1)*6;
	$('pagecount').innerHTML+=' - '+(parseInt(o.page)*6)+' of '+o.questiontotalcount;
	$('pagenav').innerHTML='';
	if (o.page > 1) {
		new Element('a',{'href':'javascript:;','html':'Previous','events':{'click': function () { show_next_h(o.type,parseInt(o.page)-1) } }}).injectInside('pagenav');
	}
	
	if (o.page * 6 < o.questiontotalcount) {
		if (o.page >1 ) { 		new Element('span',{'html':' | '}).injectInside('pagenav'); }
		new Element('a',{'href':'javascript:;','html':'Next','events':{'click': function () { show_next_h(o.type,parseInt(o.page)+1) } }}).injectInside('pagenav');	
	}
	
	$('queslist').innerHTML='';
	o.questions.forEach(function (q) { 
		var li=new Element('li').injectInside('queslist');
		new Element('span',{'class':'money','html':'$'+q.bounty}).injectInside(li);
		new Element('span',{'html':'&nbsp;'}).injectInside(li);
		new Element('a',{'href':q.link,'html':q.title,'style':'font-weight:bold;'}).injectInside(li);
	});
	
}}).send('type='+type+'&page='+page);
}

