var isiPod = navigator.userAgent.match(/iPod/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isAndroid = false;
var isAndroidMobile = false;
var isAndroidTablet = false;
if (navigator.userAgent.match(/Android/i) != null) {
	isAndroid = true;
	if (navigator.userAgent.match(/Mobile/i) != null) {
		isAndroidMobile = true;
	} else {
		isAndroidTablet = true;
	}
}
var isMobile = false;
var isTouch = false;
if (isiPod || isiPhone || isiPad || isAndroid) {
	isTouch = true;
}
if (isiPod || isiPhone || isAndroidMobile) {
	isMobile = true;
}

// remove margin-top if touch device
if (isTouch) {
	document.write('<style type="text/css">#content { margin-top: 0; }</style>');
}

var userAction = 'Click';
if (isTouch) {
	userAction = 'Tap';
}


// On page load add a timeout to do a scrollto and content reveal if isMobile
if (isMobile) {
	window.addEventListener("load",function() {
		setTimeout(function(){ 
			window.scrollTo(0, 1); }, 0);
	});
}


function sbar(msg)
{
	window.status = msg;
}

// get url parameter
function getURLParameter( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
		
} // getURLParameter

// get cookie value
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}

} // getCookie

function emailShareOnclick() {
	var encodedTitle = encodeURIComponent(pageTitle);
	var encodedBodyText = encodeURIComponent('Here\'s a web page you should visit.\n\n' + pageTitle + ' : ' + pageURL);
	var shareURL = 'mailto:ENTER_RECEIVERS_EMAIL_ADDRESS?subject=' + encodedTitle + '&body=' + encodedBodyText;
	window.open(shareURL, '_blank');
	return false;
} // emailShareOnclick

function facebookShareOnclick() {
	var encodedTitle = encodeURIComponent(pageTitle);
	var shareURL = 'http://www.facebook.com/sharer.php?u=' + pageURL + '&t=' + encodedTitle;
	window.open(shareURL,'_blank','width=500,height=370');
	return false;
} // facebookShareOnclick

function twitterShareOnclick() {
	var encodedTitle = encodeURIComponent(pageTitle);
	var shareURL = 'http://twitter.com/share?url=' + pageURL + '&text=' + encodedTitle;
	window.open(shareURL,'_blank','width=550,height=450');
	return false;
} // twitterShareOnclick

function show_underline(elementID){
	document.getElementById(elementID).className='text-decoration_underline';
}

function hide_underline(elementID){
	document.getElementById(elementID).className='text-decoration_none';
}

function backToTop() {
	scroll(0,0);
}

function displayLogo() {
	document.write("<img border=\"0\" src=\"/vbirder/graphic2/tvb_logo.gif\" width=\"160\" height=\"226\" alt=\"The Virtual Birder Logo\" title=\"The Virtual Birder Logo\" />");
}

// initialize banner titles array
var bannerTitles = new Array()
bannerTitles["tvb20"] = "The Virtual Birder 2.0";
bannerTitles["vbt20"] = "Virtual Birding Tours 2.0";
bannerTitles["onloc"] = "OnLocation";
bannerTitles["gallery"] = "Gallery";
bannerTitles["shelf"] = "Media Shelf";
bannerTitles["hotlinks"] = "Hot Links";
bannerTitles["realbirds"] = "Real Birds";

function displayBanner(bannerType) {
	var bannerTitle = "";
	if (bannerType in bannerTitles) {
		bannerTitle = bannerTitles[bannerType];
	} else {
		bannerTitle = "TVB Banner";
	}
	document.write("<img border=\"0\" src=\"/tvb20/graphics/" + bannerType + "_banner.png\" width=\"1054\" height=\"134\" alt=\"" + bannerTitle + "\" title=\"" + bannerTitle + "\" />");
}


//+ ----------------------------------------------------------------------------
//    Virtual Tour Stuff
//- ----------------------------------------------------------------------------

function initVirtualTourLaunchPage(onLocID) {

	kc = getURLParameter('KC');
	
	sub = subscribedCookie(onLocID);
	
	restart = getCookie('RESTART_' + onLocID);

}

