var greatestArea = 0; 
var largestFrame;
var isClosed = false;
var AIDM_onresizeEH;
var AIDM_onloadEH;
var AIDM_timerHandle = -1;
var aicontent = ".";
var aidisplay = false;
var AIDM_hasDisplayed = false;
var AIDM_currentLayer = '1';
var AIDM_bootstrapped = false;
var firstLoad = true;

AIDM_bootstrap();

// Our default AIDM, this is overwritten by aicontent.js from the preproxy.
function AIDM_bootstrap() {
  if (!AIDM_bootstrapped) {
  // ensure that we do this only once - allows for cascaded preproxies and static use of
  // ai.js
  AIDM_bootstrapped = true;

  if (AIDM_isValidBrowser()) {
    AIDMaijs_display=true;

    AIDM_reloadPage(true);
    AIDM_writeIframeFunction();
    AIDM_writeAreaCalculations();

    AIDM_maintainCurrentEventHandlers();
    window.onresize=AIDM_onresize;
    window.onload=AIDM_onload;

    // ensure that we only request an ad for the targetted frame.
    var targetWindow = AIDM_getTargetWindow(top);

      if ((this == targetWindow) && (AIDM_correctSize(window))) {
        AIDM_writeStart();
	    AIDM_hasDisplayed = true;
      }
  }
}
}
// Library functions below, in context executed code (what does that mean?) above.

function AIDM_clearSearchField() {
  if(document.ai_searchform.q.value.indexOf("Search at") >=0) {
    AIDM_disarmTimer();
    document.ai_searchform.q.value = ""
    }
}    

function AIDM_isValidBrowser() {
  if(!( ((navigator.userAgent.toUpperCase().indexOf("MSIE") != -1) && (navigator.userAgent.toUpperCase().indexOf("MSIE 4") != -1)) || ((navigator.appName.indexOf("Netscape") != -1) && (parseInt(navigator.appVersion) < 4)))) {
    return(true)
  } else {
    return(false)
  }
}

function AIDM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {
    if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
      document.AIDM_pgW=innerWidth; document.AIDM_pgH=innerHeight; 
    }
  } else if (innerWidth!=document.AIDM_pgW || innerHeight!=document.AIDM_pgH) 
    location.reload();
}

function AIDM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=AIDM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function AIDM_toggleMe() {
	if (AIDM_currentLayer == "1") {
		AIDM_showMe("0");
	}
	else {
		AIDM_showMe("1");
	}
}


function AIDM_showMe(id){
  AIDM_disarmTimer();

  var i;
  var layers;
  if((parseInt(navigator.appVersion) == 4) && ((navigator.appName) == "Netscape")){
    layers = document.layers;
    for (i=0; i<layers.length; i++){
      if ((layers[i].id).indexOf("AIDMlayer") >= 0){
        layers[i].visibility = "hide";
      }
    }
    layers[id].visibility = "show";
  } else{
    var layerName;
    layers = document.getElementsByTagName("div");
    for (i=0; i<layers.length; i++){
      layerName = (layers[i].id);
      if (layerName.indexOf("AIDMlayer") >= 0){
        layers[i].style.visibility = "hidden";
      }
    }
    var vlayer = "AIDMlayer" + id;
    for (i=0; i<layers.length; i++){
      layerName = (layers[i].id);
      if (layerName.indexOf(vlayer) >= 0){
        layers[i].style.visibility = "visible";
      }
    }
  }
  AIDM_currentLayer = id;
}

function AIDM_closeMe(){
  AIDM_disarmTimer();
  var browserName = navigator.userAgent.toUpperCase()
  var layers;
  if((parseInt(navigator.appVersion) == 4) && ((navigator.appName) == "Netscape")){
    layers = document.layers;
    for (i=0; i<layers.length; i++){
      if ((layers[i].id).indexOf("AIDMlayer") >= 0){
        layers[i].visibility = "hide";
      }
    }
  } else {
    layers = document.getElementsByTagName("div");
    for (i=0; i<layers.length; i++) {
      var layerName = (layers[i].id);
      if (layerName.indexOf("AIDMlayer") >= 0) {
        layers[i].style.visibility = "hidden";
      }
    }
  }
}

function AIDMhideLayer(){
  if(!isClosed) { AIDM_showMe('0') }
}

function AIDM_onload(){
    if(AIDM_onloadEH) {
      AIDM_onloadEH();
    }
    AIDM_armTimer()
}

