// JavaScript Document
function adjust(){
	leftH = $('left').getHeight();
	rightH = $('right').getHeight();
	centerH = $('center').getHeight();
	minH = (leftH > rightH)? leftH:rightH;
	//minH += 60;
	//alert(centerH+' > '+minH);
	if(centerH < minH){
		$('center').setStyle({height:minH+'px'});
	}
	var fade = $('fade')
	if(fade){
		fade.style.height = $('border').getHeight()+'px';
	}
}

function validate(){
	pass = true;
	x = $(document.vform); // Enter Form Name !!!!
	for(a=0;a<=x.length;a++){
		if(x.elements[a]){
			thisLbl = $$('label[for="'+x.elements[a].id+'"]');
			if(thisLbl.length>0){
				if(x.elements[a].value == ''){
					thisLbl[0].style.color = '#FF0000';
					pass = false;
				}else{
					thisLbl[0].style.color = '#000000';
				}
			}
		}
	}
	if(!pass){
		alert('Please fill in the fields with red labels');
	}
	// if pass is still true submit the form
	if(pass){
		x.submit();
	}
}

function showPhoto(path){
	var photo = $('photo');
	photo.src = path;
	pageScroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	viewHeight = self['innerHeight'] || (document.documentElement['clientHeight'] || document.body['clientHeight']);
	
	var fade = $('fade');
	fade.style.display = 'block';
	
	var holder = $('photo_holder');
	holder.style.display = 'block';
	holder.style.top = pageScroll+'px';
}

function hidePhoto(){
	$('fade').hide();
	$('photo_holder').hide();
	$('photo').src = '/photos/loading.gif';
}

var isIE = (navigator.appName == "Microsoft Internet Explorer") ?true:false;
var pad = 5;
function showTip(tip, event) {
  var _x;
  var _y;
  if (!isIE) {
    _x = event.pageX + pad;
    _y = event.pageY + pad;
  }
  if (isIE) {
    _x = event.clientX + document.body.scrollLeft + pad;
    _y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop + pad;
  }
  $('tip').update(tip).setStyle({ left:_x +'px',top:_y +'px'}).show();
  
  return true;
}
function hideTip(){ $('tip').hide();}

function updateTxt(id){
	$(id+'Txt').innerHTML = $(id).value;
}

function copy(pre){
	num = 1;
	main = $(pre);
	last = main;
	while(cur = $(pre+num)){
		num++;
		last = cur;
	}
	last.insert({after: '<span id="F'+num+'"><a href="javascript:remove(\''+pre+num+'\');">Remove</a> '+main.innerHTML+'</span>'});
	cur = $(pre+num);
	cur.select('input').each( function(a){
		a.name = a.name+num;
		a.id = a.id+num;
	});
	cur.select('label').each( function(a){
		a.writeAttribute('for', a.readAttribute('for')+num);
	});
}
function remove(element){
	$(element).remove();
}

var archiveIndex = false;
var archiveShowDate = null;
var archiveShowSection = null;
var archiveSearch = false;
var runShort = false;
var runTime = null;

