// taken from x_core.js
// X v3.15.1, Cross-Browser DHTML Library from Cross-Browser.com
// Copyright (c) 2002,2003,2004 Michael Foster (mike@cross-browser.com)
// This library is distributed under the terms of the LGPL (gnu.org)

/* all functions here are used cross browser */

// Variables:
var xVersion='3.15.1',xNN4=false,xOp7=false,xOp5or6=false,xIE4Up=false,xIE4=false,xIE5=false,xUA=navigator.userAgent.toLowerCase();
if(window.opera){
  xOp7=(xUA.indexOf('opera 7')!=-1 || xUA.indexOf('opera/7')!=-1);
  if (!xOp7) xOp5or6=(xUA.indexOf('opera 5')!=-1 || xUA.indexOf('opera/5')!=-1 || xUA.indexOf('opera 6')!=-1 || xUA.indexOf('opera/6')!=-1);
}
else if (document.all) {
  xIE4Up=xUA.indexOf('msie')!=-1 && parseInt(navigator.appVersion)>=4;
  xIE4=xUA.indexOf('msie 4')!=-1;
  xIE5=xUA.indexOf('msie 5')!=-1;
};
// Object:
function xGetElementById(e) {
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}
/* Cookie handler */
function readCookie(cookieName) {
  var docCookies = document.cookie;
  var startIndex = docCookies.indexOf(cookieName);
  if (startIndex == -1) return false;
  startIndex += cookieName.length + 1;
  var endIndex = docCookies.indexOf(";",startIndex);
  if (endIndex == -1) endIndex = docCookies.length;
  var cookieValue = docCookies.substring(startIndex, endIndex);
  return unescape(cookieValue);
}


 /* 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;};
			}
		}
	   }
}


/* 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();
    }
  }
}



function openwindow(url)
{
	window.open(url, "popme", "width=560, height=420, location=no, menubar=no, status=no, toolbar=no, scrollbars=auto, resizable=yes");
	focus();
}





/* 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)
{
	//ignore button presses that aren't left button in Firefox
	if(typeof e != 'undefined' && typeof e.which != 'undefined' && e.which != 1) { return true; }

	//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; }
	}

	//return false on beatbox links
	if(node.className.indexOf('beatbox') != -1) { return false; }

	//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
		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;
};

