function getClientWidth(){return document.documentElement.clientWidth}
function getClientHeight(){return document.documentElement.clientHeight}
function by_id (id) {
	var res = document.getElementById(id);
	return res;
}
function by_tag (tag,where) {
	var res=0;
	var where_=where ? where:document;
	var out_ = where_.getElementsByTagName(tag);
	if (out_.length == 1) {res=out_[0]} else {res=out_}
	return res;
}
function by_class(clname,obj,tag_name){
  var tn=tag_name ? tag_name:'*';
  var elmts=by_tag(tn,obj);
  var _out=new Array();
  var i=0, j=0;
  while(elmts[i]){
	if(elmts[i].className==clname){
	  _out[j]=elmts[i];
	  j++;
	}
	i++;
  }
  var res=0;
  if (_out.length == 1) {res=_out[0]} else {res=_out}
  return res;
}

function cumulativeOffset(element) {
  var valueT = 0, valueL = 0;
  do {
    valueT += element.offsetTop  || 0;
    valueL += element.offsetLeft || 0;
    element = element.offsetParent;
  } while (element);
  return {'top':valueT,'left':valueL};
}


var tempimg;
var loader;
//

function createLoader(m,t,l){
  if(m=='create'){
    loader=document.createElement('img');
    document.body.insertBefore(loader,document.body.firstChild);
    loader.src='images/loader.gif';
    loader.width='31'; loader.height='31';
    loader.style.position='absolute';
    loader.style.display='none';
    loader.style.top=t+'px';
    loader.style.left=l+'px';
    loader.style.display='block';
  }else{
    if(!loader) return 0;
    document.body.removeChild(loader);
    delete loader;
    loader=null;
  }  
}

function _setStyle(o,st){
  for(var key in st) if(st.hasOwnProperty(key)){
    o.style[key]=st[key];
  }
}
function createCloseBtn(m,forwhat,callback,side){
  if(m=='create'){    
    var closeBtn=document.createElement('div');
    closeBtn.id='closeBtn0';    
    closeBtn.innerHTML='<strong>CLOSE</strong>';
    _setStyle(closeBtn,{
      position:'absolute',
      zIndex:'1010',
      border:'2px outset #B00',
      backgroundColor:'#B00',
      fontFamily:'Arial',
      fontSize:'12px',
      fontWeight:'normal',
      padding:'0 3px',
      color:'#FFF',
      cursor:'pointer'
    });
    closeBtn.onmouseover=function(ev){
      _setStyle(this,{backgroundColor:'#F00',borderColor:'#F00'});
    }
    closeBtn.onmouseout=function(ev){
      _setStyle(this,{backgroundColor:'#B00',borderColor:'#B00'});
    }
    closeBtn.onmousedown=function(ev){
      _setStyle(this,{border:'2px inset #F00'});
    }
    closeBtn.onclick=function(ev){      
      callback(ev);      
    }
    document.body.insertBefore(closeBtn,document.body.firstChild);
    var forwhatOffs=cumulativeOffset(forwhat);
    var t=0,l=0;
    t=forwhatOffs.top+6;
    switch(side){
      case 'left':{
        l=forwhatOffs.left+5;
      }break;
      case 'right':{
        l=forwhatOffs.left+forwhat.offsetWidth-closeBtn.offsetWidth-5;
      }break;
    }    
    _setStyle(closeBtn,{top:t+'px',left:l+'px'});
  }else{
    var closeBtn=by_id('closeBtn0');
    if(closeBtn){
      document.body.removeChild(closeBtn);
      delete closeBtn;
      closeBtn=null;
    }    
  }
  
}

function showPhoto(lobj,purl){
  var deleteImg=function(iobj){
    iobj.onload=null;
    iobj.onclick=null;  
    document.body.removeChild(iobj);
    createLoader(0);
    delete iobj;
  }  
  var imgborderw=1;
  var cw=getClientWidth()-imgborderw; var ch=getClientHeight()-imgborderw;
  
  tempimg=by_id('tempimg');  
  if(tempimg){deleteImg(tempimg);} 
  var timg=by_tag('img',lobj);
  var timgOffs=cumulativeOffset(timg);  
  createLoader('create',timgOffs.top+Math.round((timg.offsetHeight-31)/2)+3,timgOffs.left+Math.round((timg.offsetWidth-31)/2)+3);  
  
  
  
  tempimg=document.createElement('img');
  document.body.insertBefore(tempimg,document.body.firstChild);
  tempimg.id='tempimg';  
  tempimg.style.position='absolute';
  tempimg.style.zIndex='1000';  
  
  tempimg.style.visibility='hidden';
  setTimeout('tempimg.src=\''+purl+'\'',20);
  
  
  
  tempimg.onclick=function(ev){
    createCloseBtn(0);
    document.body.removeChild(tempimg);
    createLoader(0,0,0);
    createFade(0);
  }
  
  tempimg.onload=function(ev){
    //alert('aaa');   return false;    
    createLoader(0,0,0);
    createFade('create');
    
    var ih=tempimg.offsetHeight; var iw=tempimg.offsetWidth;
    var cih=ih; var ciw=iw;
    var iar=iw/ih;
    if(cih>ch){tempimg.height=ch;cih=ch;ciw=Math.round(cih*iar);tempimg.width=ciw;}
    if(ciw>cw){tempimg.width=cw;ciw=cw;cih=Math.round(ciw/iar);tempimg.height=cih;}
    var itop=(document.documentElement.scrollTop+Math.round((ch-cih)/2));
    var ileft=Math.round((cw-ciw)/2);
    tempimg.style.visibility='visible';
    tempimg.style.top=itop+'px';
    tempimg.style.left=ileft+'px';    
    tempimg.style.border=imgborderw+'px solid #FFFFFF';    
    createCloseBtn('create',tempimg,tempimg.onclick,'right');
    //alert(fade.style.filter);
  }   
}


var fade;
function createFade(m){
  if(m!='create'){
    if(fade){
      document.body.removeChild(fade);
      delete fade;fade=null;
    }
    return true;
  }
  if(fade){
    delete fade; fade=null;
  }
  var cw=getClientWidth(); var ch=getClientHeight();
  fade=document.createElement('div');
  var st=fade.style;
  st.position='absolute';
  st.zIndex='900';
  st.width='100%'; st.height=document.body.offsetHeight+'px';
  st.opacity='0.8'; st.filter='alpha(opacity=80)';
  st.background='#000';
  document.body.insertBefore(fade,document.body.firstChild);
  /*
  fadeFadein=function(o){
    if(o>=80){      
      return true;
    }
    o+=5;
    st.opacity=(o/100);
    st.filter='alpha(opacity='+o+')';
    this.setTimeout('fadeFadein('+o+')',20);
  }
  this.setTimeout('fadeFadein(0)',300);
  */
  
}
