/*
Author: Joe Tan (joe.tan@trinetsolutions.com)
*/

// ********************
// General page scripts
// ********************

// get rid of convio stylesheet
Event.onReady(function() {
	$$('link[rel=stylesheet]').each(function(elem, i) {
		var sheets = ['CustomStyle.css', 'CustomWysiwygStyle.css'];
		for (var j=0;j<sheets.length;j++) {
			if (elem.href.indexOf(sheets[j]) > 0) {
		//console.log(elem.href, sheets[j])
				elem.disabled=true;
				elem.parentNode.removeChild(elem);
			}
		}
	});
});

Event.onReady(function() {
	var killRules = [
		// remove storybuilder page elements
		'.ConvioApp_95 #catselect',
		'.ConvioApp_95 .appArea table td[width=20%]', 
		'.ConvioApp_95 .appArea table td[width=2%]',
		'.ConvioApp_95 .appArea table td[colspan=3]'
	];
	for (var i=0;i<killRules.length;i++) {
		$$(killRules[i]).each(Element.hide);
	}
});

//
// Forms
//
Event.onReady(function() {
	$$('input.text').each(function(elem, i) {
		Event.observe(elem, 'focus', function(event) {
			if (elem.defaultValue == elem.value) elem.value='';
			Element.addClassName(elem, 'focused');
		});
	});
});


//
// Find Rep
//
Event.onReady(function() {
	$$('.ConvioApp_200 p.Smaller a').each(function(elem, i) {
		if (elem.innerHTML.length > 35) elem.innerHTML = elem.innerHTML.substring(0, 35)+'...';
		Element.addClassName(elem, 'truncated');
	});
	$$('.ConvioApp_200 .findrepComponent table td[width=45%]').each(function(elem, i) {
		if (i == 0) {
			Element.addClassName(elem, 'title');
			var h2 = document.createElement('h1');
			var strongs = elem.getElementsByTagName('strong');
			Element.wrap(strongs[0], h2);
			
		} else if (i == 1) {
			Element.addClassName(elem, 'switch');
		}
	});
})

//
// Bind print page events
//
Event.onReady(function() {

	var returnScroll = 0;
	var toPrint;
	var objs = $$('.print');
	objs.each(function(obj, i) {
		Event.observe(obj, 'click', function(event) {
			
			returnScroll = document.body.scrollTop;
			$$('link[media=screen]').each(function(css,j) {
				css.disabled=true;
			});
			$('stylesheet-print').media='';
			obj.href='#printer-friendly';
			
			window.scrollTo(0, 0)
			
			toPrint = setTimeout('window.print()', 1000);
			Event.stop(event);
			return false;
		});
	});
	
	if ($('print-done')) Event.observe('print-done', 'click', function(event) {
		$$('link[media=screen]').each(function(css,j) {
			css.disabled=false;
		});
		$('stylesheet-print').media='print';
		if (returnScroll > 0) {
			setTimeout('window.scrollTo(0, '+returnScroll+')', 10);
		}
		if (toPrint) clearTimeout(toPrint)
		Event.stop(event);
		return false;
	});
}); //onReady

//
// Bind font resizer
//
Event.onReady(function() {
	if (!$('banner')) return;
	var sizer = document.createElement('div');
	sizer.id = 'font-sizer';
	sizer.innerHTML = '<a href="/#small" class="small">A</a> | <a href="/#medium" class="medium">A</a> | <a href="/#large" class="large">A</a>';
	
	$('content-wrap').insertBefore(sizer, $('banner'));

	var ahrefs = $$('#font-sizer a');
	var found = false;
	ahrefs.each(function(ahref,i) {
		var fs = Element.getStyle(ahref, 'fontSize');
		Event.observe(ahref, 'click', function(event) {
			if (!fs) return false;
			setFontSize(fs);

			document.cookie = 'fontSize='+fs+'; expires=Thr, 1 Jan 2015 00:00:00 UTC; path=/';
			Element.siblings(ahref).each(function(s,i) {
				Element.removeClassName(s, 'selected');
			});
			Element.addClassName(ahref, 'selected');
			Event.stop(event);
			return false;
		});
		
		if (document.cookie.indexOf('fontSize='+fs) >= 0) {

			setFontSize(fs);
			Element.addClassName(ahref, 'selected');
			found = true;
		}
	});
	if (!found) Element.addClassName(ahrefs[1], 'selected');

});

function setFontSize(fs) {
	Element.setStyle($('content-wrap'), {
		fontSize:fs
	});
	var rules = ['body', 'p', 'li', 'ol', 'ul', '.Explicit']; //'table', 'tr', 'th', 'td', 
	for (var r=0;r<rules.length;r++) {
		$$(rules[r]).each(function(tag) {
			if (!Element.hasClassName(tag, 'ObjTitle')) Element.setStyle(tag, {fontSize:fs});
		});
	}
}

// pop open links
Event.onReady(function() {
	var types = ['pdf'];
	$$('#content a').each(function(elem, i) {
		for (var i=0;i<types.length;i++) {
			if (elem.getAttribute('href') && elem.getAttribute('href').indexOf('.'+types[i]) >=0) {
				Event.observe(elem, 'click', function(event) {
					var w = window.open(elem.href);
					w.focus();
					Event.stop(event);
				});
			}
		}
	});
});

