/*#####################
Code Index Version 1
Coded By Wrighty 
Do Not: Rip, Repost, Claim or Rehost
#####################*/


var root = "/?board=" + board + "&action=display&thread=";
var featured = [];
var y;
function $(id){
	return document.getElementById(id);
}
var index = {
	version: 'TruBlu',
	featured_show: 5,
	changelog: '',
	newest: '',
	featured: '',
	cats: '',
	creats: '',
	creats_perc: '',
	content: '',
	title_left: '',
	title_right: '',
	codes_perc: '',
	links: '<a href=javascript:void(0); onclick=\"index.show_hide(\'show_creat_list\')\">Authors</a> | <a href="javascript:index.show_search()">Search</a> | <a href="javascript:index.show_about()">About</a>',
	pref: '',
	total_codes: codes.length,

	y: function(){ //Column Splitter
		this.content = "<table border=0 width=100% cellspacing=1 cellpadding=2><tr><td width=50% valign='top'>";
		for(i=0;i<y.length;i++){
			if(i == parseInt((y.length+1)/2) && y.length>1){
				this.content+='</td><td width=50% valign="top">';
			}
			this.content+= y[i];
		}
		this.content+='</td></tr></table>';
	},

	x: function(){//Show Codes (Not Featured/Viewed/New/Coder)
		if(creators[a][1] != ''){
			return "? <a href= "+root+codes[i][1] + ">"+codes[i][0] +"</a> - <i><a href=/index.cgi?action=viewprofile&user="+creators[a][1]+" style='color:"+creators[a][3]+"'>"+creators[a][0]+"</a></i><br>";
		}else{
			return "? <a href= "+root+codes[i][1] + ">"+codes[i][0] +"</a> - <i>"+creators[a][0]+"</i><br>";
		}
	},

	z: function(){  //Show Coder's Codes
		return "? <a href= "+root+codes[i][1] + ">"+codes[i][0] +"</a><br>";
	},

	template: "<table width='100%' cellspacing='0' cellpadding='3' border='0' align='center'><tr><td class='window1' id='left' align='left' width=25% valign='top' style='height: 100%; overflow:scroll;border-right: 1px dashed #0d181e'><div align=left id='links'></div><br><div><b>Categories:</b><br /><a href='javascript:index.show_all()'>All</a><br><span id='cats'></span><br></div><div  id='show_creat_list' style='display: none;'><b>Authors:</b><br /><span id='creators'></span></div></td><td class='window1' align='left' width=75% valign='top' style='height: 100%; overflow: scroll;'><div id='main'></div></td></tr></table>",

	title: '<div id="title_left" style="float:left; font-size:14px;"></div><div id="title_right" style="float:right; font-size:13px;"></div>',


	change_log: function(){
		this.changelog += '<b>v. TruBlu:</b><br>- Sub-Categories Support [Trublu Custom Version]<br>';
		this.changelog += '<b>v1.7.6:</b><br>- Non Member Coders Now Supported<br>';
		this.changelog += '<b>v1.7.5:</b><br>- Multiple Categories Now Supported<br>';
		this.changelog += '<b>v1.7.1:</b><br>- Two Columns Standardised For Everywhere<br>';
		this.changelog += '<b>v1.6:</b><br>- Side Bar Redesigned<br>- Categories Not Hideable<br>- Coders Hidden By Default<br>- All font sizes standardised<br>';
		this.changelog += '<b>v1.5.7:</b><br>- \'All\' Section Split Into Two Columns<br>';
		this.changelog += '<b>v1.5.6:</b><br>- Featured Codes Are Randomised<br>';
		this.changelog += '<b>v1.5.5:</b><br>- Changelog Added<br>';
		this.changelog += '<b>v1.5:</b><br>- Percentages For Each Coder<br>- \'Home\' Option Fixed<br>';
	},


	init: function(){
		$('status_holder').style.display='';
		var title = $('status_holder_title').parentNode;
		var hold = $('status_holder_message');
		this.featured_codes();
		this.newest_codes();
		this.creats_list();
		this.category_list();
		this.title_build();
		hold.innerHTML = this.template;
		$('main').innerHTML = '<table border=0 width=100% cellspacing=1 cellpadding=2><tr><td width=50% valign="top">' + this.newest + '</td><td width=50% valign="top">' + this.featured + '</td</td></table>';
		$('cats').innerHTML = this.cats;
		$('links').innerHTML = this.links;
		$('creators').innerHTML = this.creats;
		title.innerHTML = this.title;
		$('title_left').innerHTML = this.title_left;
		$('title_right').innerHTML = this.title_right;
		var t =document.getElementsByTagName('table');
		for(i=0;i<t.length;i++){
			if(t[i].className == 'bordercolor' && t[i].parentNode.getElementsByTagName('a')[0].className == 'nav'){
				t[i].parentNode.replaceChild($('status_holder'),t[i]);
				break;
			}
		}
	},

	home: function(){
		$('main').innerHTML = '<table border=0 width=100% cellspacing=1 cellpadding=2><tr><td width=50% valign="top">' + this.newest + '</td><td width=50% valign="top">' + this.featured + '</td</td></table>';
	},

	title_build: function(){
		this.title_left = '<b><a href="javascript:index.home()">' + document.title.split(/\s-\s/)[0] + ' Fanfiction Index</a></b>';
		this.title_right = 'Total Fics: ' + this.total_codes;
	},
	
	show_hide: function(id){
		$(id).style.display = ($(id).style.display == 'none')? '' : 'none';
	},

	newest_codes: function(){
		codes.reverse();
var ar = (codes.length < 15)? codes.length : 15;
		this.newest= "<b>" + ar + " Newest Fics:</b><br><br>";

		for(i=0;i<ar;i++){
			for(a=0;a<creators.length;a++){
				if(creators[a][2] == codes[i][3]){
					if(creators[a][1] != ''){
						this.newest+= parseInt(i+1)+". <a href= "+root+codes[i][1]+">"+codes[i][0] +"</a> - <i><a href=/index.cgi?action=viewprofile&user="+creators[a][1]+" style='color:"+creators[a][3]+"'>"+creators[a][0]+"</a></i><br>";
					}else{
						this.newest+= parseInt(i+1)+". <a href= "+root+codes[i][1]+">"+codes[i][0] +"</a> - <i>"+creators[a][0]+"</i><br>";
					}
				}
			}
		}
	},

	show_all: function(){
		this.content = "<table border=0 width=100% cellspacing=1 cellpadding=2><tr><td width=50%>";
		codes.sort();
		for(i=0;i<parseInt(codes.length/2);i++){
			for(a=0;a<creators.length;a++){
				if(creators[a][2] == codes[i][3]){
					this.content+= this.x();
				}
			}
		}
		this.content+='</td><td width=50%>';
		for(i=parseInt(codes.length/2);i<codes.length;i++){
			for(a=0;a<creators.length;a++){
				if(creators[a][2] == codes[i][3]){
					this.content+= this.x();
				}
			}
		}
		this.content+='</td></tr></table>'
		this.content = (this.content=="")? "There aren't any fics in this category. Please check back later." : this.content
		$('main').innerHTML = this.content
	},

	show_creat: function(id){
		y = [];
		codes.sort();
		for(i=0;i<codes.length;i++){
			if(codes[i][3] == id){
				y.push(this.z());
			}
		}
		this.y();
		this.content = (this.content=="")? "This user hasn't submitted any fics." : this.content
		$('main').innerHTML = this.content
	},

	show_search: function(){
		this.content = "";
		this.content+="Use the box below to search for the fic that you are after.<br><input type='text' id='wR_search' size='50' onKeyUp='index.do_search();'/><div id=search_box></div>";
		$('main').innerHTML = this.content
	},

	show_about: function(){
		this.creats_perc_list();
		this.codes_perc_list();
		this.change_log();
		this.content = "";
		this.content+="<b>Last Updated:</b> "+last_updated+"<br><b>Current Version: </b><a href=javascript:index.show_hide('change_log')>"+this.version+"</a><div id='change_log' style='display:none; padding-left: 10px'>"+this.changelog+"</div><br><b>Total Fics: </b><a href=javascript:index.show_hide('codes_perc_list')>"+this.total_codes+"</a><div id='codes_perc_list' style='display:none; padding-left: 10px'><a href=javascript:index.show_all()>All</a><br>"+this.codes_perc+"</div><br><b>Total Authors: </b><a href=javascript:index.show_hide('creat_perc_list')>"+creators.length+"</a><div id='creat_perc_list' style='display:none; padding-left: 10px'>"+this.creats_perc+"</div><br>This code index is copyrighted to <a href=http://studiozero.proboards44.com/index.cgiaction=viewprofile&user=wrighty style = 'color: #800000'>Wrighty</a> and is not to be reposted, rehosted or claimed. ";
		$('main').innerHTML = this.content
	},

	do_search: function(){
		$("search_box").innerHTML='';
		y = [];
		num = 0;
		var q = $('wR_search').value.replace(/</g,'').replace(/>/g,'').replace(/\(/g,'').replace(/\)/g,'');
		if(q != ''){
			for(i=0;i<codes.length;i++){
				if(codes[i][0].toLowerCase().match(new RegExp(q, 'i'))){
					num++
					for(a=0;a<creators.length;a++){
						if(creators[a][2] == codes[i][3]){
							y.push(this.x());
						}
					}
				}
			}
		}
		if(y.length!=0){
			this.y();
		}else{
			this.content = '';
		}
		this.pref=''
		if(this.content!='' && q!=''){
			this.pref='<b>Search Results: ('+num+')</b><br>';
		}
		$("search_box").innerHTML=this.pref+this.content;
		if(this.content=='' && $('wR_search').value!=''){
			$("search_box").innerHTML = "<i>There were no results for: <b>"+q+"</b>. Please try another search term.";
		}
	},

	show_cat: function(id){
		codes.sort();
		y = [];
		for(i=0;i<codes.length;i++){
			for(z=0;z<codes[i][2].length;z++){
				if(id.match(new RegExp('^' + codes[i][2][z] + '$'))){
					for(a=0;a<creators.length;a++){
						if(creators[a][2] == codes[i][3]){
							y.push(this.x());
						}
					}
				}
			}
		}
		this.y();
		this.content = (this.content=="")? "There aren't any fics in this category. Please check back later." : this.content
		$('main').innerHTML = this.content
	},

	creats_list: function(){
		creators.sort();
		for(i=0; i<creators.length; i++){
			this.creats +=  "<a href=javascript:index.show_creat('"+creators[i][2]+"') style='color:"+creators[i][3]+"'>"+creators[i][0] + "</a><br>";
		}
	},

	creats_perc_list: function(){
		creators.sort();
		this.creats_perc = '';
		for(i=0; i<creators.length; i++){
			num = "0";
				for(a=0;a<codes.length;a++){
					if(codes[a][3] == creators[i][2]){
						num ++
					}
				}
			this.creats_perc +=  num + " - <a href=javascript:index.show_creat('"+creators[i][2]+"') class=group"+creators[i][3]+">"+creators[i][0] + "</a> (" +parseInt((num/this.total_codes)*100)+"%)<br>";
		}
	},

	category_list: function(){
		parents.sort();
		for(n = 0; n < parents.length; n++){
			this.cats += "<br /><u>" + parents[n][0] + ":</u><br />";
		
			cats_list.sort();
			for(i=0; i<cats_list.length; i++){
				if(cats_list[i][2] == parents[n][1]){
					num="0";
					this.cats +=  "<a href=javascript:index.show_cat('"+cats_list[i][1]+"')>"+cats_list[i][0] + "</a><br>";
				}
			}
		}
	},

	codes_perc_list: function(){
		this.codes_perc = '';
		cats_list.sort();
		for(i=0; i<cats_list.length; i++){
			num="0";
			for(a=0;a<codes.length;a++){
				for(z=0;z<codes[a][2].length;z++){
					if(codes[a][2][z].match(new RegExp('^'+cats_list[i][1] + '$'))){
						num ++
					}
				}
			}
			this.codes_perc +=  "<a href=javascript:index.show_cat('"+cats_list[i][1]+"')>"+cats_list[i][0] + "</a> ("+num+")<br>";
		}
	},

	post: function(id){
		var a = document.getElementsByTagName('a');
		for(i=0;i<a.length;i++){
			if(a[i].href.match(/thread=(\d+)/) && RegExp.$1 == id && a[i].innerHTML.match(/Code Index/)){
				a[i].innerHTML += " ["+this.total_codes+"]";
				break;
			}
		}
	},

	featured_codes: function(){
		for(i=0;i<this.featured_show;i++){
			var n = Math.floor(Math.random()*codes.length);
			featured.push(codes[n][1]);
		}
		this.featured="<b>"+this.featured_show+" Featured Fics:</b><br><br>";
		for(f=0;f<codes.length;f++){
			for(i=0;i<featured.length;i++){
				if(featured[i]==codes[f][1]){
					for(a=0;a<creators.length;a++){
						if(creators[a][2] == codes[f][3]){
							if(creators[a][1] != ''){
								this.featured+= "- <a href= "+root+codes[f][1] + ">"+codes[f][0] +"</a> - <i><a href=/index.cgi?action=viewprofile&user="+creators[a][1]+" style='color:"+creators[a][3]+"'>"+creators[a][0]+"</a></i><br>";
							}else{
								this.featured+= "- <a href= "+root+codes[f][1] + ">"+codes[f][0] +"</a> - <i>"+creators[a][0]+"</i><br>";
							}
						}
					}
				}
			}
		}
	}
};


if(location.href.match(new RegExp('thread=' + thread)) && location.href.match(/action=display(&|$)/)){
	index.init();

}
if(location.href.match(new RegExp('board=' + board)) && !location.href.match(/action=/)){
	index.post(thread);
}