// Copyright 2012 Matt Conescu. All rights reserved.

var indx = 0;
var inits = [];
var heads = [];
var areas = [];
var count = [0,0];
var sheet = document.styleSheets;
var year = new Date().getFullYear();
var parm = unescape(location.search.slice(1));
var cmds = parm.match(/(^sort,|^text|^view|doc$|pdf$)/);		 

var html = ['<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>',
		'\n<TR>\n<TD NOWRAP COLSPAN=3 CLASS=',
		'col VALIGN=top>',
		'<FONT CLASS=txt>',
		'\n<TR>\n<TD>&nbsp;',
		'\n</TABLE>\n',
		'<TR CLASS=box>',
		'<OPTION VALUE=',
		'<OPTION SELECTED DISABLED>',
		'<BR>&nbsp;\n<CENTER>&copy; ' + year + ' Matt Conescu. All rights reserved.'];

var menus = [html[8] + 'Resume Formats',
		html[7] + '?sort,0> &raquo; Printer Friendly',
		html[7] + '?sort,1,2,3> &raquo; Recruiter (Blind)',    
		html[7] + '?sort,0,3,2,1> &raquo; Curriculum Vitae',
		html[7] + '?text> &raquo; Plain Text',
		html[7] + '?.doc> &raquo; Word', 
		html[7] + '?.pdf> &raquo; PDF']; 

if (cmds) {	html[7] += '?ask> &laquo; ' + document.title;
		for (var w=1; w<menus.length; w++) {
			if (menus[w].match(parm + '>') || menus[w].match('.' + cmds[0] + '>')) { 
				indx = w; var work = menus[w].split(' &raquo; ');
				menus[0] = html[7]; menus[w] = html[8] + work[1];   
				document.title = work[1] + ' Resume'; break;}}
		if (indx == 0) {document.title = 'Custom (' + parm + ') Resume'; menus[0] += html[7];}												
		work = 'print';}		
								
else {	if (parm.match(/^find,/)) {var click = parm.substr(5); document.title += ' about ' + click;}
		work = 'resume';}

document.write('<SCRIPT SRC=resume.php?page=' + escape(document.title) + '><\/SCRIPT><LINK REL=stylesheet TYPE=text/css HREF=' + work + '.css>');

function search(text, sort, edits) {

var n = 0;
var ranges = [];
var newHTML = '';
var pass = [1,0,0];
var span = /<\/SPAN>(.|\s*)<SPAN>/;
var state = /\b(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])\b\S/;

if (!sort) {sort = '0';}
var sorts = sort.split(',');
var lines = text.split('\n');
if (!lines[lines.length-1]) {lines.splice(lines.length-1,1);}

if (cmds || (click && edits)) {
	work = parm.split(','); 
	for (var i=0; i<work.length; i++) {
		if (work[0] == 'view') {newHTML += '<PLAINTEXT><HTML>\n<HEAD>\n';
			if (document.title) {newHTML += '<TITLE>' + document.title + '</TITLE>\n';} 
			if (sheet[0].cssText) {
			newHTML += '<STYLE>\n' + sheet[sheet.length-1].cssText + '</STYLE>';}
			else {newHTML += '<LINK REL=stylesheet TYPE=text/css HREF=' + sheet[sheet.length-1].href + '>';}
			newHTML += '\n</HEAD>\n<BODY>\n'; html[9] += '\n</BODY>\n</HTML>';
			if (i == work.length-1) {break;}
			else {work.splice(i,1);}}
		if (work[i] == 'sort') {sorts = work.slice(i+1); break;}
		if (work[0] == 'find') {if (i > 0) {if (parm.split(work[i]).length > 2) {continue;} 
				                     finds.push(work[i],'<SPAN>' + work[i] + '</SPAN>');}
						     else {var finds = [];}}}} 

