
//////////////////////////////////////////////////////////
// HOW TO DECLARE A ROTATING FUNCTION WITH MOOTOOLS //////
//////////////////////////////////////////////////////////
/*
(function(){
    alert('Cookie Monster must of eaten it!');
}).periodical(5000);
*/

var thisDomain = "http://"+document.domain+"/";

////////////////////////////////////////////
// MY HISTORY STUFF   //////////////////////
////////////////////////////////////////////
var theHistory = new Array();
var histIndexNow = null;
function makeHistory(newPage) {
	if(histIndexNow < theHistory.length-1) {
		theHistory.splice(histIndexNow+1,theHistory.length-(histIndexNow+1));
	}
	theHistory.push(newPage);
	histIndexNow = (histIndexNow == null) ? 0: histIndexNow+1;
	//alert(theHistory+" => "+theHistory.length+", indexNow => "+histIndexNow);
	checkHistoryButtons();
}

function timeTravel(direction) {
	$('pagePrev').blur();
	$('pageNext').blur();
	$('pagePrev2').blur();
	$('pageNext2').blur();
	if(direction == "prev" && histIndexNow != 0 && switching != true) {
		histIndexNow--;
		linkEvent(thisDomain+theHistory[histIndexNow],true);
	}
	else if(direction == "next" && histIndexNow < theHistory.length-1 && switching != true) {
		histIndexNow++;
		linkEvent(thisDomain+theHistory[histIndexNow],true);
	}
	//alert(theHistory+" => "+theHistory.length+", indexNow => "+histIndexNow);
	checkHistoryButtons();
}

function checkHistoryButtons() {
	var prevRem = (histIndexNow == 0) ? "active": "inactive";
	var prevAdd = (histIndexNow == 0) ? "inactive": "active";
	$('pagePrev').removeClass(prevRem);
	$('pagePrev').addClass(prevAdd);
	$('pagePrev2').removeClass(prevRem);
	$('pagePrev2').addClass(prevAdd);

	
	var nextRem = (histIndexNow >= theHistory.length-1) ? "active": "inactive";
	var nextAdd = (histIndexNow >= theHistory.length-1) ? "inactive": "active";
	$('pageNext').removeClass(nextRem);
	$('pageNext').addClass(nextAdd);
	$('pageNext2').removeClass(nextRem);
	$('pageNext2').addClass(nextAdd);

}

function lyricsPopup(theID) {
	window.open("/lyrics.php?ID="+theID,"lyricsWindow","menubar=0,resizable=0,location=0,status=0,scrollbars=0,width=450,height=540"); 
}

function bigImagePopup(theImage) {
	window.open("/bigpicture.php?image="+theImage,"imageWindow","menubar=0,resizable=0,location=0,status=0,scrollbars=0,width=450,height=540"); 
}



