var newwindow = new Array();
var array_index = 0;
var name = "text";

function popitup(url, form)
{
  if (form != 'null') {
    var property_name = form.description_contains.value;
    url = 'list_property?description%5Bcontains%5D=' + property_name + '&order=1';
  }
//  name = 'text' + Math.floor(Math.random() * 100);
  name = 'text';
//alert("called popitup, url = " + url + " name = " + name + " property_name = " + property_name);
  newwindow[array_index] = window.open(url,'name','width=1024,height=650');
//  newwindow[array_index] = window.open('http://www.scoutdfw.com', 'name', 'width=1024,height=650');
//alert("array_index = "  + array_index);
//alert("newwindow[array_index] = "  + newwindow[array_index]);
//    newwindow[array_index] = window.open('', name, 'width=1024, height=650');
//    newwindow[array_index].location.href = url;
//  if ( !newwindow[array_index] ) {
//  }
  if (window.focus) { newwindow[array_index].focus() }
  array_index++;
  return false;
}

function writeText(id, text)
{
info("write text - " + text);
  if (document.getElementById) {
    x = document.getElementById(id);
    x.innerHTML = '';
    x.innerHTML = text;
  }
  else if (document.all) {
    x = document.all[id];
    x.innerHTML = text;
  }
}

function changeMargin(id, margin)
{
  if (document.getElementById) {
    x = document.getElementById(id);
  }
  else if (document.all) {
    x = document.all[id];
  }
//  x.style.marginTop = margin + "px";
}

function changeImage(image_name, text, number)
{
  document['slidek'].src = image_name;
  number = 470 - number;
  writeText("slidek_caption", text);
//  changeMargin("scrollbox", number);
}

function element(id)
{
  if (document.getElementById) {
    x = document.getElementById(id);
  }
  else if (document.all) {
    x = document.all[id];
  }
  return(x);
}

function generate(x,y) {
  var range = y - x + 1;
  return Math.floor(Math.random() * range) + x;
}

var array_position = 1;
var next_array_position = 0;
var calls = 0;
var id = 0;

function revealImage()
{
  num = revealImage.arguments[0];

// this function is called 9 times every 5 sconds,
// the following code ensures that the body of the function
// is only executed once per set of calls with the the random
// id equal to the grid position.
  if (calls == 0) {  id = generate(0,8); }
  if (id != num) {
    calls += 1;
    if (calls == 9) { calls = 0; }
    return; 
  }

  front_div = "fposition" + id;
  rear_div = "position" + id;
  rear = $(rear_div);
  Element.show(rear);
  new_image_name = revealImage.arguments[array_position];
  var functRef = swapImages(id, new_image_name);

// pos is the pos/neg value of array_position to control the Fade/Appear Effects
  pos = array_position % 2;

// be careful to only increment the array position every 9th call
  if (next_array_position == 8) {
    array_position += 1;
    if (array_position == 11) { array_position = 1; }
    next_array_position = 0;
  }
  next_array_position += 1;

// pos is odd if pos = 1
  var functRef = swapImages(id, new_image_name, pos);
  if (pos == 1) { Effect.Fade(front_div, { duration: 3.0 }); }
  else { Effect.Appear(front_div, { duration: 3.0 }); }

  swap = setTimeout(functRef, 4000);
  functRef = null;
}

function swapImages(id, new_image_name) {
  return (function() {
    var front_div = "fposition" + id;
    var rear_div = "position" + id;

    rear = $(rear_div);
    front = $(front_div);

    var new_innerHTML = "<img src=\"/images/" + new_image_name + "\">";

    if (pos == 1) { Element.update(front, new_innerHTML); }
    else { Element.update(rear, new_innerHTML); }
  });
}  

function revealImage2()
{
  num = revealImage2.arguments[0];

// this function is called 9 times every 5 sconds,
// the following code ensures that the body of the function
// is only executed once per set of calls with the the random
// id equal to the grid position.
//  if (calls == 0) {  id = generate(0,8); }
//  if (id != num) {
//    calls += 1;
//    if (calls == 9) { calls = 0; }
//    return; 
//  }
id = 4;

  front_div = "fposition" + id;
  rear_div = "position" + id;
  rear = $(rear_div);
  Element.show(rear);
  new_image_name = revealImage2.arguments[array_position];

// be careful to only increment the array position every 9th call
//  if (next_array_position == 8) {
//    array_position += 1;
//    if (array_position == 11) { array_position = 1; }
//    next_array_position = 0;
//  }
//  next_array_position += 1;

if (array_position > 10) { array_position = 1; }
pos = array_position % 2;

// pos is odd if pos = 1
  var functRef = swapImages2(id, new_image_name, pos);
  if (pos == 1) { Effect.Fade(front_div, { duration: 3.0 }); }
  else { Effect.Appear(front_div, { duration: 3.0 }); }
  swap = setTimeout(functRef, 4000);
  functRef = null;

array_position += 1;
}

function swapImages2(id, new_image_name, pos) {
  return (function() {
    var front_div = "fposition" + id;
    var rear_div = "position" + id;

    rear = $(rear_div);
    front = $(front_div);

    var new_innerHTML = "<img src=\"/images/" + new_image_name + "\">";

//    new Effect.Opacity(front, { to: 0.0 });
//    Element.update(front, rear.innerHTML);
//    Element.show(front);
    if (pos == 1) { Element.update(front, new_innerHTML); }
    else { Element.update(rear, new_innerHTML); }


  });
}  

