/* book utilities */

/* Stack up window.onload event using this function from Simon Willison - http://www.sitepoint.com/blog-post-view.php?id=171578 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*old popup code*/

buttonClicked=false;
function myOpenWindow() {
    myWindowHandle = window.open('about:blank','myWindowName','width=450,height=421');
}

 /* popup window generating code */
function acpopup(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,scrollbars,status,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="resizable,scrollbars,status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="resizable,scrollbars,status,height="+strHeight+",width="+strWidth;
window.open(strURL, '', strOptions);
}


/* unobtrusive popup code [PROBABLY REDUNDENT] */

/* XHTML target attribute */
function windowLinks() {
    if(!document.getElementsByTagName) {
         return;
    }

    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var relIndex = anchor.rel;
		 if (relIndex){
		 var relSplit = relIndex.split("|");
		 /* XHTML compliant target attribute */
		 if (relSplit[0] == "external")
            anchor.target = "_blank";
		/* XHTML compliant popup attribute */
   			else if (relSplit[0] == "popup") {
				anchor.popupWidth = relSplit[1];
				anchor.popupHeight = relSplit[2];
	      anchor.popupMode = relSplit[3] ? relSplit[3] : 'console';
	      anchor.onclick = function() {acpopup(this.href,this.popupMode,this.popupWidth,this.popupHeight);return false;};
			}
		}
	   }
}




/* OLD POPUP INITIALISATION CODE
addLoadEvent(function() {
	windowLinks();
});
*/




/* NEW POPUP INITIALISATION CODE based on document click handler to bypass load dependency
   this probably makes the windowLinks() function redundent,
   but I'm leaving it there just in case it's used elsewhere

   This code uses an unencapsulated document.onclick handler, which is necessary
   because it's the only way to suppress the default action in bloody Safari */
document.onclick = function(e)
{
	//get event node node reference
	var node = e ? e.target : window.event.srcElement;

	//iterate upwards from event node until we find a link
	//but abandon iteration and return true on the link
	//if we run out of nodes (because we're already at the body) or reach the body
	while(node.nodeName.toLowerCase() != 'a')
	{
		if(!node.parentNode) { return true; }
		else if(node.nodeName.toLowerCase() == 'body') { return true; }
		else { node = node.parentNode; }
	}

	//if we got this far we have a link reference
	//so look for the rel attribute to get popup information
	if(node.getAttribute('rel'))
	{
		//split for data
		var rel = node.rel.split('|');

		//there's no such thing as an XHTML compliant target attribute
		//but ho hum, better preserve the functionality anyway [for now ;)]
		if (rel[0] == 'external')
		{
			node.target = '_blank';
			return true;
		}

		//if we have popup data, create the popup and don't follow the regular link
		else if(rel[0] == 'popup')
		{
			// logging via remote scripting
			var now = new Date(), uid = now.getTime() + '-' + now.getMilliseconds() + '-' + Math.floor(Math.random() * 10000000);
			var popupzone = '', popupid = '';
			if(node.href.indexOf('?') != -1)
			{
				var params = node.href.split('?')[1].split('&'), info = {};
				for(var i=0; i<params.length; i++)
				{
					params[i] = params[i].split('=');
					info[params[i][0]] = params[i][1];
				}
				for(i in info)
				{
					if(i == 'popupzone' || i == 'zone' || i == 'zoneid') { popupzone = info[i]; }
					if(i == 'popupid') { popupid = info[i]; }
				}
			}
			var logu = document.createElement('img');
			logu.src = '/popup/popuplog.php?zoneid='+popupzone+'&popupid='+popupid+'&type=booksample&path=' + encodeURI(document.location.pathname) + '&uid=' + uid;

			//open the popup, passing the uid for conversion tracking
			var nodehref = node.href + (node.href.indexOf('?') == -1 ? '?' : '&') + 'uid=' + uid;
			acpopup(nodehref, (typeof rel[3] != 'undefined' ? rel[3] : 'console'), rel[1], rel[2]);

			//don't follow the regular link
			return false;
		}
	}

	//if we got this far we don't have rel information
	//so just follow the link as normal
	return true;
};


