var sid;

// Function for loading source in left & right frame
function loadMain() {
  var rightPos=location.search.indexOf('right=')+6;
  var mainURL;
  if (rightPos>5) {
    mainURL=location.search.substring(rightPos,location.search.length);
    if (mainURL.indexOf('&')>-1) {
      mainURL=mainURL.substring(0,mainURL.indexOf('&'));
    }
    mainURL=unescape(mainURL);
    if (typeof(location.replace)!='undefined') {
      self.frames.right.location.replace(mainURL);
    } else self.frames.right.location.href=mainURL;
  }
  var leftPos=location.search.indexOf('left=')+5;
  if (leftPos>4) {
    mainURL=location.search.substring(leftPos,location.search.length);
    if (mainURL.indexOf('&')>-1) {
      mainURL=mainURL.substring(0,mainURL.indexOf('&'));
    }
    mainURL=unescape(mainURL);
    if (typeof(location.replace)!='undefined') {
      self.frames.left.location.replace(mainURL);
    } else self.frames.left.location.href=mainURL;
  }
}

function showImage(width, height, link) {
  var imgWndw;

  var w = width + 20;
  if (w > screen.availWidth - 55) w = screen.availWidth - 50;
  var h = height;
  if (h > screen.availHeight - 55) h = screen.availHeight - 50;
  imgWndw = window.open('',link.target,'width=' + w + 
          ',height=' + h + ',toolbar=no,menubar=no,location=no,status=no,'+
          'resizable=yes,scrollbars=yes,left=5,top=5');
  with (imgWndw.document){
    open();
    write('<?xml version="1.0" encoding="windows-1251"?>\n'+
          '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' +
          '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">\n'+
          '<html><head><title>'+link.title+'</title>'+
          '</head><body leftmargin="0" topmargin="0" '+
          'rightmargin="0" bottommargin="0" marginwidth="0" '+
          'style="margin:0;padding:0;position:fixed;overflow:none;" '+
          'marginheight="0"><img src="'+link.href+'" border="0" '+
          ' alt="'+link.title+'" title="'+link.title+'"/></body></html>');
    close();
  }
  return false;
}

var preloads = {};

function imagePopup(element) {
  var title;
  var src;
  if ('img' == element.tagName.toLowerCase()) {
    title = element.alt;
    src = element.src.replace('thumb-small', 'thumb-big');
    var link = $('image_preview_popup_link');
    if (link) {
      link.title = title;
      link.href = src.replace('thumb-big', 'original');
    }
  } else {
    title = element.title;
    src = element.href;
  }
  $('image_preview_popup_title').innerHTML = title;
  var new_id = uid();
  var preload = new Image();
  preload.alt = title;
  preload.src = src;
  preloads[new_id] = preload;
  if (preload.complete) {
    showImagePopup(new_id);
  } else {
    setTimeout("showImagePopup(" + new_id + ")", 250);
  }
}

function showImagePopup(new_id) {
  var preload = preloads[new_id];
  if(preload.complete) {
    var image = $('image_preview_popup_content');
    image.src = preload.src;
    image.alt = preload.alt;
    $('image_preview_popup').style.width = preload.width + 20 + 'px';
    popups[createPopup('image_preview_popup')].popup.show();
    preloads[new_id] = null;
  } else {
    setTimeout("showImagePopup(" + new_id + ")", 250);
  }
}
