// FLAGS
isIE = !!window.ActiveXObject;
downloadComplete = false;
// GLOBALS
var bodyTag, feedbackEl, indicatorNode, altUrl, mapType, mediaSource, unique;
var searchableMap, fadeDeck = new Array();
fadeDeck = [ ['card1'], ['card2'], ['card3'] ];
function $tco() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$tcoc(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}
function contextualSwitch() {
	var trigger = this;
	var target;
	for (i=1; i < 100; i++) {
		target = $tco(this.id + i);
		if ( target == undefined ) { i=101; break; }
		if ( target.className.indexOf('setRQ') != -1 ) { toggleReq(target); }
		if ( target.className.indexOf('setVIS') != -1 ) { toggle(target); }
	}
}
function toggleReq(el) {
	if ( el.tagName.toLowerCase() == 'label' || el.tagName.toLowerCase() == 'p' ) { 
		alert(el.tagName);
		if ( el.innerHTML.indexOf('required') != -1 ) { el.innerHTML = el.innerHTML.replace(/<em>(required)<\/em>/, ''); } 
		else { el.innerHTML += '<em>(required)</em>'; }
	}
	if ( el.tagName.toLowerCase() == 'input' ) {
		if ( el.className.indexOf('req') != -1) { 
			el.className = el.className.replace(/ req/, ''); 
			if ( el.className.indexOf('err') != -1 ) { el.className = el.className.replace(/err/, ''); } // if this is marked as a current error remove this as well since we are marking input as optional
		} else { el.className += ' req '; }
	}
}
function resetValidState() {
	var callingElement = this; 
	if (callingElement.className.indexOf('reqSet') != -1 ) {
		while ( callingElement.tagName.toLowerCase() != 'form' && callingElement.className.indexOf('reqSetContainer') == -1 )  { //  climb till we hit reqSetContainer with a safety stop at form el
			callingElement = callingElement.parentNode; 
		}
	}
	if (callingElement.className.indexOf('err') != -1) {
		callingElement.className = callingElement.className.replace(/ err/, '');
	}
}

function CHSCinit() {
	if(top.location != location) { top.location.href = document.location.href;}
	var el; // general use 
	if ( el = $tco('png')) { fixPng(el); el = null; }
	prepJSLinks();
	if ( document.getElementById('carousel') ) { 
		$(function() {
			$(".carousel").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				visible: 1
			});
		});
	}
	if (!!document.getElementById('fadeDeck')) { // set up fadeDeck; in place on index and retirement-communities
		topCard = 0;
		next = topCard + 1;
		lastCard = fadeDeck.length-1;
		iePre = null;
		iePost = null;
		opacStep = .05;
		opaque = 1;
		currentOpacity = 0;
		(isIE) ? setIEOpacity() : styler = 'opacity';
		homeGallery = window.setInterval('switchDeck()', 9000);
	}
	if (document.body.className && document.body.className.indexOf('autoDL') != -1) { autoDownload(); }
	
	if (el = document.getElementById('iconPrintPage')) { el.onclick = printPage; el = null; }
	if (el = document.getElementById('iconEmailPage')) { el.href += '&body=' + document.location; el = null; }
	if (el = document.getElementById('txtWidget')) { el.onclick = toggleTextSize; el = null; }
	if (el = document.getElementById('iconSizeText')) { el.onclick = toggleTextSize; el = null; }
}
function emailPage() {
	this.href += '&amp;body='+document.location;
	return true;
}
function initDynContent(){
	var el;
	if (document.getElementById('hg')) { 
		el = document.getElementById('imageGallery');
		crawl( el, prepHGLinks ); 
		fullSizePhoto = document.getElementById('bigPhoto');
		addEvent( fullSizePhoto, 'click', toggleHG );
	}
	if( $tco('media')) { addControls(); }
	if ( el = $tco('dynPng')) { fixPng(el); el=null; }
	if ( el = $tco('interfacePng')) { fixPng(el); el=null; } //  interfacePng is a js created img element used in the user feedback-error/success messaging
	if ( $tco('dynForm')) { 
		formStr = 'formData=';
		$tco('formSubmit').onclick = handleFormSubmission; 
		crawl($tco('dynForm'), formBehavior ); 
		feedbackEl = $tco('userFeedback');
	}
	if ( $tco('stepForm')) { 
		totalFormSteps = -1; // number of steps
		currentFormStep = 0; // users position in step order
		formStr = '';
		formStepsArray = new Array;
		masterReqSetArray = new Array;
		$tco('formSubmit').onclick = handleFormSubmission; 
		crawl($tco('stepForm'), formStepBehavior );
		$tco('totalPages').innerHTML = totalFormSteps + 1;
		feedbackEl = $tco('userFeedback'); 
	}
}
function prepJSLinks() {
		var x = document.links.length;
		for( i=0; i < x; i++ ) {
			if ( document.links[i].className.search(/linktype/) != -1 ) { document.links[i].onclick=handleJSClick;}
		}
	}
