// *******  application script for neximage.com  *******
// *******  © by nexbyte gmbh, switzerland       *******
// *******  create: 17.02.2002 - tbruederli      *******
// *******  modify: 16.06.2004 - tbruederli      *******
// *******  requires: global.js, rollver.js, magnify.js  *******


// global vars
img_pas_markup = '_pas';
img_act_markup = '_act';
img_sel_markup = '_sel';

var zoomomatic_objects = new Array();
var active_zoomomatic_object = null;
var zoomomatic_frame, zoomomatic_timer, zoomomatic_help_layer, enlarge_win;


// startup function
function init(sel)
  {
  // preload rollover images
  preload_img();
  loaded = true;

  select_nav(sel);
  window.name = 'neximage';

  // set PNG image in those browser which support it
  // with PNG behaviour for IE on windows
  if(bw.dom && !(bw.ie && bw.win && bw.vendver<5.5))
    {
    var img = nex_get_object('zoomomaticimg');
    if(img && bw.ie && !bw.opera)
      {
      var src = String(img.src).replace(/\.gif$/, '.png');
      img.src = '/global/images/blank.gif';
      img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
      }
    else if(img)
      img.src = String(img.src).replace(/\.gif$/i, '.png');
    }

 init_forms();
 window.onunload = save_maginifation_state;
 }


// show browser status text
function wst(a)
  {
  window.status = a;
  return true;
  }

function init_forms()
  {
  for (var i=0; i<document.forms.length; ++i)
    document.forms[i].action += '?_verified=1';
  }

function submit_form(to)
  {
  if (document.form)
    {
    document.form.action = to;
    document.form.submit();
    }
  else
    location.href = to;
  }


// make the navigation button look selected
function select_nav(sel)
  {
  var elm = document.images[sel];
  if(!elm) return;
  elm.src = switch_img_path(elm.src, 'sel');
  a_sel_img['default'] = {img:sel};
  }

// check the given path and changes it with act/pas
function switch_img_path(my_source,mode)
  {
  var pos_pas = my_source.indexOf(img_pas_markup+'.');
  var pos_act = my_source.indexOf(img_act_markup+'.');
  var pos_sel = my_source.indexOf(img_sel_markup+'.');
  var suffix  = my_source.substring(my_source.lastIndexOf('.'),my_source.length);

  var pos = (pos_sel>0) ? pos_sel : (pos_act>0) ? pos_act : pos_pas;
  var end = (mode=='sel') ? img_sel_markup : (mode=='act') ? img_act_markup : img_pas_markup;

  var new_source = my_source.substring(0,pos)+end+suffix;
  if(new_source==my_source) return false;
  return new_source;
  }


// open a new window with the given screenshot
function open_full_screenshot(url)
  {
  var loc = '../include/enlarge.html?_src='+escape(url);
  if(window.enlarge_win && !enlarge_win.closed)
    enlarge_win.location.href = loc;
  else
    enlarge_win = window.open(loc, 'neximageEnlargeWin', 'width=300,height=150,toolbar=0,location=0,menubar=0,scrollbars=0,resizable=1,status=0');

  if(enlarge_win)
    setTimeout('enlarge_win.focus()', 20);

  return false;
  }


