// JavaScript Document
function hideElement(elementId){
	if(document.getElementById(elementId)){
		document.getElementById(elementId).style.display = 'none';
	}
}

function showElement(elementId){
	if(document.getElementById(elementId)){
		document.getElementById(elementId).style.display = '';
	}
}

enabledChange = true;
function changeImage(newImage) {
	if(!enabledChange){
		return false;	
	}
	newImage = newImage.replace(/thmb-/, ""); // replace the "thmb-" part with nothing, since this is our large image
	fadeTime = 1000;
	newImageNumStart = (newImage.length-17);

	newImageNum = newImage.substr(newImageNumStart,2);
	colorStart = newImage.length-14;
	opacityStart = newImage.length-7;
	newColor = newImage.substr(colorStart,6);
	newOpacity = newImage.substr(opacityStart,2);
	showElement('blackScreen');
	opacity('blackScreen', 0, 100, fadeTime);
	newBodyImage = 'url(/i/backgrounds/'+newImageNum+'-bg.jpg)';
	setTimeout("changeBackgroundImage('"+newBodyImage+"', 'body')", fadeTime);
	setTimeout("changeBackgroundImage('"+newImage+"', 'mainDiv')", fadeTime);
	setTimeout("opacity('mainMenuBackground', 50, "+newOpacity+", 1)", fadeTime);
	setTimeout("changeBackgroundColor('"+newColor+"', 'body')", fadeTime);
	setTimeout("opacity('blackScreen', 100, 0, 1000)", fadeTime);
	setTimeout("hideElement('blackScreen')", (fadeTime*2));
}

function changeBackgroundImage(newImage, elementId){
	document.getElementById(elementId).style.backgroundImage = newImage;
}

function changeBackgroundColor(newColor, elementId){
	document.getElementById(elementId).style.backgroundColor = '#'+newColor;
}


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++){
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
	return true;
}


function indexemailform(){
	ifo=document.getElementById("emailshade");
	ifo.style.width="100%";
	ifo.style.height="250%";
	ifo2=document.getElementById("emailshade2");
	ifo2.style.width="100%";
	ifo2.style.height="100%";
	opacity("emailshade", 0, 75, 500);
	opacity("emailshade2", 0, 100, 500);
}



function indexemailformClose(){
	opacity("emailshade", 75, 0, 500);
	opacity("emailshade2", 100, 0, 500);
	setTimeout("document.getElementById('emailshade').style.width='1px';document.getElementById('emailshade').style.height='1px';", 500);
	setTimeout("document.getElementById('emailshade2').style.width='1px';document.getElementById('emailshade2').style.height='1px';", 500);
}

curIm=0;
function slideBar(direction){
	for(i=0;i<backgroundImages.length;i++) { //Remove blank entries
		if(backgroundImages[i]==null || !backgroundImages[i] || backgroundImages[i]=="") {
			backgroundImages.splice(i, 1);
		}
	}
	if(direction==0) {
		curIm++;
		if(curIm>(backgroundImages.length-1)) curIm=0;
	} else {
		curIm--;
		if(curIm<0) curIm=(backgroundImages.length-1);
	}
	imk=curIm;
	i=0;
	while(document.getElementById('slideImage'+i)) {
		if(imk>(backgroundImages.length-1)) imk=0;
		changeBackgroundImage('url(/i/backgrounds/thmb-'+backgroundImages[imk]+')', 'slideImage'+i);
		imk++;
		i++;
	}
}

//nextPage is used by the google map display pages to show the next block of listings
function nextPage(pageNum)
{
	var theLocation = new String(document.location)
	var varCheck = theLocation.indexOf("?");
	var placeholder = theLocation.indexOf("currentPage=");
	if(varCheck == -1)//there were no URL vars, so append currentpage and load new url
	{
		document.location = document.location+"?currentPage="+pageNum;
	}
	else if(placeholder != -1)//there was already a currentPage var , get rid of it and reload w/ new page num.
	{
		var tmpLocation = theLocation.slice(0,placeholder);
		document.location = tmpLocation+"currentPage="+pageNum;
	}
	else
	{
		document.location = document.location+"&currentPage="+pageNum;
	}
}