function putVidVarInHash(theID) {
	if(document.location.hash != "") {
		var currentLink = (document.location.hash.search(/v/) == -1) ? "v0"+document.location.hash.substr(1): document.location.hash.substr(1);
		var theVars = ripHashVars(currentLink);
		var vidVar = "v"+theID;
		var catVar = (theVars.catNum) ? "c"+theVars.catNum: "";
		var itemVar = (theVars.itemNum) ? "i"+theVars.itemNum: "";
		var finalHash = vidVar+catVar+itemVar;
	}
	else {
		var finalHash = "v"+theID;	
	}
	document.location.hash = finalHash;
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function getMedia(theNum) {
	if(flashExists == true) {
		var myFx = new Fx.Scroll(window,{transition: Fx.Transitions.Quad.easeInOut}).toTop();
		getFlashMovie('bobPlayer').standAloneMedia(theNum);
	}
	else {
		alert('This media will not play until you download the Adobe Flash Plugin.');
	}
}



function scrollThis() {
	var scroll = new Fx.Scroll(window, {offset: {'x': 0, 'y': 347},transition: Fx.Transitions.Quad.easeInOut}).toTop();
}

//SEARCHBOX ONFOCUS/ONBLUR
function flipInputBG(input,txt,state) {
	var theInput = document.getElementById(input);
	if(state == "on" && theInput.value == txt) {
		theInput.value = "";
	}
	else if(state == "off" && (theInput.value == "" || theInput.value == " ")) {
		theInput.value = txt;
	}
}

/////////////////////////////
// PAGE SWITCHING ///////////
/////////////////////////////

//ARRAY OF TOP LEVEL NAV ITEMS
var navCat = new Array('ontheshow','blogs','staff','photos','podcasts','contact','search');
var currentCat;
var currentItem;
var switching = false;
var postString;

function initiateLinks(section) {
	if(section == "#navlinks a") {
		var navLinks = $$(section);
		var theLinks = navLinks;
	}
	else if(section == "#itemList a") {
		var catLinks = $$(section);
		var theLinks = catLinks;
	}
	else if(section == "#theItem a") {
		var itemLinks = $$(section);
		var theLinks = itemLinks;
	}
	else if(section == ".element a") {
		var accLinks = $$(section);
		var theLinks = accLinks;
	}
	theLinks.each(function(theLink) {
		if(section == "#navlinks a") {
			if(theLink.id == "searchTab") {
				//var linkStyle = new Fx.Style(theLink, 'background').set("transparent url('/images/searchIconOff.gif') center center no-repeat");
				//theLink.removeClass('searchTabOn');theLink.addClass('searchTabOff');
				theLink.addEvent('mouseover', function() { if(theLink.hasClass("current") != true) { theLink.removeClass('searchTabOff'); theLink.addClass('searchTabOn'); }});
				theLink.addEvent('mouseout', function() { if(theLink.hasClass("current") != true) { theLink.removeClass('searchTabOn'); theLink.addClass('searchTabOff'); }});
			}
			else {
				var linkColor = new Fx.Styles(theLink, {duration:200, wait:false});
				theLink.addEvent('mouseover', function() { if(theLink.hasClass("current") != true) { linkColor.start({'color': '#ffffff'}); }});
				theLink.addEvent('mouseout', function() { if(theLink.hasClass("current") != true) { linkColor.start({'color': '#fffc00'}); }});
			}
		}
		theLink.addEvent('click', function(e) {
			if((theLink.toString().match(/http:\/\/www.bobrivers.com/) != null || theLink.toString().match(/http:\/\/bobrivers.com/) != null) && theLink.toString().match(/@bobrivers.com/) == null) {
				new Event(e).stop();
				if(switching != true) {
					linkEvent(theLink);
				}
			}
		});
	});
}

function linkEvent(thisLink,isHistory) {
	
	var theVars = ripLinkVars(thisLink);
	document.location.hash = "v"+theVars.vidNum+theVars.theHash;
	if(!isHistory) { makeHistory(theVars.theHash); }
	var catVar = theVars.catNum;
	var itemVar = (!theVars.itemNum) ? null : theVars.itemNum;
	
	if(catVar == currentCat && itemVar != null) {
		switchItem(catVar,itemVar);
	}	
	else {
		var thisNav;
		var navLinks = $$('#navlinks a');
		navLinks.each(function(element) { 
			var catCompare1 = element.toString().replace(thisDomain+"c", "");
			var catCompare2 = catVar;
			if(catCompare1 == catCompare2) { 
				thisNav = element; 
			} 
		});
		
		navLinks.each(function(el) { 
			if(el.id == "searchTab") {
				el.removeClass('searchTabOn');
				el.addClass('searchTabOff');
			}
			else {
				el.style.color = "#fffc00";
			}
			el.removeClass("current");
		});
		
		if(thisNav.id == "searchTab") {
			thisNav.removeClass('searchTabOff');
			thisNav.addClass('searchTabOn');
		}
		else {
			thisNav.style.color = "#ffffff";
		}
		thisNav.addClass("current");
		window.focus();
					
		var navlinkLeft = thisNav.offsetLeft;
		var navlinkWidth = thisNav.offsetWidth;
		var slideSelector = new Fx.Styles('navselector', {duration:300, transition: Fx.Transitions.Cubic.easeInOut, wait:false}).start({'left': navlinkLeft});
		var sizeSelector = new Fx.Styles('navselectorLeft', {duration:300, transition: Fx.Transitions.Cubic.easeInOut, wait:false}).start({'width' : navlinkWidth-4})
		.chain(function() {
			switchCat(catVar,itemVar);
		});
	}
}


function switchCat(catID,itemID) {
	currentCat = catID;
	currentItem = itemID;
	switching = true;
	
	var contentFade = new Fx.Styles('content', {duration: 200, wait: true});
	var contentResize = new Fx.Styles($('main'), {duration: 200, transition: Fx.Transitions.Cubic.easeInOut, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		
		var thisPage = navCat[catID];
		
		new Ajax("page.php", { method: 'get', data: "catID="+catID+"&itemID="+itemID, update: $('content'), evalScripts:true,
			onComplete: function() {
				var minHeight = ($('sideBar').offsetHeight > $('itemList').offsetHeight) ? $('sideBar').offsetHeight: $('itemList').offsetHeight;
				var newHeight = (minHeight < $('content').offsetHeight) ? $('content').offsetHeight : minHeight;
				contentResize.start({'height': newHeight})
				.chain(function() {
					
					//RESET MAIN HEIGHT
					$('main').setStyle('height', 'auto');
					if(catID == 0 || catID == 1 || catID == 2 || catID == 4) { playAccordion(); }
					contentFade.start({'opacity': 1}) 
					.chain(function() {
												
						initiateLinks('#itemList a');
						if(catID != 3) { initiateLinks('#theItem a'); }
						if(catID == 3) { initiateThumbs(); }
						switching = false;
					});
				});
			},
			onFailure: function() { 
				$('content').innerHTML = "<center><h1>Sorry...page didn't load!</h1></center>";
				$('main').setStyle('height', 'auto');
				$('content').setStyle('height', 'auto');
				$('content').setStyle('opacity', 1);
				initiateLinks('#theItem a');
				switching = false;
			}})
		.request();
	});
}

function switchItem(catID,itemID) {
	
	currentItem = itemID;
	var currentLink = (document.location.hash.search(/v/) == -1) ? "v0"+document.location.hash.substr(1): document.location.hash.substr(1);
	var theVars = ripHashVars(currentLink);
	var vidVar = theVars.vidNum;
	document.location.hash = "v"+vidVar+"c"+catID+"i"+itemID;
	
	switching = true;
	var contentFade = new Fx.Styles('theItem', {duration: 200, wait: true});
	var contentResize = new Fx.Styles($('main'), {duration: 200, transition: Fx.Transitions.Cubic.easeInOut, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		
		new Ajax("item.php", { method: 'get', data: "catID="+catID+"&itemID="+itemID, update: $('theItem'), evalScripts:true,
			onComplete: function() {
				var minHeight = ($('sideBar').offsetHeight > $('itemList').offsetHeight) ? $('sideBar').offsetHeight: $('itemList').offsetHeight;
				var newHeight = (minHeight < $('theItem').offsetHeight) ? $('theItem').offsetHeight : minHeight;
				contentResize.start({'height': newHeight})
				.chain(function() {
					$('main').setStyle('height', 'auto');
					$('content').setStyle('height', 'auto');
					contentFade.start({'opacity': 1}) 
					.chain(function() {
						if(catID != 3) { initiateLinks('#theItem a'); }
						if(catID == 3) { initiateThumbs(); }
						switching = false;
					});
				});
			},
			onFailure: function() { 
				$('theItem').innerHTML = "<center><h1>Sorry...page didn't load!</h1></center>";
				$('main').setStyle('height', 'auto');
				$('content').setStyle('height', 'auto');
				$('theItem').setStyle('opacity', 1);
				initiateLinks('#theItem a');
				switching = false;
			}})
		.request();
	});
}










/////////////////////////////////////////////////
//PHOTO GALLERY       ///////////////////////////
/////////////////////////////////////////////////
function initiateThumbs() {
	var stink = 0;
	var theThumbs = $$('#galleryItems a');
	theThumbs.each(function(thumb) {
		var thisDesc = thumb.getProperty('title');
		var thumbFade = new Fx.Styles(thumb, {duration: 200, wait: false}).start({'opacity': 0.4});
		var thumbBorder = new Fx.Styles(thumb, {duration: 200, wait: false});
		thumb.addEvent('mouseover', function(e) { 
			thumb.setAttribute('title','');
			thumbFade.start({'opacity': 1});
			stink = setTimeout(function () { toggleToolTip(thumb,"on",thisDesc); }, 400);
		});
		thumb.addEvent('mouseout', function(e) { 
			thumb.setAttribute('title',thisDesc);
			thumbFade.start({'opacity': 0.4});
			toggleToolTip(thumb,"off","");
			clearTimeout(stink);
		});
		thumb.addEvent('click', function(e) {
			new Event(e).stop();
			if(switching != true) {
				thumb.blur();
				toggleToolTip(thumb,"off","");
				var theURL = thumb.toString();
				var theIDs = theURL.replace(thisDomain, "");
				var theIDsArray = theIDs.split("-");
				viewPhoto(theIDsArray[0],theIDsArray[1]);
			}
		});
	});
}

function toggleToolTip(element,way,descText) {
	$('descriptionBox').setStyle('display','none');
	
	if(way == "on" && descText != "") {
		
		var thmTop = (element.getTop()+7)-$('theItem').getTop();
		var thmLeft = (element.getLeft()+30)-$('theItem').getLeft();
		
		$('descriptionBox').innerHTML = descText;
		$('descriptionBox').setStyle('opacity',0);
		$('descriptionBox').setStyle('display','block');
		
		var correction = $('descriptionBox').offsetHeight;
		$('descriptionBox').setStyle('top',thmTop-correction);
		$('descriptionBox').setStyle('left',thmLeft);
		
		var fadeTip = new Fx.Styles($('descriptionBox'), {duration: 400, wait: false}).start({'opacity': 1});
	}
}

function viewPhoto(itemID,photoID) {
	switching = true;
	var contentFade = new Fx.Styles('viewerContent', {duration: 200, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		new Ajax("viewerresults.php", { method: 'get', data: "itemID="+itemID+"&photoID="+photoID, update: $('viewerContent'), evalScripts:true,
			onComplete: function() {
				contentFade.start({'opacity': 1}) 
				.chain(function() {
					switching = false;
				});
			},
			onFailure: function() { 
				alert('Sorry! Page did not load.');
				$('viewerContent').setStyle('opacity', 1);
				switching = false;
			}})
		.request();
	});
}

function switchPhotoPage(itemID,pageNum) {
	switching = true;
	var contentFade = new Fx.Styles('gallery', {duration: 200, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		new Ajax("thumbresults.php", { method: 'get', data: "itemID="+itemID+"&photoPage="+pageNum, update: $('gallery'), evalScripts:true,
			onComplete: function() {
				contentFade.start({'opacity': 1}) 
				.chain(function() {
					switching = false;
					initiateThumbs();
				});
			},
			onFailure: function() { 
				alert('Sorry! Page did not load.');
				$('gallery').setStyle('opacity', 1);
				switching = false;
			}})
		.request();
	});
}










function ripLinkVars(element) {
	var currentLink = (document.location.hash.search(/v/) == -1) ? "v0"+document.location.hash.substr(1): document.location.hash.substr(1);
	var theVars = ripHashVars(currentLink);
	var vidVar = theVars.vidNum;

	var theURL = element.toString();
	theHash = theURL.replace(thisDomain, "");
	var q = theHash.split(/\D/);
	if(!q[0]) {
		var catNum = q[1];
		var itemNum = q[2];
	}
	else {
		var catNum = q[0];
		var itemNum = q[1];
	}
	return {theHash:theHash, vidNum:vidVar, catNum:catNum, itemNum:itemNum};
}

function playAccordion() {
	var accordion = new Accordion('h3.atStart', 'div.atStart', {
		opacity: false,
		transition: Fx.Transitions.Cubic.easeOut, 
		wait: true,
		display:0,
		onActive: function(toggler, element){
			//element.setStyle('background-color', '#3a5b86');
			toggler.setStyle('background', '#3a5b86 url(../images/togglertop.gif) top left no-repeat');
		},
		onBackground: function(toggler, element){
			//element.setStyle('background-color', '#3a5b86');
			toggler.setStyle('background', '#3a5b86 url(../images/togglertop.gif) top left no-repeat');
		}
	}, $('accordion'));
}


/////////////////////////////////////////////
//ON THE SHOW WEEK SWITCHER /////////////////
/////////////////////////////////////////////
function switchShowWeek(firstDate) {
	switching = true;
	var contentFade = new Fx.Styles('showBox', {duration: 200, wait: true});
	var contentResize = new Fx.Styles($('main'), {duration: 200, transition: Fx.Transitions.Cubic.easeInOut, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		
		new Ajax("showResults.php", { method: 'get', data: "firstDate="+firstDate, update: $('showBox'), evalScripts:true,
			onComplete: function() {
				var minHeight = ($('sideBar').offsetHeight > $('itemList').offsetHeight) ? $('sideBar').offsetHeight: $('itemList').offsetHeight;
				var newHeight = (minHeight < $('theItem').offsetHeight) ? $('theItem').offsetHeight : minHeight;
				contentResize.start({'height': newHeight})
				.chain(function() {
					$('main').setStyle('height', 'auto');
					playAccordion();
					$('content').setStyle('height', 'auto');
					contentFade.start({'opacity': 1}) 
					.chain(function() {
						initiateLinks('#itemList a');
						switching = false;
					});
				});
			},
			onFailure: function() { 
				alert('Sorry! Page did not load.');
				$('main').setStyle('height', 'auto');
				$('content').setStyle('height', 'auto');
				$('showBox').setStyle('opacity', 1);
				initiateLinks('#theItem a');
				switching = false;
			}})
		.request();
	});
}




///////////////////////////
//SEARCH //////////////////
///////////////////////////

//TOP RIGHT SEARCH
var bodyScroll = new Fx.Scroll($(document.body));
function topSearch() {
	var keywords = $('searchtextTop').value;
	if(keywords == "Search BobRivers.com" || keywords == "") {
		alert('Please enter search keywords.');
	}
	else {
		if(currentCat != 6) {
			thisSearch.set('thisSearch',keywords);
			linkEvent(thisDomain+'c6');
		}
		else {
			switchSearch(keywords,"");
			$('searchtext').value = keywords;
		}
		var scroll = new Fx.Scroll(window, {offset: {'x': 0, 'y': 426},transition: Fx.Transitions.Quad.easeInOut}).toTop();
	}
	return false;
}

//SEARCH PAGE
function searchThis(theForm) {
	var keywords = theForm.keywords.value;
	if(keywords != "") {
		switchSearch(keywords,"");
		$('searchtextTop').value = keywords;
	}
	else {
		alert('Please enter a search keyword.');
	}

	//$('resultsBox').innerHTML = keywords;
	return false;
}

function switchSearch(keywords,page) {
	switching = true;
	var contentFade = new Fx.Styles('resultsBox', {duration: 200, wait: true});
	var contentResize = new Fx.Styles($('main'), {duration: 200, transition: Fx.Transitions.Cubic.easeInOut, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		
		new Ajax("searchresults.php", { method: 'get', data: "keywords="+keywords+"&page="+page, update: $('resultsBox'), evalScripts:true,
			onComplete: function() {
				var minHeight = ($('sideBar').offsetHeight > $('itemList').offsetHeight) ? $('sideBar').offsetHeight: $('itemList').offsetHeight;
				var newHeight = (minHeight < $('theItem').offsetHeight) ? $('theItem').offsetHeight : minHeight;
				contentResize.start({'height': newHeight})
				.chain(function() {
					$('main').setStyle('height', 'auto');
					$('content').setStyle('height', 'auto');
					contentFade.start({'opacity': 1}) 
					.chain(function() {
						initiateLinks('#itemList a');
						switching = false;
					});
				});
			},
			onFailure: function() { 
				$('theItem').innerHTML = "<center><h1>Sorry...page didn't load!</h1></center>";
				$('main').setStyle('height', 'auto');
				$('content').setStyle('height', 'auto');
				$('theItem').setStyle('opacity', 1);
				initiateLinks('#theItem a');
				switching = false;
			}})
		.request();
	});
}



///////////////////////////
//PODCAST /////////////////
///////////////////////////
function switchPodcast(category,page) {
	switching = true;
	var contentFade = new Fx.Styles('podcastItems', {duration: 200, wait: true});
	var contentResize = new Fx.Styles($('main'), {duration: 200, transition: Fx.Transitions.Cubic.easeInOut, wait: true});
	contentFade.start({'opacity': 0})
	.chain(function() {
		
		new Ajax("podcastresults.php", { method: 'get', data: "category="+category+"&page="+page, update: $('podcastItems'), evalScripts:true,
			onComplete: function() {
				var minHeight = ($('sideBar').offsetHeight > $('itemList').offsetHeight) ? $('sideBar').offsetHeight: $('itemList').offsetHeight;
				var newHeight = (minHeight < $('theItem').offsetHeight) ? $('theItem').offsetHeight : minHeight;
				contentResize.start({'height': newHeight})
				.chain(function() {
					$('main').setStyle('height', 'auto');
					$('content').setStyle('height', 'auto');
					contentFade.start({'opacity': 1}) 
					.chain(function() {
						initiateLinks('#itemList a');
						switching = false;
					});
				});
			},
			onFailure: function() { 
				$('podcastItems').innerHTML = "<center><h1>Sorry...page didn't load!</h1></center>";
				$('main').setStyle('height', 'auto');
				$('content').setStyle('height', 'auto');
				$('podcastItems').setStyle('opacity', 1);
				initiateLinks('#theItem a');
				switching = false;
			}})
		.request();
	});
}



////////////////////////////////////////////////////////////////////
// IDEA FORM SUBMISSION        /////////////////////////////////////
////////////////////////////////////////////////////////////////////
function submitIdea() {
	
	//VALIDATE EMAIL ADDRESS
	function validateEmail(addr) {
		var emailErrors = false;
		var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
		for (i=0; i<invalidChars.length; i++) {
		   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		      var emailErrors = true;
		   }
		}
		for (i=0; i<addr.length; i++) {
		   if (addr.charCodeAt(i) > 127) {
		      var emailErrors = true;
		   }
		}
		var atPos = addr.indexOf('@',0);
		if (atPos == -1 || atPos == 0 || addr.indexOf('@', atPos + 1) > - 1 || addr.indexOf('.', atPos) == -1 || addr.indexOf('@.',0) != -1 || addr.indexOf('.@',0) != -1 || addr.indexOf('..',0) != -1) {
			var emailErrors = true;
		}
		if(emailErrors == true) {return false;}
		return true;
	}


	var submitError = false;
	
	if($('ideaSubject').value == "" || $('ideaName').value == "" || $('ideaEmail').value == "" || $('ideaMessage').value == "") {
		alert('You must fill in all fields.');
		submitError = true;
	}
	if(validateEmail($('ideaEmail').value) == false) {
		alert('This is not a valid email address. Please retype it.');
		submitError = true;
	}

	if(submitError == false) {
		var subject = $('ideaSubject').value;
		var name = Url.encode($('ideaName').value);
		var email = Url.encode($('ideaEmail').value);
		var message = Url.encode($('ideaMessage').value);
		
		switching = true;
		var formFade = new Fx.Styles('ideaForm', {duration: 200, wait: true});
		var responseFade = new Fx.Styles('ideaResponse', {duration: 200, wait: true});
		formFade.start({'opacity': 0})
		.chain(function() {
			$('ideaForm').style.display = "none";
			new Ajax("submitIdea.php", { method: 'get', data: "subject="+subject+"&name="+name+"&email="+email+"&message="+message, update: $('ideaResponse'), evalScripts:true,
				onComplete: function() {
					$('ideaResponse').style.display = "block";
					responseFade.start({'opacity': 1}) 
					.chain(function() {
						//ideaCookie.set('submitted', 'true');
						switching = false;
					});
				},
				onFailure: function() { 
					$('ideaForm').style.display = "block";
					formFade.start({'opacity': 1}) 
					.chain(function() {
						alert('Sorry! The network was busy. Please try again later.');
						switching = false;
					});
					switching = false;
				}})
			.request();
		});
	}
}