function AIDM_armTimer(){     var t = 15000; AIDM_timerHandle = setTimeout(AIDMhideLayer, t); }
function AIDM_disarmTimer(){  if(AIDM_timerHandle > 0) { clearTimeout(AIDM_timerHandle); AIDM_timerHandle = -1; } } 

function AIDM_swapImage() { //v3.0
  var i,j=0,x,a=AIDM_swapImage.arguments; document.AIDM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=AIDM_findObj(a[i]))!=null){document.AIDM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function AIDM_swapImgRestore() { //v3.0
  var i,x,a=document.AIDM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function AIDM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.AIDM_p) d.AIDM_p=new Array();
   var i,j=d.AIDM_p.length,a=AIDM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.AIDM_p[j]=new Image; d.AIDM_p[j++].src=a[i];}}
}

function AIDM_writeStart() {
   var aiLayer;
  var AIleft;
  var browserName = navigator.userAgent.toUpperCase()
  var useDiv;
  if((browserName.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) > 3) || ((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5)  ){
    AIleft = screen.width - 167;
	useDiv="t";
}
  else {
    AIleft = window.innerWidth - 160;
    useDiv="f"
   }

var aiLayer = "<script LANGUAGE=\"JavaScript\" src=\"http://preproxy.interfusion.net.au/aicontent.js?div=" + useDiv + "&l=" + AIleft + "&url=" + escape(document.location) + "\"></script>";
  document.write(aiLayer)
}

//function must be a string for nn4 does not allow "try" to be used
function AIDM_writeIframeFunction (){
  var iframeFunction = "<SCRIPT LANGUAGE=\"JavaScript\">"
  if((navigator.userAgent.toUpperCase().indexOf("MSIE 5") != -1) || ((navigator.appName  == "Netscape") && (parseInt(navigator.appVersion) >= 5))){
    iframeFunction += "function isIframe() {"
    iframeFunction +="  try{"
    iframeFunction +="    if(top.document.domain){"
    iframeFunction +="      return(false)"
    iframeFunction +="    }"
    iframeFunction +="  }"
    iframeFunction +="  catch(e){"
    iframeFunction +="    return(true)"
    iframeFunction +="  }"
    iframeFunction +="}"
  }else {
    iframeFunction += "function isIframe() {" 
    iframeFunction +="  return(false)"
    iframeFunction +="}"
  }
  iframeFunction += "</SCRIPT>"
  document.write(iframeFunction)
 
}

function AIDM_onresize() {
  if(AIDM_onresizeEH) { AIDM_onresizeEH(); }
  AIDM_repositionDiv();
}
  
function AIDM_repositionDiv() {
  if ((AIDM_hasDisplayed == false)  || (aidisplay == false)){
     return;
  }
   
  if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5)){
    var divPositionMax = parseInt(document.getElementById("AIDMlayer1").style.left);
    var divPositionMin = parseInt(document.getElementById("AIDMlayer0").style.left);
    var divAdjustedPos = window.innerWidth -160;
    divPositionMax = divAdjustedPos;
    divPositionMin = divAdjustedPos;
    document.getElementById("AIDMlayer0").style.left = divPositionMin + "px"
    document.getElementById("AIDMlayer1").style.left = divPositionMax + "px"
  }else if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) < 5)){
    AIDM_reloadPage();
  }else{
    var moveByInIE=document.body.clientWidth-147;
    document.all.AIDMlayer1.style.left=document.body.clientWidth-147;
    document.all.AIDMlayer0.style.left=document.body.clientWidth-147;
  }
}
function AIDM_maintainCurrentEventHandlers() {
  if(window.onresize){
    AIDM_onresizeEH = window.onresize;
    }else {
      AIDM_onresizeEH = function noResize() {}
     }
  if(window.onload){
    AIDM_onloadEH = window.onload;
  }else {
    AIDM_onloadEH = function noLoad() {}
  }
}

function AIDM_areaOfFrame(targetFrame) {
  var area = 0;
  if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5)){
    //nn6
    area = calcNN6AreaOfFrame(targetFrame);
  } else if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) < 5)) {
    //nn4
    area = targetFrame.innerWidth*targetFrame.innerHeight;
  } else {
   area = calcIEAreaOfFrame(targetFrame);
  }
  return(area);
}