function handleJSClick() {
	if ( this.className.search(/overlay/) != -1 ) { startMedia.apply(this); return false; 
	} else if ( this.className.search(/contextual/) != -1 ) { contextLink.apply(this); return false;
	} else { return true; }
	
}
function contextLink() {
	var obj = this;
	do { 
		obj = obj.parentNode; 
	} while ( !obj.className || (!!obj.className && obj.className.indexOf('contextual') == -1) )
	if ( !!this.className || this.className.indexOf('expand') == -1 ) { toggleClass(obj, 'active'); } 
}
function setIEOpacity () {
	iePre = 'alpha(opacity=';
	iePost = ')';
	styler = 'filter';
	opaque = 100;  //fully opaque setting for ie
	opacStep = 5;  // adjust the step size to reflect larger opaque value
}
function runCard(card) {
	var limit = 0;
	var obj;
	limit = (card.length);
	
	for ( i = 0; i != limit; i++) { 
		switch ( card[i][1] ) {
			case 'c':
				obj = document.getElementById(card[i][0]); obj.className= card[i][2];
				break;
			
			case 'f':
				card[i][2](card[i][0]);
				break;
			
			case 'd':
				obj = document.getElementById(card[i][0]); obj.innerHTML= card[i][2];
			 	break;
			 
			default:
				 obj = document.getElementById(card[i][0]); obj.style[card[i][1]]= card[i][2];
		}
	}
}

function loadAlphaVid(vid) {
	var objCode = vid.innerHTML;
	objCode = objCode.replace(/<noscript>/gi,"");
	objCode = objCode.replace(/<\/noscript>/gi,"");
	if (readCookie('tcoVid') == null) { // cookie not found, vid not seen
		createCookie("tcoVid","true",true);
		objCode = objCode.replace(/button/gi,"autoPlay");
	}
	vid.innerHTML = objCode;// Embedding
}
function printPage() { window.print(); return false; }
function toggle(el) { el.style.display == 'block' ? el.style.display = 'none' : el.style.display = 'block'; }
function turnOn(caller) { $tco('overlay').style.display = 'block'; $tco( caller ).style.display = 'block' }
function fixPng (el) { // enables PNG transparency for IE 5-6; tag transparent png's with an id of png, then png1, then png2 and so on
	if ( isIE && ((navigator.userAgent.indexOf('MSIE 6') != -1) || (navigator.userAgent.indexOf('MSIE 5') != -1)) ) {
		var origSrc, ref, i;
		ref = el.id;
		i = 0;
		while(el) {
			origSrc = el.src;
			el.src = "images/x.gif";
			el.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + origSrc + "',sizingMethod='scale')";
			if (el.id.indexOf('interfacePng') != -1) { el.style.position = 'absolute'; el.style.top = 0; el.style.left = 0; }
			i++;
			el = document.getElementById(ref + i);
		}
	}
	el = null;
}
function attachSeries(elId, fn) {
	var temp;
	var i = 1;
	if (temp = $tco(elId)) { 
		do { temp.onclick = fn; } 
		while (temp = $tco(elId + i++));
	}
}
function crawl(el, fn) {  // takes an element and crawls every element inside and runs a specified function on it
	var level= 0;
	do {
		while ( el.firstChild ) {
			el = el.firstChild;
			fn(el);
			level++;
		}
		if ( el.nextSibling ) {
			el = el.nextSibling;
			fn(el);
		}
		else {
			climb:
			for ( i = level; i > 0; i-- ) {
				el = el.parentNode;
				level--;
				if ( el.nextSibling ) {
					el = el.nextSibling;
					fn(el);
					break climb;
				}
			}
		}
	} while ( level > 0 )
	return false;
}
function addEvent( obj, type, fn ) { // addEvent via Dustin Diaz at http://www.dustindiaz.com/rock-solid-addevent/
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window, 'load', CHSCinit);
addEvent(window,'unload',EventCache.flush);
