// main js that must be loaded prior to page display

// cache common images
var logo=new Image(246,216);
logo.src='/images/logo.gif';
var pawGrey=new Image(58,48);
pawGrey.src='/images/paw.gif';
var pawRed=new Image(58,48);
pawRed.src='/images/paw_red.gif';

// disable right button function set
var warningMessage='Right mouse button is disabled to deter picture theft. Sorry.';
function onMouseDownIE4(){
  if(event.button==2){
    alert(warningMessage);
    return false;
  }
};

function onMouseDownNS4(e){
  if(document.layers||document.getElementById&&!document.all){
    if(e.which==2||e.which==3){
      alert(warningMessage);
      return false;
    }
  }
};

if(document.layers){
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=onMouseDownNS4;
}else if(document.all&&!document.getElementById){
  document.onmousedown=onMouseDownIE4;
};

document.oncontextmenu=new Function("alert(warningMessage);return false");

// rotate animal pictures
var delay=5000 //5 seconds

//Counter for array
var count=0;

var cubeimage=new Array()
for (i=0;i<specifyimage.length;i++){
  cubeimage[i]=new Image()
  cubeimage[i].src=specifyimage[i]
}

function movecube(){
  if (window.createPopup) cube.filters[0].apply()
  document.images.cube.src=cubeimage[count].src;
  if (window.createPopup) cube.filters[0].play()
  count++;
  if (count==cubeimage.length) count=0;
  setTimeout("movecube()",delay)
}

// from main body onLoad. main display is up. do some housekeeping
function postDisplay(){
//  detectPopUpBlocker();
//  setTimeout('movecube()',delay);
  return;
}

// a shorter getElementByID
function getMyElement(id){
  var d=document.getElementById?document.getElementById(id):document.all?document.all[id]:null;
  return d;
}

// a 2 stage popup blocker checker
// used wherever there is a div w id='PopupBlocker' (currently just home page)
function detectPopUpBlocker(){
return;
  var d=getMyElement('PopupBlocker'); if(!d) return;
  var tP=0; var eF=1;
  var popWin=window.open('','ptest','width=100,height=100');
  if(popWin && typeof(popWin)!='undefined'){
    popWin.blur();
    var pW = window.open('','ptest','width=100,height=100');
    if(pW && typeof(pW)!='undefined'){ eF=(pW==popWin)?0:1; }
    popWin.close();
  }
  tP=(eF==0)?1:0;
  if(!tP){
    var s=''; d.innerHTML='';
    s+="<span class='warning'>Please Enable Popups For This Site</span><br>";
    s+="This site uses popups for more intuitive navigation.<br>";
    s+="We DO NOT employ ad popups.";
    s+="We DO NOT popup off this site.";
    d.innerHTML=s;
  } 
  return;
}

var stackExtrasOn=0;
var startScroller=0;
// load a new main page text
function newPage(p){
  var url="http://"+location.hostname+"/"+p;
  try{ req=new XMLHttpRequest(); }catch(e){
    try{ req=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){
      try{ req=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ req=null; }
    }
  }
  if(req==null){
    alert("INTERNAL ERROR P1: Unable to request new page."); return;
  }
  req.onreadystatechange=pageResp;
  req.open("GET",url,true);
  req.send(null);
  return;
}

// respond to page request 
function pageResp(){
 if(req.readyState!=4){ return; }
  if(req.status!=200){
    alert("INTERNAL ERROR P2: ["+req.status+"]"+req.statusText); return;
  }
  if(req.responseText!=''){
    var e=getMyElement('mainText'); e.innerHTML='';
    e.innerHTML=req.responseText;
    e=getMyElement('bigolddogs');
    if(e){
      var now=new Date(); var s='';
      var sec=now.getSeconds(); var img=sec%2;
      switch(img){
	case 0: s="<br><img src='/images/bigdogad.gif'>"; break;
	case 1: s="<br><img src='/images/maturepet.gif'>"; break;
      }
      e.innerHTML=s;
    }
    if(startScroller){ fillup(); startScroller=0; }
//    if(stackExtrasOn){ stackExtras('/feral_extra.shtml'); stackExtrasOn=0; }
//    else{ e=getMyElement('stackExtras'); e.innerHTML=''; }
  }
  return;
} 

// load extra stack text
function stackExtras(p){
  var url="http://"+location.hostname+"/"+p;
  try{ req=new XMLHttpRequest(); }catch(e){
    try{ req=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){
      try{ req=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ req=null; }
    }
  }
  if(req==null){
    alert("INTERNAL ERROR P1: Unable to request new page."); return;
  }
  req.onreadystatechange=stackResp;
  req.open("GET",url,true);
  req.send(null);
  return;
}

// respond to stack extras request 
function stackResp(){
 if(req.readyState!=4){ return; }
  if(req.status!=200){
    alert("INTERNAL ERROR P2: ["+req.status+"]"+req.statusText); return;
  }
  if(req.responseText!=''){
    var d=getMyElement('stackExtras'); d.innerHTML='';
    d.innerHTML=req.responseText; d.style.visibility='visible';
  }
  return;
} 


// button stack paw flasher
function redPaw(b){
  var e=getMyElement(b); if(!e){ return false; }
  e.src='/images/paw_red.gif';
  return true;
}
function greyPaw(b){
  var e=getMyElement(b); if(!e){ return false; }
  e.src='/images/paw.gif';
  return true;
}

// various popup windows
function newWindow(w){
  var win=window.open(w,"New_Window",
    "height=600,width=700,resizable,scrollbars,menubar"
  );
  win.window.focus();
}
function helpWindow(w) {
  var win=window.open(w,"Help_Window",
    "height=500,width=430,resizable,scrollbars,menubar"
  );
  return;
}

// respond to shelter selection
function shelter(b){
  var f=eval("document.shelterForm"+b);
  var sh=f.shelter;
  if(sh.selectedIndex<1){
    alert("No shelter selected"); return;
  }
  var i=sh.selectedIndex;
  var s=sh.options[i].value;
  var w="/cgi-bin/search_shelter.pl?sp="+b+"&s="+s;
  var win=window.open(w,"New_Window",
    "height=600,width=700,resizable,scrollbars,menubar"
  );
  win.focus();
  return;
}

// respond to dog form if using 'search' list
function searchBy(){
  var f=document.dogForm;
  var i=f.size.selectedIndex;
  var s=f.size.options[i].value;
  var w="/cgi-bin/search_dog.pl?size="+s;
  var win=window.open(w,"New_Window",
    "height=600,width=700,resizable,scrollbars,toolbar"
  );
  win.focus();
  return;
}

// expand/contract paragraphs on the feral cat page
function toggle(id){
  var d=getMyElement(id); if(!d) return;
  if(d.style.visibility=='hidden'){
    d.style.visibility='visible'; d.style.height='auto';
  }else{
    d.style.visibility='hidden'; d.style.height=0;
  }
  return;
}