function AIDM_writeAreaCalculations(){
  var funcstring= "<SCRIPT LANGUAGE=\"JavaScript\">"
  if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5)){
    funcstring += "function calcNN6AreaOfFrame(targetFrame) {"
    funcstring +="  try{"
    funcstring +="    area = targetFrame.document.width*targetFrame.document.height;"
    funcstring +="  } catch(e) {"
    funcstring +="    area = 0;"
    funcstring +="  }"
    funcstring +="  return(area);"
    funcstring +="}"
  } else if (navigator.userAgent.toUpperCase().indexOf("MSIE") != -1) {
    funcstring += "function calcIEAreaOfFrame(targetFrame){"
    funcstring +="  try{"
    funcstring +="    area = targetFrame.document.body.clientWidth*targetFrame.document.body.clientHeight;"
    funcstring +="  } catch(e) {"
    funcstring +="    area = 0;"
    funcstring +="  }"
    funcstring +="  return(area);"
    funcstring +="}"
  }
  funcstring += "</SCRIPT>"
  document.write(funcstring)
}

function AIDM_correctSize(w) {
  var windowHeight;
  var windowWidth;
  var AIDMWIDTH = 500;
  var AIDMHEIGHT = 300;
  if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5)){
    //nn6
    windowHeight = w.document.height;
    windowWidth = w.document.width;
    if((windowHeight >= AIDMHEIGHT) && (windowWidth >= AIDMWIDTH)) {
      return (true);
    } else {
      return (false);
    }

  } else if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) < 5)){
    //nn4

    windowHeight = w.innerHeight;
    windowWidth = w.innerWidth;
    if((windowHeight >= AIDMHEIGHT) && (windowWidth >= AIDMWIDTH)) {
      return (true);
    }
    else {
      return (false);
    }

  } else {
    //ie
    if(w.document.body){
      windowHeight = w.document.body.clientHeight;
      windowWidth = w.document.body.clientWidth;
      if((windowHeight >= AIDMHEIGHT) && (windowWidth >= AIDMWIDTH)) {
        return (true);
      }
      else {
        return (false);
      }
    } else {
      //no body object
      return(false);
    }
  }
}

function AIDM_getTargetWindow(w) {
  if(!(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5))){ 
    if(isIframe()) {
      return(null);
    }
  }
  var i = 0;
  var thisFrameArea;
  if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) >= 5)){
    //nn6
    //if(w.frames.length == 0 || w.document.getElementsByTagName('iframe')[0]) { 
    // BELOW IS LINE THAT IS CAUSING ISSUES!
    if(w.frames.length == 0) {
      return(this);
    } else if (w.document.getElementsByTagName('iframe')[0]) { 
      return(this);
    }else if(w.frames.length != 0) {
      for(i=0;i<w.frames.length;i++) {
        if(w.frames[i].frames.length>0) {
          //contains a framset
          AIDM_getTargetWindow(w.frames[i]);
        } else {
          thisFrameArea = AIDM_areaOfFrame(w.frames[i]);
          if(thisFrameArea > greatestArea) {
            greatestArea = thisFrameArea;
            largestFrame = w.frames[i];
          }
        }
      }//end for
      return(largestFrame);
    }//end else

  }else if(((navigator.appName) == "Netscape") && (parseInt(navigator.appVersion) < 5)){
    //nn4
    if(w.frames.length == 0) { 
      return(this);
    }else if(w.frames.length != 0) {
      for(i=0;i<w.frames.length;i++) {
        if(w.frames[i].frames.length>0) {
          //contains a framset
          AIDM_getTargetWindow(w.frames[i]);
        } else {
          thisFrameArea = AIDM_areaOfFrame(w.frames[i]);
          if(thisFrameArea > greatestArea) {
            greatestArea = thisFrameArea;
            largestFrame = w.frames[i];
          }
        }
      }//end for
      return(largestFrame);
    }//end else
  }else{
    //ie
    if(w.frames.length == 0 || w.document.getElementsByTagName('iframe')[0]) { 
      return(this);
    }else if(w.frames.length != 0) {
      for(i=0;i<w.frames.length;i++) {
        if(w.frames[i].frames.length>0) {
          //contains a framset
          AIDM_getTargetWindow(w.frames[i]);
        } else {
          thisFrameArea = AIDM_areaOfFrame(w.frames[i]);
          if(thisFrameArea > greatestArea) {
            greatestArea = thisFrameArea;
            largestFrame = w.frames[i];
          }
        }
      }//end for
      return(largestFrame);
    }//end else
  }
  return(null);
}

function AIDMwriteContent(aicontent) {
  var targetWindow = AIDM_getTargetWindow(top);
  if ((!aidisplay) || (this != targetWindow) || (!AIDM_correctSize(window))) {
    // Not a target window
    return;
  }
  document.write(aicontent);
  AIDM_repositionDiv();
  if (firstLoad) {
        firstLoad = false;
	AIDM_showMe("1");
  }
}