function displayForms() {

	if (restart) {
		document.write("	<div class=\"restart_form\">");
		document.write("		<form id=\"restart_tour_form\" method=\"post\" action=\"/cgi-bin/vbirder-onLoc6/" + onLocInst + "\" name=\"RESTART_FORM\">");
		document.write("			<input type=\"hidden\" name=\"ON_LOC_ID\" value=\"" + onLocID + "\" />");
		if (kc) {
			document.write("			<input type=\"hidden\" name=\"TOUR_ACCESS\" value=\"SUB_PASSED\" />");
			document.write("			<input type=\"hidden\" name=\"KC\" value=\"" + kc + "\" />");
		} else if ((sub != 'FREE') && (sub != null)) {
			document.write("			<input type=\"hidden\" name=\"TOUR_ACCESS\" value=\"SUB_COOKIE\" />");
			document.write("			<input type=\"hidden\" name=\"KC\" value=\"" + sub + "\" />");
		} else {
			document.write("			<input type=\"hidden\" name=\"TOUR_ACCESS\" value=\"FREE\" />");
		}
		if ((sub == null) || (sub == 'FREE')) {
			document.write("			<p class=\"red_prompt center\">You already started a free tour.</p>");
		} else {
			document.write("			<p class=\"red_prompt center\">You already started a tour.</p>");
		}
		document.write("            <input type=\"hidden\" name=\"RESTART\" value=\"1\"/>");
		document.write("			<p class=\"center\"><a href=\"javascript:document.forms['restart_tour_form'].submit();\" class=\"button bigrounded gray\"><nobr>Continue Previous " + onLocSubmitReturn + " Virtual Tour</nobr></a></p>");
		document.write("		</form>");
		document.write("	</div>");
	
		document.write("	<hr/>");
	}
	
	if (!restart || (sub || kc)) {
		document.write("	<div class=\"location_form\">");
	
		document.write("		<form id=\"new_tour_form\" method=\"post\" action=\"/cgi-bin/vbirder-onLoc6/" + onLocInst + "\" name=\"ONLOC_FORM\">");
		document.write("			<input type=\"hidden\" name=\"ON_LOC_ID\" value=\"" + onLocID + "\">");
	
		if (restart) {
			document.write("			<p class=\"red_prompt\">Complete the following form to start a new tour.</p>");
		} else {
			document.write("			<p class=\"red_prompt\">Complete the following form to start your tour.</p>");
		}
	
		document.write("			<dl class=\"fields\">");
			
		if (kc) {
			document.write("				<dt>Full Paid Tour: <span class=\"license_key\">Use purchased license key: " + kc + ".</span></dt>");
			document.write("				<input type=\"hidden\" name=\"TOUR_ACCESS\" value=\"SUB_COOKIE\" />");
			document.write("				<input type=\"hidden\" name=\"KC\" value=\"" + sub + "\" />");
		} else if ((sub != 'FREE') && (sub != null)) {
			document.write("				<dt>Full Paid Tour: <span class=\"license_key\">Use purchased license key: " + sub + ".</span></dt>");
			document.write("				<input type=\"hidden\" name=\"TOUR_ACCESS\" value=\"SUB_COOKIE\" />");
			document.write("				<input type=\"hidden\" name=\"KC\" value=\"" + sub + "\" />");
		} else {
			document.write("				<dt>Free " + onLocFreeTime + " Tour or Full Paid Tour:</dt>");
			document.write("				<dd>");
			document.write("					<dl>");
			document.write("						<dt><input type=\"radio\" name=\"TOUR_ACCESS\" value=\"FREE\" checked=\"checked\" onclick=\"document.ONLOC_FORM.KC.value=''\" /> Free " + onLocFreeTime + " Tour</dt>");
			document.write("						<dd>Includes interstitial banner breaks (similar to TV commercial breaks).</dd>");
			document.write("						<dt><input type=\"radio\" name=\"TOUR_ACCESS\" value=\"SUB_ENTER\" onclick=\"document.ONLOC_FORM.KC.focus();\" /> Full Paid Tour - License Key:");
			document.write("							<input name=\"KC\" type=\"text\" size=\"19\" value=\"\" onchange=\"document.ONLOC_FORM.TOUR_ACCESS[1].click();document.ONLOC_FORM.KC.focus();\" />");
			document.write("						</dt>");
			document.write("						<dd>Enter your license key from your order confirmation.<br/><br/>");
			document.write("							<table border=\"0\" cellpadding=\"5\" cellspacing=\"1\" class=\"pay_now\">");
			document.write("								<tr bgcolor=\"#dcdcb4\"><td align=\"right\">Purchase a key for this tour for " + onLocPrice + "</td><td><a href=\"/vbirder/onLoc/payNowButtons/placeholder.html\" onclick=\"return hs.htmlExpand(this, { objectType: 'iframe', wrapperClassName: 'no-controls', dimmingOpacity: 0.3} )\"><img src=\"https://www.paypal.com/en_US/i/btn/btn_dg_pay_w_paypal.gif\" border=\"0\"></a></td><tr>");
			document.write("								<tr bgcolor=\"#dcdcb4\"><td align=\"right\">Purchase a key for <a href=\"/vbirder/onLoc/virtualtours_popup.html\" onclick=\"return hs.htmlExpand(this, { objectType: 'iframe', wrapperClassName: 'no-controls', dimmingOpacity: 0.3 } )\">2011-2012 tours</a> for " + onLocsPrice + ".</td><td><a href=\"/vbirder/onLoc/payNowButtons/placeholder.html\" onclick=\"return hs.htmlExpand(this, { objectType: 'iframe', wrapperClassName: 'no-controls', dimmingOpacity: 0.3} )\"><img src=\"https://www.paypal.com/en_US/i/btn/btn_dg_pay_w_paypal.gif\" border=\"0\"></a></td><tr>");
			document.write("							</table>");
			document.write("						</dd>");
			document.write("					</dl>");
			document.write("				</dd>");
		}
		
		document.write("					<p>");
		document.write("					<dt><div class=\"field_guide_prompt\"><b>Include field guide page numbers:</b></div>");
		document.write("						<div class=\"field_guide_select\"><select name=\"FIELD_GUIDE\">");
		document.write("							<option value=\"\" selected=\"selected\">-- Choose a Field Guide --</option>");
		document.write("							<option value=\"\">**None**</option>");
		document.write("							<option value=\"gold3\">Golden, 3rd Edition</option>");
		document.write("							<option value=\"kauf1\">Kaufman, 1st Edition</option>");
		document.write("							<option value=\"kauf2\">Kaufman, 2rd Edition</option>");
		document.write("							<option value=\"ng4\">National Geographic, 4th Edition</option>");
		document.write("							<option value=\"ng5\">National Geographic, 5th Edition</option>");
		document.write("							<option value=\"pe4\">Peterson Eastern, 4th Edition</option>");
		document.write("							<option value=\"pe5\">Peterson Eastern, 5th Edition</option>");
		document.write("							<option value=\"pw3\">Peterson Western, 3rd Edition</option>");
		document.write("							<option value=\"sib1\">Sibley, 1st Edition</option>");
		document.write("							<option value=\"sibe1\">Sibley Eastern, 1st Edition</option>");
		document.write("							<option value=\"sibw1\">Sibley Western, 1st Edition</option>");
		document.write("							<option value=\"se1\">Stokes Eastern, 1st Edition</option>");
		document.write("							<option value=\"sw1\">Stokes Western, 1st Edition</option>");
		document.write("						</select>");
		if (fieldGuidePrompt) {
			document.write("					<p class=\"field_guide_note\">" + fieldGuidePrompt + "</p>");
		}
		document.write("						</div>");
		document.write("						<p class=\"clear\"></p>");
		document.write("					</dt>");
				
		document.write("				<input type=\"hidden\" name=\"SKILL\" value=\"NOV\">");
		document.write("				<input type=\"hidden\" name=\"SIZE\" value=\"3\">");
		document.write("				<input type=\"hidden\" name=\"AUDIO\" value=\"mp3\">");
				
		document.write("				<p>");
		document.write("				<dt>Email Address (optional): <input type=\"text\" name=\"EMAIL\" size=\"30\"  placeholder=\"email address\"></dt>");
		document.write("				<dd>");
		document.write("					<dl>");
		document.write("						<dd>Enter your email address to be notified when new tours and other content is added to The Virtual Birder.<br/>");
		document.write("						</dd>");
		document.write("					</dl>");
		document.write("				</dd>");
				
		document.write("				<p class=\"center\">");
	
		if (restart) {			
			document.write("				<input type=\"hidden\" name=\"IGNORE_RESTART_COOKIE\" value=\"1\"/>");
			document.write("				<a href=\"javascript:document.forms['new_tour_form'].submit();\" class=\"button bigrounded gray\"><nobr>Start a New " + onLocSubmitReturn + " Virtual Tour</nobr></a>");
		} else {
			document.write("				<a href=\"javascript:document.forms['new_tour_form'].submit();\" class=\"button bigrounded gray\"><nobr>" + onLocSubmitFirst + "</nobr></a>");
		}		
	
		document.write("				</p>");
				
		document.write("			</dl>");
			
		document.write("			</form>");
	
		document.write("		</div>");
	
		document.write("	<hr />");
	
	} /* !restart && !sub && !kc */

} /* displayForms */