for (i=0; i<lines.length; i++) {

	while (lines[i].match(/\s$/)) {lines[i] = lines[i].slice(0,-1);}

	if (finds && lines[i] == heads[2]) {edits = finds; pass.push(1);}
	
	if (edits) {for (w=0; w<edits.length; w++) { 
				if (lines[i].match(edits[w])) {
					if (edits[w+1].match(/^(mailto|\?)/)) {
					lines[i] = lines[i].replace(edits[w],'<A HREF=' + edits[w+1] + '>' + edits[w] + '</A>');
					edits.splice(w,2); break;}
					else {lines[i] = lines[i].split(edits[w]).join(edits[w+1]);}} w += 1;}

			while (lines[i].match(span)) {work = lines[i].match(span); lines[i] = lines[i].replace(work[0],work[0].replace(/<\/SPAN>|<SPAN>/g,''));}}				
	
	else {	if (lines[i].match(/~/)) {words = lines[i].split('~'); lines[i] = words[0]; 
				if (!cmds) { if (!words[words.length-1]) {lines[i] = words[1];}  
						 else if (words[words.length-1].substr(0,1) != ',') {lines[i] = words.join('');}
					   	 	else {work = words[words.length-2].substr(words[words.length-2].match(/\w/).index); 
							 	if (click && click == work) {lines[i] = '* ' + work; parm = 'find' + words[words.length-1];}
							 	else	{lines[i] = work; inits.push(work,'?find,' + escape(work));}}}}

			else if (!cmds && n > 1) {work = text.match(/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/gi); 
							  if (work) {inits.push(work[0],'mailto:' + work[0] + '?subject=' + escape(document.title));}
							  inits.push(heads[0],'<BR>Contact Information<BR>',heads[1],' ',heads[2],heads[2].toUpperCase());	
							  return(lines.join('\n'));}}
	
	var words = lines[i].replace(/\s+/,' ').split(' ');
	
	if (words.length > 1 && pass[1] == 0 || words[0].match(/^[A-Z]+[a-z]*$/) && (words.length < 3 && pass[2] == 0 || lines[i] == lines[i].toUpperCase())) { 
			
		if (lines[i] == lines[i].toUpperCase()) {
						for (w=0; w<words.length; w++) { 
							if (words[w].length < 3) {words[w] = words[w].toLowerCase();} 
							else {words[w] = words[w].substr(0,1) + words[w].slice(1).toLowerCase();}}
						heads.push(words.join(' ')); if (parm != 'text') {lines[i] = words.join(' ');} 
						pass[0] = 1;}
		if (pass[0] > 2 ) 	{areas[n] += html[1].substr(5) + html[2] + lines[i];
						pass[1] = 1;}	    
		if (pass[0] == 2) 	{areas[n] += html[4] + html[5] + html[0] + html[1] + html[2] + lines[i];}
		if (pass[0] == 1) 	{if (pass[1] == 0) {areas[n] = html[0] + html[1] + 'top>' + lines[i]; 
							if (!heads[0]) {heads.push(lines[i]);} 
						pass[0] = 0;}
						else {areas[n] += html[5]; n += 1; 
							if (sorts[0] == n) {areas[n] = html[0];} 
							else {areas[n] = html[0] + html[4];} 
							areas[n] += html[1] + 'bar>' + lines[i];}
						pass[1] = 1;}	    
		pass[0] += 1; pass[2] = 0; words = '';}
	
	if (words.length > 2 && ((lines[i].match(/^\w/) && (lines[i].match(state) || lines[i].match(/(19|20)\d\d\s*[\-]/))))) {
		
		pass[2] = 0;
		if (pass[3]) 	{var years = lines[i].match(/(19|20)\d\d/g);
					if (years && !years[1]) {years[1] = year;}}
 
		for (w=words.length-1; w>0; w--) { 
						if (pass[2] == 2 && words[w].match(/\W$/)) {words[w] += html[3]; break;}    
						if (pass[2] == 1 && words[w].match(/\W$/)) {pass[2] = 2;} 
						if (pass[2] == 0 && words[w].match(state)) {pass[2] = 1;}} 
		areas[n] += html[4] + html[1] + 'row>' + words.join(' ');
		pass[0] = 1; pass[1] = 1; words = '';}

	if (words.length > 0 && words[0].match(/^(\w|\W)/)) { 
		
		if (pass[1] == 1) 	{if (pass[0] == 2) {areas[n] += html[1] + 'txt>\n<BR>';
						pass[0] = 1;}
						else {if (pass[2] == 0) {areas[n] += html[3];} areas[n] += '\n<BR>';}
						pass[1] += 1;}
		
		if (words[0].match(/^\W+$/)) {words[0] = '\n<LI>'; areas[n] += words.join(' ');}
	 	else if (pass[2] == 2) {areas[n] += '\n<I>' + lines[i] + '</I>\n<BR>';}
			else {areas[n] += '\n' + words.join(' ') + '\n<BR>';}
		pass[2] = 0;}

	if (pass[3]) {work = lines[i].match(/\<S/g); 
			  if (work) {count[0] += work.length; 
			  		   if (years[0]) {for (w=years[1]; w>years[0]; w--) {
								if (!ranges[year-w]) {count[1] += 1; ranges[year-w] = 1;}} years = [];}}}}

