var cat=new Array();
var fdata=new Array();
var adata=new Array();
var mdata=new Array();
var total=0;
var forumid=0;
var todo='Add';
var forum=[];
forum.init=function() {
	$('#page1').show();
	$('#page2').hide();
	$('#page3').hide();
	if(window.localStorage.getItem('rec')!=null) {
	var rec=JSON.parse(window.localStorage.getItem('rec'));
	var info=rec.profile;
	if(info._class!=null) {
		$('body').removeClass();
		var myclass=info._class;
		if(myclass.length > 30) {
			$('body').attr('style',info._class);
		} else {
			$('body').addClass(info._class);
		}
	}	
	}
	forum.getcat();	
}

forum.getcat=function() {
	$.getJSON('/json/forumcatjson.js',function(data,status) {
	if(status=='success') {	
		cat=data;
		forum.showcat();
		forum.gettopic();
	}
	});
}

forum.showcat=function() {
	var str='';
	str+='<div class="pop in">';
	for(var i=0; i<cat.length; i++) {
		str+='<div class="block400" onclick="forum.getforum('+i+');">'+cat[i]._title+'&nbsp;('+cat[i]._total+')</div>';	
	}
	str+='</div>';
	$('#page1').html(str);
}

forum.getforum=function(i) {
	var id=cat[i]._subcatid;
	var title=cat[i]._title;
	$.getJSON('/json/forumjson.php',{subcatid:id},function(data,status) {
		fdata=data;	
		forum.showforum(title,0);
	});
}

forum.showforum=function(title,page) {
	var i=page * 10;
	total=Math.round(fdata.length / 10);
	var count=0;
	var str='';
	str+='<h3><div onclick="forum.init();" class="block">Forum</div>&gt;<div class="block">'+title+'</div></h3>';
	str+='<div class="pop in">';
	while((i < fdata.length)&&(count < 10)) {
		var url='/users/'+fdata[i]._uid+'/';
		str+='<div class="space50">';
		str+='<fieldset class="border">';
		str+='<img src="'+fdata[i]._photo+'" class="photo50 moveleft" onclick="goto(\''+url+'\');"/>';
		str+='<p>'+fdata[i]._comment+'</p>';
		str+='<div>Date : '+fdata[i]._date+'</div>';
		str+='</fieldset>';
		str+='<div class="menu">';
		str+='<button onclick="forum.getanswer('+i+');">Read response</button>';
		str+='&nbsp;<button onclick="forum.reply('+i+');">Reply</button>';
		str+='<div id="'+i+'"></div>';
		str+='</div></div>';
		i++;
		count++;
	}
	str+='</div>';
	if(total > 0) {
		var mypage=page + 1;
		str+='<center><table border="0" cellpadding="10">';
		str+='<tr><td><img src="image/blackarrowleft.png" class="left"/></td>';
		str+='<td>Page '+mypage + ' of ' + total+'</td>';
		str+='<td><img src="image/blackarrowright.png" class="right"/></td></tr>';
		str+='</table></center>';
	}
	$('#page1').html(str);
	$('img.right').click(function() {
		if(page < total) {
			page++;
			forum.showforum(title,page);
		}
	});
	
	$('img.left').click(function() {
		if(page > 0) {
			page--;
			forum.showforum(title,page);
		}
	});
}

forum.getanswer=function(i) {
	var id=fdata[i]._forumid;
	$.getJSON('/json/forumanswer.php',{forumid:id},function(data,status) {
		if(status=='success') {
			adata=data;
			forum.showanswer(i);
		}
	});
}
var flag=true;
forum.showanswer=function(k) {
	var h=$('#'+k).height();
	var str='';
	if(adata.length > 0) {
	for(var i=0; i < adata.length; i++) {
		var url='/users/'+adata[i]._uid+'/';
		str+='<div class="space50"><fieldset class="border">';
		str+='<img src="'+adata[i]._photo+'" class="moveleft" onclick="goto(\''+url+'\');"/>';
		str+=adata[i]._comment;
		str+='</fieldset></div>';
	}
	} else {
		str+='<h4>No response to this question.</h4>';	
	}
	if(h==0) {
		$('#'+k).html(str);
	} else {
		$('#'+k).html('');	
	}
}

forum.reply=function(k) {
	var h=$('#'+k).height();
	var str='';
	str+='<form name="f" method="get" action="" onsubmit="return false">';
	str+='<p><textarea rows="10" cols="50" id="textbox'+k+'"></textarea></p>';
	str+='<button class="button" onclick="forum.submitA('+k+');">Submit</button>';
	str+='</form>';
	if(h==0) {
		$('#'+k).html(str);
	} else {
		$('#'+k).html('');	
	}
}