function showAllTours() {
	window.open("/vbirder/onLoc/virtualtoursPopup.html", 
				"allTours", 'width=600,height=350,menubar=no,resizable=yes,toolbar=no,location=no,status=no,scrollbars=yes', 
				true);
}


function tourOptGroupOpen(optGroup) {

	document.write("<optgroup label=\"" + optGroup + "\">");

}

function tourOptGroupClose() {

	document.write("</optgroup>");

}

function tourOption(onLocID, onLocName) {

	document.write("<option value=\"" + onLocID + "\">" + onLocName + "</option>");

}

function tourOptions() {

	tourOptGroupOpen("Released Tours");
		tourOption('NCAL', 'Northern California (Coastal &amp; Pelagic Species) ***');
		tourOption('HAWK', 'Cape May, New Jersey &amp; Hawk Mountain, Pennsylvania (Hawk Watch!)');
	tourOptGroupClose() 
	tourOptGroupOpen("Tours that are Coming Late 2011/Early 2012");
		tourOption('BOSSPR', 'Mount Auburn Cemetery, Massachusetts (Spring Songbirds)');
		tourOption('DOWNEAST', 'Down East Maine (Pelagic &amp; Boreal Species)');
		tourOption('CAPECOD', 'Monomoy NWR &amp; South Beach, Massachusetts (Autumn Shorebirds)');
		tourOption('CHIN', 'Chincoteague NWR, Virginia (Fall Dabblers)');
		tourOption('SACH', 'Sachuest Point NWR, Rhode Island (Winter Sea Ducks)');
		tourOption('FLORIDA', 'Southwest Florida (Long-legged Waders)');
		tourOption('CHURCHILL', 'Churchill, Manitoba (Churchill Specialties)');
		tourOption('HUAC', 'Huachuca Canyons, Arizona (Arizona Specialties)');
		tourOption('ILSUM', 'Research in Illinois (Grassland &amp; Shrubland Species)');
	tourOptGroupClose() 
	
}

