
$(document).ready(function(){
    if(document.getElementById('ctl00_ContentPlaceHolder1_StationTZ')) {
        calculate_time_zone();
    }
    if(document.getElementById('mainSlide')) {
		slideshow_init();
	}
});

//$(window).load(function() {
//	if (document.getElementById("giving-small")){
//		document.getElementById("giving-small").innerHTML = renderSmallFlashGraph("GivingFlash.xml");
//	}
//	if (document.getElementById("giving-big")){
//		document.getElementById("giving-big").innerHTML = renderBigFlashGraph("GivingFlash.xml");
//	}
//});

/* Playing Tabs */
var playing_current = '1';
function playing_show(num) {
	document.getElementById('play_'+playing_current).className = "";
	document.getElementById('playing_'+playing_current).className = "hide";
	document.getElementById('play_'+num).className = "on";
	document.getElementById('playing_'+num).className = "display";
	playing_current = num;
}


/* Sign-Up Popup Section */
/* ---------------------------------------------------------------------------- */
function signUp_init() {
	var object = document.createElement("div");
	object.id = "overlay";
	document.getElementsByTagName('div')[0].appendChild(object);
	var object = document.createElement("div");
	object.id = "termsbox";
	object.innerHTML = document.getElementById('terms').innerHTML;
	document.getElementsByTagName('div')[0].appendChild(object);
}
function signUp_open() {
	document.getElementById('overlay').className = "";
	document.getElementById('signUp').className = "";
	if(is_ie6) {
		hideSelects();
	}
}
function signUp_close() {
	document.getElementById('overlay').className = "hide";
	document.getElementById('signUp').className = "hide";
	if(is_ie6) {
		showSelects();
	}
}
function signUp_window() {
    var inputs = document.getElementById('ctl00_SignupFormPanel').getElementsByTagName('input');
    for(var x=0;x<inputs.length;x++) {
        if(inputs[x].name == "SignUpOption" && inputs[x].checked == true) {
            var newwindow = window.open(inputs[x].value, "SignUp", 'width=560,height=600,toolbar=0,resizable=1,status=0,scrollbars=1');
            newwindow.focus();
        }
    }
}

/* IE6 Hacks */
/* ---------------------------------------------------------------------------- */
scrollOverlay = function() {
	document.getElementById('overlay').style.top = document.documentElement.scrollTop + "px";
	if(document.getElementById('termsbox')) {
		document.getElementById('termsbox').style.marginTop = document.documentElement.scrollTop - 160 + "px";
	}
}
function hideSelects() {
	var selects = document.getElementsByTagName('select');
	for(var x = 0; x < selects.length; x++) {
		selects[x].style.visibility = "hidden";
	}
}
function showSelects() {
	var selects = document.getElementsByTagName('select');
	for(var x = 0; x < selects.length; x++) {
		selects[x].style.visibility = "visible";
	}
}


var slideshow_defaults = {slideSpeed:5000, fadeSpeed:25, fadeChange:5};
var slideshow = new Array();
function slideshow_init() {

	var frame = $('.slideshow');
	for(var y=0;y<frame.length;y++) {
		slideshow[y] = {id:'slideshow',status:0,rotate:null,fade:null,number:0,previous:0,count:0,opacity:0};
		
		var currentSlide = frame[y];
		currentSlide.style.position = "relative";
		if(currentSlide.id.length < 1) {
			currentSlide.id = "slideshow"+y;
		}
		slideshow[y].id = currentSlide.id;
		
		var slides = $(currentSlide).children('div');
		slideshow[y].count = slides.length;
		for(var x=0;x<slides.length;x++) {
			slides[x].className = "slide";
			slides[x].style.position = "absolute";
			slides[x].style.top = "0px";
			slides[x].style.left = "0px";
			if(x > 0) {
				slides[x].style.zIndex = "100";
				slides[x].style.opacity = "0";
				slides[x].style.filter = "alpha(Opacity=0)";
			}
			else {
				slides[0].style.zIndex = "200";
			}
		}
		slideshow_callInterval(y);
	    slideshow_activateNum(0,0);
	}

	$('.slideNext').click(function() {
		if(this.rel.length > 0) {
			var slideId = this.rel;
			var slideNum = slideshow_getFromId(slideId);
			slideshow_rotate(slideNum,'++');
		}
		else {
			for(var j=0;j<slideshow.length;j++) {
				slideshow_rotate(j,'++');
			}
		}
		return false;
	});
	$('.slidePrevious').click(function() {
		if(this.rel.length > 0) {
			var slideId = this.rel;
			var slideNum = slideshow_getFromId(slideId);
			slideshow_rotate(slideNum,'--');
		}
		else {
			for(var j=0;j<slideshow.length;j++) {
				slideshow_rotate(j,'--');
			}
		}
		return false;
	});
	$('.slidePause').click(function() {
		if(this.rel.length > 0) {
			var slideId = this.rel;
			var slideNum = slideshow_getFromId(slideId);
			if(slideshow[slideNum].status == 0) {
				clearTimeout(slideshow[slideNum].rotate);
				slideshow[slideNum].status = 1;
				$(this).removeClass('slidePause');
			}
			else {
				slideshow_callInterval(slideNum);
				slideshow[slideNum].status = 0;
				$(this).addClass('slidePause');
			}
		}
		else {
			for(var j=0;j<slideshow.length;j++) {
				if(slideshow[j].status == 0) {
					clearTimeout(slideshow[j].rotate);
					slideshow[j].status = 1;
				}
				else {
					slideshow_callInterval(j);
					slideshow[j].status = 0;
				}
			}
		}
		return false;
	});

}

