
// oas configuration
OAS_url = 'http://oascentral.scientificamerican.com/RealMedia/ads/';
//OAS_target = '_top';
//end of configuration
OAS_version = 10;
OAS_rn = '001234567890'; OAS_rns = '1234567890';
OAS_rn = new String (Math.random()); OAS_rns = OAS_rn.substring (2, 11);
function OAS_NORMAL(pos) {
  document.write('<A HREF="' + OAS_url + 'click_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '" TARGET=' + OAS_target + '>');
  document.write('<IMG SRC="' + OAS_url + 'adstream_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '" BORDER=0></A>');
}

/* ---------------------------
// global functions - sciam.com
------------------------------ */

function DoOpen(url,win,w,h,left,top) {
	if (!win) { win='_blank'; }
	if (!w) { w=500; }
	if (!h) { h=400; }
	if (!left) { left=80; }
	if (!top) { top=80; }
	Win=window.open(url,win,'width='+w+',height='+h+',left='+left+',top='+top+',status=yes,toolbar=no,menubar=no,scrollbars=1,resizable=yes');
	Win.focus();
}

// article image functions
var thisImagePos = 0;
function ArticleImageNext(){
	thisImagePos++;
	if (thisImagePos > aArticleImages.length-1) {
		thisImagePos = 0;	
	}
	setArticleImage(thisImagePos);
	return false;
}

function ArticleImagePrev(){
	thisImagePos--;
	if (thisImagePos < 0) {
		thisImagePos = aArticleImages.length-1;
	}
	setArticleImage(thisImagePos);
	return false;
}

function setArticleImage(pos){
	document.getElementById('articleImg').src=aArticleImages[thisImagePos].src;
	document.getElementById('articleImg').alt=aArticleImages[thisImagePos].alt;
	var sCaption = '<strong>' + aArticleImages[thisImagePos].title + '</strong> ';
	sCaption = sCaption + aArticleImages[thisImagePos].caption + '<br/>';
	sCaption = sCaption + '<span>' + aArticleImages[thisImagePos].credit + '</span>';
	document.getElementById('articleImgCap').innerHTML=unescape(sCaption);
	document.getElementById('currCount').innerHTML=aArticleImages[thisImagePos].thisImageNumber;
	return false;
}
// end article image functions

// text counter
function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		document.getElementById(cntfield).innerHTML = maxlimit - field.value.length;	
	}
}

// IE 6- nav dropdowns fix
startList = function(nav) {
    var sfEls = document.getElementById(nav).getElementsByTagName("li");
    for (var i=0; i<sfEls.length; i++) {
      sfEls[i].onmouseover=function() {
        this.className+=" sfhover";
      }
      sfEls[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp("\\s?sfhover\\b"), "");
      }
    }

    var listItem = document.getElementById(nav).getElementsByTagName('ul');
    for(var i=0;i<listItem.length;i++) {
      listItem[i].onmouseover=function() {
        var changeStyle = this.parentNode.getElementsByTagName('a');
        changeStyle[0].className+=" active";
      }

      listItem[i].onmouseout=function() {
        var changeStyle = this.parentNode.getElementsByTagName('a');
        changeStyle[0].className=changeStyle[0].className.replace(new RegExp("\\s?active\\b"), "");
      }
    }
  }
//window.onload=startList()

