
// Zoo Weekly Tabs -- start //
var panes          = new Array();
var visiblePaneIds = new Array();
var weightingTable = new Array();

setupWeightingTable();

function setupPanes(containerId) {
  
  panes[containerId] = new Array();
  var maxHeight      = 0; 
  var maxWidth       = 0;

  var container = document.getElementById(containerId);
  var paneContainer = container.getElementsByTagName("div")[0];
  var paneList = paneContainer.childNodes;
  
  for (var i=0; i < paneList.length; i++ ) {
    var pane = paneList[i];
    if (pane.nodeType != 1) continue;
    if (pane.offsetHeight > maxHeight) maxHeight = pane.offsetHeight;
    if (pane.offsetWidth  > maxWidth ) maxWidth  = pane.offsetWidth;
    panes[containerId][pane.id] = pane;
    pane.style.display = "none";
  }
  
  paneContainer.style.height = maxHeight + "px";
  paneContainer.style.width  = maxWidth + "px";

  var randPane = getWeightedRandomPane(containerId);

  if (panes[containerId][randPane] == null) {
	document.getElementById(containerId).style.display = "none";
	return;
  }
  var visiblePane = panes[containerId][randPane].id;
  visiblePaneIds[visiblePaneIds.length] = visiblePane;

  var selectedLink = visiblePane.replace("-tab","-a");
  document.getElementById(selectedLink).onclick();
}

function setupWeightingTable() {
  var zooweeklyWeights = [["zooweekly-diaries-tab", 1], ["zooweekly-news-tab", 1], ["zooweekly-gossip-tab", 1]];
  loadWeights("zooweekly", zooweeklyWeights);
}

function loadWeights(containerId, weightsToLoad) {

  weightingTable[containerId] = new Array();

  var count = 0;
  for (var i=0; i < weightsToLoad.length; i++) {
    for (var j=0; j < weightsToLoad[i][1]; j++) {
      weightingTable[containerId][count] = weightsToLoad[i][0];
	  count++;
	}
  }
}

function getWeightedRandomPane(containerId) {

  if (weightingTable[containerId] != undefined) {
    var randArrayIndex = Math.round(Math.random() * (weightingTable[containerId].length-1));
    return weightingTable[containerId][randArrayIndex];
  }
  return false;
}

function showPane(paneId, activeTab) {
  for (var con in panes) {
    activeTab.blur();
    activeTab.className = "tab-active";
    if (panes[con][paneId] != null) { 
      var pane = document.getElementById(paneId);
      pane.style.display = "block";
      var container = document.getElementById(con);
      var tabs = container.getElementsByTagName("ul")[0];
      var tabList = tabs.getElementsByTagName("a")
      for (var i=0; i<tabList.length; i++ ) {
        var tab = tabList[i];
        if (tab != activeTab) tab.className = "tab-disabled";
      }
      for (var i in panes[con]) {
        var pane = panes[con][i];
        if (pane == undefined) continue;
        if (pane.id == paneId) continue;
        pane.style.display = "none";
      }
    }
  }
  return false;
}
// Zoo Weekly Tabs -- end //

function popImg(windowName, URL, width, height, scrolling) {
	width = width || 400;
	height = height || 360;
	scrolling = scrolling || 0;
	
	var topX = (window.screen.width / 2) - ( width / 2);
	var topY = (window.screen.height / 2) - ( height / 2);
	window.open(URL, windowName, 'width=' + width + ',height=' + height +',location=no,resizable=yes,scrollbars=' + scrolling + ',top=' + topX + ',left=' + topY);
}


// pop up script -- start //
var sUserAgent = navigator.userAgent.toLowerCase();
var isOp = (sUserAgent.indexOf('opera')!=-1)?true:false;
function pop(oAnchor,sProps,sWindow){
	var sUrl = '';
	if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
	if(sUrl=='') sUrl = oAnchor.href;
	if(sUrl=='') return true;
	var sWindowName = sWindow?sWindow:'reusedWindow';
	if(!sProps) sProps = null;
	if(sUrl) var oPopup = window.open(sUrl,sWindowName,sProps);
	if(oPopup && !isOp) oPopup.focus();
	return (oPopup)?false:true;
}
function switchImg(sImg,sSrc){
	oImg = document.images[sImg];
	return !(oImg.src = 'img/' + sSrc);
}
function verifyKey(oElement,oEvent){
	if(oEvent.keyCode==13 && oElement.onclick) oElement.onclick();
}
// pop up script -- end //