if (parm == 'text') {html[9] = '\n\nCopyright' + html[9].substr(25); return('<PLAINTEXT>' + lines.join('\n'));}

if (areas[n]) {areas[n] += html[5];}

if (sorts.length > 1) { 
	for (w=0; w<sorts.length; w++) { 	
	if (areas[sorts[w]]) {newHTML += areas[sorts[w]]; areas[sorts[w]] = '';}}}
else {if (sorts[0] > 0) {areas[0] = areas[sorts[0]]; areas.splice(sorts[0],1);} newHTML += areas.join('');}

return(newHTML);

}

function resume() { 

if (indx < 5) {
	init = search(document.getElementById('text').innerHTML); 
	if (cmds) {document.getElementById('text').innerHTML = heading() + html[5] + init + html[9];}
	else	{init = search(init,'1,2,3,0',inits).replace(/bar\>/g,'top>');
		if (click) {init = init.replace('<LI> ' + click,html[6] + '<TD NOWRAP CLASS=tab>' + click + '<BR><I>' + count[0] + ' Matches Totaling ' + count[1] + ' Years' + 
	      		'<TD WIDTH=14px CLASS=exit>' + corners('tabc','1') + '<A HREF=?ask>X</A>' + corners('tabc') + 
              		'<TD WIDTH=16px>&nbsp;' + html[1] + 'top>' + html[3]);}
		document.getElementById('text').innerHTML = html[0] + '<TR><TD COLSPAN=2>' + heading() + areas.join('').replace(html[4],'') + '<TD NOWRAP ROWSPAN=2 VALIGN=top WIDTH=20%>' +
		html[0] + html[4] + init.replace(html[0],'') + html[6] + '<TD>' +
		corners('tabe') + html[6] + '<TD COLSPAN=2>&nbsp;' + html[1] + '>' + corners('boxc') + html[5] + html[9];}
	if (sheet[0].addRule) {sheet[0].addRule('.hide','display:inline');}
	else if (sheet[0].insertRule) {sheet[0].insertRule('.hide{display:inline}',sheet[0].cssRules.length);}}
else {location = parm;}

}

function heading() {

var head = html[0] + '<TR><TD><SELECT onchange="location=this.value; selectedIndex=indx;">' + menus.join('</OPTION>').replace(/\?\./g,'?' + escape(heads[0]) + '.') + '</SELECT>';
if (!cmds) {head += '<TD ALIGN=center WIDTH=100% CLASS=name>Resume of ' + heads[0] + '</TABLE><TD NOWRAP CLASS=ask><I>Ask my Resume about&mdash;' + 
		html[1] + '>&nbsp;' + corners('boxc','1') + html[6] + '<TD NOWRAP ROWSPAN=3 WIDTH=18px>&nbsp;<TD NOWRAP VALIGN=top>&nbsp;'  + corners('tabe','1');} 
return(head);

}

function corners(cls,top) {
 
if (top) {work = '<DIV><b CLASS=' + cls + '><b CLASS=' + cls + '1><b></b></b><b CLASS=' + cls + '2><b></b></b><b CLASS=' + cls + '3></b><b CLASS=' + cls + '4></b><b CLASS=' + cls + '5></b></b><DIV CLASS=' + cls + 'fg>';}
else {work = '</DIV><b CLASS=' + cls + '><b CLASS=' + cls + '5></b><b CLASS=' + cls + '4></b><b CLASS=' + cls + '3></b><b CLASS=' + cls + '2><b></b></b><b CLASS=' + cls + '1><b></b></b></b></DIV>';}
return(work);

}