categoryList = function catList() {
if (document.all&&document.getElementById) {
		navRoot = document.getElementById("cat");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

blognav = function blognavList() {
if (document.all&&document.getElementById) {
		navRoot = document.getElementById("blognav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

// Adds event to window.onload without overwriting onload functions (in this case - add catList to startList).
function addLoadEvent(func) {    
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    	} 
    else {
        window.onload = function() {
            oldonload();
            func();
        	}
   		 }
}

addEvent(window, 'load', function () { startList('nav'); });
addEvent(window, 'load', function () { categoryList(); });
addEvent(window, 'load', function () { blognav(); });

//addLoadEvent(catList);
//addLoadEvent(blognavList);


  function addEvent( obj, type, fn ) {
    if ( obj.attachEvent ) {
      obj['e'+type+fn] = fn;
      obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
      obj.attachEvent( 'on'+type, obj[type+fn] );
    } else
      obj.addEventListener( type, fn, false );
    }





// subscribe: a/b split
var howMany = 1;  // max number of items listed below
var page = new Array(howMany+1);

page[0]="http://www.sciam.com/subscribe/subscribe_sciam.cfm?lsource=topnav";
page[1]="http://www.sciam.com/subscribe/combo_sciam.cfm?lsource=topnav_sciam";

function rndnumber(){
	var randscript = -1;
	while (randscript < 0 || randscript > howMany || isNaN(randscript)){
		randscript = parseInt(Math.random()*(howMany+1));
	}
	return randscript;
}

quo = rndnumber();
quox = page[quo];

function subscribeLink(){
	window.open(quox);
}

// bookmark page function
function bookmarkpage() {
	var bookmark_url = document.location.href;
	var bookmark_title = document.title;
	var ua=navigator.userAgent.toLowerCase();
	var isSafari=(ua.indexOf('webkit')!=-1);
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		window.external.AddFavorite(bookmark_url,bookmark_title);
	} else if (navigator.appName == "Netscape") {
		alert("Please press CTRL-D or Cmd-D to bookmark this page");
    } else if(window.opera) {
      	alert('Please press CTRL-D or Cmd-D to bookmark this page');		
    } else if(window.home || isSafari) { // IE5/Mac and Safari 1.0
      	alert('Please press CTRL-D or Cmd-D to bookmark this page'); 
} else {
		alert('Please press CTRL-D or Cmd-D to bookmark this page');
	}
}

// check site search and comment forms
function checkSiteSearch(thisform) {
	if (thisform.q.value == "Search entire site" || thisform.q.value == ""){
		alert("Please enter a search term");
		thisform.q.focus();
		return false}
}

function checkCommentForm(thisform) {
	if (thisform.body.value == "Enter Your Comment Here." || thisform.body.value == ""){
		alert("Please enter a comment");
		thisform.body.focus();
		return false
	} else {
		document.getElementById('btn_submit').disabled=true;
	}
}

// jQuery Paging
function GeneratePaging (isf, itr) {
	cp = (isf-1)/iRowsOnPage;
	tp = Math.ceil(itr/iRowsOnPage);
	sText = '';
	iStartPage = 0;
	iEndPage = 0;

	if (cp+1 > iPagesOnBlock) {
		iStartPage = (Math.ceil((cp+1)/iPagesOnBlock)-1)*iPagesOnBlock;
	}
	iEndPage = iStartPage + iPagesOnBlock;
	if (iEndPage > tp) {
		iEndPage = tp;
	}

	// add prev page block link
	if (iStartPage != 0) {
		sText = sText + '<a href="#" onclick="goToPage(' + eval((iStartPage-iPagesOnBlock)*iRowsOnPage+1) + '); return false;">&lt; Prev</a> '
	}

	// create paging block
	for (var i = iStartPage; i < iEndPage; i++) {
		is = (i)*iRowsOnPage+1;
		ie = (i+1)*iRowsOnPage;
		pgDisplay = i + 1;
		if (ie > itr) ie = itr;

		if (i == cp) {
			sText = sText + ' <span class="currentPage">' + pgDisplay + '</span> '
		}
		else {
			sText = sText + ' <a href="#" onclick="goToPage(' + is + '); return false;">' + pgDisplay + '</a> '
		}
	}

	// add next page block link
	if (iEndPage < tp) {
		sText = sText + ' <a href="#" onclick="goToPage(' + eval(iEndPage*iRowsOnPage+1) + '); return false;">Next &gt;</a>'
	}

	$(".pagination").html(sText);
}
//-- end jQuery paging

//  functions for cookies
function getCookie(NameOfCookie) { 
	if (document.cookie.length > 0) { 
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(begin, end));
		}
	}
	return null;
}

function setCookie(NameOfCookie, value, expiredays, domain) { 
	var ExpireDate = new Date ();
	var DomainString = "";
	
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	if (domain) {
		DomainString = "; domain=" + domain; // : "" 
	}
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; path=/; expires=" + ExpireDate.toGMTString()) + DomainString;
}

function delCookie (NameOfCookie) { 
	if (getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Wed, 24-Oct-79 00:00:01 GMT";
	}
}


//  enlarge font function
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}