// JavaScript Document
// JavaScript Document
//must have an element id=article_content
//must create array to search
//should have style .autolink set

//when possible, some of these functions should be moved to images.lancasteronline.com
//: sort_by_value_length, MakePlaceHolder,replace_linked_text, MakeHrefPlaceHolder, swap_link_placeholder, get_content, swap_content because they are shared with classified/javascript/hightlight.js

var current_search = '';
var original_text = new Array();
var linked_text = new Array();
var code_placeholder = new Array();
function get_content(ID){
	content = document.getElementById(ID).innerHTML;
	return content;
}

//function replace_linked_text(string){   //depreciated moved to modify_content.js

//function MakeHrefPlaceHolder(string){ //depreciated moved to modify_content.js

//function swap_link_placeholder(string,backref1){ //depreciated moved to modify_content.js

function autolink(ID,content_array,linkFunctionName){ //needs more work, right now swaps the regexp search for original text, but has no overwriting issues.
	if(!linkFunctionName) linkFunctionName = 'MakeURL2';
	linkFunction = eval(linkFunctionName);
	content_array = sort_by_value_length(content_array)
	smart_content_array = expand_by_rules(content_array);
	content = get_content(ID);
	content = replace_linked_text(content);
	content = replace_code_placeholder(content);
	for(k in smart_content_array){
		//current_search = smart_content_array[k][0];
		current_search_place_holder = k
		//alert(smart_content_array[k][1]); //w/ reg exp
		word_boundary_search = '\\b('+smart_content_array[k][1]+"'*(s|es)*)\\b";
		search_regexp = new RegExp(word_boundary_search, "gi");
		//prompt('',search_regexp);
		content = content.replace(search_regexp, MakePlaceHolder);
	}
	//swap out the placeholders:
	search_regexp = new RegExp('placeholder_(\\d+)_original_text_id_(\\d+)', "gi");
	content = content.replace(search_regexp, linkFunction);
	search_regexp = new RegExp('HrefPlaceholder_id_(\\d+)', "gi");
	content = content.replace(search_regexp, swap_link_placeholder);
	search_regexp = new RegExp('CodePlaceholder_id_(\\d+)', "gi");
	content = content.replace(search_regexp, swap_code_placeholder);
	//destroy all the temporary arrays to avoid memory issues:
	smart_content_array = Array();
	original_text = Array();
	swap_content(ID,content);
}

function MakeURL2(matched_text,backref1,backref2){
	return ('<div class="autolink"><a href="'+urlPrefix+'/'+escape(smart_content_array[backref1][0])+'/">'+original_text[backref2]+'</a></span>' );
}

function MakeURLFancy(matched_text,backref1,backref2){
	return ('<span class="autolink"><a href="'+urlPrefix+'/'+escape(smart_content_array[backref1][0])+'" style="text-decoration:none">'+original_text[backref2]+'</a><a href="'+urlPrefix+'/'+escape(smart_content_array[backref1][0])+' onClick="return clickreturnvalue()" onMouseover="dropdownmenu(this, event, makemenu(linkArray,\''+escape(smart_content_array[backref1][0])+'/\'),\'60px\')" onMouseout="delayhidemenu()"><img src="/images/reverse_carat.gif" border="0"></a></span>' );
}

//function MakePlaceHolder(string,backref1,backref2){ //depreciated moved to modify_content.js

//function sort_by_value_length(original_array){  //depreciated moved to modify_content.js

function expand_by_rules(content_array){
	var smart_content_array = new Array();
	for(k in content_array){
		//add it to the "smart" array
		current_length = smart_content_array.length;
		smart_content_array[current_length] = new Array();//make it multi-dimensional
		//requires grammar.js:
		var plural = pluralize(content_array[k]);
		
		var tmp = posessive(plural); //posessive plural, DO NOT MOVE!
		smart_content_array[current_length] = new Array();//make it multi-dimensional
		smart_content_array[current_length][0] = content_array[k];
		smart_content_array[current_length][1] = tmp;
		
		var tmp = posessive(content_array[k]); //posessive
		smart_content_array[current_length + 1] = new Array();//make it multi-dimensional
		smart_content_array[current_length + 1][0] = content_array[k];
		smart_content_array[current_length + 1][1] = tmp;
		
		//plural , DO NOT MOVE!
		var tmp = pluralize(content_array[k]);
		smart_content_array[current_length + 2] = new Array();//make it multi-dimensional
		smart_content_array[current_length + 2][0] = content_array[k];
		smart_content_array[current_length + 2][1] = plural;
		
		var tmp = singularize(pluralize(content_array[k]));
		smart_content_array[current_length + 3] = new Array();//make it multi-dimensional
		smart_content_array[current_length + 3][0] = content_array[k];
		smart_content_array[current_length + 3][1] = tmp;
		
		var tmp = abandon(content_array[k]);
		smart_content_array[current_length + 4] = new Array();//make it multi-dimensional
		smart_content_array[current_length + 4][0] = content_array[k];
		smart_content_array[current_length + 4][1] = tmp;
		
		smart_content_array[current_length + 5] = new Array();//make it multi-dimensional
		metachars = new RegExp('([\(\)\.\/])',"g");
		smart_content_array[current_length + 5][0] = content_array[k];
		smart_content_array[current_length + 5][1] = content_array[k].replace(metachars,'\\'+"$1");//we need to change the parenthesis in the search string to escaped
		//ends with content in parenthesis, probably a joined acronym
		if(content_array[k].indexOf('(') != -1 && content_array[k].indexOf(')') != -1){
			org_full_name = content_array[k].substring(0,content_array[k].indexOf('('));
			org_acronym = content_array[k].substring(content_array[k].indexOf('(')+1,content_array[k].indexOf(')'));
			smart_content_array[current_length + 6] = new Array();//make it multi-dimensional
			smart_content_array[current_length + 6][0] = content_array[k];
			smart_content_array[current_length + 6][1] = org_full_name.replace(/^\s+|\s+$/g,""); //no leading/trailing space.
			smart_content_array[current_length + 7] = new Array();//make it multi-dimensional
			smart_content_array[current_length + 7][0] = content_array[k];
			smart_content_array[current_length + 7][1] = org_acronym.replace(/^\s*\(*|\)*\s*$/g,""); //no leading/trailing space.
		}
		//First Name Last Name, Optional Suffix, modify original so additional search term includes possibility of middle initial or middle name.
		if(content_array[k].match(/^[A-Z][a-zA-Z]+\s[A-Z][a-zA-Z]+(,*\s*[SJ]r\.)*(\s*[IV])*$/)) { //
			current_length = smart_content_array.length;
			smart_content_array[current_length] = new Array();
			smart_content_array[current_length][0] = content_array[k];
			smart_content_array[current_length][1] = content_array[k].replace(/\s/,'\\s*[A-Z][a-zA-Z\\.]+\\s'); //replace first space with regexp to allow a middle initial or middle name.
		}
	}
	return smart_content_array;
}