// start opening a zoomed image in the zoom-o-matic
function load_zoom_image(base_image, url, rectangle, button)
  {
  var id = null; // zoomomatic_objects.length;
  var pos = null;

  // check if we loaded this image already
  for(var n=0; n<zoomomatic_objects.length; n++)
    if(zoomomatic_objects[n].base_image==base_image /* && zoomomatic_objects[n].loaded */)
      {
      id = n;
      break;
      }

  // disable the active object
  if(active_zoomomatic_object!==null)
    {
    if(zoomomatic_objects[active_zoomomatic_object].obj)
      zoomomatic_objects[active_zoomomatic_object].obj.disable();

    if(zoomomatic_objects[active_zoomomatic_object].button)
      {
      var btn = nex_get_object(zoomomatic_objects[active_zoomomatic_object].button);
      swap_image(zoomomatic_objects[active_zoomomatic_object].button, switch_img_path(btn.src, 'pas'), btn);
      }

    if(id!==null && active_zoomomatic_object===id)
      return cancel_magnification();
    }

  // create maginication frame object if not done yet
  if(!window.zoomomatic_frame)
    {
    zoomomatic_frame = new layer_object('zoomomaticframe');
    var pos = read_maginifation_state();

    zoomomatic_frame.draggable(1);
    zoomomatic_frame.clear_message = function()
      {
      if(this.cleared) return;
      var layer = new layer_object('zoomomaticbusy', 'zoomomaticframe');
      if(layer.elm) layer.show(0);
      this.cleared = true;
      }
    }

  // check if frame is in the visible part of the page
  if(!pos)
    pos = new Array(zoomomatic_frame.x, zoomomatic_frame.y);

  if(pos && document.body)
    {
    var win = get_window_size();
    if(pos[1] < document.body.scrollTop)
      pos[1] = document.body.scrollTop + 10;
    else if (pos[1] + zoomomatic_frame.height > win.height + document.body.scrollTop)
      pos[1] = win.height + document.body.scrollTop - zoomomatic_frame.height - 10;
    }

  if(pos[1] != zoomomatic_frame.y);
    zoomomatic_frame.move(pos[0], pos[1]);

  // show frame layer
  zoomomatic_frame.show(1);


  // set button image as 'selected'
  if(button)
    {
    var btn = nex_get_object(button);
    swap_image(button, switch_img_path(btn.src, 'sel'), btn);
    }

  // create a new object and load image
  if(id===null)
    {
    id = zoomomatic_objects.length;
    var base_img_obj = nex_get_object(base_image);

    zoomomatic_objects[id] = new Object();
    zoomomatic_objects[id].button = button;
    zoomomatic_objects[id].base_image = base_image;
    zoomomatic_objects[id].rectangle = rectangle ? rectangle : [0,base_img_obj.width,base_img_obj.height,0];

    zoomomatic_objects[id].zoom_image = new Image();
    zoomomatic_objects[id].zoom_image.onload = function(){ init_maginfication(id); };
    zoomomatic_objects[id].zoom_image.onerror = function(){ /* alert('error loading image '+this.src) */ };
    zoomomatic_objects[id].zoom_image.src = url;
    }
  else
    {
    zoomomatic_objects[id].obj.activate();
    zoomomatic_timer = setTimeout('show_magnification_help(1)', 2000);
    }

  active_zoomomatic_object = id;
  return false;
  }


// initialize the magnification object (when image loaded completely)
function init_maginfication(id)
  {
  if(id===null || !zoomomatic_objects[id] || !zoomomatic_frame.elm)
    return;

  var zobj = zoomomatic_objects[id];

  // create nexmagnify object
  zoomomatic_objects[id].obj = new nexmagnify_object({
    image:zobj.base_image,
    rectangle:zobj.rectangle,
    large_src:zobj.zoom_image.src,
    factor:zobj.zoom_image.width / zobj.rectangle[1],
    display:{width:280, height:210, top:28, left:9, parent:zoomomatic_frame}});

  // set eventhandler to the object in order to hide the help
  zoomomatic_objects[id].obj.onmouseover = function(){ show_magnification_help(0); };

  // activate the magnification object
  zoomomatic_objects[id].loaded = true;

  if(active_zoomomatic_object==id)
    {
    zoomomatic_objects[id].obj.activate();
    zoomomatic_frame.clear_message();
    zoomomatic_timer = setTimeout('show_magnification_help(1)', 2000);
    }
  }


// show/hide the heptext within the frame
function show_magnification_help(vis)
  {
  if(window.zoomomatic_timer)
    clearTimeout(zoomomatic_timer);

  if(!window.zoomomatic_help_layer)
    zoomomatic_help_layer = new layer_object('zoomomatichelp', 'zoomomaticframe');

  zoomomatic_help_layer.show(vis?2:0);
  }


// switch off the maginfication utility
function cancel_magnification()
  {
  if(window.zoomomatic_frame)
    {
    zoomomatic_frame.show(0);
    show_magnification_help(0);
    }

  if(active_zoomomatic_object!==null)
    {
    var m_obj = zoomomatic_objects[active_zoomomatic_object];
    m_obj.obj.disable();
    }

  active_zoomomatic_object = null;
  return false;
  }



// disable the activated magnification button 
function disable_magnification_button()
  {
  var m_obj = zoomomatic_objects[active_zoomomatic_object];
  if(m_obj.button)
    {
    var btn = nex_get_object(m_obj.button);
    swap_image(m_obj.button, switch_img_path(btn.src, 'pas'), btn);
    }
  cancel_magnification()
  }



// write current position of the maginfication frame to a cookie
function save_maginifation_state()
  {
  if(!window.zoomomatic_frame)
    return;

  var c = 'magnify_position='+zoomomatic_frame.x+','+zoomomatic_frame.y;
  document.cookie = c;
  }

// read the saved position on the client
function read_maginifation_state()
  {
  if(!document.cookie)
    return null;

  var pos;
  var reg = /magnify_position=([0-9]+),([0-9]+)/;
  var a_cookie = document.cookie.split(';');
  for(var n in a_cookie)
    if(window.RegExp && reg.test(a_cookie[n]))
      {
      pos = new Array(parseFloat(RegExp.$1), parseFloat(RegExp.$2));
      break;
      }

  return pos;
  }