var counter = 0;
var display = false;
var margin = 10;
var padding = 15;
var def = 'zoom';
var image = 'detail';
var text = 'plot';
    
function decrease(arr,num)
{
  var fst = arr[0] - num;
  var scd = arr[1] - num;
  return [fst,scd]
}
    
function objectPosition(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent)
  {
    do
    {
    	curleft += obj.offsetLeft;
    	curtop += obj.offsetTop;
    }while (obj = obj.offsetParent);
  }
  return [curleft,curtop];
}
    
function increaseCounter()
{
  counter = counter + 1;
  return true;
}
    
function zoom(e,obj)
{
  var href = obj.href;
  var post = obj.className.substr(obj.className.lastIndexOf(" ")+1);
  var object = decrease(objectPosition(obj),10);

  if(display) forceDrop(def + counter);
  if(!increaseCounter()) return false;

  var alt = document.getElementById('alt-' + post).innerHTML; //proper texts loading
  var title = document.getElementById('title-' + post).innerHTML;
  var content = document.getElementById('desc-' + post).innerHTML;

  var graph = append(def + counter);
  graph.className = 'zoom';
  graph.style.width = '167px';
  graph.style.height = '109px';
  graph.style.left = object[0] + 'px';
  graph.style.top = object[1] + 'px';
  graph.innerHTML = '<img id="' + image + counter + '"' +
                    'src="' + href + '"' +
                    'alt="' + alt + '"' +
                    //'title="' + title + '" ' +
                    'tiptitle="' + title + '" ' +
                    'onmouseover="tooltip.show(this.getAttribute(\'tiptitle\'))"' +
                    'onmouseout="tooltip.hide()" />' +
                    '<p id="' + text + counter + '">' + content + '</p>';
  graph.style.display = 'block';
  graph.onclick = niceDrop;
      
  return grow(graph.id,counter,'320','320',object[0],object[1],'280','211');
}
    
function grow(id,counter,width,height,left,top,mimgw,mimgh)
{
  var obj = document.getElementById(id);
  var obr = document.getElementById(image + counter);
  var curX = obj.style.width.replace('px','');
  var curY = obj.style.height.replace('px','');
  var posX = obj.style.left.replace('px','');
  var posY = obj.style.top.replace('px','');
  if((width - curX) < 2 && (height - curY) < 2) //almost the right size
  {
    obj.style.width = width + 'px';
    obj.style.height = height + 'px';
    obr.style.width = (mimgw > (width-2*padding)) ? (width-2*padding) + 'px' : mimgw + 'px';
    obr.style.height = (mimgh > (height-2*padding)) ? (height-2*padding) + 'px' : mimgh + 'px';
    come((text + counter),1); //display description text
    return true; //successfully displayed
  }
  var difX = Math.ceil((width - curX)/10);
  var difY = Math.ceil((height - curY)/10);
  curX = difX + curX*1;
  curY = difY + curY*1;
  left = (left -  Math.floor(difX/2) < margin) ? margin : left -  Math.floor(difX/2);
  top = (top -  Math.floor(difY/2) < margin) ? margin : top -  Math.floor(difY/2);
  obj.style.width = curX + 'px';
  obj.style.height = curY + 'px';
  obj.style.left = left + 'px';
  obj.style.top = top + 'px';
  obr.style.width = (mimgw > (curX-2*padding)) ? (curX-2*padding) + 'px' : mimgw + 'px';
  obr.style.height = (mimgh > (curY-2*padding)) ? (curY-2*padding) + 'px' : mimgh + 'px';
  setTimeout("grow('"+id+"','"+counter+"',"+width+","+height+","+left+","+top+","+mimgw+","+mimgh+")",12);
  display = true;
  return false;
}
    
function append(id)
{
  var zoom = document.createElement('div');
  var body = document.getElementsByTagName('body');
  zoom.id = id;
  body[0].appendChild(zoom);
  return zoom;
}
    
function hide(id)
{
  document.getElementById(id).style.display = 'none';
  return false;
}
    
function drop(id)
{
  var thing = document.getElementById(id);
  var body = document.getElementsByTagName('body');
  document.getElementById(image + id.replace(def,'')).onmouseout();
  body[0].removeChild(thing);
  display = false;
  return true;
}
    
function forceDrop(id)
{
  //document.getElementById(image + id.replace(def,'')).onmouseout();
  //var img = document.getElementById(image + id.replace(def,''));
  //img.onmouseover = '';
  //img.onmouseout();
  setTimeout("drop('"+id+"')",90);
  return fade(id,9);
}
    
function niceDrop()
{
  return forceDrop(this.id);
}
    
function fade(id,lvl)
{
  if(lvl >= 1) setTimeout("fade('"+id+"',"+(lvl-1)+")",20);
  return !visibility(id,lvl/10);
}
    
function come(id,lvl)
{
  if(lvl <= 9) setTimeout("come('"+id+"',"+(lvl+1)+")",20);
  if(!document.getElementById(id)) return false;
  document.getElementById(id).style.display = 'block';
  return visibility(id,lvl/10);
}
    
function visibility(id,opacity)
{
  if(thing = document.getElementById(id))
  {
    thing.style["-khtml-opacity"] = opacity;
    thing.style.opacity = opacity;
    thing.style.filter = "alpha(opacity="+Math.round(100*opacity)+")";
    return true;
  }
  return false;
}

var form = false; 
var rss = false;
    
function hideForm(id)
{
  setTimeout("hide('"+id+"')",90);
  return fade(id,10);
}

function switchForm()
{
  var id = 'contact';
  form = (form) ? hideForm(id) : come(id,1);
  return true;
}

function switchRss()
{
  var id = 'rss';
  rss = (rss) ? hideForm(id) : come(id,1);
  return true;
}
    
//document.onclick = function () { forceDrop(def + counter); }; //disabled, because of an collision
    
