// SiteCraft 2 Generic JavaScript Library 1.6
// Copyright (C) 2006-2008 ConsidoNet Solutions Paweł Gabryelewicz
var im, imopened = false, clfading = false, closehold = false, tm;

function PM(l, p1, p2) {
  var link = document.getElementById(l+p1+p2);
  link.href = 'mailto:'+l+'@'+p1+'.'+p2;
  link.innerHTML = l+'@'+p1+'.'+p2;
}

var menuexp = false;
var menureadytoclose = false;

function SetTransparency(obj, ratio) {
  if(ratio<0) ratio=0;
  if(ratio>100) ratio=100;
  obj.style.MozOpacity=(ratio*0.01);
  obj.style.KhtmlOpacity=(ratio*0.01);
  obj.style.filter="alpha(opacity=" + ratio + ")"; 
  obj.style.opacity=(ratio*0.01);
} 

function GetTopOffset(Obj) {
  var OffsetTop = Obj.offsetTop; var OffsetParent = Obj.offsetParent;
  while(OffsetParent) { OffsetTop+= OffsetParent.offsetTop; OffsetParent = OffsetParent.offsetParent; }
  return OffsetTop;
}

function GetLeftOffset(Obj) {
  var OffsetLeft = Obj.offsetLeft; var OffsetParent = Obj.offsetParent;
  while(OffsetParent) { OffsetLeft+= OffsetParent.offsetLeft; OffsetParent = OffsetParent.offsetParent; }
  return OffsetLeft;
}

function ShowFS(img, imwidth, imheight) {
  // Viewport
  
  var brwh;
  var brww;
  
  if( typeof( window.innerWidth ) == 'number' ) { brwh = window.innerHeight; brww = window.innerWidth; } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { brwh = document.documentElement.clientHeight; brww = document.documentElement.clientWidth; } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { brwh = document.body.clientHeight; brww = document.body.clientWidth; }  
  
  if(document.all && !window.opera) brww+=20; // IE shows dimensions without scrollbar
  
  var safex = brww - 40;
  var safey = brwh - 50;
  
  
  var dimensions = GetResizedDimensions(imwidth, imheight, safex, safey);
  
  ShowBig('./imageget.php?width='+dimensions[0]+'&height='+dimensions[1]+'&image='+img+'&quality=90', dimensions[0], dimensions[1]);
}

function ShowBig(img, imwidth, imheight) {  
  imopened = true;
  var brwh=0, brww=0, scrOfX = 0, scrOfY = 0, pageWidth = 0, pageHeight = 0;
  var ajaxinner = document.getElementById('ajaxinner');
  
  // Viewport
  if( typeof( window.innerWidth ) == 'number' ) { brwh = window.innerHeight; brww = window.innerWidth; } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { brwh = document.documentElement.clientHeight; brww = document.documentElement.clientWidth; } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { brwh = document.body.clientHeight; brww = document.body.clientWidth; }  
  
  // Scrolled screen offsets
  if( typeof(window.pageYOffset) == 'number' ) { scrOfY = window.pageYOffset; scrOfX = window.pageXOffset; } 
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; } 
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; }

  // Full body dimensions
  if( window.innerHeight && window.scrollMaxY ) { pageWidth = window.innerWidth + window.scrollMaxX; pageHeight = window.innerHeight + window.scrollMaxY; } 
  else if( document.body.scrollHeight > document.body.offsetHeight ) { pageWidth = document.body.scrollWidth; pageHeight = document.body.scrollHeight; } 
  else { pageWidth = document.body.offsetWidth + document.body.offsetLeft; pageHeight = document.body.offsetHeight + document.body.offsetTop; }
  
  
  ajaxinner.style.display = 'block';
  
  ajaxinner.innerHTML='';
  ajaxinner.innerHTML+='<div id="fsbg" class="fsbg" onclick="javascript: CloseIm();"></div>';
  ajaxinner.innerHTML+='<div id="imgwindow" class="imgwindow"></div>';
  
  var fsbg = document.getElementById('fsbg');
  SetTransparency(fsbg, 0);
  fsbg.style.left = scrOfX+'px';
  fsbg.style.top = '0';
  fsbg.style.width = '100%';
  fsbg.style.height = pageHeight+'px';
  fsbg.style.display = 'block';
  
  for(i=0; i<7; i++) setTimeout("SetTransparency(document.getElementById('fsbg'), "+i+"*10)", (parseInt((i*25))));
  
  var imgwindow = document.getElementById('imgwindow');
  
  if(document.all && !window.opera) {
    var appver = navigator.appVersion;
    var verpos = appver.indexOf('MSIE ');
    var browserversion = parseInt(appver.substr(verpos+5, 1));
    if(browserversion>=7) { scrOfY = 0; scrOfX = 0; }
  }
  
  if(!(document.all && !window.opera)) { scrOfY = 0; scrOfX = 0; }
  
  imgwindow.style.width = (imwidth+20)+'px';
  imgwindow.style.height = (imheight+20)+'px';
  imgwindow.style.display = 'block';
  imgwindow.style.top = (scrOfY+(brwh-imheight-20)/2)+'px';
  imgwindow.style.left = (scrOfX+(brww-imwidth-20)/2)+'px';
  
  imgwindow.innerHTML='<div id="gp1"><img style="margin-left: '+((imwidth-32)/2)+'px; margin-top: '+((imheight-50)/2)+'px;" src="./gfx/loader.gif" /></div><img id="galim" style="padding: 10px; display: none;" onmousemove="javascript: imRefreshClose();" onload="javascript: imLoaded();" src="'+img+'" /><div id="gp2" class="closeim"><a href="#" onmouseover="javascript: imHoldClose();" onclick="javascript: CloseIm(); return false;">&nbsp;</a></div>';
  
  var gp2 = document.getElementById('gp2');
  gp2.style.left = (imwidth+10-gp2.offsetWidth) + 'px';
  SetTransparency(gp2, 0);
  clfading = true;
  closehold = false;
  if(tm) clearTimeout(tm);
  setTimeout("clfading = false", 200);
  
  var im = document.getElementById('galim');
  SetTransparency(im, 0);
  im.style.display = 'block';
   
  return false;
}