function MakeURL(string){ //unused?
	return ('<span class="autolink"><a href="http://local.lancasteronline.com/15/'+escape(current_search)+'">'+string+'</a></span>' );
}

//function swap_content(ID,content){ //depreciated moved to dhtml.js

function toggle_linking(){
	tag_linking = !getCookie('no_tag_linking'); //get the cookie 'no_tag_linking'
	if(tag_linking){
		setCookie('no_tag_linking',1,86400*365); //one year.
	}else{
		setCookie('no_tag_linking',0,-1); //one second ago.
	}
	try{
		top.location.href=top.location.href;
	}catch(error){
		alert('Could not reload page. Please click the refresh/reload button in your browser');					
	}
}

function autolinkV2(tag_array,urlPrefix){
	appendArray = new Array();
	if(navigator.userAgent.indexOf('MSIE') == -1){
		appendArray[0] = new Array();
		appendArray[0]['name'] = 'drop_down';
		appendArray[0]['type'] = 'img';
		appendArray[0]['attributes'] = new Array();
		appendArray[0]['attributes']['src'] = 'http://articles.lancasteronline.com/images/reverse_carat.gif';
		appendArray[0]['attributes']['border'] = 0;
	}
		
	attributes = new Array();
	attributes['onClick'] = 'return clickreturnvalue()';
	//attributes['onMouseover'] = "dropdownmenu(this, event, makemenu(linkArray,TAGNAME,\'60px\')";
	attributes['onMouseout'] ="delayhidemenu()";
	//attributes['href'] = 'javascript:void(0);';
	attributes['class'] = 'autolink';
		
	//probably need to add an append function, to slap the image on the end of this, we'll see...
	//if that doesn't work, can always add the onMouseOver, etc to the attributes like it's commented above..., don't want to do that though...
	
	tag_array = sort_by_value_length(tag_array);
	expanded_array = expand_by_rules(tag_array);
	//need to add the following:
	//tags should be surrounded by a space or other word boundary character.
	//tag expansion needs to include (php regexp examples):
	/*plural_rules = array(
			'/(x|ch|ss|sh)$/'         => '\1es',       # search, switch, fix, box, process, address
			'/series$/'               => '\1series',
			'/([^aeiouy]|qu)ies$/'    => '\1y',
			'/([^aeiouy]|qu)y$/'      => '\1ies',      # query, ability, agency
			'/(?:([^f])fe|([lr])f)$/' => '\1\2ves',    # half, safe, wife
			'/sis$/'                  => 'ses',        # basis, diagnosis
			'/([ti])um$/'             => '\1a',        # datum, medium
			'/person$/'               => 'people',     # person, salesperson
			'/man$/'                  => 'men',        # man, woman, spokesman
			'/child$/'                => 'children',   # child
			'/s$/'                    => 's',          # no change (compatibility)
			'/$/'                     => 's'
	)
	*/
	//foreach over the tags, just slightly altering the attributes each time:
	for(i=0;i<expanded_array.length;i++){
		tagAttributes = attributes;
		attributes['onMouseover'] = "dropdownmenu(this, event, makemenu(linkArray,'"+escape(expanded_array[i][0])+"/'),'60px')";
		attributes['href'] = urlPrefix+'/1/'+escape(expanded_array[i][0])+"/";
		//tagattributes[''] = 'value'; //use to make minor changes to the attributes passed.
		linkString(expanded_array[i][1], document.getElementById('article_content'),tagAttributes,appendArray);
	}
	//foreach over the tags, just slightly altering the attributes each time:
	/*for(i=0;i<tag_array.length;i++){
		tagAttributes = attributes;
		attributes['onMouseover'] = "dropdownmenu(this, event, makemenu(linkArray,'"+escape(tag_array[i])+"/'),'60px')";
		attributes['href'] = urlPrefix+'/1/'+escape(tag_array[i])+"/";
		//tagattributes[''] = 'value'; //use to make minor changes to the attributes passed.
		linkString(tag_array[i], document.getElementById('article_content'),tagAttributes,appendArray);
	}*/
	//end for each
}
