var popup_last_url='';
var popup_window,popup_window_td,popup_window_content,popup_window_height=0;
var popup_onload=false;
function popupWindow(url,title,width,height,fade,onload){
  if(typeof(onload)=='function') popup_onload=onload;
  if(typeof(fade)=='undefined') var fade=true;
  popup_window=document.getElementById('popup_window');
  popup_window_content=document.getElementById('popup_window_content');
  body_bounds=getBounds(document.body);
  if(url){
    document.getElementById('popup_window_title').innerHTML=title;
    if(url!=popup_last_url){
      popupWindowContent(url);
      popup_last_url=url;
    }
    if(width){
      popup_window.style.width=(width+60)+'px';
      popup_window_content.style.width=width+'px';
    }
    if(height) popup_window_height=height;
    showAplhaBg(true);
    show_or_hide(true,'popup_window',fade);
    popupWindowPosition();
    addHandler(document,'keypress',keyClose);
  }else{
    removeHandler(document,'keypress',keyClose);
    showAplhaBg(false);
    show_or_hide(false,popup_window.id);
  }
  function keyClose(e){
    if(getCharCode(e)==27) popupWindow(false);
  }
}
function popupWindowContent(url,form){
  popup_window_td=document.getElementById('popup_window_td');
  popup_window_content.style.height='0px';
  popup_window_content.innerHTML='&nbsp;';
  loadContent(url,popup_window_content,popup_window_content,form,function(){
    popup_window_content.style.height='0px';
    popup_window_content.style.overflow='hidden';
    popupWindowDuration();
    popupWindowPosition();
  });
}
function popupWindowDuration(iter,val,step,last_height){
  if(opac_fade_interval){
    setTimeout('popupWindowDuration('+iter+','+val+','+step+','+last_height+')',100);
    return false;
  }
  if(!step) step=1;
  if(!iter) iter=0;
  if(!val) val=(popup_window_height ? popup_window_height : popup_window_content.scrollHeight);
  if(!last_height) last_height=0;
  iter+=step;
  var new_height=Math.pow(iter,2);
  popup_window_content.style.height=new_height+'px';
  if(new_height<val){
    setTimeout('popupWindowDuration('+iter+','+val+','+step+','+new_height+')',10);
  }else{
    popup_window_content.height=val+'px';
    popup_window_content.style.display='block';
    popup_window_content.style.overflow='auto';
    if(typeof(popup_onload)=='function') popup_onload();
  }
  popupWindowPosition();
}
function popupWindowPosition(){
  popup_window_bounds=getBounds(popup_window);
  var new_top=(f_clientHeight()/2-popup_window_bounds.height/2);
  if(new_top<10) new_top=10;
  popup_window.style.top=(new_top+f_scrollTop())+'px';
  popup_window.style.left=(f_clientWidth()/2-popup_window_bounds.width/2+f_scrollLeft())+'px';
  document.getElementById('popup_window_head').style.width=(popup_window_bounds.width-4)+'px';
}

var dragObject=resizeObject=false;
var dragDiffY=dragDiffX=resizeStartX=resizeStartY=resizeStartWidth=resizeStartHeight=0;
document.onmouseup=function(){
  removeHandler(document,'mousemove',photoMove);
}
document.onmousedown=function(e){
  var tempobj=(IE ? event.srcElement : e.target);
  var topelement='HTML';
  while(tempobj.tagName!='HTML' && tempobj.tagName!='BODY' && tempobj.id!='popup_window_head' && tempobj.id!='popup_window_resizer'){
    tempobj=(IE ? tempobj.parentElement : tempobj.parentNode);
  }
  if(tempobj.id=='popup_window_head' || tempobj.id=='popup_window_resizer'){
    if(tempobj.id=='popup_window_head'){
      dragObject=popup_window;
      drag_bounds=getBounds(dragObject);
      dragDiffY=mouseY-drag_bounds.top;
      dragDiffX=mouseX-drag_bounds.left;
      addHandler(document,'mousemove',photoMove);
    }else{
      resizeObject=document.getElementById('popup_window_table');
      resize_bounds=getBounds(resizeObject);
      resizeStartX=mouseX;
      resizeStartY=mouseY;
      resizeStartWidth=resize_bounds.width;
      resizeStartHeight=resize_bounds.height;
      addHandler(document,'mousemove',photoResize);
    }
    return false;
  }
}

function photoMove(e){
  dragObject.style.left=(mouseX-dragDiffX)+'px';
  dragObject.style.top=(mouseY-dragDiffY)+'px';
  return false;
}
function photoResize(e){
  if(resizeObject){
    resizeObject.style.width=(resizeStartWidth+mouseX-resizeStartX)+'px';
  }
}