function addToTripPlanner(theID,theCAT){
	theWindow = window.open("/addToTripPlanner.cfm?id="+theID+"&category="+theCAT,"w","width=200,height=125,top=150,left=150"); 
	theWindow.focus();
}

function popGoogleMap(URLStr){
	var left = 10;
	var top = 10;
	var width = 1024;
	var height = 700;
  if(window.googleWindow){
  	if(!googleWindow.closed){
		googleWindow.close();
	}
  }
  googleWindow = open(URLStr, 'googleWindow', 'toolbar=1,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=1,copyhistory=0,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function popListingDetails(id,cat){
  //we need to determine which category we are dealing w/.  hikes, paddles, birding are all unique, the rest we can render dynamically
  if(cat == 'paddles'){
	  theWindow = open('/popPaddleDetails.cfm?id='+id, 'theWindow2', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=yes,copyhistory=yes,width=900,height=650,left=10,top=10,screenX=10,screenY=10');
  }
  else if(cat == 'hikes') {
	  theWindow = open('/popHikeDetails.cfm?id='+id, 'theWindow2', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=yes,copyhistory=yes,width=900,height=600,left=10,top=10,screenX=10,screenY=10');
  }
  else if(cat == 'photos'){
	  theWindow = open('/popPhoto.cfm?id='+id, 'theWindow2', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=yes,copyhistory=yes,width=900,height=600,left=10,top=10,screenX=10,screenY=10');
  }
  else{
  	theWindow = open('/popListingDetails.cfm?id='+id+'&category='+cat, 'theWindow2', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=yes,copyhistory=yes,width=900,height=600,left=10,top=10,screenX=10,screenY=10');
  }
  
  theWindow.focus();

}

function pop(url){
  if(popUpWin){
	  	if(!popUpWin.closed) popUpWin.close();
  }
	popUpWin=window.open(url,"popUpWin","width=630,height=400,scrollbars,resizable,status,top=20,left=20");
}

function pop2(url){
	window.open(url,"Link","width=800,height=600,scrollbars,resizable,status,top=20,left=20");
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height){
  if(popUpWin){
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function popVideo(swf){
	var width = 360;
	var height = 300;
	var left,top = 40;
	vidPop = open('/video.cfm?theSWF='+swf, 'vidPop', 'toolbar=no,location=no,directories=no,statusbar=no,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	vidPop.focus()
}


function mapSubmit(formObject) {
	formObject.action = '/googleMaps/googleMap.cfm';
	formObject.submit();
	return true;
}

function popRules()
{
	var width = 700;
	var height = 500;
	var left = 20;
	var top = left;
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open('http://www.thisiscooperstown.com/rules.htm', 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function popPrivacy(){
	var left = 10; var top = 10; var width = 400; var height = 500;
	if(popUpWin){
    	if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open('popPrivacy.htm', 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function emailCheck(emailStr,k) {
	var re = new RegExp("^[A-Za-z0-9_-]+[A-Za-z0-9_\.\-]*[A-Za-z0-9_-]+@[A-Za-z0-9_-]+[A-Za-z0-9\.-]*\\.[a-zA-Z]{2,4}$","i");

	if(!re.test(emailStr)){
		alert("Your email address is incorrect.");
		return false;
	}
	//test for required fields
	var state = document.form1.state.value;
	if(document.form1.firstname.value == ""){alert("Please enter your first name."); return false;}
	if(document.form1.lastname.value == ""){alert("Please enter your last name."); return false;}
	if(document.form1.address1.value == ""){alert("Please enter your street address."); return false;}
	if(document.form1.city.value == ""){alert("Please enter your city."); return false;}
	if(state == "none"){alert('Please select a state.'); return false;}
	if(document.form1.zip.value == ""){alert("Please enter your zip code."); return false;}
	// If we've gotten this far, everything's valid!
	return true;
}

function outgoing_links() {
	var links = document.getElementsByTagName("a");
	var urlRe = new RegExp("http:\/\/((www\.)?)([a-zA-Z0-9\\_\\-\\.]*)\.([a-zA-Z]{1,5})/");
	for(i=0;i<links.length;i++) {
		if(links[i].href.toLowerCase().indexOf("thisiscooperstown.com") == -1 && urlRe.test(links[i].href)) {
			links[i].onclick=function() {
				pageTracker._trackPageview("/outgoing/"+this.href);
			};
		}
	}
}