// Setup Lightbox Style functionality
if (typeof lightwindow != 'undefined')
Event.observe(window, 'load', function() {
	myLightWindow = new lightwindow({
		classNames: {
			standard:'popup',
			action:'popup_action'
		},
		overlay : {
			opacity: 0.7,
			image : '/trinet/site/js/lightwindow/images/black.png',
			presetImage : '/trinet/site/js/lightwindow/images/black-70.png'
		}
	});
	lbox = $$('.popup');
	lbox.each(function(obj, i) {
		var pos = lbox[i].href.indexOf('#')
		if (pos >= 0) {
			var popupdiv = $(lbox[i].href.substring(pos+1))
			if (popupdiv) {
				Element.setStyle(popupdiv, {display:'none'});
				Element.remove(popupdiv);
				Element.addClassName(popupdiv, 'lightbox');
				document.getElementsByTagName('body')[0].appendChild(popupdiv);
			}
		}
	});
});

//
// This creates hidden / reveal functionality for a set of given headers. 
// Content to be revealed for current header is assumed to be all HTML nodes until the next header is encountered
//
function initializeContentDropdown(tag, parentDiv) {
	var id = 0;
	if (!parentDiv) parentDiv = 'content2';
	Element.addClassName(parentDiv, 'contentdropdown');
	$(parentDiv).immediateDescendants().each(function(elem,i) {
		if (id < 0) {
			return;
		} else if (elem.tagName.toLowerCase() == 'hr') {
			id = -1;
			return;
		} else if (elem.tagName.toLowerCase() == tag.toLowerCase()) {
			id++;
			elem.id = 'content-'+id;
			Element.addClassName(elem, 'header');

			Event.observe(elem, 'click', function(event) {
				if (Element.hasClassName(elem, 'header-opened')) {
					Element.removeClassName(elem, 'header-opened');
					$$('#content .'+elem.id).each(function(elem2) { 
						if (!Element.hasClassName(elem2, 'visible')) {
							Effect.BlindUp(elem2, {duration:0.2, afterFinish:fixFooter});
							Element.removeClassName(elem2, 'opened');
						}
					});

				} else {
					Element.addClassName(elem, 'header-opened');
					$$('#content .'+elem.id).each(function(elem2) {
						if (!Element.hasClassName(elem2, 'visible')) {
							Effect.BlindDown(elem2, {duration:0.2, afterFinish:fixFooter});
							Element.addClassName(elem2, 'opened');
						}
					});
				}
				Event.stop(event);
			});
			
		} else if (id > 0) {
			Element.addClassName(elem, 'content-'+id);
			Element.addClassName(elem, 'content');
			if (!Element.hasClassName(elem, 'visible')) {
				Element.setStyle(elem, {display:'none'});
			}
		}
	});
	
	$$('a.expand').each(function(elem, i) {
		Event.observe(elem, 'click', function(event) {
			var id = 0;
			$(parentDiv).immediateDescendants().each(function(elem,i) {
				if (id < 0) {
					return;
				} else if (elem.tagName.toLowerCase() == 'hr') {
					id = -1;
					return;
				} else if (elem.tagName.toLowerCase() == tag.toLowerCase()) {
					if (!Element.hasClassName(elem, 'header-opened')) {
						Element.addClassName(elem, 'header-opened');
						$$('#content .'+elem.id).each(function(elem2) {
							if (!Element.hasClassName(elem2, 'visible')) {
								Effect.BlindDown(elem2, {duration:0.2, afterFinish:fixFooter});
								Element.addClassName(elem2, 'opened');
							}
						});
					}
				}
			});
			
			Event.stop(event);
			
		});
	});
}

// fix ie bug
function fixFooter() {
	$('footer').setStyle({paddingBottom:'1px'}).setStyle({paddingBottom:'0px'});
}

/* Formatting on legislation_updates (replaces a set of *'s with an HTML list */ 
if(document.location.href.indexOf('/legislation/legislative_updates') != -1)
Event.onReady(function() {
	$$('td p a.NewsHeadlineList').each(function(elem, i) {
		var elem2 = $(elem).up().up();
		var listItems = elem2.innerHTML;
		if (listItems.indexOf('*') < 0) return;
		
		listItems = listItems.replace(/\*/,"<ul class=\"summaryText\"><li>");
		listItems = listItems.replace(/\*\s*/g,"</li><li>");
		listItems = listItems.replace(/<p>/i,"<div>");
		listItems = listItems.replace(/<li>\s*<\/p>/i,"</ul></div>");
		elem2.innerHTML = listItems;
	});
});

/* Turn | into br tags in subheadings since convio doesn't allow it */
if(document.location.href.indexOf('news_iv_ctrl=1701') != -1)
Event.onReady(function() {
	$$('#content p.subheading')[0].innerHTML = $$('#content p.subheading')[0].innerHTML.replace(/\s*\|\s*/g,"<br>");		
	$$('#content p.subheading')[0].innerHTML = $$('#content p.subheading')[0].innerHTML.replace(/\s*\|\|\s*/g,"<br><br>");		
});


// Added by Diana Laforteza 12/01/08
// This formats the way publication images are displayed in the Publications page
Event.onReady(function() {
	if(document.location.href.indexOf('/issues/publications') != -1) {
		$$('#publications img').each(function(elem, i) {
			var img = $(elem).up().innerHTML;
			img = img.replace("border=\"0\"","border=\"0\" style=\"width: 100px; float: left; margin-right: 5px; margin-top: 5px; margin-bottom: 10px; padding: auto;\"");
			$(elem).up().innerHTML = img;
		});
		
		$$('#publications p').each(function(elem,i) {
			var paragraph = $(elem).up().innerHTML;
			paragraph = paragraph.replace("></a><br>","></a>");
			$(elem).up().innerHTML = paragraph;
		});
	}
})