function popUp(contents,pos,id){
  if(id && document.getElementById(id) ){
    var pup=document.getElementById(id);
    pup.style.zIndex=pg.zCount++;
    pup.getElementsByTagName('input')[0].focus();
    return;
  }

  var frame=document.createElement('div');
  if(id) frame.setAttribute('id',id);
  frame.className="info_window";


  var container=document.createElement('div');
  container.innerHTML=contents;
  frame.appendChild(container);
  document.body.appendChild(frame);


  var shim=this.mkShim();

  var pos = pos || { X:0,Y:0 }
  frame.style.left=pos.X+(-1/2*frame.offsetWidth)+"px";
  frame.style.top=pos.Y+(-1/2*frame.offsetHeight)+"px";
  frame.style.zIndex=pg.zCount++;

  shim.style.left=frame.style.left;
  shim.style.top=frame.style.top;
  shim.style.width=frame.offsetWidth+"px";
  shim.style.height=frame.offsetHeight+"px";
  shim.style.zIndex=1;

  document.body.appendChild(shim);

  this.frame=frame;
  this.shim=shim;
  this.container=container;
  this.mkCloseButton();
  this.mkDraggable();

  this.closeButton.focus();
};

popUp.prototype.mkCloseButton=function(txt){
  var txt=txt || ' X ';
  var button=document.createElement('input');
  button.setAttribute('type','button');
  button.setAttribute('value',txt);
  var frame=this.frame;
  var shim=this.shim;
  button.onclick=function(){ document.body.removeChild(frame); document.body.removeChild(shim) }
  this.frame.insertBefore(button,this.frame.firstChild);
  this.closeButton=button;
};

popUp.prototype.mkShim=function(){
  var shim=document.createElement('iframe');
  shim.setAttribute('scrolling','no');
  shim.setAttribute('src',"javascript:void(0)");
  shim.setAttribute('frameborder',"0");
  shim.style.position="absolute";
  shim.style.overflow="hidden";
  return shim;
}

popUp.prototype.mkDraggable=function(){
  var frame=this.frame;
  var shim=this.shim;
  Drag.init(this.container,frame);
  frame.onDragStart=function(){
    frame.style.zIndex=pg.zCount++
  }
  frame.onDrag=function(xpos,ypos){
    shim.style.left=xpos+"px";
    shim.style.top=ypos+"px";
  }
};


function getX(e){
  e = e || window.event;
  return e.pageX || (document.documentElement && document.documentElement.scrollLeft) + e.clientX || e.clientX + document.body.scrollLeft;
}

function getY(e){
  e = e || window.event;
  return e.pageY || (document.documentElement && document.documentElement.scrollTop) + e.clientY || e.clientY + document.body.scrollTop;
}

function getCoords(e){
  return { X: getX(e), Y: getY(e) }
}

function cancelClick(e){
  if (window.event){
          window.event.cancelBubble = true;
          window.event.returnValue = false;
  }
  if (e && e.stopPropagation && e.preventDefault){
          e.stopPropagation();
          e.preventDefault();
  }
}

function addEvent(ob,ev,Fn,capture){
  if(ob.addEventListener)
    ob.addEventListener(ev,Fn,capture || false);
  else if(ob.attachEvent)
    ob.attachEvent('on'+ev,Fn)
  else ob['on'+ev]=Fn
}

pg={ zCount:20,timer:null };

bgChange={
  picFrameId:'college-pic',
  listId:'college_list',
  init:function(){
    if(!document.getElementById || !document.createTextNode) return;
    bgChange.bg=document.getElementById(bgChange.picFrameId);
    if(!bgChange.bg) return
    bgChange.college_list=document.getElementById(bgChange.listId).getElementsByTagName("a");
    if(!bgChange.college_list) return;
    for(var i=0; i<bgChange.college_list.length; i++){
      bgChange.college_list[i].index=i;
      bgChange.college_list[i].onmouseover=function(){
        var dx=this.index;
        clearTimeout(pg.timer);
        pg.timer=setTimeout(function(){
          bgChange.bgShift(bgChange.bg, (-162*(dx+1) ) , 0 );
        },500)
      }
      bgChange.college_list[i].onmouseout=function(){
        clearTimeout(pg.timer);
        pg.timer=setTimeout(function(){ bgChange.bgShift(bgChange.bg, 0 , 0 ) },500);
      }
    }
  },
  bgShift:function(obj,xshift,yshift){
    obj.style.backgroundPosition=xshift + "px " + yshift + "px";
} }

function flinks(){
  var a=document.getElementsByTagName('a');
  for(var i=0; i<a.length; i++){
    if(a[i].getAttribute("rel")=='cal' || a[i].getAttribute("rel")=='news') {
      a[i].setAttribute('rel',a[i].search);
//       a[i].setAttribute('href','javascript:void(0)');
      a[i].onclick=function(e){
        var searchString=this.getAttribute('rel');
        pos=getCoords(e);
        ajax({
          url:'/index.php'+searchString+'&xhr',
          onSuccess:function(html){ var pup = new popUp( html, pos, searchString.replace(/.*=/,'evid') );  }
        })
        cancelClick(e);
      }
} } }


function windowInit(){
  if(!document.getElementById || !document.createTextNode) return;
  bgChange.init();
  flinks();
}


addEvent(window,'load',windowInit);

