addEvent(window, 'load', externalLinks);
// Opens external window from a link via rel="external"; mimics target="_blank", a W3C deprecated attribute
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') anchor.target = '_blank';
	}
}
// Adds an event to the page
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	} else {
		return false;
	}
}
// Invoke mouseover menu functionality for IE
startList = function() {
	if (document.all && document.getElementById('mainNav')) {
		var node = document.getElementById('mainNav').getElementsByTagName('li');
		for (i=0; i<node.length; i++) {
			if (node[i].getElementsByTagName('ul').length > 0) {
				node[i].onmouseover = function() {
					this.className += ' over';
				}
				node[i].onmouseout = function() {
					this.className = this.className.replace(new RegExp(' over\\b'), '');
				}
			}
		}
	}
}
addEvent(window, 'load', startList);
// Rotating banner ads
var iCurrentAd = function() {
	if (iAdIndex == aAdVals.length) iAdIndex = 0;
	var iReturnVal = aAdVals[iAdIndex];
	iAdIndex++;
	return iReturnVal;
}
var bPlay = 1;
function adTransition() {
	Element.hide('currentAd');
	var iAd = iCurrentAd();
	var onComplete = function() {
		new Effect.Appear('currentAd');
		if (aAdVals.length <= 1) {
			PE.stop();
		}
	}
	new Ajax.Updater('currentAd', 'http://clcaustin.com/wp-content/themes/clc/updater.bannerAds.php?bPlay='+bPlay+'&iAdId='+iAd+'&sUrl=http://clcaustin.com', {onComplete:onComplete});
	new Ajax.Updater('adNav', 'http://clcaustin.com/wp-content/themes/clc/updater.bannerAds.php?bNav=1&bPlay='+bPlay+'&iAdId='+iAd);
	return false;
}
PeriodicalExecuter.prototype.registerCallback = function() {
    this.intervalID = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
}
PeriodicalExecuter.prototype.stop = function() {
    clearInterval(this.intervalID);
}
function adTransitionReset() {
	if (bPlay) {
		PE.stop();
		PE = new PeriodicalExecuter(adTransition, iFreq);
	}
	return adTransition();
}
function adControl(sAction) {
	switch (sAction) {
		case 'previous':
			iAdIndex = (iAdIndex == 1) ? aAdVals.length - 1 : iAdIndex - 2;
			adTransitionReset();
		break;
		case 'next':
			adTransitionReset();
		break;
		case 'play':
			if (!bPlay) {
				bPlay = 1;
				Element.hide('adControl_play');
				Element.show('adControl_pause');
				adTransitionReset();
			}
		break;
		case 'pause':
			if (bPlay) {
				PE.stop();
				bPlay = 0;
				Element.hide('adControl_pause');
				Element.show('adControl_play');
			}
	}
	return false;
}
function toggleSiteMap() {
	var onComplete = function() {
		new Effect.toggle('siteMapContainer', 'slide');
	}
	if ($('siteMapContainer').empty()) {
		new Ajax.Updater('siteMapContainer', '/sitemap?a=1', {method:'post', evalScripts:true, onComplete:onComplete});
	} else onComplete();
	return false;
}
// Tabbed content
var enabletabpersistence=1 //enable tab persistence via session only cookies, so selected tab is remembered?
var tabcontentIDs=new Object()
function expandcontent(linkobj){
var ulid=linkobj.parentNode.parentNode.id //id of UL element
var ullist=$(ulid).getElementsByTagName("li") //get list of LIs corresponding to the tab contents
for (var i=0; i<ullist.length; i++){
ullist[i].className=""  //deselect all tabs
if (typeof tabcontentIDs[ulid][i]!="undefined") //if tab content within this array index exists (exception: More tabs than there are tab contents)
$(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents
}
linkobj.parentNode.className="current"  //highlight currently clicked on tab
$(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content
saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"))
}
function savetabcontentids(ulid, relattribute){// save ids of tab content divs
if (typeof tabcontentIDs[ulid]=="undefined") //if this array doesn't exist yet
tabcontentIDs[ulid]=new Array()
tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute
}
function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie
if (enabletabpersistence==1) //if persistence feature turned on
SetCookie(ulid, selectedtabid, 1)
}
function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
var ullist=$(ulid).getElementsByTagName("li")
for (var i=0; i<ullist.length; i++){
if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
return ullist[i].getElementsByTagName("a")[0]
break
}
}
}
function initializetabcontent(){
for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
if (enabletabpersistence==0 && GetCookie(arguments[i])!="") //clean up cookie if persist=off
SetCookie(arguments[i], "", 1)
var clickedontab=GetCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any
var ulobj=$(arguments[i])
var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
for (var x=0; x<ulist.length; x++){ //loop through each LI element
var ulistlink=ulist[x].getElementsByTagName("a")[0]
if (ulistlink.getAttribute("rel")){
savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs
ulistlink.onclick=function(){
expandcontent(this)
return false
}
if (ulist[x].className=="current" && clickedontab=="") //if a tab is set to be selected by default
expandcontent(ulistlink) //auto load currenly selected tab content
}
} //end inner for loop
if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value
var culistlink=getullistlinkbyId(arguments[i], clickedontab)
if (typeof culistlink!="undefined") //if match found between tabcontent id and rel attribute value
expandcontent(culistlink) //auto load currenly selected tab content
else //else if no match found between tabcontent id and rel attribute value (cookie mis-association)
expandcontent(ulist[0].getElementsByTagName("a")[0]) //just auto load first tab instead
}
} //end outer for loop
}