function startArchiveIndex(){
	var aDiv = $('archiveIndex');
	aDiv.insert({before:'<div id="archiveControls">Group By: <a onclick="archiveSort(0);" id="archiveBut0">Date</a> <a onclick="archiveSort(1);" id="archiveBut1">Section</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a onclick="archiveFindActivate();" id="archiveFindBut">Find:</a> <input type="text" size="10" onkeyup="runShort=false;archiveFind();" id="archiveFindTxt" onfocus="archiveFindActivate();"></div>'});
	if(archiveShowSection){
		archiveSort(1);
		if(archiveShowSection=='FeaturedArticle'){ archiveShowSection = "AAA_FeaturedArticle"; }
		var sepDiv = aDiv.select('.sep_s_'+archiveShowSection)[0];
		archiveShowClass('s_'+archiveShowSection,sepDiv);
	}else{
		archiveSort(0);
		var sepDiv = aDiv.select('.sep_d_'+archiveShowDate)[0];
		archiveShowClass('d_'+archiveShowDate,sepDiv);
	}
}
function archiveSort(by){
	$('archiveControls').select('a').invoke('removeClassName','sel');
	$('archiveBut'+by).addClassName('sel');
	var which = new Array('articleDate','articleSection');
	var aDiv = $('archiveIndex');
	var Articles = aDiv.select('table');
	Articles.each(function(a){ 
			if(archiveSearch){
				a.select('td').each( function(td){ td.update(td.innerHTML.replace(/<(\/)?dfn>/gi,'')); });
			} 
			a.hide();
		});
	$('archiveFindTxt').value='';
	archiveSearch = false;
	aDiv.select('.'+which[by],'.articleSectionPre').invoke('hide');
	var other = (by==0)? 1:0;
	aDiv.select('.'+which[other]).invoke('show');
	aDiv.select('.indexSep').invoke('remove');
	var aSections = new Array();
	var classes = new Array();
	var dateArr = new Array();
	var dateSort = '';
	for(var a=0;a<Articles.length;a++){
		classes = Articles[a].className.split(' ');
		dateArr = classes[0].split('_');
		dateSort = 1000000-parseInt(dateArr[1]+dateArr[2]);
		if(by==0){
			aSections.push(dateSort+'|'+classes[1]+'|'+Articles[a].id+'|'+classes[0]);
		}else{
			aSections.push(classes[1]+'|'+dateSort+'|'+Articles[a].id+'|'+classes[1]);
		}
	}
	aSections.sort();
	var sDiv = document.createElement('div');
	Element.extend(sDiv);
	aDiv.insert({top:sDiv});
	var preSec = '';
	var aArr = new Array();
	var thisA = null;
	var sectionTitle = '';
	//var sfa = null;
	for(var a=0;a<aSections.length;a++){
		aArr = aSections[a].split('|');
		thisA = $(aArr[2]);
		if(aArr[3] != preSec){
			//alert(thisA.innerHTML);
			if(aArr[3] == 's_ZZZ_pre2008' || aArr[1] == 's_ZZZ_pre2008'){
				sectionTitle = 'Pre-2008 Newsletters';
			}else{
				if(sfa = thisA.select('.'+which[by])[0]){
					sectionTitle = sfa.innerHTML;
				}else{
					sectionTitle = '';
				}
			}
			if(sectionTitle=='Featured Article'){ sectionTitle+='s'; }
			sDiv.insert({before:'<div class="indexSep sep_'+aArr[3]+'" onclick="archiveShowClass(\''+aArr[3]+'\',this);"><span>+</span><span style="display:none">&ndash;</span>'+sectionTitle+'</div>'});
		}
		preSec = aArr[3];
		sDiv.insert({before:thisA});
	}
	sDiv.remove();
}
function archiveShowClass(theClass,div){
	$('archiveIndex').select('.'+theClass).invoke('toggle');
	var theDiv = $(div);
	theDiv.select('span').invoke('toggle');
	if(theDiv.hasClassName('selSep')){
		theDiv.removeClassName('selSep');
	}else{
		theDiv.addClassName('selSep');
	}
}
function archiveFindActivate(on){
	var aDiv = $('archiveIndex');
	aDiv.select('table').invoke('show');
	aDiv.select('.indexSep').invoke('remove');
	aDiv.select('.articleDate','.articleSection','.articleSectionPre').invoke('show');
	$('archiveControls').select('a').invoke('removeClassName','sel');
	archiveTxt = $('archiveFindTxt');
	$('archiveFindBut').addClassName('sel');
	archiveTxt.value = '';
	archiveTxt.focus();
}
function doHighlight(bodyText, searchTerm){
	highlightStartTag = '<dfn>';
	highlightEndTag = '</dfn>';
	
	bodyText = bodyText.replace(/&amp;/g,'&');
 
	var newText = "";
	var i = -1;
	var lcSearchTerm = searchTerm.toLowerCase();
	var lcBodyText = bodyText.toLowerCase();
	var success = false;
	
	while (bodyText.length > 0) {
		i = lcBodyText.indexOf(lcSearchTerm, i+1);
		if (i < 0) {
		  newText += bodyText;
		  bodyText = "";
		} else {
			// skip anything inside an HTML tag
			if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
				// skip anything inside a <script> block
				if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
					newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
					bodyText = bodyText.substr(i + searchTerm.length);
					lcBodyText = bodyText.toLowerCase();
					i = -1;
					success = true;
				}
			}
		}
	}
	if(success){
		newText = newText.replace(/&/g,'&amp;');
		return newText;
	}else{
		return false;
	}
}
function archiveFind(){
	clearTimeout(runTime);
	var aDiv = $('archiveIndex');
	var searchText = $('archiveFindTxt').value.strip();
	var allArticles = aDiv.select('table');
	if(searchText.length > 2 || (runShort && searchText.length > 0)){
		runShort = false;
		archiveSearch = true;
		var thisText = '';
		for(var a=0;a<allArticles.length;a++){
			var newText = '';
			
			var thisText = allArticles[a].innerHTML.replace(/<(\/)?dfn>/gi,'');
			if(doHighlight(thisText, searchText)){
				allArticles[a].show();
				//allArticles[a].update(newText);
				var tds = allArticles[a].select('td');
				for(var td=0;td<tds.length;td++){
					var cleanText = tds[td].innerHTML.replace(/<(\/)?dfn>/gi,'');
					if(newText = doHighlight(cleanText, searchText)){
						isVisible = true;
						tds[td].update(newText);
					}else{
						tds[td].update(cleanText);
					}
				}
			}else{
				allArticles[a].hide();
			}
		}
	}else if(runShort){
		runShort = false;
		if(archiveSearch){
			aDiv.select('table').invoke('show');
			aDiv.select('td').each( function(td){ td.update(td.innerHTML.replace(/<(\/)?dfn>/gi,'')) });
		}
		archiveSearch = false;
	}else{
		runShort = true
		runTime = setTimeout('archiveFind()',1000);
	}
}

Event.observe(window, 'load', function() {
	adjust();
	Event.observe('george', 'click', function(){document.location = 'http://newsdev.gw.edu/admin/login.php';} );
	if(archiveIndex){ startArchiveIndex(); }
});