/////////////////// START AJAX sign up form code ////////////////////////
function isValidEmail(email, required) 
{
	if (required==undefined) 
	{
		required=true;
	}
	if (email==null) 
	{
		if (required) 
		{
			return false;
		}
		return true;
	}
	if (email.length==0) 
	{  
		if (required) 
		{
			return false;
		}
		return true;
	}
	if (! allValidChars(email)) 
	{
		return false;
	}
	if (email.indexOf("@") < 1) 
	{
		return false;
	} 
	else if (email.lastIndexOf(".") <= email.indexOf("@")) 
	{
		return false;
	} 
	else if (email.indexOf("@") == email.length) 
	{
		return false;
	} 
	else if (email.indexOf("..") >=0) 
	{
		return false;
	} 
	else if (email.indexOf(".") == email.length) 
	{
		return false;
	}
	return true;
}
				
function allValidChars(email) 
{
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) 
	{
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		continue;
		parsed = false;
		break;
	}
	return parsed;
}
				   
var http_request = false;

function makePOSTRequest(url, parameters) 
{
	http_request = false;
	if (window.XMLHttpRequest) 
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) 
	{
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{}
		}
	}
	if (!http_request) 
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
				      
	http_request.onreadystatechange = function ()
	{
		document.getElementById('subscribeForm').innerHTML = "<p class='requiredField'>Loading..</p>";
		if (http_request.readyState == 4)
		{
			if (http_request.status == 200) 
			{
	        	document.getElementById('subscribeForm').innerHTML = "<p><span style='color:green;'>Thank you for registering</span><br />Please check your e-mail<br>to confirm your registration.</p>";		
	        } 
	        else 
	        {			
	        	alert('There was a problem with the request.');
	        }
		}
	}
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.send(parameters);
	return true;
}

// START Zoo weekly sign up form //
function validatezw() 
{
	if (! isValidEmail(document.signup.email.value)) 
	{
		document.getElementById('invalidEmail').innerHTML = "Please enter a valid email address<br />";
		return false;
	}
	getZw(document.getElementById('signup'));
	return true;
}
				   
function getZw(obj) {
	var poststr = "email=" + encodeURI( document.getElementById("email").value);
	makePOSTRequest('/zoo-weekly/process.php', poststr);
}			   

function clearZw() {
	if (document.signup.email.value == 'Enter Email') {
		document.signup.email.value = '';
	}
}
// END Zoo weekly sign up form //

// START Animal Facts sign up form //
function validateaf() 
{
	if (! isValidEmail(document.facts.email.value)) 
	{
		document.getElementById('invalidEmail').innerHTML = "Please enter a valid email address<br />";
		return false;
	}
	getAf(document.getElementById('facts'));
	return true;
}
				   
function getAf(obj) {
	var poststr = "email=" + encodeURI( document.getElementById("email").value);
	makePOSTRequest('/animal-facts/process.php', poststr);
}			   

function clearAf() {
	if (document.facts.email.value == 'Enter Email') {
		document.facts.email.value = '';
	}
}
// END Animal Facts sign up form //

/////////////////// END AJAX sign up form code ////////////////////////

// Utilities font sizer //
var curFontSize = 1;
var fontModifier = 0.1;

function fontSize(act) {
    if (document.getElementById) {
        storyBody = document.getElementById("content");
		
        if (act === 1) {
            curFontSize += fontModifier;
            curFontSize = Math.min(curFontSize, 1.4);
        } else if (act === 0) {
            curFontSize -= fontModifier;
            curFontSize = Math.max(curFontSize, 1);
        }
        storyBody.style.fontSize = curFontSize + "em";
		
    }
	return(false);
}
// end Utilities font sizer //