function refreshIdea() {
	var formFade = new Fx.Styles('ideaForm', {duration: 200, wait: true});
	var responseFade = new Fx.Styles('ideaResponse', {duration: 200, wait: true});
	responseFade.start({'opacity': 0})
	.chain(function() {
		$('ideaSubject').value = "";
		$('ideaName').value = "";
		$('ideaEmail').value = "";
		$('ideaMessage').value = "";
		$('ideaResponse').style.display = "none";
		$('ideaForm').style.display = "block";
		formFade.start({'opacity': 1});
	});
}

function submitPoll() {
	var thisPollID = $('pollID').value;
	var radios = $$('#pollForm input');
	var answered = false;
	var theOption = null;
	radios.each(function(radio) {
		if(radio.checked == true) { 
			answered = true;
			theOption = radio.value;
		}
	});
	
	if(answered != false && theOption != null) {
		switching = true;
		var pollFade = new Fx.Styles('pollForm', {duration: 200, wait: true});
		pollFade.start({'opacity': 0})
		.chain(function() {
			new Ajax("submitPoll.php", { method: 'get', data: "option="+theOption, update: $('pollForm'), evalScripts:true,
				onComplete: function() {
					thisPoll.set('thisPoll',thisPollID);
					pollFade.start({'opacity': 1}) 
					.chain(function() {
						switching = false;
					});
				},
				onFailure: function() { 
					pollFade.start({'opacity': 1}) 
					.chain(function() {
						alert('Sorry! The network was busy. Please try again later.');
						switching = false;
					});
					switching = false;
				}})
			.request();
		});
	}
	else {
		alert('Please choose an option.');
	}
}