function subscribedCookie(onLocID) {

	sub = getCookie('SUB_TVB11');
	if (sub == null) {
		if (onLocID != 'TVB11') {
			sub = getCookie('SUB_' + onLocID);
			//if (sub == 'FREE') {
			//	sub = null;
			//}
		}
	}

	return sub;
}

function subscribedTableRow(onLocID) {

	if (subscribedCookie(onLocID) != null) {
		document.write("<tr>");
		document.write("	<td align=\"right\">Subscribed:</td>");
		document.write("	<td>Yes</td>");
		document.write("</tr>");
	}
	
}

function subscribedOption(onLocID, onLocName) {
	var subscribed = false;

	document.write("<option value=\"" + onLocID + "\">");
	if (subscribedCookie(onLocID) != null) {
		document.write("&#10004; ");
		subscribed = true;
	}
	document.write(onLocName + "</option>");

	return subscribed;
}

function subscribedOptions() {

	var subscribed = false;
	if (subscribedOption('BOSSPR', 'Mount Auburn Cemetery, Massachusetts (Spring Songbirds)')) subscribed = true;
	if (subscribedOption('DOWNEAST', 'Down East Maine (Pelagic &amp; Boreal Species)')) subscribed = true;
	if (subscribedOption('CAPECOD', 'Monomoy NWR &amp; South Beach, Massachusetts (Autumn Shorebirds)')) subscribed = true;
	if (subscribedOption('HAWK', 'Cape May, New Jersey &amp; Hawk Mountain, Pennsylvania (Hawk Watch!)')) subscribed = true;
	if (subscribedOption('CHIN', 'Chincoteague NWR, Virginia (Fall Dabblers)')) subscribed = true;
	if (subscribedOption('SACH', 'Sachuest Point NWR, Rhode Island (Winter Sea Ducks)')) subscribed = true;
	if (subscribedOption('FLORIDA', 'Southwest Florida (Long-legged Waders)')) subscribed = true;
	if (subscribedOption('CHURCHILL', 'Churchill, Manitoba (Churchill Specialties)')) subscribed = true;
	if (subscribedOption('HUAC', 'Huachuca Canyons, Arizona (Arizona Specialties)')) subscribed = true;
	if (subscribedOption('NCAL', 'Northern California (Coastal &amp; Pelagic Species)')) subscribed = true;
	if (subscribedOption('ILSUM', 'Research in Illinois (Grassland &amp; Shrubland Species)')) subscribed = true;
	
	return subscribed;
}

function hasSubscription() {

	if (subscribedCookie('TVB11')) return true;
	if (subscribedCookie('BOSSPR')) return true;
	if (subscribedCookie('DOWNEAST')) return true;
	if (subscribedCookie('CAPECOD')) return true;
	if (subscribedCookie('HAWK')) return true;
	if (subscribedCookie('CHIN')) return true;
	if (subscribedCookie('SACH')) return true;
	if (subscribedCookie('FLORIDA')) return true;
	if (subscribedCookie('CHURCHILL')) return true;
	if (subscribedCookie('HUAC')) return true;
	if (subscribedCookie('NCAL')) return true;
	if (subscribedCookie('ILSUM')) return true;
	
	return false;
}

function selectOption(selectID, optionValue) {

	var selectObject = document.getElementById(selectID);
	
	for (i=0; i<= selectObject.options.length; i++) {
		if (selectObject.options[i].value == optionValue) {
			selectObject.selectedIndex = i;
			return;
		}
	}
	
} // selectOption