forum.submitA=function(k) {
	var id=fdata[k]._forumid;
	var text=$('#textbox'+k).val();
	var userid=window.localStorage.getItem('cuserid');
	if((text!='')&&(userid!=null)) {
		text=encodeURIComponent(text);
		$.ajax({url:'/json/save_forumanswer.php',
			   type:'post',
			   data:'userid='+userid+'&forumid='+id+'&comment='+text,
			   success:function(data,status) {
				  if(status=='success') {
				   $('#'+k).html(data);
				  }
			   }
		});
	}
}

forum.add=function(id) {
	$('#page1').hide();
	$('#page3').hide();
	$('#page2').show();
	todo=id;
}

forum.submitq=function() {
	var msg = FCKeditorAPI.GetInstance("textbox").GetXHTML();
	var userid=window.localStorage.getItem('cuserid');	
	msg=encodeURIComponent(msg);
	var subcatid=document.frm.cat.options[document.frm.cat.selectedIndex].value;
	if(msg=='') {
		$('#result').html('You need to enter message.');
	} else if(userid==null) {
		$('#result').html('You need to login in order to submit your message.');
	} else {
	$.ajax({url:'/json/save_forum.php',
		   type:'post',
		   data:'forumid='+forumid+'&todo='+todo+'&userid='+userid+'&subcatid='+subcatid+'&comment='+msg,
		   success:function(data,status) {
			  $('#result').html(data); 
			  FCKeditorAPI.GetInstance("textbox").SetHTML('');
		   }
	});
	}
}

forum.gettopic=function() {
	var str='';
	for(var i=0; i < cat.length; i++) {
		str+='<option value="'+cat[i]._subcatid+'">'+cat[i]._title+'</option>';
	}
	$('#cat').html(str);
}

forum.edit=function() {
	var userid=window.localStorage.getItem('cuserid');
	$.getJSON('/json/myforumjson.php',{userid:userid,id:Number(new Date)},function(data,status) {
		if(status=='success') {
			mdata=data;
			forum.myblog(0);
		}
	});
}

forum.myblog=function(page) {
	$('#page1').hide();
	$('#page2').hide();
	$('#page3').show();
	var i=page * 10;
	var count=0;
	total=Math.round(mdata.length / 10);
	var str='';
	if(mdata.length > 0) {
	while((i<mdata.length)&&(count<10)) {
		str+='<fieldset class="border" id="'+i+'">';
		str+='<img src="/image/x.png" class="moveright" onclick="forum.del('+i+');"/>';
		str+='<p>'+mdata[i]._comment+'</p>';
		str+='<button class="button" onclick="forum.myedit('+i+');">Edit</button>';
		str+='</fieldset>';
		count++;
		i++;
	}
	} else {
		str+='<h3>You have no forum.</h3>';	
	}
	if(total > 0) {
		var mypage=page + 1;
		str+='<center><table border="0" cellpadding="10">';
		str+='<tr><td><img src="image/blackarrowleft.png" class="left"/></td>';
		str+='<td>Page '+mypage + ' of ' + total+'</td>';
		str+='<td><img src="image/blackarrowright.png" class="right"/></td></tr>';
		str+='</table></center>';
	}
	$('#page3').html(str);
	$('img.right').click(function() {
		if(page < total) {
			page++;
			forum.myblog(page);
		}
	});
	
	$('img.left').click(function() {
		if(page > 0) {
			page--;
			forum.myblog(page);
		}
	});
}

forum.myedit=function(i) {
	$('#page2').show();
	$('#page3').hide();
	$('#page1').hide();
	todo='Update';
	var subcatid=mdata[i]._subcatid;
	forumid=mdata[i]._forumid;
	FCKeditorAPI.GetInstance("textbox").SetHTML(mdata[i]._comment);
	var len=document.frm.cat.length;
	for(var k=0; k < len; k++) {
		if(document.frm.cat.options[i].value==subcatid) {
			document.frm.cat.selectedIndex=i;	
		}
	}
}

forum.del=function(i) {
	forumid=mdata[i]._forumid;
	mdata[i]._comment='';
	mdata[i]._forumid='';
	var userid=window.localStorage.getItem('cuserid');
	$.ajax({url:'/json/save_forum.php',
		   type:'post',
		   data:'todo=Delete&forumid='+forumid+'&userid='+userid,
		   success:function(data) {
			   $('#'+i).toggle('slow');
			   //$('#'+i).remove();
				//alert(data);   
		   }
	});
}

function goto(url) {
	window.location.href=url;	
}