var Url = {

	encode : function (string) { return escape(this._utf8_encode(string)); },
	decode : function (string) { return this._utf8_decode(unescape(string)); },
	
	_utf8_encode : function (string) {
	  	string = string.replace(/\r\n/g,"\n");
	 	var utftext = "";
	  	for (var n = 0; n < string.length; n++) {
	   		var c = string.charCodeAt(n);
	   		if (c < 128) {
	    			utftext += String.fromCharCode(c);
	 		} 
	 		else if((c > 127) && (c < 2048)) {
	  			utftext += String.fromCharCode((c >> 6) | 192);
	  			utftext += String.fromCharCode((c & 63) | 128);
	 		} 
	 		else {
	  			utftext += String.fromCharCode((c >> 12) | 224);
	  			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
	 	 		utftext += String.fromCharCode((c & 63) | 128);

	 		}
	 	}
		return utftext;
	},
	
	_utf8_decode : function (utftext) {
	 	var string = "";
	 	var i = 0;
	 	var c = c1 = c2 = 0;
	  	while ( i < utftext.length ) {
	  		c = utftext.charCodeAt(i);
	   		if (c < 128) {
	    		string += String.fromCharCode(c);
	    		i++;
	  		} 
	  		else if((c > 191) && (c < 224)) {
	 			c2 = utftext.charCodeAt(i+1);
	    		string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
	    		i += 2;
	  		} 
	  		else {
	 			c2 = utftext.charCodeAt(i+1);
	    		c3 = utftext.charCodeAt(i+2);
	    		string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
	   			i += 3;
	 		}
		}
		return string;
	}
}