function imLoaded() {
  document.getElementById('gp1').style.display = 'none';
  document.getElementById('gp1').innerHTML = '';
  
  var im = document.getElementById('galim');
  
  SetTransparency(im, 0);
  
  for(i=0; i<10; i++) setTimeout("SetTransparency(document.getElementById('galim'), "+i+"*10)", (parseInt((i*25))));
  i++;
  setTimeout("SetTransparency(document.getElementById('galim'), 100)", (parseInt((i*25))));
}

function imRefreshClose() {
  if(clfading) return false;
  if(closehold) return false;
  clfading = true;
  for(i=0; i<10; i++) setTimeout("SetTransparency(document.getElementById('gp2'), "+i+"*10)", (parseInt((i*25))));
  i++;
  setTimeout("SetTransparency(document.getElementById('gp2'), 100)", (parseInt((i*25))));
  setTimeout("imHoldClose()");
}

function imHoldClose() {
  closehold = true;
  clfading = false;
  if(tm) clearTimeout(tm);
  tm = setTimeout('imHideClose()', 2000);
}

function imHideClose() {
  if(clfading) return false;
  clfading = true;
  for(i=0; i<10; i++) setTimeout("SetTransparency(document.getElementById('gp2'), "+(10-i)+"*10)", (parseInt((i*25))));
  i++;
  setTimeout("SetTransparency(document.getElementById('gp2'), 0)", (parseInt((i*25))));
  setTimeout("clfading=false", (parseInt((i*25))));
  setTimeout("closehold=false", (parseInt((i*25))));
}

function CloseIm() {
  if(!imopened) return false;
  imopened = false;
  for(i=0; i<7; i++) setTimeout("SetTransparency(document.getElementById('fsbg'), "+(7-i)+"*10)", (parseInt((i*25))));
  i++;
  setTimeout("SetTransparency(document.getElementById('fsbg'), 0)", (parseInt((i*25))));
  setTimeout("document.getElementById('fsbg').style.display='none'", (parseInt((i*25))));
  
  for(i=0; i<10; i++) setTimeout("SetTransparency(document.getElementById('imgwindow'), "+(10-i)+"*10)", (parseInt((i*25))));
  i++;
  setTimeout("SetTransparency(document.getElementById('imgwindow'), 0)", (parseInt((i*25))));
  setTimeout("document.getElementById('imgwindow').style.display='none'", (parseInt((i*25))));
}

function GetResizedDimensions(imgwidth, imgheight, maxwidth, maxheight) {

  if((imgwidth < maxwidth) && (imgheight < maxheight)) return new Array(imgwidth, imgheight);

  if(imgwidth > imgheight) {
    ratio = maxwidth/imgwidth;
    destwidth = maxwidth;
    destheight = imgheight * ratio;
      
    if(destheight>maxheight) {
      ratio = maxheight/imgheight;
      destheight = maxheight;
      destwidth = imgwidth * ratio;
    }
    
  } else {
    ratio = maxheight/imgheight;
    destheight = maxheight;
    destwidth = imgwidth * ratio;
    
    if(destwidth>maxwidth) {
      ratio = maxwidth/imgwidth;
      destwidth = maxwidth;
      destheight = imgheight * ratio;
    }
  }
  
  return new Array(Math.round(destwidth), Math.round(destheight));
}