function slideshow_getFromId(id) {
	var num = -1;
	for(var i=0;i<slideshow.length;i++) {
		if(id == slideshow[i].id) {
			num = i;
		}
	}
	return num;
}
function slideshow_callInterval(num) {
	slideshow[num].rotate = setTimeout(function() { slideshow_rotate(num); }, slideshow_defaults.slideSpeed);
}
function slideshow_state(y,num,state) {
	var node = $('#'+slideshow[y].id).children('.slide')[num];
	switch(state) {
		case 1:
			node.style.zIndex = "100";
			node.style.opacity = "0";
			node.style.filter = "alpha(Opacity=0)";
			break;
		case 2:
			node.style.zIndex = "150";
			node.style.opacity = "1";
			node.style.filter = "alpha(Opacity=100)";
			break;
		case 3:
			node.style.zIndex = "200";
			node.style.opacity = (slideshow[y].opacity / 100);
			node.style.filter = "alpha(Opacity="+slideshow[y].opacity+")";
			break;
	}
}
function slideshow_bounds(y,num) {
	if(num >= slideshow[y].count) {
		num = 0;
	}
	if(num < 0) {
		num = slideshow[y].count - 1;
	}
	return num;
}

function slideshow_rotate(y,operator) {
	clearInterval(slideshow[y].fade);
	clearTimeout(slideshow[y].rotate);
	
	if(slideshow[y].previous != slideshow[y].number) {
		// hide the previous slide
		slideshow_state(y,slideshow[y].previous,1);
		// set the previous slide to the current
		slideshow[y].previous = slideshow[y].number;
		slideshow[y].opacity = 50;
	}

	// increment the current slide
	if(operator == '++') {
		slideshow[y].number = slideshow[y].number + 1;
	}
	else if(operator == '--') {
		slideshow[y].number = slideshow[y].number - 1;
	}
	else {
		slideshow[y].number = slideshow[y].number + 1;
	}
	slideshow[y].number = slideshow_bounds(y,slideshow[y].number);
	
	// set the slides to the correct states
	slideshow_activateNum(slideshow[y].previous,slideshow[y].number);
	slideshow_state(y,slideshow[y].previous,2);
	slideshow_state(y,slideshow[y].number,3);
	
	slideshow[y].fade = setInterval(function() {slideshow_fade(y); }, slideshow_defaults.fadeSpeed);
}

function slideshow_jump(y,number) {
	clearInterval(slideshow[y].fade);
	clearTimeout(slideshow[y].rotate);
	
	if(slideshow[y].previous != slideshow[y].number) {
		// hide the previous slide
		slideshow_state(y,slideshow[y].previous,1);
		// set the previous slide to the current
		slideshow[y].previous = slideshow[y].number;
		slideshow[y].opacity = 50;
	}


	slideshow[y].number = number;
	
	// set the slides to the correct states
	slideshow_activateNum(slideshow[y].previous,slideshow[y].number);
	slideshow_state(y,slideshow[y].previous,2);
	slideshow_state(y,slideshow[y].number,3);
	
	slideshow[y].fade = setInterval(function() {slideshow_fade(y); }, slideshow_defaults.fadeSpeed);
}

function slideshow_fade(y) {
	var nextNode = $('#'+slideshow[y].id).children('.slide')[slideshow[y].number];
	slideshow[y].opacity = slideshow[y].opacity + slideshow_defaults.fadeChange;
	if(slideshow[y].opacity >= 100) {
		nextNode.style.opacity = "1";
		nextNode.style.filter = "alpha(Opacity=100)";
		// hide the previous slide
		slideshow_state(y,slideshow[y].previous,1);
		
		slideshow[y].opacity = 0;
		slideshow[y].previous = slideshow[y].number;
		clearInterval(slideshow[y].fade);
		if(slideshow[y].status == 0) {
			slideshow[y].rotate = setTimeout(function() { slideshow_rotate(y); }, slideshow_defaults.slideSpeed);
		}
	}
	else {
		nextNode.style.opacity = (slideshow[y].opacity / 100);
		nextNode.style.filter = "alpha(Opacity="+slideshow[y].opacity+")";
	}
}

function slideshow_activateNum(prev,current) {
    if(document.getElementById('mainSlideNumber'+(prev+1))) {
        document.getElementById('mainSlideNumber'+(prev+1)).className = "";
    }
    if(document.getElementById('mainSlideNumber'+(current+1))) {
        document.getElementById('mainSlideNumber'+(current+1)).className = "active";
    }
}

function slideshow_number(num) {
    slideshow_jump(0,num-1);
}

function open_window(url, name){
    newwindow = window.open(url, name, 'width=560,height=600,toolbar=0,resizable=1,status=0,scrollbars=1');
    newwindow.focus();
}

function LoadSearch(ddl,searchtext){

    var dropdown = document.getElementById(ddl);
    var searchtxt = document.getElementById(searchtext);
    var searchValue = searchtxt.value.replace(/[^A-Za-z0-9_ "'\.,+-]/gi,"");

    if (searchValue != '')
    {
        if (dropdown.value != '')
        {
            location.href = dropdown.value+"&SearchText="+escape(searchValue);
        }      
    }
}